Skip to content
Open
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
39 changes: 39 additions & 0 deletions docs/data/material/components/menus/CheckboxMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as React from 'react';
import Paper from '@mui/material/Paper';
import MenuList from '@mui/material/MenuList';
import MenuItem from '@mui/material/MenuItem';
import ListItemText from '@mui/material/ListItemText';
import ListItemIcon from '@mui/material/ListItemIcon';
import Check from '@mui/icons-material/Check';

const options = ['Show toolbar', 'Show sidebar', 'Show status bar'];

export default function CheckboxMenu() {
const [checked, setChecked] = React.useState({
'Show toolbar': true,
});

const handleToggle = (option) => () => {
setChecked((prev) => ({ ...prev, [option]: !prev[option] }));
};

return (
<Paper sx={{ width: 320, maxWidth: '100%' }}>
<MenuList>
{options.map((option) => (
<MenuItem
key={option}
role="menuitemcheckbox"
selected={Boolean(checked[option])}
onClick={handleToggle(option)}
>
<ListItemIcon>
{checked[option] ? <Check fontSize="small" /> : null}
</ListItemIcon>
<ListItemText>{option}</ListItemText>
</MenuItem>
))}
</MenuList>
</Paper>
);
}
39 changes: 39 additions & 0 deletions docs/data/material/components/menus/CheckboxMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as React from 'react';
import Paper from '@mui/material/Paper';
import MenuList from '@mui/material/MenuList';
import MenuItem from '@mui/material/MenuItem';
import ListItemText from '@mui/material/ListItemText';
import ListItemIcon from '@mui/material/ListItemIcon';
import Check from '@mui/icons-material/Check';

const options = ['Show toolbar', 'Show sidebar', 'Show status bar'];

export default function CheckboxMenu() {
const [checked, setChecked] = React.useState<Record<string, boolean>>({
'Show toolbar': true,
});

const handleToggle = (option: string) => () => {
setChecked((prev) => ({ ...prev, [option]: !prev[option] }));
};

return (
<Paper sx={{ width: 320, maxWidth: '100%' }}>
<MenuList>
{options.map((option) => (
<MenuItem
key={option}
role="menuitemcheckbox"
selected={Boolean(checked[option])}
onClick={handleToggle(option)}
>
<ListItemIcon>
{checked[option] ? <Check fontSize="small" /> : null}
</ListItemIcon>
<ListItemText>{option}</ListItemText>
</MenuItem>
))}
</MenuList>
</Paper>
);
}
38 changes: 38 additions & 0 deletions docs/data/material/components/menus/RadioMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react';
import Paper from '@mui/material/Paper';
import MenuList from '@mui/material/MenuList';
import MenuItem from '@mui/material/MenuItem';
import ListItemText from '@mui/material/ListItemText';
import ListItemIcon from '@mui/material/ListItemIcon';
import RadioButtonChecked from '@mui/icons-material/RadioButtonChecked';
import RadioButtonUnchecked from '@mui/icons-material/RadioButtonUnchecked';

const options = ['Name', 'Date modified', 'Size'];

export default function RadioMenu() {
const [selected, setSelected] = React.useState('Name');

return (
<Paper sx={{ width: 320, maxWidth: '100%' }}>
<MenuList>
{options.map((option) => (
<MenuItem
key={option}
role="menuitemradio"
selected={selected === option}
onClick={() => setSelected(option)}
>
<ListItemIcon>
{selected === option ? (
<RadioButtonChecked fontSize="small" />
) : (
<RadioButtonUnchecked fontSize="small" />
)}
</ListItemIcon>
<ListItemText>{option}</ListItemText>
</MenuItem>
))}
</MenuList>
</Paper>
);
}
38 changes: 38 additions & 0 deletions docs/data/material/components/menus/RadioMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as React from 'react';
import Paper from '@mui/material/Paper';
import MenuList from '@mui/material/MenuList';
import MenuItem from '@mui/material/MenuItem';
import ListItemText from '@mui/material/ListItemText';
import ListItemIcon from '@mui/material/ListItemIcon';
import RadioButtonChecked from '@mui/icons-material/RadioButtonChecked';
import RadioButtonUnchecked from '@mui/icons-material/RadioButtonUnchecked';

const options = ['Name', 'Date modified', 'Size'];

