Todae

Todae

Menu

A button that opens a list of actions. Menu.Trigger is a Button.Root with aria-haspopup="menu" and aria-expanded. Menu.Content is a role="menu" in a non-modal popover anchored to the trigger with CSS anchor positioning, the same surface as Popover. The menu is named by its trigger unless it gets aria-label or aria-labelledby. Its items mount on the first open and stay mounted after it, so a page of row menus that never open renders no items; a submenu's items mount on its own first open.

On this page
import { Menu, Stack, Text } from '@tounsoo/todae';
import { useState } from 'react';

export default function MenuActions() {
  const [last, setLast] = useState('nothing yet');
  return (
    <Stack.Root>
      <Menu.Root>
        <Menu.Trigger variant="secondary">Actions</Menu.Trigger>
        <Menu.Content>
          <Menu.Item onSelect={() => setLast('Rename')}>Rename</Menu.Item>
          <Menu.Item onSelect={() => setLast('Duplicate')}>Duplicate</Menu.Item>
          <Menu.Item disabled>Move to…</Menu.Item>
          <Menu.Separator />
          <Menu.Item onSelect={() => setLast('Delete')}>Delete</Menu.Item>
        </Menu.Content>
      </Menu.Root>
      <Text.Root tone="muted">
        Last action: {last}
      </Text.Root>
    </Stack.Root>
  );
}

Last action: nothing yet

Keyboard

Enter, Space, a click or ArrowDown on the trigger opens the menu on its first item; ArrowUp opens it on the last. In a vertical toolbar, Up and Down move between the toolbar's controls instead. In the menu, Up and Down move and loop, Home and End jump, and typing a letter moves to the next item that starts with it. Enter, Space or a click chooses an item. Choosing one that closes the menu returns focus to the trigger, unless the choice moved focus elsewhere, also when the choice moves the trigger in the page, as reordering a list does. Escape closes and returns focus to the trigger; Tab closes and moves focus past the trigger (in a toolbar, past the trigger's run of controls); Shift+Tab closes and stops on the trigger. Focus that moves anywhere else outside the menu, by a script, a screen reader or a press into an iframe, closes it too. Pointer movement focuses the item under it, so the pointer and the keyboard share one highlight. Disabled items carry aria-disabled and stay in the arrow-key order, as menus conventionally do, so a screen reader announces them as unavailable; they cannot be chosen. Hovering one focuses it, as the arrow keys do, so the highlight never stays on another item.

Checkbox and radio items

An item's children are presentational to assistive technology, as ARIA defines for menuitem, so keep links, inputs and buttons out of items (an item can be a link itself, as Links shows); keys pressed in one are left alone, but a click on it also chooses the item. Menu.CheckboxItem toggles aria-checked. Menu.RadioGroup owns one value for its Menu.RadioItem children. Both stay open after a choice so several settings can change in one visit; set closeOnSelect to close. Menu.Group and Menu.RadioGroup need aria-label or aria-labelledby.

import { Menu } from '@tounsoo/todae';
import { useState } from 'react';

export default function MenuOptions() {
  const [grid, setGrid] = useState(true);
  const [rulers, setRulers] = useState(false);
  const [sort, setSort] = useState('name');
  return (
    <Menu.Root>
      <Menu.Trigger variant="secondary">View</Menu.Trigger>
      <Menu.Content>
        <Menu.Group aria-label="Show">
          <Menu.CheckboxItem checked={grid} onCheckedChange={setGrid}>
            Grid
          </Menu.CheckboxItem>
          <Menu.CheckboxItem checked={rulers} onCheckedChange={setRulers}>
            Rulers
          </Menu.CheckboxItem>
        </Menu.Group>
        <Menu.Separator />
        <Menu.RadioGroup aria-label="Sort by" value={sort} onValueChange={setSort}>
          <Menu.RadioItem value="name">Name</Menu.RadioItem>
          <Menu.RadioItem value="modified">Date modified</Menu.RadioItem>
          <Menu.RadioItem value="size">Size</Menu.RadioItem>
        </Menu.RadioGroup>
      </Menu.Content>
    </Menu.Root>
  );
}

