From 875db235449d0772c1044e5b6f0326568ba6208d Mon Sep 17 00:00:00 2001 From: Alex Carpenter Date: Tue, 24 Nov 2026 17:41:49 -0500 Subject: [PATCH 1/2] feat(mosaic)!: remove Menu.Indicator and add checkbox and radio items to Menu --- .changeset/remove-menu-indicator.md | 22 ++ .../action-menu/action-menu.test.tsx | 6 +- .../components/action-menu/action-menu.tsx | 4 +- packages/mosaic/src/components/menu/index.ts | 6 +- .../mosaic/src/components/menu/menu.doc.ts | 116 +++++++-- .../mosaic/src/components/menu/menu.parts.ts | 6 +- .../mosaic/src/components/menu/menu.styles.ts | 36 +-- .../mosaic/src/components/menu/menu.test.tsx | 95 ++++++-- packages/mosaic/src/components/menu/menu.tsx | 167 +++++++++---- .../__tests__/switcher.feature.test.tsx | 38 ++- .../src/features/switcher/switcher.view.tsx | 55 +++-- packages/mosaic/src/primitives/menu/README.md | 99 ++++++-- packages/mosaic/src/primitives/menu/index.ts | 5 + .../primitives/menu/menu-checkable-context.ts | 40 ++++ .../primitives/menu/menu-checkbox-item.tsx | 48 ++++ .../primitives/menu/menu-item-indicator.tsx | 56 +++++ .../src/primitives/menu/menu-radio-group.tsx | 40 ++++ .../src/primitives/menu/menu-radio-item.tsx | 39 +++ .../mosaic/src/primitives/menu/menu.doc.ts | 71 +++++- .../mosaic/src/primitives/menu/menu.test.tsx | 226 ++++++++++++++++++ packages/mosaic/src/primitives/menu/parts.ts | 9 + packages/mosaic/src/styles/index.ts | 6 +- .../swingset/src/stories/menu.component.mdx | 121 ++++++---- .../src/stories/menu.component.stories.tsx | 79 ++++-- 24 files changed, 1165 insertions(+), 225 deletions(-) create mode 100644 .changeset/remove-menu-indicator.md create mode 100644 packages/mosaic/src/primitives/menu/menu-checkable-context.ts create mode 100644 packages/mosaic/src/primitives/menu/menu-checkbox-item.tsx create mode 100644 packages/mosaic/src/primitives/menu/menu-item-indicator.tsx create mode 100644 packages/mosaic/src/primitives/menu/menu-radio-group.tsx create mode 100644 packages/mosaic/src/primitives/menu/menu-radio-item.tsx diff --git a/.changeset/remove-menu-indicator.md b/.changeset/remove-menu-indicator.md new file mode 100644 index 00000000000..826e2d7e253 --- /dev/null +++ b/.changeset/remove-menu-indicator.md @@ -0,0 +1,22 @@ +--- +'@clerk/mosaic': minor +--- + +**Breaking:** Remove `Menu.Indicator` and the `size` prop on `Menu.Media`. Menu now has checkbox and radio items, so a checkmark that marks the current choice goes in their indicator and is announced with `aria-checked`. Anything else at the end of a row, such as a spinner, goes in a trailing `Menu.Media`, which keeps its width while empty. `Menu.Media` is now always 20px wide; `size='xs'` made it 16px without changing the row height, so the label no longer lined up with the other rows. + +| Before | After | +| ------------------------------------------------------- | -------------------------------------------------------------------------------------------------- | +| `` with a check in `` | `` in a ``, with the check in `` | +| A toggled `` with a check in `` | ``, with the check in `` | +| Any other `…` | `…` | +| `MenuIndicatorProps` | `MenuMediaProps`, `MenuRadioItemIndicatorProps` or `MenuCheckboxItemIndicatorProps` | +| `.cl-menu-item-indicator` | `.cl-menu-item-media`, `.cl-menu-radio-item-indicator` or `.cl-menu-checkbox-item-indicator` | +| `` or `size='sm'` | `` | +| `MenuMediaSize` | Remove it | +| `.cl-menu-item-media[data-size]` | `.cl-menu-item-media` | + +Find usages with `git grep -nE 'Menu\.Indicator|MenuIndicatorProps|cl-menu-item-indicator|MenuMediaSize|Menu\.Media[^>]*size='`. + +Checkbox and radio items keep the menu open on click. Pass `closeOnClick` to close it. + +In the Switcher's accounts flyout, each account is now a `menuitemradio` inside a group named "Switch account", and the active one has `aria-checked="true"` in place of `aria-current="true"`. diff --git a/packages/mosaic/src/components/action-menu/action-menu.test.tsx b/packages/mosaic/src/components/action-menu/action-menu.test.tsx index 0e93acbc5df..c79669dcdd9 100644 --- a/packages/mosaic/src/components/action-menu/action-menu.test.tsx +++ b/packages/mosaic/src/components/action-menu/action-menu.test.tsx @@ -90,12 +90,14 @@ describe('Mosaic ActionMenu', () => { await waitFor(() => expect(screen.queryByRole('menu')).not.toBeInTheDocument()); }); - it('reserves an indicator immediately and delays its named spinner', async () => { + it('reserves a trailing media column immediately and delays its named spinner', async () => { const user = userEvent.setup(); render(); await user.click(screen.getByRole('button', { name: 'Manage passkey' })); const item = await screen.findByRole('menuitem', { name: 'Regenerate' }); - expect(item.querySelector('.cl-menu-item-indicator')).toBeInTheDocument(); + expect(item.lastElementChild).toHaveClass('cl-menu-item-media'); + expect(item.lastElementChild).toBeEmptyDOMElement(); + expect(screen.getByRole('menuitem', { name: 'Remove' }).querySelector('.cl-menu-item-media')).toBeNull(); vi.useFakeTimers(); try { fireEvent.click(item); diff --git a/packages/mosaic/src/components/action-menu/action-menu.tsx b/packages/mosaic/src/components/action-menu/action-menu.tsx index 887a80cbfd3..f6f45704150 100644 --- a/packages/mosaic/src/components/action-menu/action-menu.tsx +++ b/packages/mosaic/src/components/action-menu/action-menu.tsx @@ -78,14 +78,14 @@ export function ActionMenu({ label, actions, triggerRef, disabled, focusableWhen ) : null} {action.label} {action.status !== undefined ? ( - + {action.status === 'pending' && showSpinner ? ( ) : null} - + ) : null} ))} diff --git a/packages/mosaic/src/components/menu/index.ts b/packages/mosaic/src/components/menu/index.ts index b4647c1f23a..2e87b79b91c 100644 --- a/packages/mosaic/src/components/menu/index.ts +++ b/packages/mosaic/src/components/menu/index.ts @@ -1,11 +1,15 @@ export * as Menu from './menu.parts'; export type { + MenuCheckboxItemIndicatorProps, + MenuCheckboxItemProps, MenuItemProps, MenuLabelProps, MenuMediaProps, - MenuMediaSize, MenuPopupProps, MenuProps, + MenuRadioGroupProps, + MenuRadioItemIndicatorProps, + MenuRadioItemProps, MenuSeparatorProps, MenuTriggerProps, } from './menu'; diff --git a/packages/mosaic/src/components/menu/menu.doc.ts b/packages/mosaic/src/components/menu/menu.doc.ts index 967ba242cba..ffeaa5212c3 100644 --- a/packages/mosaic/src/components/menu/menu.doc.ts +++ b/packages/mosaic/src/components/menu/menu.doc.ts @@ -1,11 +1,15 @@ import type { ComponentDoc } from '../../docs-schema'; import type { MenuProps } from '../../primitives/menu/menu-root'; import type { - MenuIndicatorProps, + MenuCheckboxItemIndicatorProps, + MenuCheckboxItemProps, MenuItemProps, MenuLabelProps, MenuMediaProps, MenuPopupProps, + MenuRadioGroupProps, + MenuRadioItemIndicatorProps, + MenuRadioItemProps, MenuSeparatorProps, MenuTriggerProps, } from './menu'; @@ -18,8 +22,10 @@ export default { 'a list of actions behind an ellipsis button', 'a row or card overflow menu', 'a destructive action among others, with `color="negative"`', + 'a setting toggled on or off, with `Menu.CheckboxItem`', + 'marking the current choice, such as the active account, with `Menu.RadioItem`', ], - keywords: ['menu', 'dropdown', 'overflow', 'ellipsis', 'more actions', 'kebab'], + keywords: ['menu', 'dropdown', 'overflow', 'ellipsis', 'more actions', 'kebab', 'checkbox', 'radio', 'checked'], practices: [ { do: true, @@ -29,12 +35,15 @@ export default { do: true, text: 'Put the item text in `Menu.Label`, not straight in `Menu.Item`, so it lines up and truncates.', }, - { do: true, text: 'Use the same `Menu.Media` size for every item in one menu, so the text starts on one line.' }, { do: true, text: 'Pass an `aria-label` to a `Menu.Trigger` with no children. The default ellipsis has no name.', }, - { do: false, text: 'Use it to pick a value. Use `Select` instead.' }, + { + do: true, + text: 'Show a checked state with `Menu.CheckboxItem` or `Menu.RadioItem` and their indicator, not a checkmark in `Menu.Media`, so `aria-checked` announces it.', + }, + { do: false, text: 'Use it as a form field that picks a value. Use `Select` instead.' }, ], parts: { 'Menu.Root': { @@ -85,8 +94,7 @@ export default { }, }, 'Menu.Item': { - description: - 'One action, as a `button`. What it shows is the `Menu.Media`, `Menu.Label` and `Menu.Indicator` inside it.', + description: 'One action, as a `button`. What it shows is the `Menu.Media` and `Menu.Label` inside it.', slot: 'cl-menu-item', props: { label: { type: 'string', doc: 'The text typeahead matches on. Not rendered and not the accessible name.' }, @@ -96,23 +104,78 @@ export default { }, }, 'Menu.Media': { - description: 'A square leading column that centers an icon, image or avatar.', + description: + 'A square column at either end of the row that centers an icon, image, avatar or spinner. Keeps its width while empty.', slot: 'cl-menu-item-media', + }, + 'Menu.Label': { + description: 'The item text. Fills the space between the media at either end and cuts to one line.', + slot: 'cl-menu-item-label', + }, + 'Menu.CheckboxItem': { + description: + 'An item that toggles on and off, as a `button` with `role="menuitemcheckbox"`. Keeps the menu open on click by default.', + slot: 'cl-menu-checkbox-item', props: { - size: { - type: 'MenuMediaSize', - doc: '`sm` fits an icon or avatar, `xs` a bare glyph. Sets the row height.', - default: "'sm'", + label: { type: 'string', doc: 'The text typeahead matches on. Not rendered and not the accessible name.' }, + checked: { type: 'boolean', doc: 'Whether the item is checked, when controlled.' }, + defaultChecked: { + type: 'boolean', + doc: 'Whether the item starts checked, when uncontrolled.', + default: 'false', }, + onCheckedChange: { type: '(checked: boolean) => void', doc: 'Called with the new checked state.' }, + disabled: { type: 'boolean', doc: 'Disables the item and ignores its `onClick`.' }, + closeOnClick: { type: 'boolean', doc: 'Closes the menu after a click.', default: 'false' }, }, }, - 'Menu.Label': { - description: 'The item text. Fills the space between media and indicator and cuts to one line.', - slot: 'cl-menu-item-label', + 'Menu.RadioGroup': { + description: 'Holds the value of the radio items inside it. Name it with `aria-label`.', + slot: 'cl-menu-radio-group', + props: { + value: { type: 'string', doc: "The checked item's value, when controlled." }, + defaultValue: { type: 'string', doc: "The checked item's value on mount, when uncontrolled." }, + onValueChange: { + type: '(value: string) => void', + doc: "Called with the picked item's value, even when it is already checked.", + }, + disabled: { type: 'boolean', doc: 'Disables every item in the group.' }, + }, + }, + 'Menu.RadioItem': { + description: + 'An item that picks its group\'s value, as a `button` with `role="menuitemradio"`. Keeps the menu open on click by default.', + slot: 'cl-menu-radio-item', + props: { + label: { type: 'string', doc: 'The text typeahead matches on. Not rendered and not the accessible name.' }, + value: { type: 'string', doc: 'The value the group takes when this item is picked.' }, + disabled: { type: 'boolean', doc: 'Disables the item and ignores its `onClick`.' }, + closeOnClick: { type: 'boolean', doc: 'Closes the menu after a click.', default: 'false' }, + }, + }, + 'Menu.CheckboxItemIndicator': { + description: + 'A square column in every row with a check in the secondary icon color, shown only while its checkbox item is checked.', + slot: 'cl-menu-checkbox-item-indicator', + props: { + keepMounted: { + type: 'boolean', + doc: 'Keeps its column, with the check hidden, while unchecked, so labels line up. Pass `false` to render it only while checked.', + default: 'true', + }, + }, }, - 'Menu.Indicator': { - description: 'A small square trailing slot, such as for a spinner while the action runs.', - slot: 'cl-menu-item-indicator', + 'Menu.RadioItemIndicator': { + description: + 'A square column in every row with a check in the secondary icon color, shown only while its radio item is checked.', + slot: 'cl-menu-radio-item-indicator', + props: { + keepMounted: { + type: 'boolean', + doc: 'Keeps its column, with the check hidden, while unchecked, so labels line up. Pass `false` to render it only while checked.', + default: 'true', + }, + }, }, 'Menu.Separator': { description: 'A full-width divider between groups of items.', @@ -135,6 +198,19 @@ export default { +`, + }, + { + title: 'Picking the current account', + code: ` + + {accounts.map(account => ( + + {account.name} + + + ))} + `, }, ], @@ -146,6 +222,10 @@ export default { 'Menu.Item': MenuItemProps; 'Menu.Media': MenuMediaProps; 'Menu.Label': MenuLabelProps; - 'Menu.Indicator': MenuIndicatorProps; + 'Menu.CheckboxItem': MenuCheckboxItemProps; + 'Menu.RadioGroup': MenuRadioGroupProps; + 'Menu.RadioItem': MenuRadioItemProps; + 'Menu.CheckboxItemIndicator': MenuCheckboxItemIndicatorProps; + 'Menu.RadioItemIndicator': MenuRadioItemIndicatorProps; 'Menu.Separator': MenuSeparatorProps; }>; diff --git a/packages/mosaic/src/components/menu/menu.parts.ts b/packages/mosaic/src/components/menu/menu.parts.ts index 01246afffbd..39e3a815948 100644 --- a/packages/mosaic/src/components/menu/menu.parts.ts +++ b/packages/mosaic/src/components/menu/menu.parts.ts @@ -3,8 +3,12 @@ export { MenuTrigger as Trigger, MenuPopup as Popup, MenuItem as Item, + MenuCheckboxItem as CheckboxItem, + MenuRadioGroup as RadioGroup, + MenuRadioItem as RadioItem, + MenuCheckboxItemIndicator as CheckboxItemIndicator, + MenuRadioItemIndicator as RadioItemIndicator, MenuMedia as Media, MenuLabel as Label, - MenuIndicator as Indicator, MenuSeparator as Separator, } from './menu'; diff --git a/packages/mosaic/src/components/menu/menu.styles.ts b/packages/mosaic/src/components/menu/menu.styles.ts index 6a9d4111ec9..1d712ede0ce 100644 --- a/packages/mosaic/src/components/menu/menu.styles.ts +++ b/packages/mosaic/src/components/menu/menu.styles.ts @@ -143,19 +143,7 @@ export const media = stylex.create({ display: 'flex', flexShrink: 0, justifyContent: 'center', - }, - xs: { width: space['4'] }, - sm: { width: space['5'] }, -}); - -// The item lays its children out in one flat row, so the label is what has to take the space -// between the media and whatever trails it, rather than the row spacing the three evenly. -// `minWidth: 0` is what lets it shrink past its text and truncate instead of pushing the row wide. -export const label = stylex.create({ - base: { - paddingInline: space['1'], - flexGrow: 1, - minWidth: 0, + width: space['5'], }, }); @@ -167,7 +155,27 @@ export const indicator = stylex.create({ display: 'flex', flexShrink: 0, justifyContent: 'center', - width: space['4'], + visibility: { default: 'hidden', ':is([data-checked])': 'visible' }, + width: space['5'], + }, +}); + +export const radioGroup = stylex.create({ + base: { + gap: space['0.5'], + display: 'flex', + flexDirection: 'column', + }, +}); + +// The item lays its children out in one flat row, so the label is what has to take the space +// between the media and whatever trails it, rather than the row spacing the three evenly. +// `minWidth: 0` is what lets it shrink past its text and truncate instead of pushing the row wide. +export const label = stylex.create({ + base: { + paddingInline: space['1'], + flexGrow: 1, + minWidth: 0, }, }); diff --git a/packages/mosaic/src/components/menu/menu.test.tsx b/packages/mosaic/src/components/menu/menu.test.tsx index 5ac6451fc2a..47f8eaf6b06 100644 --- a/packages/mosaic/src/components/menu/menu.test.tsx +++ b/packages/mosaic/src/components/menu/menu.test.tsx @@ -295,6 +295,78 @@ describe('Mosaic Menu', () => { expect(screen.getByRole('menuitem', { name: 'Sign out' }).querySelector('.cl-menu-item-media')).toBeInTheDocument(); }); + it('styles checkbox and radio items as rows and marks their indicator with its own slot', async () => { + const user = userEvent.setup(); + render( + + + + + Show archived + + + + + Alice + + + + + , + ); + + const archived = screen.getByRole('menuitemcheckbox', { name: 'Show archived' }); + expect(archived).toHaveClass('cl-menu-checkbox-item'); + expect(screen.getByRole('group', { name: 'Accounts' })).toHaveClass('cl-menu-radio-group'); + expect(screen.getByRole('menuitemradio', { name: 'Alice' })).toHaveClass('cl-menu-radio-item'); + expect(screen.getByTestId('alice-check')).toHaveClass('cl-menu-radio-item-indicator'); + expect(screen.getByTestId('archived-check')).not.toHaveAttribute('data-checked'); + + await user.click(archived); + + expect(screen.getByTestId('archived-check')).toHaveClass('cl-menu-checkbox-item-indicator'); + }); + + it('keeps a check mounted in every row by default, marking only the checked one', () => { + render( + + + + + + Alice + + + + Bob + + + + + , + ); + + expect(screen.getByTestId('alice-check').querySelector('svg')).toBeInTheDocument(); + expect(screen.getByTestId('bob-check').querySelector('svg')).toBeInTheDocument(); + expect(screen.getByTestId('alice-check')).toHaveAttribute('data-checked', ''); + expect(screen.getByTestId('bob-check')).not.toHaveAttribute('data-checked'); + }); + it('forwards the media ref and swaps its element via render', () => { const ref = React.createRef(); render( @@ -334,29 +406,6 @@ describe('Mosaic Menu', () => { expect(label).toHaveTextContent('colin@clerk.dev'); }); - it('sizes the media to sm by default, and reflects the size it is given', () => { - render( - - - - - - Sign out - - - - colin@clerk.dev - - - , - ); - - const mediaOf = (name: string) => screen.getByRole('menuitem', { name }).querySelector('.cl-menu-item-media'); - - expect(mediaOf('Sign out')).toHaveAttribute('data-size', 'sm'); - expect(mediaOf('colin@clerk.dev')).toHaveAttribute('data-size', 'xs'); - }); - it('forwards the trigger ref', () => { const ref = React.createRef(); render( diff --git a/packages/mosaic/src/components/menu/menu.tsx b/packages/mosaic/src/components/menu/menu.tsx index 594b53c9729..79889b3d814 100644 --- a/packages/mosaic/src/components/menu/menu.tsx +++ b/packages/mosaic/src/components/menu/menu.tsx @@ -3,12 +3,18 @@ import * as stylex from '@stylexjs/stylex'; import React from 'react'; +import { Checkmark } from '../../icons/glyphs/checkmark'; import { EllipsisHorizontal } from '../../icons/glyphs/ellipsis-horizontal'; import type { + MenuCheckboxItemIndicatorProps as PrimitiveMenuCheckboxItemIndicatorProps, + MenuCheckboxItemProps as PrimitiveMenuCheckboxItemProps, MenuItemProps as PrimitiveMenuItemProps, MenuPopupProps as PrimitiveMenuPopupProps, MenuPortalProps, MenuProps, + MenuRadioGroupProps as PrimitiveMenuRadioGroupProps, + MenuRadioItemIndicatorProps as PrimitiveMenuRadioItemIndicatorProps, + MenuRadioItemProps as PrimitiveMenuRadioItemProps, MenuSeparatorProps as PrimitiveMenuSeparatorProps, } from '../../primitives/menu'; import { Menu as Primitive } from '../../primitives/menu'; @@ -107,27 +113,15 @@ export const MenuPopup = React.forwardRef(functi ); }); -/** The width of the media column, and so the height of the row it sits in. */ -export type MenuMediaSize = 'xs' | 'sm'; - /** `span`, not `div`: the item this sits in is a button, so its children are phrasing content. */ -export type MenuMediaProps = MosaicComponentProps<'span'> & { - /** - * Column width: `sm` fits an icon or an avatar, `xs` a bare glyph. The row takes its height - * from this, so every item in one menu wants the same value or their text no longer starts on - * one line. - * - * @default 'sm' - */ - size?: MenuMediaSize; -}; +export type MenuMediaProps = MosaicComponentProps<'span'>; /** - * Square leading column that centers its media (icon, image, or avatar), so every item's - * text starts on the same line whatever each one leads with. + * Square column at either end of the row that centers its content (icon, image, avatar, spinner), + * so every item's text lines up whatever each one leads or trails with. */ export const MenuMedia = React.forwardRef(function MosaicMenuMedia( - { size = 'sm', render, xstyle, ...rest }, + { render, xstyle, ...rest }, ref, ) { return useRender({ @@ -135,11 +129,7 @@ export const MenuMedia = React.forwardRef(funct render, ref, props: { - ...mergeStyleProps( - themeProps('menu-item-media', { size }), - stylex.props(reset.base, slots.media.base, slots.media[size], xstyle), - rest, - ), + ...mergeStyleProps(themeProps('menu-item-media'), stylex.props(reset.base, slots.media.base, xstyle), rest), }, }); }); @@ -170,31 +160,11 @@ export const MenuLabel = React.forwardRef(funct }); }); -export type MenuIndicatorProps = MosaicComponentProps<'span'>; - -export const MenuIndicator = React.forwardRef(function MosaicMenuIndicator( - { render, xstyle, ...rest }, - ref, -) { - return useRender({ - defaultTagName: 'span', - render, - ref, - props: { - ...mergeStyleProps( - themeProps('menu-item-indicator'), - stylex.props(reset.base, slots.indicator.base, xstyle), - rest, - ), - }, - }); -}); - export interface MenuItemProps extends Omit, MosaicStyleProps { /** Semantic color of the action. */ color?: 'neutral' | 'negative'; /** - * The row's content, composed from `Menu.Media`, `Menu.Label` and `Menu.Indicator`. Required, and text goes in + * The row's content, composed from `Menu.Media` and `Menu.Label`. Required, and text goes in * `Menu.Label` rather than straight in here: a bare text node is not a flex item the row can * size, so it neither lines up with the other rows nor truncates. */ @@ -231,6 +201,119 @@ export const MenuItem = React.forwardRef(funct ); }); +export interface MenuCheckboxItemProps + extends Omit, MosaicStyleProps { + /** The row's content, composed from `Menu.Media`, `Menu.Label` and `Menu.CheckboxItemIndicator`. */ + children: React.ReactNode; +} + +/** + * A menu item that toggles a setting on and off, announced through `aria-checked`. Keeps the menu + * open on click unless `closeOnClick` is set. + */ +export const MenuCheckboxItem = React.forwardRef( + function MosaicMenuCheckboxItem({ xstyle, ...rest }, ref) { + return ( + + ); + }, +); + +export type MenuRadioGroupProps = Omit & MosaicStyleProps; + +/** Holds the value of the `Menu.RadioItem`s inside it. Name it with `aria-label`. */ +export const MenuRadioGroup = React.forwardRef(function MosaicMenuRadioGroup( + { xstyle, ...rest }, + ref, +) { + return ( + + ); +}); + +export interface MenuRadioItemProps extends Omit, MosaicStyleProps { + /** The row's content, composed from `Menu.Media`, `Menu.Label` and `Menu.RadioItemIndicator`. */ + children: React.ReactNode; +} + +/** + * A menu item that picks its `Menu.RadioGroup`'s value, announced through `aria-checked`. Keeps the + * menu open on click unless `closeOnClick` is set. + */ +export const MenuRadioItem = React.forwardRef(function MosaicMenuRadioItem( + { xstyle, ...rest }, + ref, +) { + return ( + + ); +}); + +export type MenuCheckboxItemIndicatorProps = Omit & + MosaicStyleProps; + +/** Square column with a check in the secondary icon color, in every row and shown only while its `Menu.CheckboxItem` is checked. */ +export const MenuCheckboxItemIndicator = React.forwardRef( + function MosaicMenuCheckboxItemIndicator({ xstyle, children, keepMounted = true, ...rest }, ref) { + return ( + + {children ?? } + + ); + }, +); + +export type MenuRadioItemIndicatorProps = Omit & + MosaicStyleProps; + +/** Square column with a check in the secondary icon color, in every row and shown only while its `Menu.RadioItem` is checked. */ +export const MenuRadioItemIndicator = React.forwardRef( + function MosaicMenuRadioItemIndicator({ xstyle, children, keepMounted = true, ...rest }, ref) { + return ( + + {children ?? } + + ); + }, +); + export type MenuSeparatorProps = Omit & MosaicStyleProps; /** A full-bleed divider between groups of items. */ diff --git a/packages/mosaic/src/features/switcher/__tests__/switcher.feature.test.tsx b/packages/mosaic/src/features/switcher/__tests__/switcher.feature.test.tsx index 9f90c56f998..ee6ba9d4fb8 100644 --- a/packages/mosaic/src/features/switcher/__tests__/switcher.feature.test.tsx +++ b/packages/mosaic/src/features/switcher/__tests__/switcher.feature.test.tsx @@ -198,7 +198,7 @@ describe('Switcher', () => { expect(screen.getByRole('button', { name: 'Sign out' })).toBeInTheDocument(); const menu = await openAccounts(user); - expect(within(menu).getByRole('menuitem', { name: 'bob@example.com' })).toBeInTheDocument(); + expect(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })).toBeInTheDocument(); }); }); @@ -273,7 +273,7 @@ describe('Switcher', () => { const user = await open(); const menu = await openAccounts(user); - expect(within(menu).getByRole('menuitem', { name: 'bob@example.com' })).toBeInTheDocument(); + expect(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })).toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'Other' })).toBeNull(); }); }); @@ -284,13 +284,25 @@ describe('Switcher', () => { const user = await open(); const menu = await openAccounts(user); - await user.click(within(menu).getByRole('menuitem', { name: 'bob@example.com' })); + await user.click(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })); await waitFor(() => expect(trigger()).toHaveAccessibleName('Open account menu for Bob Jones')); await waitFor(() => expect(waiting()).toBe(false)); expect(popup()).toBeInTheDocument(); }); + it('closes the flyout without switching when the account it is on is picked', async () => { + const { navigate } = await renderSwitcher(); + const user = await open(); + + const menu = await openAccounts(user); + await user.click(within(menu).getByRole('menuitemradio', { name: 'alice' })); + + await waitFor(() => expect(menu).not.toBeInTheDocument()); + expect(navigate).not.toHaveBeenCalled(); + expect(waiting()).toBe(false); + }); + it('lands a switched account where the instance sends it', async () => { const { navigate } = await renderSwitcher( {}, @@ -299,7 +311,7 @@ describe('Switcher', () => { const user = await open(); const menu = await openAccounts(user); - await user.click(within(menu).getByRole('menuitem', { name: 'bob@example.com' })); + await user.click(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })); await waitFor(() => expect(navigate).toHaveBeenCalledWith(expect.stringMatching(/\/switched$/))); }); @@ -717,7 +729,7 @@ describe('Switcher', () => { const user = await open(); const menu = await openAccounts(user); - await user.click(within(menu).getByRole('menuitem', { name: 'bob@example.com' })); + await user.click(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })); await waitFor(() => expect(navigate).toHaveBeenCalledWith('/sign-in#/tasks/choose-organization')); }); @@ -810,7 +822,7 @@ describe('Switcher', () => { try { const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime }); const menu = await openAccounts(user); - await user.click(within(menu).getByRole('menuitem', { name: 'bob@example.com' })); + await user.click(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })); expect(waiting()).toBe(true); await waitFor(() => expect(touch.requests).toHaveLength(1)); @@ -963,7 +975,9 @@ describe('Switcher', () => { await renderSwitcher({ mode: 'user' }); const user = await open(); - const items = within(await openAccounts(user)).getAllByRole('menuitem'); + const items = Array.from( + (await openAccounts(user)).querySelectorAll('[role="menuitemradio"], [role="menuitem"]'), + ); expect(items).toHaveLength(4); expect(items[0]).toHaveAccessibleName('alice'); @@ -1042,8 +1056,12 @@ describe('Switcher', () => { const menu = await openAccounts(user); - expect(within(menu).getByRole('menuitem', { name: 'alice' })).toHaveAttribute('aria-current', 'true'); - expect(within(menu).getByRole('menuitem', { name: 'bob@example.com' })).not.toHaveAttribute('aria-current'); + expect(within(menu).getByRole('group', { name: 'Switch account' })).toBeInTheDocument(); + expect(within(menu).getByRole('menuitemradio', { name: 'alice' })).toHaveAttribute('aria-checked', 'true'); + expect(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })).toHaveAttribute( + 'aria-checked', + 'false', + ); expect(within(menu).getByRole('menuitem', { name: 'Add account' })).toBeInTheDocument(); expect(within(menu).getByRole('menuitem', { name: 'Sign out of all accounts' })).toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'Add account' })).toBeNull(); @@ -1290,7 +1308,7 @@ describe('Switcher', () => { const touch = holdRequests('post', '/v1/client/sessions/:id/touch'); await openAccounts(user).then(menu => - user.click(within(menu).getByRole('menuitem', { name: 'bob@example.com' })), + user.click(within(menu).getByRole('menuitemradio', { name: 'bob@example.com' })), ); const after = screen.getByRole('button', { name: label }); diff --git a/packages/mosaic/src/features/switcher/switcher.view.tsx b/packages/mosaic/src/features/switcher/switcher.view.tsx index b693da662ca..9c0daa37165 100644 --- a/packages/mosaic/src/features/switcher/switcher.view.tsx +++ b/packages/mosaic/src/features/switcher/switcher.view.tsx @@ -541,23 +541,17 @@ function PendingRows() { } /** - * A signed-in account inside the accounts flyout: a menu item you pick to switch to, checked where + * A signed-in account inside the accounts flyout: a radio item you pick to switch to, checked where * it is already the active one. Its organizations cannot be listed here — they are scoped to the * session that fetches them — so switching is all it offers. */ -function SessionMenuItem({ session, active }: { session: SwitcherSession; active: boolean }) { - const data = useSwitcherContext(); - const switchSession = data.onSwitchSession; - +function SessionMenuItem({ session }: { session: SwitcherSession }) { return ( - switchSession(session.sessionId)} + value={session.sessionId} + closeOnClick > {session.identifier} - {active ? ( - - - - ) : null} - + + ); } @@ -631,6 +621,8 @@ function SwitchAccountRow() { const data = useSwitcherContext(); const addAccount = data.onAddAccount; const signOutAll = data.onSignOutAll; + const switchSession = data.onSwitchSession; + const activeSessionId = data.activeSession.sessionId; const { pendingKey } = data; const busy = pendingKey === switcherBusyKeys.signOutAll() || @@ -644,17 +636,24 @@ function SwitchAccountRow() { > {/* The account it is on leads, checked: the flyout is the full set of accounts rather than a list of somewhere else to go. */} - - {data.additionalSessions.map(s => ( - - ))} + { + if (id !== activeSessionId) { + switchSession?.(id); + } + }} + > + + {data.additionalSessions.map(s => ( + + ))} + {addAccount ? ( ` inside a parent `` — the nested trigger automa ``` +### Checkbox and Radio Items + +`Menu.CheckboxItem` toggles a setting; `Menu.RadioItem` picks one value from its `Menu.RadioGroup`. Both keep the menu open on click unless you pass `closeOnClick`. Their indicator renders only while the item is checked. + +```tsx + + Show archived + ✓ + + + + Name + ✓ + + +``` + ### Controlled ```tsx @@ -74,16 +102,21 @@ const [open, setOpen] = useState(false); ## Parts -| Part | Default Element | Description | -| ----------------- | --------------- | -------------------------------------- | -| `Menu.Root` | — | Root context provider | -| `Menu.Trigger` | `