export default function RadioMenu() {
const [selected, setSelected] = React.useState('Name');

return (
<Paper sx={{ width: 320, maxWidth: '100%' }}>
<MenuList>
{options.map((option) => (
<MenuItem
key={option}
role="menuitemradio"
selected={selected === option}
onClick={() => setSelected(option)}
>
<ListItemIcon>
{selected === option ? (
<RadioButtonChecked fontSize="small" />
) : (
<RadioButtonUnchecked fontSize="small" />
)}
</ListItemIcon>
<ListItemText>{option}</ListItemText>
</MenuItem>
))}
</MenuList>
</Paper>
);
}
11 changes: 11 additions & 0 deletions docs/data/material/components/menus/menus.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ To use a selected menu item without impacting the initial focus, set the `varian

{{"demo": "SimpleListMenu.js"}}

## Checkbox and radio menu items

To build a menu of toggleable options, set each item's `role` to `menuitemcheckbox` for independent toggles, or `menuitemradio` for a single choice within a group.
For these roles, the `selected` prop drives `aria-checked`, so assistive technologies announce the checked state.

{{"demo": "CheckboxMenu.js", "bg": true}}

For a single choice within a group, use `menuitemradio`:

{{"demo": "RadioMenu.js", "bg": true}}

## Positioned menu

Because the `Menu` component uses the `Popover` component to position itself, you can use the same [positioning props](/material-ui/react-popover/#anchor-playground) to position it.
Expand Down
4 changes: 3 additions & 1 deletion docs/translations/api-docs/menu-item/menu-item.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"focusVisibleClassName": {
"description": "This prop can help identify which element has keyboard focus. The class name will be applied when the element gains the focus through keyboard interaction. It&#39;s a polyfill for the <a href=\"https://drafts.csswg.org/selectors-4/#the-focus-visible-pseudo\">CSS :focus-visible selector</a>. The rationale for using this feature <a href=\"https://github.com/WICG/focus-visible/blob/HEAD/explainer.md\">is explained here</a>. A <a href=\"https://github.com/WICG/focus-visible\">polyfill can be used</a> to apply a <code>focus-visible</code> class to other components if needed."
},
"selected": { "description": "If <code>true</code>, the component is selected." },
"selected": {

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.

is this API consistent with other such examples in the library?

if we want to keep this stateless, why not just create an example with both role and aria-checked passed directly by the users? we're not helping much with the selected prop, we just translate it for them.

the other option in my opinion is go stateful, and manage the checked state ourselves. in this case, we probably need a Context wrapper and maybe new component wrapping MenuItem: MenuItemCheckbox and MenuItemRadio or something.

what do you think? @siriwatknp @mj12albert

@mj12albert mj12albert Jun 16, 2026

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.

Re-using the name selected for checkbox/radio items adds indirection because you typically think of them as "checked" and not "selected".

Additionally if the API was to switch components via prop (e.g. MenuItem variant="checkbox"), "selected" could influence any of these 3 things depending on the component:

  1. styling - whether .Mui-selected is added or not
  2. the aria-checked state
  3. the initial focus target - when the selected prop is passed into the roving focus hook

which could get hard to reason about when it's coupled to combinations of the variant and selected props, does that make sense?

So having new checkbox/radio item components gives us a way out of some restrictions/shortcomings of the current API

"description": "If <code>true</code>, the component is selected. For <code>menuitemcheckbox</code> and <code>menuitemradio</code> roles, this also drives <code>aria-checked</code>."
},
"sx": {
"description": "The system prop that allows defining system overrides as well as additional CSS styles."
}
Expand Down
1 change: 1 addition & 0 deletions packages/mui-material/src/MenuItem/MenuItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface MenuItemOwnProps {
divider?: boolean | undefined;
/**
* If `true`, the component is selected.
* For `menuitemcheckbox` and `menuitemradio` roles, this also drives `aria-checked`.
* @default false
*/
selected?: boolean | undefined;
Expand Down
7 changes: 7 additions & 0 deletions packages/mui-material/src/MenuItem/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ const MenuItem = React.forwardRef(function MenuItem(inProps, ref) {
...other
} = props;

// `menuitemcheckbox`/`menuitemradio` require `aria-checked`; derive it from `selected`
// (an omitted `selected` means unchecked). Other roles keep `selected` presentational.
const isCheckableRole = role === 'menuitemcheckbox' || role === 'menuitemradio';
const ariaChecked = isCheckableRole ? Boolean(props.selected) : undefined;

const focusSource = useSelectFocusSource();
const context = React.useContext(ListContext);
const childContext = React.useMemo(
Expand Down Expand Up @@ -250,6 +255,7 @@ const MenuItem = React.forwardRef(function MenuItem(inProps, ref) {
<MenuItemRoot
ref={handleRef}
role={role}
aria-checked={ariaChecked}
tabIndex={tabIndex}
component={component}
internalNativeButton={false}
Expand Down Expand Up @@ -328,6 +334,7 @@ MenuItem.propTypes /* remove-proptypes */ = {
role: PropTypes.string,
/**
* If `true`, the component is selected.
* For `menuitemcheckbox` and `menuitemradio` roles, this also drives `aria-checked`.
* @default false
*/
selected: PropTypes.bool,
Expand Down
55 changes: 55 additions & 0 deletions packages/mui-material/src/MenuItem/MenuItem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,61 @@ describe('<MenuItem />', () => {

expect(menuitem).to.have.class(classes.selected);
expect(menuitem).not.to.have.attribute('aria-selected');
expect(menuitem).not.to.have.attribute('aria-checked');
});

it('drives aria-checked from `selected` for `role="menuitemcheckbox"`', () => {
const { rerender } = render(
<MenuList>
<MenuItem role="menuitemcheckbox" selected />
</MenuList>,
);
const menuitem = screen.getByRole('menuitemcheckbox');

expect(menuitem).to.have.attribute('aria-checked', 'true');
expect(menuitem).not.to.have.attribute('aria-selected');

rerender(
<MenuList>
<MenuItem role="menuitemcheckbox" selected={false} />
</MenuList>,
);

expect(screen.getByRole('menuitemcheckbox')).to.have.attribute('aria-checked', 'false');
});

it('sets aria-checked="false" for a `menuitemcheckbox` when `selected` is omitted', () => {
render(
<MenuList>
<MenuItem role="menuitemcheckbox" />
</MenuList>,
);

expect(screen.getByRole('menuitemcheckbox')).to.have.attribute('aria-checked', 'false');
});

it('drives aria-checked from `selected` for `role="menuitemradio"`', () => {
render(
<MenuList>
<MenuItem role="menuitemradio" selected />
</MenuList>,
);
const menuitem = screen.getByRole('menuitemradio');

expect(menuitem).to.have.attribute('aria-checked', 'true');
expect(menuitem).not.to.have.attribute('aria-selected');
});

it('keeps `selected` presentational and omits aria-checked for non-checkable roles', () => {
render(
<MenuList>
<MenuItem role="option" selected aria-selected />
</MenuList>,
);
const option = screen.getByRole('option');

expect(option).to.have.class(classes.selected);
expect(option).not.to.have.attribute('aria-checked');
});

it('can have a role of option', () => {
Expand Down
Loading