onOpenChange(open, reason) reports trigger, context (a Menu.ContextTrigger), escape, outside (a press or focus outside), select or tab. A controlled open that is not updated vetoes the change; a vetoed tab still lets focus move on, the way a vetoed outside still lets the press land.

Menu.Submenu puts a list inside a list. Menu.SubmenuTrigger is an item with aria-haspopup="menu" and a chevron at the end of its line, and Menu.SubmenuContent is the role="menu" it opens, named by the trigger. The submenu opens past the trigger's end with its first item level with the trigger, and flips to the other side, or up, where there is no room. A submenu can hold any item, a group or another submenu.

import { Menu, Stack, Text } from '@tounsoo/todae';
import { useState } from 'react';

export default function MenuSubmenu() {
  const [last, setLast] = useState('nothing yet');
  return (
    <Stack.Root>
      <Menu.Root>
        <Menu.Trigger variant="secondary">File</Menu.Trigger>
        <Menu.Content>
          <Menu.Item onSelect={() => setLast('Rename')}>Rename</Menu.Item>
          <Menu.Submenu>
            <Menu.SubmenuTrigger>Share</Menu.SubmenuTrigger>
            <Menu.SubmenuContent>
              <Menu.Item onSelect={() => setLast('Copy link')}>Copy link</Menu.Item>
              <Menu.Item onSelect={() => setLast('Email')}>Email</Menu.Item>
              <Menu.Submenu>
                <Menu.SubmenuTrigger>Export as</Menu.SubmenuTrigger>
                <Menu.SubmenuContent>
                  <Menu.Item onSelect={() => setLast('Export as PDF')}>PDF</Menu.Item>
                  <Menu.Item onSelect={() => setLast('Export as PNG')}>PNG</Menu.Item>
                </Menu.SubmenuContent>
              </Menu.Submenu>
            </Menu.SubmenuContent>
          </Menu.Submenu>
          <Menu.Separator />
          <Menu.Item onSelect={() => setLast('Delete')}>Delete</Menu.Item>
        </Menu.Content>
      </Menu.Root>
      <Text.Root tone="muted">
        Last action: {last}
      </Text.Root>
    </Stack.Root>
  );
}

Last action: nothing yet

Right arrow, Enter or Space on the trigger opens the submenu on its first item; in a right-to-left page, Left arrow does. Left arrow (Right arrow in right-to-left) or Escape closes only that submenu and puts focus back on its trigger. Up, Down, Home, End and typeahead move within the submenu's own items. Choosing an item in it, or Tab, closes the whole menu, as in the menu itself. The pointer resting on the trigger for menu.submenu.delay.show opens the submenu and leaves focus on the trigger; a click or tap opens it at once. Focus moving to another item of the menu it sits in closes it.

On its way from the trigger to the submenu, the pointer may cross other items. For menu.submenu.delay.hide, while the pointer stays inside the triangle between where it left the trigger and the submenu's near edge, those items leave focus where it is, so the submenu stays open. A pointer that stops, turns back or runs out of time focuses the item under it as usual.

