Skip to content
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

Menu: throw when subcomponents are not rendered inside top level Menu #67411

Merged
merged 4 commits into from
Nov 29, 2024
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
6 changes: 5 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- `BoxControl`: Passive deprecate `onMouseOver`/`onMouseOut`. Pass to the `inputProps` prop instead ([#67332](https://github.com/WordPress/gutenberg/pull/67332)).
- `BoxControl`: Deprecate 36px default size ([#66704](https://github.com/WordPress/gutenberg/pull/66704)).

### Experimental

- `Menu`: throw when subcomponents are not rendered inside top level `Menu` ([#67411](https://github.com/WordPress/gutenberg/pull/67411)).

### Internal

- Upgraded `@ariakit/react` (v0.4.13) and `@ariakit/test` (v0.4.5) ([#65907](https://github.com/WordPress/gutenberg/pull/65907)).
Expand All @@ -20,7 +24,7 @@
- `FontSizePicker`: Deprecate 36px default size ([#66920](https://github.com/WordPress/gutenberg/pull/66920)).
- `ComboboxControl`: Deprecate 36px default size ([#66900](https://github.com/WordPress/gutenberg/pull/66900)).
- `ToggleGroupControl`: Deprecate 36px default size ([#66747](https://github.com/WordPress/gutenberg/pull/66747)).
- `RangeControl`: Deprecate 36px default size ([#66721](https://github.com/WordPress/gutenberg/pull/66721)).
- `RangeControl`: Deprecate 36px default size ([#66721](https://github.com/WordPress/gutenberg/pull/66721)).

### Bug Fixes

Expand Down
10 changes: 8 additions & 2 deletions packages/components/src/menu/checkbox-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,22 @@ export const MenuCheckboxItem = forwardRef<
) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.CheckboxItem can only be rendered inside a Menu component'
);
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for context to anyone else reading this: throwing an error in this scenario makes sense even if we just look at Ariakit - it is consistent with how Ariakit does it:

https://github.com/ariakit/ariakit/blob/22b84f02b85bbc8c66b2c6b58cf947f24a5316ce/packages/ariakit-react-core/src/menu/menu-item.tsx#L77-L81

https://github.com/ariakit/ariakit/blob/22b84f02b85bbc8c66b2c6b58cf947f24a5316ce/packages/ariakit-core/src/utils/misc.ts#L217-L232

As you can see, if we don't have this check, Ariakit will throw this error:

MenuItem must be wrapped in a MenuList, Menu or Menubar component

however, relying on this would be confusing, since our Menu components don't necessarily follow the same naming scheme.

}

return (
<Styled.MenuCheckboxItem
ref={ ref }
{ ...props }
accessibleWhenDisabled
hideOnClick={ hideOnClick }
store={ menuContext?.store }
store={ menuContext.store }
>
<Ariakit.MenuItemCheck
store={ menuContext?.store }
store={ menuContext.store }
render={ <Styled.ItemPrefixWrapper /> }
// Override some ariakit inline styles
style={ { width: 'auto', height: 'auto' } }
Expand Down
9 changes: 8 additions & 1 deletion packages/components/src/menu/group-label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export const MenuGroupLabel = forwardRef<
WordPressComponentProps< MenuGroupLabelProps, 'div', false >
>( function MenuGroup( props, ref ) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.GroupLabel can only be rendered inside a Menu component'
);
}

return (
<Styled.MenuGroupLabel
ref={ ref }
Expand All @@ -31,7 +38,7 @@ export const MenuGroupLabel = forwardRef<
/>
}
{ ...props }
store={ menuContext?.store }
store={ menuContext.store }
/>
);
} );
9 changes: 8 additions & 1 deletion packages/components/src/menu/group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ export const MenuGroup = forwardRef<
WordPressComponentProps< MenuGroupProps, 'div', false >
>( function MenuGroup( props, ref ) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.Group can only be rendered inside a Menu component'
);
}

return (
<Styled.MenuGroup
ref={ ref }
{ ...props }
store={ menuContext?.store }
store={ menuContext.store }
/>
);
} );
11 changes: 10 additions & 1 deletion packages/components/src/menu/item-help-text.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
import { forwardRef, useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import type { WordPressComponentProps } from '../context';
import { MenuContext } from './context';
import * as Styled from './styles';

export const MenuItemHelpText = forwardRef<
HTMLSpanElement,
WordPressComponentProps< { children: React.ReactNode }, 'span', true >
>( function MenuItemHelpText( props, ref ) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.ItemHelpText can only be rendered inside a Menu component'
);
}

return (
<Styled.MenuItemHelpText numberOfLines={ 2 } ref={ ref } { ...props } />
);
Expand Down
11 changes: 10 additions & 1 deletion packages/components/src/menu/item-label.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
import { forwardRef, useContext } from '@wordpress/element';

/**
* Internal dependencies
*/
import type { WordPressComponentProps } from '../context';
import { MenuContext } from './context';
import * as Styled from './styles';

export const MenuItemLabel = forwardRef<
HTMLSpanElement,
WordPressComponentProps< { children: React.ReactNode }, 'span', true >
>( function MenuItemLabel( props, ref ) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.ItemLabel can only be rendered inside a Menu component'
);
}

return (
<Styled.MenuItemLabel numberOfLines={ 1 } ref={ ref } { ...props } />
);
Expand Down
8 changes: 7 additions & 1 deletion packages/components/src/menu/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ export const MenuItem = forwardRef<
) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.Item can only be rendered inside a Menu component'
);
}

return (
<Styled.MenuItem
ref={ ref }
{ ...props }
accessibleWhenDisabled
hideOnClick={ hideOnClick }
store={ menuContext?.store }
store={ menuContext.store }
>
<Styled.ItemPrefixWrapper>{ prefix }</Styled.ItemPrefixWrapper>

Expand Down
10 changes: 8 additions & 2 deletions packages/components/src/menu/radio-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,22 @@ export const MenuRadioItem = forwardRef<
) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.RadioItem can only be rendered inside a Menu component'
);
}

return (
<Styled.MenuRadioItem
ref={ ref }
{ ...props }
accessibleWhenDisabled
hideOnClick={ hideOnClick }
store={ menuContext?.store }
store={ menuContext.store }
>
<Ariakit.MenuItemCheck
store={ menuContext?.store }
store={ menuContext.store }
render={ <Styled.ItemPrefixWrapper /> }
// Override some ariakit inline styles
style={ { width: 'auto', height: 'auto' } }
Expand Down
11 changes: 9 additions & 2 deletions packages/components/src/menu/separator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ export const MenuSeparator = forwardRef<
WordPressComponentProps< MenuSeparatorProps, 'hr', false >
>( function MenuSeparator( props, ref ) {
const menuContext = useContext( MenuContext );

if ( ! menuContext?.store ) {
throw new Error(
'Menu.Separator can only be rendered inside a Menu component'
);
}

return (
<Styled.MenuSeparator
ref={ ref }
{ ...props }
store={ menuContext?.store }
variant={ menuContext?.variant }
store={ menuContext.store }
variant={ menuContext.variant }
/>
);
} );
Loading