View as Markdown

Menus

Menu Button and Menu

MenuButton opens a Menu of items, checkable items, groups, separators and submenus, with the APG menu button keyboard model, typeahead, RTL and lazy popups.

import { MenuButton, MenuItem, MenuSeparator, Submenu } from '@salmonui/buttons/menu';
import '@salmonui/buttons/button/styles.css';
import '@salmonui/buttons/menu/styles.css';

<MenuButton
  onAction={(event) => console.log(event.id)}
  menu={
    <>
      <MenuItem id="new">New file</MenuItem>
      <MenuItem id="open">Open…</MenuItem>
      <MenuSeparator />
      <Submenu label="Export">
        <MenuItem id="pdf">PDF</MenuItem>
        <MenuItem id="csv">CSV</MenuItem>
      </Submenu>
    </>
  }
>
  File
</MenuButton>

MenuButton is a Button (same tone, variant, size, iconStart, iconEnd, iconOnly and disabled) whose children are the trigger content. A trailing chevron shows the open state; indicator replaces it and indicator={false} hides it. While the menu is open the trigger has aria-expanded="true", aria-controls and data-open. ref is the trigger <button>.

The menu content is either JSX in the menu prop or data in items. JSX wins when both are given, with a development warning. Menu renders the same list in place, without a button, with JSX children or items.

Menu Button

Open demo

A MenuButton with JSX items, a link item, a submenu and a labelled group, and a second one built from an items array with a disabled item and bottom-end placement.

Last action: nothing yet

Items

Component Role Notes
MenuItem menuitem id (required, stable, reported in onAction, rendered as data-item-id), icon, disabled, href / target render <a role="menuitem">, render for router links, textValue for typeahead
MenuCheckboxItem menuitemcheckbox checked / defaultChecked / onCheckedChange(checked, event); optional id for onAction, whose item handler runs after the toggle
MenuRadioGroup + MenuRadioItem group + menuitemradio value / defaultValue / onValueChange(value, event) and an optional visible label on the group; value per item (also its onAction id unless id is set)
MenuGroup group label is visible and names the group through aria-labelledby
MenuSeparator separator never focusable
Submenu menuitem with aria-haspopup="menu" label, icon, disabled, textValue; menuClassName / menuStyle style the nested popup; its children are the nested entries

Item content is any ReactNode. The items form takes MenuItemData entries: { id, label, icon, disabled, href, target, title, textValue }, { type: 'separator' }, { type: 'group', label, items }, and an entry with items is a submenu; renderItem(item) customizes the content of each entry. Router links use render, which receives every computed prop (role, tabIndex, ARIA, handlers, a callback ref) and must spread them:

<MenuItem id="settings" render={(props) => <Link {...props} to="/settings" />}>Settings</MenuItem>

Styling hooks: .salmon-menu on the popup, .salmon-menu-item with data-highlighted, data-disabled and data-checked, .salmon-menu-separator, .salmon-menu-group, .salmon-menu-group-label. className and style reach each item; classNames.menu / styles.menu reach the popup, whose default minimum width is the trigger width.

Menu items from data

Open demo

MenuButtons built from items - item icons, a renderItem template with counts, a wider popup through styles.menu, trigger tone, variant, size and icon, and a custom or hidden open indicator.

Last action: nothing yet

Checkable menu items

Open demo

Controlled MenuCheckboxItem and MenuRadioGroup items in a MenuButton; Space toggles and keeps the menu open.

Word wrap on, minimap off, zoom 100%. Space toggles an item and keeps the menu open; Enter toggles and closes.

Opening, closing and actions

  • open / defaultOpen / onOpenChange(open, event): the event has reason ('trigger' | 'escape' | 'outside' | 'action' | 'tab') and source ('pointer' | 'keyboard' | 'touch' | 'api'). A controlled menu vetoes a change by not updating open, which is how you cancel opening or closing. defaultOpen applies after hydration, so server and client markup match.
  • onAction(event) on MenuButton / Menu, and per item, fires for a chosen item at any level with { id, item, source }. The item handler runs first. event.preventDefault() keeps the menu open, and on a link item it also stops navigation.
  • placement: 'bottom-start' (default), 'bottom-end', 'top-start', 'top-end'; logical, so start is the right edge under dir="rtl". The popup flips to the other side and shifts to stay in the viewport, and follows the trigger while the page scrolls.
  • submenuOpen: 'hover' (default; a submenu opens after the pointer rests on its item for hoverDelay ms, default 200) or 'click'. The keyboard always opens submenus.
  • The popup is portaled to the trigger's document.body (or container) and uses the browser top layer where supported. A closed menu renders no markup on the server or the client, whatever the item count.
  • The menu closes on an item action, Escape, Tab, and a pointer press outside it (submenus count as inside). Other DOM events do not close it: drive the controlled open for that.