Menu.Submenu takes open, defaultOpen and onOpenChange(open, reason), where reason is trigger (a click or key on the trigger), pointer (the pointer resting on it), escape, arrow (the arrow key toward the line's start) or parent (focus moved to another item, or the menu it sits in closed).

An item that goes somewhere, such as Help or Settings, is a link. Pass render and return the link with the props it is handed; the element keeps role="menuitem" and the menu's keyboard. Enter, Space or a click follows the link, runs onSelect and closes the menu. A disabled item can't be chosen; leave out its href too, so a middle click or the browser's Open in new tab can't follow it.

import NextLink from 'next/link';

<Menu.Item render={(props) => <NextLink {...props} href="/settings" />}>Settings</Menu.Item>

Context menus

Menu.ContextTrigger is a region, such as a row, a file or a message, that opens the menu on a right click, a long press on Android, Shift+F10 or the ContextMenu key. A pointer opens the menu at the pointer; a key opens it below the focused element. The menu flips to stay in the viewport, as from Menu.Trigger, and opens on its first item. Escape gives focus back to whatever had it when the menu opened, or to the Menu.Trigger when that is gone or could not take focus, as after a right click on text. The region renders a div; pass render to make it a table row or a list item, and spread the props it is handed. While its menu is open, the region carries data-open, so a product can highlight the row that was clicked.

Give each region a visible Menu.Trigger in the same Menu.Root as well. A context menu can't be seen, few people know Shift+F10, Mac keyboards have no ContextMenu key, and iOS Safari fires no contextmenu on a long press, so the button is how many people reach the same menu.

import { Button, Menu, Stack, Text } from '@tounsoo/todae';
import { Ellipsis } from 'lucide-react';
import { useState } from 'react';

const FILES = ['Q3 report.pdf', 'Roadmap.key', 'Budget.xlsx'];

export default function MenuContext() {
  const [last, setLast] = useState('nothing yet');
  return (
    <Stack.Root>
      <Stack.Root as="ul" gap="condensed">
        {FILES.map((file) => (
          <Menu.Root key={file}>
            <Menu.ContextTrigger
              render={(props) => (
                <li {...props}>
                  <Stack.Root direction="row" align="center" justify="between">
                    <Text.Root>{file}</Text.Root>
                    <Menu.Trigger variant="ghost" aria-label={`Actions for ${file}`}>
                      <Button.Icon>
  • Q3 report.pdf

  • Roadmap.key

  • Budget.xlsx

Last action: nothing yet

A right click or Shift+F10 in a text field inside the region leaves the browser's own menu, which holds the field's editing commands. A region inside another region takes the request first. A right click inside an open menu doesn't open the browser's menu over it.

Icons

Menu.CheckboxItem shows the check glyph while checked: Todae's check mark, unless a GlyphsProvider sets one. The check, and a radio item's dot, show at once. A brand can grow them in with menu.item.check.motion.duration (0ms by default) and menu.item.check.motion.easing, as Checkbox's check grows; they still go at once, and under reduced motion they only fade, since a fade moves nothing. The highlight that follows the keyboard moves at once. Menu.SubmenuTrigger shows the chevron glyph, turned toward the end of the line. An icon-only trigger takes an icon from any library in Button.Icon, and needs a name.

A command can show a leading icon and a trailing shortcut. Put Menu.ItemIcon first and Menu.ItemShortcut last. Items in the menu share those columns: shortcuts sit on the trailing edge, just before a submenu's chevron, and a menu that uses an icon keeps that column on an item that has no icon, so the labels start together. A check or radio item already uses that leading column for its indicator, so leave Menu.ItemIcon off it. An item joins those columns only when it sits directly in Menu.Content, Menu.Group, or Menu.RadioGroup. Both slots are aria-hidden. The item's name and its typeahead use the label text only. Put aria-keyshortcuts on the item, with Alt, Control, Meta and Shift, the same names useHotkey takes, and bind those keys with the hook, so assistive technology can announce a shortcut the item actually has.

import { Kbd, Menu, Stack, Text, useHotkey } from '@tounsoo/todae';
import { FilePlus, Save } from 'lucide-react';
import { useState } from 'react';

const newKeys = ['Alt+Shift+N'];
const saveKeys = ['Alt+Shift+S'];

export default function MenuCommands() {
  const [last, setLast] = useState('nothing yet');
  useHotkey(newKeys, () => setLast('New'));
  useHotkey(saveKeys, () => setLast('Save'));
  return (
    <Stack.Root>
      <Menu.Root>
        <Menu.Trigger variant="secondary">File</Menu.Trigger>
        <Menu.Content>
          <Menu.Item aria-keyshortcuts={newKeys.join(' ')} onSelect={() => setLast('New')}>
            <Menu.ItemIcon>
              <FilePlus />
            </Menu.ItemIcon>

Last action: nothing yet

import { Check, Ellipsis } from 'lucide-react';

<GlyphsProvider glyphs={{ check: <Check /> }}>
  <Menu.Root>
    <Menu.Trigger variant="ghost" aria-label="View options">
      <Button.Icon>
        <Ellipsis />
      </Button.Icon>
    </Menu.Trigger>
    <Menu.Content>
      <Menu.CheckboxItem checked={grid} onCheckedChange={setGrid}>
        Show grid
      </Menu.CheckboxItem>
    </Menu.Content>
  </Menu.Root>
</GlyphsProvider>

Accessible composition

Todae handles: Menu.Trigger sets aria-haspopup="menu", aria-expanded and aria-controls, and its id names the role="menu" content through aria-labelledby. Items get role="menuitem", menuitemcheckbox or menuitemradio, with aria-checked and aria-disabled kept in step. Menu.SubmenuTrigger sets aria-haspopup="menu", aria-expanded and aria-controls, and names its submenu. Menu.Group and Menu.RadioGroup are role="group", and Menu.Separator is role="separator". Focus moves into the menu when it opens and follows the keyboard model above. Menu.ContextTrigger handles the contextmenu event and Shift+F10 and the ContextMenu key, places the menu at the pointer or the focused element, and returns focus when it closes. A menu or submenu that scrolls past its max height shows arrows at the cut-off edges, as on ScrollArea; resting the pointer on one scrolls without moving focus, and they are hidden from assistive technology.

You supply: A name for the trigger, which also names the menu. Text children are enough. An icon-only trigger needs aria-label or aria-labelledby; without one, Button.Root logs an error in development. A menu opened from code or from a Menu.ContextTrigger with no Menu.Trigger needs its own aria-label or aria-labelledby (without one, Menu.Content logs an error in development when it opens), a context menu needs a visible Menu.Trigger too, and every Menu.Group and Menu.RadioGroup needs one, as its type requires. Pass textValue when an item's children are not plain text, so typeahead can match it. Menu.ItemIcon and Menu.ItemShortcut are already hidden, so a plain label needs no textValue. Put aria-keyshortcuts on an item that shows a shortcut.

import type { ReactNode } from 'react';
import { Button, Menu } from '@tounsoo/todae';
import { Ellipsis } from 'lucide-react';

export function RowActions({ label, children }: { label: string; children: ReactNode }) {
  return (
    <Menu.Root>
      <Menu.Trigger variant="ghost" aria-label={label}>
        <Button.Icon>
          <Ellipsis />
        </Button.Icon>
      </Menu.Trigger>
      <Menu.Content>{children}</Menu.Content>
    </Menu.Root>
  );
}

Watch for: A Kbd in the item's label instead of Menu.ItemShortcut, which joins the name and the typeahead text. A wrapper that takes a label prop but drops it before Menu.Trigger, leaving both the trigger and the menu unnamed. Links, inputs or buttons inside an item: its content is presentational, so they are not announced as controls. Hiding the check or radio indicator and showing the state with color alone. A Menu.Trigger onClick that calls preventDefault(), which stops the trigger from opening or closing the menu. A context menu that is the only way to reach its actions: a long press on iOS doesn't open it, and few people know Shift+F10. A context menu over content whose browser menu people need, such as links and images, since it replaces Open in new tab and Save image. Submenus nested more than two deep, which are hard to follow and to reach with a pointer.

When to use

When: A short list of commands or view settings behind one button, such as row actions or a toolbar overflow. The same commands on a right click or Shift+F10 over a row, file or message, with Menu.ContextTrigger beside the button.

When not: Picking a value for a form → Field.Select or the Combobox recipe. Navigation between pages → links. Content with its own controls or text → Popover with role="dialog".

Props

Menu.Root props
NameTypeDescription
openboolean

Controlled open state.

defaultOpenboolean

Initial open state when uncontrolled.

onOpenChange(open: boolean, reason: MenuOpenChangeReason) => void

Fires on open and close. reason is trigger, context, escape, outside, select or tab. Leaving open unchanged is a veto.

childrenReact.ReactNode

Menu.Trigger, Menu.ContextTrigger and Menu.Content.

Menu.Trigger props
NameTypeDescription
variantButtonVariant

Button variant.

Default: "primary"

tooltipboolean

Button tooltip. Off by default on a menu trigger.

Default: false

childrenReact.ReactNode

Trigger label.

Menu.ContextTrigger props
NameTypeDescription
render(props: MenuContextTriggerRenderProps) => ReactElement

Renders the region as another element, such as a table row or a list item. It receives every prop, ref included, and must pass them on.

childrenReact.ReactNode

The content a context request opens the menu from.

Menu.Content props
NameTypeDescription
aria-labelstring

Names the menu instead of the trigger.

aria-labelledbystring

Id of the element that names the menu. Default is the trigger.

childrenReact.ReactNode

Items, groups, separators and submenus. Mounted on the first open, and kept after it.

classNamestring

Optional class.

Menu.Item props
NameTypeDescription
onSelect() => void

Runs when the item is chosen.

closeOnSelectboolean

Close the menu after choosing.

Default: true

disabledboolean

aria-disabled. Stays in the arrow-key order, so it is announced, but cannot be chosen.

textValuestring

Typeahead text when children are not plain text.

childrenReact.ReactNode

Item label.

classNamestring

Optional class.

render(props: MenuItemRenderProps) => React.ReactElement

Renders the item on another element, such as an a or a router link that goes somewhere when chosen. It receives every prop, ref included, and must pass them on.

Menu.ItemIcon props
NameTypeDescription
childrenReact.ReactNode

Icon graphic.

Menu.ItemShortcut props
NameTypeDescription
childrenReact.ReactNode

The shortcut, such as nested Kbd.Root elements.

Menu.CheckboxItem props
NameTypeDescription
checkedboolean

Controlled checked state.

defaultCheckedboolean

Initial checked state when uncontrolled.

onCheckedChange(checked: boolean) => void

Fires with the next checked state.

closeOnSelectboolean

Close the menu after toggling.

Default: false

disabledboolean

aria-disabled. Stays in the arrow-key order, so it is announced, but cannot be chosen.

textValuestring

Typeahead text when children are not plain text.

childrenReact.ReactNode

Item label.

classNamestring

Optional class.

Menu.Group props
NameTypeDescription
aria-labelstring

Group name. Exactly one of aria-label or aria-labelledby.

aria-labelledbystring

Id of a visible group caption.

childrenReact.ReactNode

Items.

classNamestring

Optional class.

Menu.RadioGroup props
NameTypeDescription
valuestring | null

Controlled value. null is controlled with nothing checked.

defaultValuestring | null

Initial value when uncontrolled.

onValueChange(value: string) => void

Fires with the next value.

aria-labelstring

Group name. Exactly one of aria-label or aria-labelledby.

aria-labelledbystring

Id of a visible group caption.

childrenReact.ReactNode

Menu.RadioItem children.

classNamestring

Optional class.

Menu.RadioItem props
NameTypeDescription
value Requiredstring

Value this item sets on its group.

closeOnSelectboolean

Close the menu after choosing.

Default: false

disabledboolean

aria-disabled. Stays in the arrow-key order, so it is announced, but cannot be chosen.

textValuestring

Typeahead text when children are not plain text.

childrenReact.ReactNode

Item label.

classNamestring

Optional class.

Menu.Separator props
NameTypeDescription
classNamestring

Optional class.

Menu.Submenu props
NameTypeDescription
openboolean

Controlled open state.

defaultOpenboolean

Initial open state when uncontrolled.

onOpenChange(open: boolean, reason: MenuSubmenuOpenChangeReason) => void

Fires on open and close. reason is trigger, pointer, escape, arrow or parent. Leaving open unchanged is a veto.

childrenReact.ReactNode

One Menu.SubmenuTrigger and one Menu.SubmenuContent.

Menu.SubmenuTrigger props
NameTypeDescription
disabledboolean

aria-disabled. Stays in the arrow-key order, so it is announced, but cannot open.

textValuestring

Typeahead text when children are not plain text.

childrenReact.ReactNode

Item label. It also names the submenu.

classNamestring

Optional class.

Menu.SubmenuContent props
NameTypeDescription
childrenReact.ReactNode

Items, groups, separators and submenus. Mounted on the first open, and kept after it.

classNamestring

Optional class.