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

MenuItem: Add Storybook stories #53613

Merged
merged 3 commits into from
Aug 24, 2023
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

- `ControlGroup`, `FormGroup`, `ControlLabel`, `Spinner`: Remove unused `ui/` components from the codebase ([#52953](https://github.com/WordPress/gutenberg/pull/52953)).
- `MenuItem`: Convert to TypeScript ([#53132](https://github.com/WordPress/gutenberg/pull/53132)).
- `MenuItem`: Add Storybook stories ([#53613](https://github.com/WordPress/gutenberg/pull/53613)).
- `MenuGroup`: Add Storybook stories ([#53090](https://github.com/WordPress/gutenberg/pull/53090)).
- Components: Remove unnecessary utils ([#53679](https://github.com/WordPress/gutenberg/pull/53679)).

Expand Down
2 changes: 1 addition & 1 deletion packages/components/src/menu-item/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Determines where to display the provided `icon`.
- Type: `boolean`
- Required: No

Whether or not the menu item is currently selected.
Whether or not the menu item is currently selected. `isSelected` is only taken into account when the `role` prop is either `"menuitemcheckbox"` or `"menuitemradio"`.

### `shortcut`

Expand Down
7 changes: 5 additions & 2 deletions packages/components/src/menu-item/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Icon from '../icon';
import type { WordPressComponentProps } from '../ui/context';
import type { MenuItemProps } from './types';

export function MenuItem(
function UnforwardedMenuItem(
props: WordPressComponentProps< MenuItemProps, 'button', false >,
ref: ForwardedRef< HTMLButtonElement >
) {
Expand Down Expand Up @@ -97,6 +97,7 @@ export function MenuItem(
* <MenuItem
* icon={ isActive ? 'yes' : 'no' }
* isSelected={ isActive }
* role="menuitemcheckbox"
* onClick={ () => setIsActive( ( state ) => ! state ) }
* >
* Toggle
Expand All @@ -105,4 +106,6 @@ export function MenuItem(
* };
* ```
*/
export default forwardRef( MenuItem );
export const MenuItem = forwardRef( UnforwardedMenuItem );

export default MenuItem;
80 changes: 80 additions & 0 deletions packages/components/src/menu-item/stories/index.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* External dependencies
*/
import type { Meta, StoryFn } from '@storybook/react';

/**
* WordPress dependencies
*/
import { link, more, check } from '@wordpress/icons';

/**
* Internal dependencies
*/
import MenuGroup from '../../menu-group';
import MenuItem from '..';
import Shortcut from '../../shortcut';

const meta: Meta< typeof MenuItem > = {
component: MenuItem,
title: 'Components/MenuItem',
argTypes: {
children: { control: { type: null } },
icon: {
control: { type: 'select' },
options: [ 'check', 'link', 'more' ],
mapping: {
check,
link,
more,
},
},
},
parameters: {
controls: {
expanded: true,
},
docs: { canvas: { sourceState: 'shown' } },
},
};
export default meta;

const Template: StoryFn< typeof MenuItem > = ( props ) => {
return (
<MenuGroup>
<MenuItem { ...props }>Menu Item 1</MenuItem>
</MenuGroup>
);
};

export const Default: StoryFn< typeof MenuItem > = Template.bind( {} );

/**
* When the `role` prop is either `"menuitemcheckbox"` or `"menuitemradio"`, the
* `isSelected` prop should be used so screen readers can tell which item is currently selected.
*/
export const IsSelected = Template.bind( {} );
IsSelected.args = {
...Default.args,
isSelected: true,
role: 'menuitemcheckbox',
};

export const WithIcon = Template.bind( {} );
WithIcon.args = {
...Default.args,
icon: link,
iconPosition: 'left',
};

export const WithInfo = Template.bind( {} );
WithInfo.args = {
...Default.args,
info: 'Menu Item description',
};

export const WithSuffix = Template.bind( {} );
WithSuffix.args = {
...Default.args,
suffix: <Shortcut shortcut="Ctrl+M"></Shortcut>,
};
5 changes: 3 additions & 2 deletions packages/components/src/menu-item/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { ReactNode } from 'react';
*/
import type { ButtonAsButtonProps } from '../button/types';

export type MenuItemProps = ButtonAsButtonProps & {
export type MenuItemProps = Pick< ButtonAsButtonProps, 'isDestructive' > & {
/**
* A CSS `class` to give to the container element.
*/
Expand All @@ -33,7 +33,8 @@ export type MenuItemProps = ButtonAsButtonProps & {
*/
iconPosition?: ButtonAsButtonProps[ 'iconPosition' ];
/**
* Whether or not the menu item is currently selected.
* Whether or not the menu item is currently selected, `isSelected` is only taken into
* account when the `role` prop is either `"menuitemcheckbox"` or `"menuitemradio"`.
*/
isSelected?: boolean;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ const ToolsPanelHeader = (
<MenuGroup>
<MenuItem
aria-disabled={ ! canResetAll }
// @ts-expect-error - TODO: If this "tertiary" style is something we really want to allow on MenuItem,
// we should rename it and explicitly allow it as an official API. All the other Button variants
// don't make sense in a MenuItem context, and should be disallowed.
variant={ 'tertiary' }
onClick={ () => {
if ( canResetAll ) {
Expand Down
Loading