Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/ui/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Internal

- `Tabs`: Add development-mode validation for Tab/Panel count matching ([#75170](https://github.com/WordPress/gutenberg/pull/75170)).

## 0.9.0 (2026-03-18)

### New Features
Expand Down
170 changes: 170 additions & 0 deletions packages/ui/src/tabs/context.tsx

Copy link
Copy Markdown
Member

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!

Copy link
Copy Markdown
Contributor Author

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 Tabs and Dialog already implement a similar structure

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;
3 changes: 3 additions & 0 deletions packages/ui/src/tabs/panel.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { forwardRef } from '@wordpress/element';
import clsx from 'clsx';
import { Tabs as _Tabs } from '@base-ui/react/tabs';
import { useRegisterPanel } from './context';
import styles from './style.module.css';
import type { TabPanelProps } from './types';

Expand All @@ -12,6 +13,8 @@ import type { TabPanelProps } from './types';
*/
export const Panel = forwardRef< HTMLDivElement, TabPanelProps >(
function TabPanel( { className, ...otherProps }, forwardedRef ) {
useRegisterPanel();

return (
<_Tabs.Panel
ref={ forwardedRef }
Expand Down
7 changes: 6 additions & 1 deletion packages/ui/src/tabs/root.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { forwardRef } from '@wordpress/element';
import { Tabs as _Tabs } from '@base-ui/react/tabs';
import { TabsValidationProvider } from './context';
import type { TabRootProps } from './types';

/**
Expand All @@ -10,6 +11,10 @@ import type { TabRootProps } from './types';
*/
export const Root = forwardRef< HTMLDivElement, TabRootProps >(
function TabsRoot( { ...otherProps }, forwardedRef ) {
return <_Tabs.Root ref={ forwardedRef } { ...otherProps } />;
return (
<TabsValidationProvider>
<_Tabs.Root ref={ forwardedRef } { ...otherProps } />
</TabsValidationProvider>
);
}
);
3 changes: 3 additions & 0 deletions packages/ui/src/tabs/tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import clsx from 'clsx';
import { Tabs as _Tabs } from '@base-ui/react/tabs';
import { chevronRight } from '@wordpress/icons';
import { Icon } from '../icon';
import { useRegisterTab } from './context';
import styles from './style.module.css';
import type { TabProps } from './types';

Expand All @@ -16,6 +17,8 @@ export const Tab = forwardRef< HTMLButtonElement, TabProps >( function Tab(
{ className, children, ...otherProps },
forwardedRef
) {
useRegisterTab();

return (
<_Tabs.Tab
ref={ forwardedRef }
Expand Down
Loading
Loading