Repository navigation
Tabs: add runtime validation for tab/panel mismatches #75170
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
15 commits
Select commit
Hold shift + click to select a range
c4734b0
Tabs: add runtime checks for matching tabs/tabpanels
ciampo 5dcd3ba
Tabs: Add runtime check to make sure all components are wrapped in Ta…
ciampo debb456
Use String() instead of JSON.stringify to prevent errors with certain…
ciampo 3eeb092
Don't run hooks conditionally, split dev vs prod implementations enti…
ciampo 643884c
Re-use tabvalue type
ciampo eced9d8
CHANGELOG
ciampo 561910b
Fix TS errors
ciampo 000d2e9
Use @wordpress/jest-console for better console assertions
ciampo 0fdfa9d
Add a test for validating dynamic value changes
ciampo f3b4b49
Use @wordpress/warning package
ciampo 2e61396
Fix validation tracking with Map instead of Set
ciampo 393fbe6
Tabs: simplify validation to count-based Tab/Panel matching
ciampo 9c5f8e0
Format tsconfig
ciampo f58032d
Warning => throw, update error message
ciampo fc7b1ba
Undo changes to package-lock and tsconfig
ciampo 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
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
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,170 @@ | ||
| import { | ||
| createContext, | ||
| useContext, | ||
| useCallback, | ||
| useMemo, | ||
| useRef, | ||
| useEffect, | ||
| } from '@wordpress/element'; | ||
|
|
||
| type TabsValidationContextType = { | ||
| registerTab: () => () => void; | ||
| registerPanel: () => () => void; | ||
| }; | ||
|
|
||
| /** | ||
| * Whether validation is enabled. This is a build-time constant that allows | ||
| * bundlers to tree-shake all validation code in production builds. | ||
| */ | ||
| const VALIDATION_ENABLED = process.env.NODE_ENV !== 'production'; | ||
|
|
||
| const TabsValidationContext = VALIDATION_ENABLED | ||
| ? createContext< TabsValidationContextType | null >( null ) | ||
| : ( null as unknown as React.Context< TabsValidationContextType | null > ); | ||
|
|
||
| function useRegisterTabDev() { | ||
| const context = useContext( TabsValidationContext ); | ||
|
|
||
| useEffect( () => { | ||
| if ( context ) { | ||
| return context.registerTab(); | ||
| } | ||
| return undefined; | ||
| }, [ context ] ); | ||
| } | ||
|
|
||
| function useRegisterTabProd() { | ||
| // No-op in production. | ||
| } | ||
|
|
||
| /** | ||
| * Hook that registers a Tab for count validation in development mode. | ||
| */ | ||
| export const useRegisterTab = VALIDATION_ENABLED | ||
| ? useRegisterTabDev | ||
| : useRegisterTabProd; | ||
|
|
||
| function useRegisterPanelDev() { | ||
| const context = useContext( TabsValidationContext ); | ||
|
|
||
| useEffect( () => { | ||
| if ( context ) { | ||
| return context.registerPanel(); | ||
| } | ||
| return undefined; | ||
| }, [ context ] ); | ||
| } | ||
|
|
||
| function useRegisterPanelProd() { | ||
| // No-op in production. | ||
| } | ||
|
|
||
| /** | ||
| * Hook that registers a Panel for count validation in development mode. | ||
| */ | ||
| export const useRegisterPanel = VALIDATION_ENABLED | ||
| ? useRegisterPanelDev | ||
| : useRegisterPanelProd; | ||
|
|
||
| /** | ||
| * Development-only provider that tracks the number of registered tabs and | ||
| * panels, and warns when the counts don't match. | ||
| */ | ||
| function TabsValidationProviderDev( { | ||
| children, | ||
| }: { | ||
| children: React.ReactNode; | ||
| } ) { | ||
| const tabCountRef = useRef( 0 ); | ||
| const panelCountRef = useRef( 0 ); | ||
| const validationScheduledRef = useRef< ReturnType< | ||
| typeof setTimeout | ||
| > | null >( null ); | ||
|
|
||
| const scheduleValidation = useCallback( () => { | ||
| if ( validationScheduledRef.current ) { | ||
| clearTimeout( validationScheduledRef.current ); | ||
| } | ||
|
|
||
| // Schedule validation for the next tick to allow all | ||
| // registrations/unregistrations to complete. | ||
| validationScheduledRef.current = setTimeout( () => { | ||
| const tabCount = tabCountRef.current; | ||
| const panelCount = panelCountRef.current; | ||
|
|
||
| if ( tabCount !== panelCount ) { | ||
| throw new Error( | ||
| `Tabs: Tab/Panel count mismatch (${ tabCount } Tabs, ${ panelCount } Panels). ` + | ||
| `Each Tab must be associated with exactly one Panel. ` + | ||
| `Mismatched or missing associations can break screen reader navigation ` + | ||
| `and violate WAI-ARIA Tabs pattern requirements.` | ||
| ); | ||
| } | ||
|
|
||
| validationScheduledRef.current = null; | ||
| }, 0 ); | ||
| }, [] ); | ||
|
|
||
| const registerTab = useCallback( () => { | ||
| tabCountRef.current += 1; | ||
| scheduleValidation(); | ||
|
|
||
| return () => { | ||
| tabCountRef.current -= 1; | ||
| scheduleValidation(); | ||
| }; | ||
| }, [ scheduleValidation ] ); | ||
|
|
||
| const registerPanel = useCallback( () => { | ||
| panelCountRef.current += 1; | ||
| scheduleValidation(); | ||
|
|
||
| return () => { | ||
| panelCountRef.current -= 1; | ||
| scheduleValidation(); | ||
| }; | ||
| }, [ scheduleValidation ] ); | ||
|
|
||
| useEffect( () => { | ||
| return () => { | ||
| if ( validationScheduledRef.current ) { | ||
| clearTimeout( validationScheduledRef.current ); | ||
| } | ||
| }; | ||
| }, [] ); | ||
|
|
||
| const contextValue = useMemo( | ||
| () => ( { | ||
| registerTab, | ||
| registerPanel, | ||
| } ), | ||
| [ registerTab, registerPanel ] | ||
| ); | ||
|
|
||
| return ( | ||
| <TabsValidationContext.Provider value={ contextValue }> | ||
| { children } | ||
| </TabsValidationContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Production no-op provider that just renders children. | ||
| */ | ||
| function TabsValidationProviderProd( { | ||
| children, | ||
| }: { | ||
| children: React.ReactNode; | ||
| } ) { | ||
| return <>{ children }</>; | ||
| } | ||
|
|
||
| /** | ||
| * Provider component that validates the number of tabs matches the number | ||
| * of panels in development mode. | ||
| * | ||
| * In production, this component is a no-op and just renders children. | ||
| */ | ||
| export const TabsValidationProvider = VALIDATION_ENABLED | ||
| ? TabsValidationProviderDev | ||
| : TabsValidationProviderProd; |
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
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
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
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.
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.
Could maybe be simplified further in the future by extracting a reusable factory to set up the dev/prod scaffolding!
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.
I thought about it — we can definitely do that the next time we have a similar need, since
TabsandDialogalready implement a similar structure