Skip to content

Commit

Permalink
MenuItem: Add Storybook stories (#53613)
Browse files Browse the repository at this point in the history
* Generate default stories for MenuItem

Add more stories for menu-item

Update MenuItem import index.story.tsx

Update packages/components/src/menu-item/index.tsx

Co-authored-by: Lena Morita <lena@jaguchi.com>

* Update changelog

* Update packages/components/src/menu-item/README.md

Co-authored-by: Lena Morita <lena@jaguchi.com>

---------

Co-authored-by: Lena Morita <lena@jaguchi.com>
  • Loading branch information
bangank36 and mirka authored Aug 24, 2023
1 parent 9a2f2cc commit 1bc4205
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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

1 comment on commit 1bc4205

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 1bc4205.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5969736936
📝 Reported issues:

Please sign in to comment.