Components / overlays

Menu

A list of actions attached to a trigger.

Rendered from the package · open on its own

Summary

A list of actions attached to a trigger.

APG patternMenu Button
Built onnative elements
Statusstable
Since0.1.0
<Menu label="Project" items={[
  { label: 'Copy link', icon: <Icon path={ICONS.linkSimple} /> },
  { separator: true },
  { label: 'Delete project', icon: <Icon path={ICONS.trash} />, intent: 'negative' }
]} onSelect={(item) => run(item.label)} />

<Menu label="View" items={[
  { value: 'dates', label: 'Show dates', kind: 'checkbox', checked: view.dates },
  { value: 'points', label: 'Show points', kind: 'checkbox', checked: view.points, description: "The ledger's total per day." },
  { separator: true },
  { value: 'week', label: 'This week', kind: 'radio', checked: range === 'week' },
  { value: 'month', label: 'This month', kind: 'radio', checked: range === 'month' }
]} onSelect={(item) => toggle(item.value)} />

The second menu is Wave 3's: checkable items (a checkbox toggles and stays open, a radio chooses and closes), a detail line, and values the caller keys on. A menu written like the first renders and behaves as it did.

Anatomy

A trigger plus a floater carrying role="menu". Menubar renders data-se="menu" for its own floaters, so one set of rules dresses both.

A menu may open with a header: a block above the first item that says whose menu it is, a name and a line under it. It is not an item. It carries no role, takes no highlight and no id, and the arrow keys, Home and End never reach it, because navigation runs over the selectable items only. A click on it neither chooses nor closes. A hairline under it ends it, so no separator follows it.

An item holds its words in label, and may hold a check mark before them — present on every checkable item so the labels stay aligned, drawn only while aria-checked="true" — and a description line under them. A checkable item carries data-kind and data-value beside its role.

PartElementRequired
check<span>no
description<span>no
floater<div>yes
header<div>no
icon<svg>yes
item<button>yes
label<span>yes
separator<div>yes
spinnerthe caller'sthe caller's
triggerthe caller'sthe caller's

Properties

PropertyValuesDefaultWhat it does
refReact.Ref<HTMLButtonElement>requiredunknown
type'button'requiredunknown
onClick() => voidrequiredunknown
onKeyDown(e: React.KeyboardEvent) => voidrequiredunknown

States

StateExpressed as
Pressed:active
Disabled:disabled
Focused:focus-visible
Hover:hover
Checkedaria-checked
Disabledaria-disabled
Has popuparia-haspopup
Highlighteddata-highlighted
Open or closeddata-state

Behavior

Arrow keys, Home and End, Enter and Escape are wired. It dismisses on outside click and Escape, and returns focus to the trigger.

The trigger is the labeled outline button unless you hand one in. trigger receives the ref and the four attributes that make a button a menu button — aria-haspopup, aria-expanded, click and keys — and renders whatever the place needs: three dots beside a title, or a page title that is itself the menu. data-part="trigger" strips button chrome and draws the ring, so a title trigger reads as the heading it replaces.

Hover moves the highlight rather than painting a second state. Two highlights at once — one from the pointer, one from the keyboard — is a reader who cannot tell which will fire.

A checkbox item toggles and the menu stays open; a radio item chooses and closes; an action closes. The reader choosing several things should not reopen the menu for each. The menu changes no checked itself — it reports the item in onSelect, keyed by its value, and the caller re-renders with the new state, the same contract Chip has.

Near a viewport edge the floater opens on the other side of the trigger. Where the browser has CSS anchor positioning, the stylesheet does it — the floater is anchored to the trigger and flips across the block axis, the inline axis or both. Where it does not, the binding measures the open floater once and writes data-side="top" and data-align="end", which the stylesheet honors on the ordinary placement. A consumer writing the anatomy by hand in such a browser writes those attributes, or accepts the floater where it falls.

Accessibility

The highlight is drawn by the anatomy, because the browser cannot draw focus on an item the roving tabindex has not focused.

aria-haspopup and aria-expanded on the trigger say what it opens and whether it is open.

A checkable item is menuitemcheckbox or menuitemradio with aria-checked, always true or false while it is checkable — never absent-for-false, which reads as a plain item. The mark is decoration (aria-hidden); the role and the state carry it.

