Repository navigation
Protect: migrate admin page tabs to @wordpress/ui Tabs #48277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1d1a9e4
Protect: migrate admin page tabs to @wordpress/ui Tabs
CGastrell 0f84592
Protect: replace brittle inspector-toggle-control IDs in firewall e2e
CGastrell 1caac95
Protect: locate firewall toggles structurally in e2e
CGastrell efc57db
Protect: restore tab-strip hairline and tighten route matching
dhasilva 87332fb
Protect: pin the tab strip on scroll
dhasilva b3319cb
Move tab-strip styling into the admin-page-layout mixin
CGastrell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/js-packages/base-styles/changelog/add-admin-page-tabs-mixin-rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| admin-page-layout mixin: style hooks for `@wordpress/ui` Tabs hosted in an AdminPage (sticky `.jp-admin-page-tabs` wrapper, inline-padding alignment for tab buttons, header bottom-border/padding suppression when tabs are present). |
4 changes: 4 additions & 0 deletions
4
projects/js-packages/components/changelog/add-toggle-control-aria-label
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| ToggleControl: forward the `aria-label` prop to the underlying checkbox so consumers can label toggles that have no visible label. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/plugins/protect/changelog/fix-firewall-toggle-aria-labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Firewall: add accessible names to the Brute force protection, Block IP addresses, and Trusted IP addresses toggles. |
4 changes: 4 additions & 0 deletions
4
projects/plugins/protect/changelog/migrate-tabs-to-wordpress-ui
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: patch | ||
| Type: changed | ||
|
|
||
| Migrate the Protect admin page tabs from react-router NavLinks to the shared @wordpress/ui Tabs component. No functional change. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 0 additions & 70 deletions
70
projects/plugins/protect/src/js/components/admin-page/index.jsx
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
projects/plugins/protect/src/js/components/admin-page/styles.module.scss
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
projects/plugins/protect/src/js/components/protect-app/index.jsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { AdminPage as JetpackAdminPage } from '@automattic/jetpack-components'; | ||
| import { useConnection } from '@automattic/jetpack-connection'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { Tabs } from '@wordpress/ui'; | ||
| import { useCallback, useEffect, useMemo, useRef } from 'react'; | ||
| import { Outlet, useLocation, useNavigate } from 'react-router'; | ||
| import useNotices from '../../hooks/use-notices'; | ||
| import useProtectData from '../../hooks/use-protect-data'; | ||
| import Notice from '../notice'; | ||
|
|
||
| /** | ||
| * Resolve the active tab value from the current pathname. | ||
| * | ||
| * Matches '/firewall' and '/settings' exactly or as path prefixes (so future | ||
| * nested routes like '/settings/foo' still select the right tab) without | ||
| * over-matching unrelated paths like '/firewall-extras'. | ||
| * | ||
| * @param {string} pathname - The current location pathname. | ||
| * @return {string} The active tab value ('scan' | 'firewall' | 'settings'). | ||
| */ | ||
| function getActiveTab( pathname ) { | ||
| if ( pathname === '/firewall' || pathname.startsWith( '/firewall/' ) ) { | ||
| return 'firewall'; | ||
| } | ||
| if ( pathname === '/settings' || pathname.startsWith( '/settings/' ) ) { | ||
| return 'settings'; | ||
| } | ||
| // Default (covers '/scan', '/scan/history', '/scan/history/:filter' and any unmatched path). | ||
| return 'scan'; | ||
| } | ||
|
|
||
| const ProtectApp = () => { | ||
| const { notice } = useNotices(); | ||
| const { isRegistered } = useConnection(); | ||
| const navigate = useNavigate(); | ||
| const location = useLocation(); | ||
| const { | ||
| counts: { | ||
| current: { threats: numThreats }, | ||
| }, | ||
| } = useProtectData(); | ||
|
|
||
| // Redirect to the setup page if the site is not registered. | ||
| useEffect( () => { | ||
| if ( ! isRegistered ) { | ||
| navigate( '/setup' ); | ||
| } | ||
| }, [ isRegistered, navigate ] ); | ||
|
|
||
| const activeTab = useMemo( () => getActiveTab( location.pathname ), [ location.pathname ] ); | ||
|
|
||
| // Reset scroll position whenever the active tab changes. The actual scroll | ||
| // container is the layout mixin's `overflow: auto` direct child of | ||
| // `.admin-ui-page` (a wrapper applied by `<AdminPage>` around `children`), | ||
| // not `Tabs.Root` itself. Walk up from Tabs.Root to find that ancestor so | ||
| // this stays robust to any future restructuring of `<AdminPage>`. | ||
| const tabsRootRef = useRef( null ); | ||
| useEffect( () => { | ||
| let el = tabsRootRef.current?.parentElement; | ||
| while ( el && el !== document.body ) { | ||
| const { overflowY } = window.getComputedStyle( el ); | ||
| if ( overflowY === 'auto' || overflowY === 'scroll' ) { | ||
| el.scrollTo( { top: 0 } ); | ||
| break; | ||
| } | ||
| el = el.parentElement; | ||
| } | ||
| }, [ activeTab ] ); | ||
|
|
||
| const onValueChange = useCallback( | ||
| value => { | ||
| navigate( `/${ value }` ); | ||
| }, | ||
| [ navigate ] | ||
| ); | ||
|
|
||
| if ( ! isRegistered ) { | ||
| return null; | ||
| } | ||
|
|
||
| const scanLabel = | ||
| numThreats > 0 | ||
| ? sprintf( | ||
| // translators: %d is the number of threats found. | ||
| __( 'Scan (%d)', 'jetpack-protect' ), | ||
| numThreats | ||
| ) | ||
| : __( 'Scan', 'jetpack-protect' ); | ||
|
|
||
| return ( | ||
| <JetpackAdminPage | ||
| title={ 'Protect' /** "Protect" is a product name, do not translate. */ } | ||
| subTitle={ __( 'Automated malware scanning and firewall protection.', 'jetpack-protect' ) } | ||
| > | ||
| { notice && <Notice floating={ true } dismissable={ true } { ...notice } /> } | ||
| <Tabs.Root ref={ tabsRootRef } value={ activeTab } onValueChange={ onValueChange }> | ||
| <div className="jp-admin-page-tabs"> | ||
| <Tabs.List> | ||
| <Tabs.Tab value="scan">{ scanLabel }</Tabs.Tab> | ||
| <Tabs.Tab value="firewall">{ __( 'Firewall', 'jetpack-protect' ) }</Tabs.Tab> | ||
| <Tabs.Tab value="settings">{ __( 'Settings', 'jetpack-protect' ) }</Tabs.Tab> | ||
| </Tabs.List> | ||
| </div> | ||
| { /* @wordpress/ui Tabs.Panel unmounts inactive panels by default, so only the | ||
| active panel's <Outlet /> mounts and the matched route renders once. */ } | ||
| <Tabs.Panel value="scan"> | ||
| <Outlet /> | ||
| </Tabs.Panel> | ||
| <Tabs.Panel value="firewall"> | ||
| <Outlet /> | ||
| </Tabs.Panel> | ||
| <Tabs.Panel value="settings"> | ||
| <Outlet /> | ||
| </Tabs.Panel> | ||
| </Tabs.Root> | ||
| </JetpackAdminPage> | ||
| ); | ||
| }; | ||
|
|
||
| export default ProtectApp; |
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
projects/plugins/protect/src/js/components/tabs/stories/index.stories.jsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tab's internal padding shouldn't ideally be manually tweaked, since it can diverge from the standard component spacing and can easily cause unexpected changes in the future.
If what you need is to align the first tab, have you tried adding a margin to the tablist, or adding padding to
.jp-admin-page-tabs?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @ciampo ! Thanks for taking a look!
Yes, I know we shouldn't be doing this kind of customization. I'm trying to keep all this things wrapped in a central place (AdminPage implements this mixin) which messes with little things that should actually be done upstream. Some are done for speed, others because I see they are already being discussed and some other because there is no real consensus on how to use them.
In this particular case, I don't see any agreement on:
All that said, yes, I could have used the padding on the tabs container rather than on the buttons, I got carried out by the Settings page, where the first tab label is snapped to the border of the page.
Here's a new PR for clean up: #48706
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those could be good questions to ask in WordPress/gutenberg#76709