When to use it#
- Use a menu for several related actions on one thing, such as print, download and withdraw on a case, or the view options of a table. It suits staff tools where space is tight.
- Menus are for actions, not navigation. A list of links to pages is a Navigation or a Disclosure, never a menu.
- Not for one or two actions, and not for the main action of a page: show visible buttons. In a service for residents, hidden actions are easy to miss, so prefer buttons that are always on screen.
- Not for a short panel with text or a form: use a Popover. Not for choosing a value in a form: use a Listbox.
- There are no submenus: a menu never opens another menu.
Example#
import { Menu } from '@kvirn-ui/react'
import { useMenuTexts } from './texts.ts'
export function DefaultMenu() {
const { texts } = useMenuTexts()
return (
<Menu.Root>
<Menu.Trigger className="kv-button">{texts.actions}</Menu.Trigger>
<Menu.Popup>
<Menu.Item onSelect={() => window.print()}>{texts.print}</Menu.Item>
<Menu.Item>{texts.download}</Menu.Item>
</Menu.Popup>
</Menu.Root>
)
}Use cases#
Actions for one row of a list
A list of cases often has the same few actions on each row. Give every trigger the row’s name, so “Actions for case 2024-118” and “Actions for case 2024-131” differ. The menu is named by its trigger.
Your cases
- Case 2024-118
- Case 2024-131
import { Menu, VisuallyHidden } from '@kvirn-ui/react'
import { useId, useState } from 'react'
import { useMenuTexts } from './texts.ts'
const cases = ['2024-118', '2024-131']
export function RowActions() {
const { texts } = useMenuTexts()
const headingId = useId()
const [last, setLast] = useState('')
return (
<>
<p id={headingId}>{texts.cases}</p>
<ul aria-labelledby={headingId}>
{cases.map((number) => (
<li key={number}>
{texts.case} {number}{' '}
<Menu.Root>
<Menu.Trigger className="kv-button">
{texts.actions}
<VisuallyHidden>
{' '}
{texts.forCase} {number}
</VisuallyHidden>
</Menu.Trigger>
<Menu.Popup>
<Menu.Item onSelect={() => setLast(`${texts.printing} ${number}.`)}>
{texts.print}
</Menu.Item>
<Menu.Item>{texts.download}</Menu.Item>
</Menu.Popup>
</Menu.Root>
</li>
))}
</ul>
<output>{last}</output>
</>
)
}Warning: Don't forget
Start the trigger’s name with its visible text, then add what it is for, hidden from sight. A name that doesn’t start with the visible text fails 2.5.3 Label in Name, and identical “Actions” buttons leave a screen reader user guessing which row each one belongs to.
An action that asks first
An action that can’t be undone, such as withdrawing an application, opens an AlertDialog from its item. Keep the open state yourself and set it in onSelect. When the dialog closes, focus goes back to the menu’s trigger.
import { AlertDialog, Menu } from '@kvirn-ui/react'
import { useRef, useState } from 'react'
import { useMenuTexts } from './texts.ts'
export function AskFirst() {
const { texts } = useMenuTexts()
const [open, setOpen] = useState(false)
const [isWithdrawn, setIsWithdrawn] = useState(false)
const keepReference = useRef<HTMLButtonElement>(null)
return (
<>
<Menu.Root>
<Menu.Trigger className="kv-button">{texts.actions}</Menu.Trigger>
<Menu.Popup>
<Menu.Item onSelect={() => window.print()}>{texts.print}</Menu.Item>
<Menu.Item onSelect={() => setOpen(true)}>{texts.withdraw}</Menu.Item>
</Menu.Popup>
</Menu.Root>
<AlertDialog.Root open={open} onOpenChange={setOpen} initialFocusRef={keepReference}>
<AlertDialog.Popup>
<AlertDialog.Title>{texts.withdrawTitle}</AlertDialog.Title>
<AlertDialog.Description>{texts.withdrawText}</AlertDialog.Description>
<AlertDialog.Actions>
<button
type="button"
className="kv-button kv-button--primary"
onClick={() => {
setIsWithdrawn(true)
setOpen(false)
}}
>
{texts.withdrawConfirm}
</button>
<AlertDialog.Close ref={keepReference} className="kv-button">
{texts.withdrawKeep}
</AlertDialog.Close>
</AlertDialog.Actions>
</AlertDialog.Popup>
</AlertDialog.Root>
<output>{isWithdrawn ? texts.withdrawn : null}</output>
</>
)
}Props in this example:
onSelecton Menu.Item
An action that is not available now
A disabled item stays in the menu, and the arrow keys still reach it, so people know the action exists. A screen reader says it is unavailable, not why. Say why in text on the page and link it to the item.
Sharing opens when the case is closed.
import { Menu } from '@kvirn-ui/react'
import { useId } from 'react'
import { useMenuTexts } from './texts.ts'
export function Unavailable() {
const { texts } = useMenuTexts()
const reasonId = useId()
return (
<>
<Menu.Root>
<Menu.Trigger className="kv-button">{texts.actions}</Menu.Trigger>
<Menu.Popup>
<Menu.Item onSelect={() => window.print()}>{texts.print}</Menu.Item>
<Menu.Item>{texts.download}</Menu.Item>
<Menu.Separator />
<Menu.Item disabled aria-describedby={reasonId}>
{texts.share}
</Menu.Item>
</Menu.Popup>
</Menu.Root>
<p id={reasonId}>{texts.shareReason}</p>
</>
)
}Props in this example:
disabledon Menu.Item
Options that stay on or off
A checkbox item turns a setting on or off, and radio items choose one value from a few. Here the checkbox item keeps the menu open, so the user can change more than one setting. A radio item closes the menu, which is the default. Name the radio group, here with aria-label. Without a name, a screen reader reads the choices without saying what they choose between (1.3.1 Info and Relationships).
import { Menu } from '@kvirn-ui/react'
import { useState } from 'react'
import { useMenuTexts } from './texts.ts'
export function CheckboxAndRadioMenu() {
const { texts } = useMenuTexts()
const [grid, setGrid] = useState(false)
const [sort, setSort] = useState('name')
const sortLabels: Record<string, string> = {
name: texts.byName,
date: texts.byDate,
status: texts.byStatus,
}
return (
<>
<output>
{grid ? texts.gridOn : texts.gridOff} {texts.sortedBy} {sortLabels[sort]}.
</output>
<Menu.Root>
<Menu.Trigger className="kv-button">{texts.view}</Menu.Trigger>
<Menu.Popup>
<Menu.CheckboxItem checked={grid} onCheckedChange={setGrid} closeOnSelect={false}>
{texts.grid}
</Menu.CheckboxItem>
<Menu.Separator />
<Menu.RadioGroup value={sort} onValueChange={setSort} aria-label={texts.sortBy}>
<Menu.RadioItem value="name">{texts.byName}</Menu.RadioItem>
<Menu.RadioItem value="date">{texts.byDate}</Menu.RadioItem>
<Menu.RadioItem value="status">{texts.byStatus}</Menu.RadioItem>
</Menu.RadioGroup>
</Menu.Popup>
</Menu.Root>
</>
)
}Props in this example:
checkedon Menu.CheckboxItemcloseOnSelecton Menu.CheckboxItemvalueon Menu.RadioGroup
Warning: Don't forget
A change is not announced once the menu has closed, and the closed menu shows nothing. Show the current setting on the page, as the line above the button does.
Know why it closed
Keep the open state yourself when the page needs to react to the menu opening or closing. onOpenChange says why it changed: a press, a key, Escape and so on. This example shows the last reason. With open set, closing is a request: you decide whether to close.
import { Menu } from '@kvirn-ui/react'
import { useState } from 'react'
import { useMenuTexts } from './texts.ts'
export function ControlledMenu() {
const { texts } = useMenuTexts()
const [open, setOpen] = useState(false)
const [reason, setReason] = useState('')
return (
<>
<output>
{texts.last} <code>{reason === '' ? texts.none : reason}</code>
</output>
<Menu.Root
open={open}
onOpenChange={(nextOpen, details) => {
setOpen(nextOpen)
setReason(details.reason)
}}
>
<Menu.Trigger className="kv-button">{texts.actions}</Menu.Trigger>
<Menu.Popup>
<Menu.Item>{texts.print}</Menu.Item>
<Menu.Item>{texts.download}</Menu.Item>
</Menu.Popup>
</Menu.Root>
</>
)
}Props in this example:
openon Menu.RootonOpenChangeon Menu.Root
Information: Did you know?
Put a confirmation in an element that is always on the page, such as an <output>, never inside the menu: the menu is closed when the action has run.
Accessibility#
What it does for you#
- APG pattern: Menu Button (the Actions Menu Button example) and the menu roles of Menubar (
menuitemcheckbox,menuitemradio, groups, separators). - Deviations: none from the APG keyboard practice. Defaults to know: no submenus and no
menubar, so ArrowLeft and ArrowRight are not handled; a disabled item stays focusable witharia-disabled(the APG's own rule for composites); items are<button>s; pointer movement over an item focuses it (one highlight). - Native elements used:
<button>(Trigger and every item),<div popover="auto" role="menu">(Popup),<div role="group">(Group, RadioGroup),<div role="separator">. Nothing for Root: it renders no element.
A Menu is a short list of actions that a button opens: print, share, delete. It is not navigation: links to pages belong in a Navigation or a Disclosure, never in role="menu", because a screen reader then switches to a mode that does not read links as links. The browser puts the menu in the top layer, KvirnUI places it next to the button, and focus moves onto its items as the user arrows through them. Enter or Space runs an item, closes the menu and returns focus to the button. Escape and a press outside close it.
| Part | Element / role | ARIA / state | Notes |
|---|---|---|---|
| Menu.Root | none: it renders no element | none | Owns the open state: open / defaultOpen / onOpenChange(open, { reason, event }), and placement, offset (4), padding (8). Reasons: trigger-press, key, item-press, escape, outside-press, light-dismiss, tab, focus-out (focus went to an element outside the menu, not by Tab) |
| Menu.Trigger | <button type="button"> | aria-haspopup="menu", aria-expanded="true" | "false", aria-controls (the popup's id). data-open while open | Class kv-menu-trigger. The anchor the popup is placed against. Its name is the consumer's content. Compose with as={Button} |
| Menu.Popup | <div popover="auto" role="menu">, always rendered | named by the trigger (aria-labelledby) unless the consumer sets aria-label or aria-labelledby. data-open, data-placement, data-detached | Class kv-menu-popup. Its items stay mounted while it is closed, so an uncontrolled checkbox or radio item keeps its state. Hidden by the browser while closed (out of the accessibility tree). Inline placement as Popover's. Scrolls inside when too tall |
| Menu.Item | <button type="button" role="menuitem">, tabindex 0 on the focused item, -1 on the rest | aria-disabled="true" while disabled. data-disabled, data-highlighted while it has focus | Class kv-menu-item. onSelect(event), closeOnSelect (default true), disabled, textValue (the typeahead label, default the trimmed text). Rendered as a link it warns: actions only |
| Menu.CheckboxItem | <button type="button" role="menuitemcheckbox"> | aria-checked="true" | "false", as Item. data-checked | Class kv-menu-checkbox-item. checked / defaultChecked / onCheckedChange(checked). Closes the menu by default |
| Menu.RadioGroup | <div role="group"> | named by aria-label or aria-labelledby (the consumer's) | Class kv-menu-radio-group. value / defaultValue / onValueChange(value). A dev warning fires without a name |
| Menu.RadioItem | <button type="button" role="menuitemradio"> | aria-checked, as Item. data-checked | Class kv-menu-radio-item. value is required. Selecting it sets the group's value |
| Menu.Group | <div role="group"> | aria-labelledby the GroupLabel inside it, unless the consumer names it | Class kv-menu-group. A dev warning fires with neither a GroupLabel nor a name |
| Menu.GroupLabel | <div> (not focusable, not an item) | its id is the group's aria-labelledby | Class kv-menu-group-label |
| Menu.Separator | <div role="separator"> | none | Class kv-menu-separator. Not an item: arrows and typeahead skip it |
useMenu | the same attributes, for your own elements | triggerProps, popupProps, getItemProps(kind, options), isOpen, placement | Options as the Root. The refs are in the props: mergeProps merges yours |
Each rule below has a test:
- Wiring.
aria-controlsis the popup's id on every render,aria-expandedfollows the state,aria-haspopup="menu"says what opens. The trigger is a real<button type="button">. The popup is named by the trigger unless the consumer names it. - Items. Roving tabindex: the focused item has
tabindex="0", every other item-1, and focus moves onto items with.focus(). While no item has focus nothing in the menu is tabbable, so the menu is never a Tab stop of its own. Items are found in the DOM at key time ([role^="menuitem"]), so a conditional item, a fragment or a custom wrapper just works. A separator and a group label are not items. - Disabled items are focusable, reachable by arrows and typeahead, and say
aria-disabled="true". Activation is blocked and the menu stays open. A screen reader reads them as "dimmed" or "unavailable". - Checkbox and radio items toggle or select on activation, are controlled or uncontrolled, and close the menu by default (
closeOnSelect={false}keeps it open). A radio item in a group withvalueis checked when itsvalueequals the group's. - Dismissal. Escape closes the innermost layer only. A press outside closes it (the trigger is not outside: its press toggles). Focus moving to an element outside the menu another way (a screen reader command, a script) closes it too (
focus-out). The platform hiding the popup (another auto popover opening) closes it in the state too (light-dismiss). - Composition. Inside a vertical
Toolbar, ArrowDown on the trigger opens the menu and the toolbar does not also move focus. UnderTooltip.Trigger, the tooltip closes while the menu is open. Inside aDialog, the menu stacks above it and Escape closes the menu only. The popup leaves the toolbar's context. - Server rendering. The server writes the trigger and the closed popup with its items (
popover,role="menu",data-placement), and no inline position. as.Menu.Triggertakes a component (as={Button}, orToolbar.Item as={Menu.Trigger}the other way round) that must render a button and spread its props on a DOM node; its props are plain props, merged with the part's (handlers chain,classNamejoins, refs merge). A trigger that is not a<button>warns in development. Allowed elements: Popup, Group and RadioGroupdiv(default) orsection; GroupLabeldiv(default),spanorp; another tag warns once in development and falls back. The items (Item,CheckboxItem,RadioItem) andSeparatorhave noas: an action is a<button>with a menu role, and a link in a menu would be announced as a menu item.- Dev warnings: a part outside a Root; a Trigger that is not a
<button>; aRadioGrouporGroupwith no name.
It announces nothing.
What you need to do#
- Render
Menu.Popupright afterMenu.Trigger, in the same parent. Tab and Shift+Tab leave the menu as documented only if the popup follows the trigger in the DOM (2.4.3 Focus Order (A)). - Use it for actions, not navigation. Links to pages are a
Navigationor aDisclosure. AMenu.Itemhas noas, so it can't be made a link. - Name the trigger with visible text, or
aria-labelfor an icon-only button (4.1.2 Name, Role, Value (A); 2.5.3 Label in Name (A)). The popup is named by it unless you passaria-label. - Name a
RadioGroupand aGroup: aGroupLabeloraria-label(1.3.1 Info and Relationships (A)). - Give an item with non-text content a
textValueso typeahead has a label. - A single action is a button, not a menu with one item.
- Don't hide the only way to do something in a menu without a visible hint that it exists: the trigger names it ("Actions").
- Move focus yourself when an item opens a Dialog, or let the Dialog do it: a Dialog with no Trigger of its own that an item opened returns focus to the menu's trigger once the menu has closed.
- Everything in
button.a11y.mdapplies to the trigger.
Warning: Don't forget
Put Menu.Popup right after Menu.Trigger, in the same parent. Otherwise Tab and Shift+Tab don’t leave the menu as described under Keyboard (2.4.3 Focus Order).
Visual and modes#
- Focus indicator: the focused item is the highlight (a solid fill in the default theme, the system's highlight colour in forced colours). A checked state is never shown by colour alone: a tick and a dot are drawn from borders. The trigger is a Button and shows the standard focus ring, at least 2px thick with at least 3:1 contrast against its background (2.4.7 Focus Visible (AA); the default theme also meets 2.4.13 Focus Appearance (AAA, not counted toward the AA bar)).
- Target size: items are never smaller than 24 by 24 CSS pixels, and larger in the default density (2.5.8 Target Size (Minimum) (AA)).
- Colour: highlighted, disabled and checked items keep at least 4.5:1 contrast for text (1.4.3 Contrast (Minimum) (AA)) and the popup's edge at least 3:1 (1.4.11 Non-text Contrast (AA)).
- forced-colors behaviour: the popup keeps a visible edge in the system's text colour, the highlighted item uses the system's highlight colours, and checked state is drawn from borders.
- reduced-motion behaviour: no positional animation, ever.
- Reflow: at 320px the popup is never wider than the screen minus its padding, and scrolls inside when it is too tall. No horizontal scrolling at 320 CSS px (1.4.10 Reflow, AA): the reflow sweep runs every story at 320 CSS px and fails on horizontal overflow.
- RTL:
bottom-startlines the popup's right edge with the trigger's. ArrowUp and ArrowDown never flip.
Focus management#
- Initial focus: moves onto an item when the menu is opened after mount, in an effect after the popup is shown: the first item for Enter, Space, a press and ArrowDown, the last for ArrowUp. A menu with no items keeps focus on the trigger. A menu that starts open (
defaultOpen, oropentrue on the first render) is open but moves no focus on load (3.2.1 On Focus (A)): focus stays where it was, and Escape and the arrows work once focus is in it. Only a later opening (a press, a key,opengoing from false to true) takes focus. - Trap: no. Nothing is
inertand Tab leaves the menu, closing it. Page content behind the menu stays reachable once it is closed. - Restore to: the trigger, then the element that had focus before the menu opened, after an item runs, Escape and an outside press that lost focus to
bodyonce focus had been in the menu (or the menu had an opener). AdefaultOpenmenu the platform closes before anyone interacted leaves focus alone. Neverbody. After an outside press the return does not scroll the page (preventScroll); after Escape or an item it does, so the trigger comes into view (2.4.11 Focus Not Obscured (Minimum) (AA)). Tab, Shift+Tab and afocus-outclose leave focus where it went. Focus the user moved to another control is never taken. - Never obscured by: the popup never covers its anchor (2.4.11 Focus Not Obscured (Minimum) (AA)), it sits in the top layer and the highlighted item is scrolled into view by focusing it. A consumer with a sticky header keeps
scroll-padding.
WCAG success criteria#
- 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): the menu roles,
aria-checked, groups, separators, the trigger'saria-haspopup,aria-expandedandaria-controls. - 2.1.1 Keyboard (A), 2.1.2 No Keyboard Trap (A): every item reachable by keys, Tab leaves.
- 2.4.3 Focus Order (A): focus enters the menu on open and returns to the trigger.
- 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA): the highlight, and the popup never covers its anchor.
- 1.4.3 Contrast (Minimum) (AA): the text of highlighted, disabled and checked items.
- 2.5.3 Label in Name (A): the trigger is named by its visible text (Consumer responsibilities).
- 2.5.8 Target Size (Minimum) (AA): items are never smaller than 24 by 24 CSS pixels.
- 3.2.1 On Focus (A): opening needs a press or a key, and nothing moves focus on its own.
Keyboard#
- Focus
- roving tabindex
- Selection follows focus
- n/a
- Arrows wrap
- yes
- Shortcuts
- none
| Key | Where | What happens |
|---|---|---|
| Tab | before the trigger | Moves focus to the trigger. The trigger is one stop and the closed menu has none Test: menu.test.tsx › Tab focuses the trigger, and is one stop |
| Shift+Tab | on the trigger | Moves focus to the previous focusable element Test: menu.test.tsx › Shift+Tab leaves the trigger backwards |
| Enter / Space | on the trigger | Opens the menu and moves focus to the first item Test: menu.test.tsx › Enter and Space on the trigger open the menu with focus on the first item |
| ArrowDown / ArrowUp | on the trigger | Opens the menu and moves focus to the first / last item Test: menu.test.tsx › ArrowDown and ArrowUp on the trigger open the menu on the first and last item |
| Pointer press | on the trigger | Opens the menu, or closes it when it is open. Opening focuses the first item. A closing press does not reopen it Test: menu.test.tsx › a pointer press on the trigger toggles the menu, and a closing press does not reopen it |
| ArrowDown / ArrowUp | in the menu | Moves focus to the next / previous item, wrapping from the last to the first and back. Disabled items are included. Up and Down never flip in right-to-left text Test: menu.test.tsx › ArrowDown and ArrowUp move between items, wrap and include disabled items |
| Home / End | in the menu | Moves focus to the first / last item Test: menu.test.tsx › Home and End move to the first and last item |
| Printable character | in the menu | Moves focus to the next item whose text starts with the characters typed, by the page's locale. The word ends after 500 ms. A repeated character cycles. Ctrl, Alt, Meta and IME input are ignored Test: menu.test.tsx › a character moves focus by typeahead, a word narrows it and a pause resets it |
| Space | in the menu, typeahead word in progress | Adds a space to the word and does not activate the item Test: menu.test.tsx › Space during a typeahead word adds a space and does not activate |
| Enter / Space | on an item | Runs the item, toggles a checkbox item or selects a radio item, closes the menu and returns focus to the trigger. With closeOnSelect={false} the menu stays openTest: menu.test.tsx › Enter and Space on an item run it, close the menu and return focus to the trigger |
| Enter / Space | on a disabled item | Does nothing. The menu stays open Test: menu.test.tsx › Enter and Space on a disabled item do nothing and keep the menu open |
| Escape | in the menu | Closes the innermost layer only and returns focus to the trigger Test: menu.test.tsx › Escape closes the menu and returns focus to the trigger, and only the menu |
| Tab | in the menu | Moves focus on to the next focusable element after the trigger and closes the menu. Tab is never prevented: the menu is no trap Test: menu.test.tsx › Tab in the menu closes it and moves focus to the next focusable after the trigger |
| Shift+Tab | in the menu | Moves focus to the previous focusable element, which is the trigger, and closes the menu Test: menu.test.tsx › Shift+Tab in the menu closes it and moves focus to the trigger |
| ArrowLeft / ArrowRight | in the menu | Not handled and not prevented: there are no submenus Test: menu.test.tsx › ArrowLeft and ArrowRight are not handled or prevented |
| Pointer move / press | on an item | Moving over an item focuses it. Pressing runs it. A press outside the menu closes it Test: menu.test.tsx › moving the pointer over an item focuses it, a press runs it and a press outside closes the menu |
The menu has no shortcuts and handles no other key: Page Up and Page Down are the browser's own, and Escape is read on the document, after the page's handlers (a handler that calls preventDefault() on it keeps the menu open). A Space after a typeahead word has been pause-ended for more than 500 ms activates the item: type, then pause, then Space.
API reference#
import { Menu, useMenu } from '@kvirn-ui/react'Menu.Root
Renders no element. It owns the open state and passes it to the parts inside it.
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | Controlled: whether the menu is open. Pair it with onOpenChange. |
defaultOpen | boolean | false | Uncontrolled: whether the menu starts open. |
onOpenChange | (open: boolean, details: MenuChangeDetails) => void | – | Called when the user opens or closes it. It only reports: with open set, you change open yourself. details.reason is trigger-press, key, item-press, escape, outside-press, light-dismiss, tab or focus-out, and details.event is the native event. |
placement | Placement | 'bottom-start' | Where the popup goes when there is room: top, bottom, start or end, with -start, -center or -end. It flips when it does not fit, and start and end follow the reading direction. |
offset | number | 4 | The gap between the trigger and the popup, in pixels. |
padding | number | 8 | The space kept to the edge of the viewport, in pixels. |
children | ReactNode | – | The Trigger and the Popup. Root renders no element. |
Menu.Trigger
Renders <button type="button">. It takes every attribute of a <button>, and the popup is placed against it.
| Prop | Type | Default | Description |
|---|---|---|---|
as | ElementType | 'button' | A component to render instead of the button, with its props set on the Trigger: as={Button} className="kv-button--primary". It must forward its ref and spread its props on a DOM node. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-trigger | always | The part class. |
aria-haspopup | "menu" | The trigger opens a menu. |
aria-expanded | "true" or "false" | Whether the popup is open. |
aria-controls | the popup’s id | Points at the popup, which stays in the page while closed. |
data-open | present or absent | The popup is open. |
Menu.Popup
Renders <div popover="auto"> with the role menu. The popup and its items stay in the page while it is closed, and the browser hides them. Render it right after the trigger.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'div' | 'section' | 'div' | The role is menu either way. Another tag warns once in development and renders a div. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-popup | always | The part class. |
popover | "auto" | Puts the popup in the top layer. The browser hides it while closed. |
role | "menu" | Named by the trigger unless you give it aria-label or aria-labelledby. |
data-open | present or absent | The popup is open. |
data-placement | a Placement, such as bottom-start | The side the popup is on now, after it flipped. |
data-detached | present or absent | The trigger is scrolled out of view. The popup is hidden until it comes back. |
Menu.Item
Renders <button type="button"> with the role menuitem. It is never a link: for a destination, use Navigation.
| Prop | Type | Default | Description |
|---|---|---|---|
onSelect | (event: MouseEvent<HTMLButtonElement>) => void | – | Runs when the item is chosen by Enter, Space or a press. It does not run on a disabled item. Call event.preventDefault() to keep the menu open and skip the toggle of a checkbox or radio item. |
closeOnSelect | boolean | true | Closes the menu after the item is chosen. Set false to keep it open. |
disabled | boolean | false | Sets aria-disabled and data-disabled. The item stays focusable, so the arrows and typeahead reach it, but it cannot be chosen. |
textValue | string | the item’s trimmed text | The text typeahead matches against, when the visible text is not the right label. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-item | always | The part class. |
role | "menuitem" | A checkbox item is "menuitemcheckbox" and a radio item "menuitemradio". |
aria-disabled | "true" or absent | The item cannot be chosen. |
data-disabled | present or absent | The item cannot be chosen. |
data-highlighted | present or absent | The item has focus. |
Menu.CheckboxItem
Renders <button> with the role menuitemcheckbox. It toggles when chosen.
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | – | Controlled: whether the item is checked. Pair it with onCheckedChange. |
defaultChecked | boolean | false | Uncontrolled: whether the item starts checked. |
onCheckedChange | (checked: boolean) => void | – | Called with the new value when the item is chosen. |
onSelect | (event: MouseEvent<HTMLButtonElement>) => void | – | Runs when the item is chosen by Enter, Space or a press. It does not run on a disabled item. Call event.preventDefault() to keep the menu open and skip the toggle of a checkbox or radio item. |
closeOnSelect | boolean | true | Closes the menu after the item is chosen. Set false to keep it open. |
disabled | boolean | false | Sets aria-disabled and data-disabled. The item stays focusable, so the arrows and typeahead reach it, but it cannot be chosen. |
textValue | string | the item’s trimmed text | The text typeahead matches against, when the visible text is not the right label. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-checkbox-item, kv-menu-radio-item | always | The part class. |
aria-checked | "true" or "false" | Whether it is checked or chosen. |
data-checked | present or absent | Whether it is checked or chosen. |
data-disabled, data-highlighted | present or absent | As on Item. |
Menu.RadioGroup
Renders <div role="group"> around the radio items that share a value. It is not the RadioGroup component.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | – | Controlled: the value of the chosen item. Pair it with onValueChange. |
defaultValue | string | – | Uncontrolled: the value of the item that starts chosen. |
onValueChange | (value: string) => void | – | Called with the value of the item that was chosen. |
as | 'div' | 'section' | 'div' | The role is group either way. Another tag warns once in development and renders a div. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-radio-group | always | The part class. |
role | "group" | Name it with aria-label or aria-labelledby. |
Menu.RadioItem
Renders <button> with the role menuitemradio. Put it inside a Menu.RadioGroup.
| Prop | Type | Default | Description |
|---|---|---|---|
value | string | Required | The value this item stands for inside its RadioGroup. |
onSelect | (event: MouseEvent<HTMLButtonElement>) => void | – | Runs when the item is chosen by Enter, Space or a press. It does not run on a disabled item. Call event.preventDefault() to keep the menu open and skip the toggle of a checkbox or radio item. |
closeOnSelect | boolean | true | Closes the menu after the item is chosen. Set false to keep it open. |
disabled | boolean | false | Sets aria-disabled and data-disabled. The item stays focusable, so the arrows and typeahead reach it, but it cannot be chosen. |
textValue | string | the item’s trimmed text | The text typeahead matches against, when the visible text is not the right label. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-checkbox-item, kv-menu-radio-item | always | The part class. |
aria-checked | "true" or "false" | Whether it is checked or chosen. |
data-checked | present or absent | Whether it is checked or chosen. |
data-disabled, data-highlighted | present or absent | As on Item. |
Menu.Group
Renders <div>. It takes every attribute of a <div> and passes ref to it. It has the role group. Name it with a GroupLabel.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'div' | 'section' | 'div' | The role is group either way. Another tag warns once in development and renders a div. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-group | always | The part class. |
role | "group" | Named by the GroupLabel inside it. |
Menu.GroupLabel
Renders <div>. It takes every attribute of a <div> and passes ref to it. It names the Group around it.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'div' | 'span' | 'p' | 'div' | Another tag warns once in development and renders a div. |
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-group-label | always | The part class. |
Menu.Separator
Renders <div>. It takes every attribute of a <div> and passes ref to it. It has the role separator: a divider between items.
| Class or attribute | Values | Meaning |
|---|---|---|
kv-menu-separator | always | The part class. |
role | "separator" | A divider between items. |
useMenu
The hook gives props for your own elements: a trigger, a popup and the items in it. Put the popup right after the trigger in the DOM, and mark the item that has focus with isCurrent.
Options
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | – | Controlled: whether the menu is open. Pair it with onOpenChange. |
defaultOpen | boolean | false | Uncontrolled: whether the menu starts open. |
onOpenChange | (open: boolean, details: MenuChangeDetails) => void | – | Called when the user opens or closes it. It only reports: with open set, you change open yourself. details.reason is trigger-press, key, item-press, escape, outside-press, light-dismiss, tab or focus-out, and details.event is the native event. |
placement | Placement | 'bottom-start' | Where the popup goes when there is room: top, bottom, start or end, with -start, -center or -end. It flips when it does not fit, and start and end follow the reading direction. |
offset | number | 4 | The gap between the trigger and the popup, in pixels. |
padding | number | 8 | The space kept to the edge of the viewport, in pixels. |
Result
| Prop | Type | Default | Description |
|---|---|---|---|
isOpen | boolean | – | Whether the menu is open. |
placement | Placement | – | The placement in use. It differs from the one asked for when the popup flipped. |
triggerProps | MenuTriggerPartProps | – | Spread on a <button>: type, aria-haspopup, aria-expanded, aria-controls, data-open, ref and the pointer, click and key handlers. |
popupProps | MenuPopupPartProps | – | Spread on a <div>: popover, role menu, aria-labelledby the trigger, data-open, data-placement, ref and the handlers. |
getItemProps | (kind: 'item' | 'checkbox' | 'radio', options?: MenuItemOptions) => MenuItemPartProps | – | The props of one item: its role, tabindex, aria-disabled, aria-checked and handlers. The options are disabled, closeOnSelect, onSelect, textValue, checked and isCurrent, true on the item that has focus. |
Known issues#
- A menu that starts open does not take focus, so a keyboard user has to Tab to the trigger and press ArrowDown (no item is tabbable until one has focus). Start a menu open only for demonstrations and tests.
- A Tab pressed during IME composition is ignored by the menu, so a Tab that then leaves the page for browser chrome (focus to nowhere) does not close it.
- The focused item has
tabindex="0", the others-1(roving tabindex). A scrolling region needs a tabbable element for axe (scrollable-region-focusable), and items at -1 do not count. Tab and Shift+Tab still leave the menu from the item, because no other element in the popup is tabbable; the popup itself is not focusable. - A checkbox or radio change is not announced after the menu closes. Focus returns to the trigger, which does not carry the new state. Show the state in the trigger's text where it matters.
- Disabled items stay reachable, so a screen reader user hears "dimmed". That is deliberate (discoverability), and a reason to explain why an action is unavailable: explain it in visible text on the page, and point the item's
aria-describedbyat it, so a screen reader reads "Share, dimmed, <reason>" (menu.test.tsx › a disabled item points at the reason on the page with aria-describedby). - Hover moves focus (one highlight, as native menus do). A magnifier user may find the focus moving under the pointer surprising.
- No submenus in this version, and no context menu or menubar.
- Tab is never intercepted. The browser moves focus from the item and the menu closes on that focus move (deferred one task, reason
tab), with no focus return. Chromium does as documented; check Safari and Firefox yourself.
Testing#
The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.
| AT + browser + OS | Agent check | Human check | Result | Notes |
|---|---|---|---|---|
| Automated (re-runnable tests) | ||||
| axe, WCAG 2.2 A and AA, every story, 4 themes | pass | n/a | pass | menu.stories.tsx, every story, 4 themes |
| Markup and ARIA (roles, names, ids, states) | pass | n/a | pass | menu.test.tsx › a disabled item has aria-disabled and data-disabled, and is still a button that takes focus; expectNoA11yViolations |
| Keyboard contract rows (Vitest browser) | pass | n/a | pass | 16 rows, each names a test (menu.test.tsx › Tab focuses the trigger, and is one stop) |
| Focus order and management | pass | n/a | pass | menu.test.tsx asserts focus (toHaveFocus); first rule: Tab focuses the trigger, and is one stop |
| Display modes (forced colours, 320px reflow, text spacing, reduced motion) | pass | n/a | pass | the four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI |
| Core (required for beta) | ||||
| NVDA + Chrome + Windows | pending | pending | pending | |
| VoiceOver + Safari + iOS | pending | pending | pending | |
| Release (before 1.0 and each minor) | ||||
| JAWS + Chrome + Windows | pending | pending | pending | |
| TalkBack + Chrome + Android | pending | pending | pending | |
| VoiceOver + Safari + macOS | pending | pending | pending |
Also pending, by hand: Tab out of the menu on Safari and Firefox (the focus start point after the popup closes), and how screen readers read disabled items.
What to check#
With a screen reader and the keyboard, try:
- Tab to the button, then press Enter or ArrowDown: the first item has focus, and its name and role are read.
- ArrowDown past the last item wraps to the first. Typing a first letter jumps to a matching item.
- A disabled item is read as unavailable, with its linked reason.
- Choose a checkbox or radio item: note what is read, and check that the page shows the new setting.
- Escape closes the menu and focus returns to the button.
- Tab while the menu is open closes it and moves to the next control.