The description is read after the label, as part of the item's name — it is text inside the button. Keep it to a few words, or the name is a paragraph.

Keyboard

KeyResult
Arrow Down / Arrow UpMove the highlight, wrapping at the ends
Home / EndJump to the first or last item
Enter, SpaceActivates the highlighted item. A checkbox item toggles and the menu stays open; anything else closes it
EscapeCloses and returns focus to the trigger
TabCloses the menu and moves on

Roles, states and properties

AttributeSet by
aria-activedescendantset at render
aria-checkedset at render
aria-disabledset at render
aria-expandedset at render
aria-haspopupalways menu
aria-hiddenalways true
aria-labelset at render
rolealways menu, always separator, set at render

Focus

DOM focus stays on the panel and aria-activedescendant names the current item, which is why the highlight is drawn by the anatomy — the browser has nothing to ring. Focus returns to the trigger on close.

Screen reader

Not recorded as verified. Stage 4 of the readiness ladder — driving each component with VoiceOver and NVDA — is unstarted across the package, and no component should claim otherwise until docs/journal/ records the session that did it.

Contrast and target size

npm run check:contrast audits every color pair this component uses across all four skins, light and dark, and fails the build on a shortfall. Two waivers are on the record in docs/audit/DEFERRED.md.

It takes the touch floor under (pointer: coarse) — the visual size is unchanged on a desktop pointer.

Tested

  • test/apg/batch-a.test.jsx
  • test/apg/batch-e.test.jsx
  • test/apg/menu-items.test.jsx
  • test/apg/partials-sweep.test.jsx
  • test/browser/keyboard.spec.mjs
  • test/browser/layers.spec.mjs
  • test/browser/menu-header.spec.mjs
  • test/browser/menu.spec.mjs
  • test/browser/upstream-natives.spec.mjs

Known gaps

  • 1 upstream ref — duplicate of button-aria-controls
  • 1 upstream ref — duplicate of button-aria-expanded
  • 1 upstream ref — duplicate of button-aria-haspopup in their other example
  • 1 upstream ref — duplicate of button-down-arrow-or-space-or-enter
  • 1 upstream ref — duplicate of button-up-arrow
  • 1 upstream ref — duplicate of menu-character
  • 1 upstream ref — duplicate of menu-down-arrow
  • 1 upstream ref — duplicate of menu-end
  • 1 upstream ref — duplicate of menu-enter
  • 1 upstream ref — duplicate of menu-escape
  • 1 upstream ref — duplicate of menu-home
  • 1 upstream ref — duplicate of menu-up-arrow
  • 1 upstream ref — APG's own page or markup, not the pattern

Appearance

TokenUsed for
--se-accentintent
--se-bgbackground, border-color
--se-borderbackground, border, border-block-end, border-color
--se-border-widthborder, border-block-end, border-width
--se-control-hblock-size, inline-size, min-block-size
--se-control-h-lgblock-size, inline-size
--se-control-h-smblock-size, inline-size
--se-duration-fastanimation
--se-duration-instanttransition
--se-duration-slowanimation
--se-ease-decelerateanimation
--se-ease-standardtransition
--se-focus-coloroutline
--se-focus-offsetoutline-offset
--se-focus-widthoutline
--se-icon-mdblock-size, inline-size
--se-icon-smblock-size, font-size, inline-size, padding-inline-start
--se-layer-floatingz-index
--se-leading-bodyline-height
--se-leading-uiline-height
--se-material-blurbackdrop-filter
--se-mix-downbackground, border-color
--se-mix-hoverbackground, border-color
--se-mix-lineborder-color
--se-mix-pressbackground
--se-mix-shadebackground, border-color
--se-mix-softbackground
--se-negativebackground, color, intent
--se-positiveintent
--se-radius-buttonborder-radius
--se-radius-lgborder-radius
--se-radius-mdborder-radius
--se-radius-pillborder-radius
--se-radius-smborder-radius
--se-scale-pressscale
--se-shadow-floatingbox-shadow
--se-space-1gap, margin, padding, padding-block
--se-space-2gap, inset-block-end, inset-block-start, padding, padding-block, padding-inline, padding-inline-start
--se-space-3padding, padding-inline
--se-space-4padding-inline
--se-space-5padding-inline
--se-surface-highlightbackground
--se-surface-raisedbackground
--se-surface-sunkbackground
--se-target-minblock-size, inline-size
--se-textcolor, intent
--se-text-disabledcolor
--se-text-mdfont-size
--se-text-mutedcolor
--se-text-on-intentcolor
--se-text-smfont-size
--se-text-xsfont-size