Menu open state, keyboard and ARIA

Open demo

A controlled MenuButton that logs every onOpenChange reason and source, can veto closing on an outside press, and reads back the trigger's ARIA and data attributes and the menu markup in the page.

Chosen: nothing yet

Trigger aria-expanded
false
Trigger aria-controls
(absent)
Trigger data-open
(absent)
Popup
(none)
Roles inside the popup
(none)
Menus for this trigger in the page
0
    Open or close the menu to log onOpenChange.

    Keyboard

    Key On the trigger In the menu
    Enter, Space, ArrowDown, Alt+ArrowDown Open, focus the first item Enter / Space activate the item and close; Space on a checkbox or radio item toggles and keeps the menu open
    ArrowUp Open, focus the last item Previous item, wrapping
    ArrowDown Next item, wrapping; separators and disabled items are skipped
    Home / End First / last enabled item
    Printable characters Typeahead: the next enabled item whose text starts with the typed characters (500 ms buffer, case and accent insensitive; a repeated letter cycles)
    ArrowRight (ArrowLeft in RTL) Open the submenu and focus its first item
    ArrowLeft (ArrowRight in RTL) Close the submenu, focus its item
    Escape, Alt+ArrowUp Close the current level; focus returns to the trigger or the submenu item
    Tab / Shift+Tab Close and move focus to the element after / before the trigger

    The pointer highlights the item under it. A submenu opens after the pointer rests on its item for 200 ms (hoverDelay) and closes 200 ms after the pointer rests on a sibling, so a diagonal move into the submenu keeps it open. A click on a submenu item opens it at once.

    Standalone menus and RTL

    Menu renders the same role="menu" list in place, with one tab stop and the same keys, for menus that are always visible. It takes JSX children or items, renderItem, onAction, submenuOpen, hoverDelay, className, style and aria-label / aria-labelledby. Under dir="rtl" the popup placement, the submenu side and the ArrowLeft / ArrowRight submenu keys mirror.

    Standalone menu and RTL menus

    Open demo

    A Menu rendered in place without a button, and a MenuButton with a submenu inside a right-to-left container whose popup and submenu mirror.

    Last action: nothing yet

    Accessibility

    The trigger follows the APG Menu Button pattern (aria-haspopup="menu", aria-expanded, aria-controls while open). Its name comes from its content; an iconOnly trigger needs aria-label. The popup itself has role="menu" and is labelled by the trigger. Items are menuitem, menuitemcheckbox or menuitemradio with aria-checked, inside groups. Disabled items keep aria-disabled="true" and are skipped by arrow keys and typeahead. When every item is disabled, the menu itself takes focus so Escape and Tab still work.

    Performance

    Menus are not virtualized. Focus, highlight, typeahead and hover intent work on the DOM and in refs, so arrow keys and pointer moves never re-render the items. Item labels are folded once per open, not per keystroke. With 1,000 items, opening stays within 100 ms and each arrow or typeahead keystroke within 8 ms. Long pickable lists belong to the Listbox component.

    Long menu

    Open demo

    A MenuButton with 100 or 1,000 items (menus are not virtualized), showing the time from the open request to the first frame with the menu.

    Open to first frame: open the menu

    Chosen: nothing yet

    SSR

    MenuButton and SplitButton render only their trigger on the server and while closed, whatever the item count, and defaultOpen applies after hydration; a standalone Menu renders its list on the server. Both hydrate without warnings.

    Design notes

    • The trigger has no icon-above-label layout; compose the content.
    • For arbitrary popup content instead of menu items, use a Popover: a Popover.Trigger rendering a Button and the content in Popover.Content, with dialog semantics instead of menu semantics.
    • Opening and closing use one fixed CSS transition with no animation options. Customize item rendering with JSX or renderItem. There are no imperative add, remove or toggle methods: change items or open.

    API reference

    @salmonui/buttons/menu: MenuButton, Menu, MenuItem, MenuCheckboxItem, MenuRadioGroup, MenuRadioItem, MenuGroup, MenuSeparator, Submenu and their types. The menu button needs both @salmonui/buttons/button/styles.css and @salmonui/buttons/menu/styles.css.