Motion

The floater rises over --se-duration-fast. A flip is a placement, not a motion: the floater opens on the other side; it does not travel there.

Content

Each item names an action. Copy link, Delete project. A menu of nouns is a navigation list and belongs in a nav.

A destructive item sits last, after a separator.

A checkable item names the thing shown or chosen, not the act of toggling it: Show dates, This week — never Toggle dates. The mark says whether it is on. A description is a few words that say what the choice means: The ledger's total per day.

Usage

A Menu is for actions. If the panel holds fields or prose, that is a Popover. If it holds a single explanation, that is a Tooltip.

A title as the trigger holds a selection, not an action. A groceries list "for Easter lunch" that opens the other events is choosing what the page is about. Give those items role="menuitemradio" through trigger's panel or keep them as actions that navigate — either is honest; a select in a heading is not.

Not a Select. Menu, Select and Combobox are three components and one temptation: a menu fires an action and forgets; a select holds a value.

A split button is a ButtonGroup of a Button and a Menu with a custom trigger — hosting-event's Create event / Create and invite. The button carries the default action and its label; the menu's trigger renders an icon-only caret button whose label names the set (More ways to create), and its items are the alternatives. Two controls, two tab stops, joined edge to edge by the group:

<ButtonGroup label="Create">
  <Button intent="accent" emphasis="solid" onClick={create}>Create event</Button>
  <Menu label="More ways to create" items={[{ value: 'invite', label: 'Create and invite' }]} onSelect={run}
    trigger={(props) => <Button {...props} intent="accent" emphasis="solid" iconOnly={<Icon path={ICONS.caretDown} />} label="More ways to create" />} />
</ButtonGroup>

It is a pattern, not a component: nothing in it needs a contract the two parts do not already have. A split button whose menu repeats the button's action is one button too many.

Checked items are a view's switches, not a form. Show dates, This week. A menu that holds a value the page submits is a Select the system does not have, or a Listbox.

When not to use it. For a menu bar across the top of an application — that is Menubar, which has a different arrow contract.

Notes

The floater anchors to the menu's own trigger wherever it sits inside the menu. A custom trigger may arrive wrapped — a tooltip's span, a split button's seam — and still anchors; a nested menu's trigger is scoped to its own menu by anchor-scope, so an outer floater never follows an inner trigger. Before 0.9.1 the anchor was named on a direct child only, and a wrapped trigger's floater opened over its button and stayed put on scroll.

The header is a part, not an item. The Brief's account menu opens on who is signed in and a status line. Rejected: an identity block as a disabled menuitem. It would be a stop for the arrow keys, a thing a screen reader announces as an unavailable action, and a row that looks like the items below it; a header is none of those. Rejected: a Popover around a menu, which would put two floaters on one trigger. Written inside the floater so one anchor places it, and carrying no role, because menu allows items, groups and separators and a header is text.

Hand-written, not Radix. Radix is permitted for six patterns and a menu button is not one of them any more — the roving highlight here is aria-activedescendant with DOM focus parked on the panel, which is why the highlight is the anatomy's job rather than a focus ring.

Checked items, a detail line, stable values and the flip arrived in Wave 3 (M1–M4), all additive. Every new rule selects on data-kind, a description part, data-side or data-align, and the flip's CSS sits under @supports — a menu written before them is dressed as it was. value keys the React element so a relabel does not remount it, and is handed back in onSelect; ids stay index-based, because aria-activedescendant has to name an element that exists. One mark serves checkbox and radio: the role tells the reader which, and the picture only has to say "on". The flip is CSS anchor positioning first, with position: fixed so the viewport is the box the floater is kept inside — absolute would measure the menu wrapper, which the floater always overflows — and the binding's measurement only where the browser has none.

The id prefix seeds every item id from useId. A constant default meant two default-configured menus on one page emitted the same ids, and aria-activedescendant then named whichever the document happened to contain first.