Menhir

Menu

View as Markdown

A list of actions or options that a button opens, with submenus: the WAI-ARIA menu button and menu patterns. The Popup is a popover="auto" element, so the browser puts it in the top layer, above every z-index, and closes it on Escape or a press outside; there is no portal. Focus goes to the first item when it opens; the arrow keys, Home and End move through the items, wrapping at the ends, and typing a character moves to the next item that starts with it. Activating an item calls its onClick and closes every open menu, and focus returns to the Trigger. A submenu opens when the pointer rests on its trigger, or by Right Arrow, Enter or Space, and it stays open while the pointer crosses to it diagonally, over the items in between. The active item has data-active. Menhir places each Popup with Floating UI, below the Trigger and aligned to its start, or at the inline end of a submenu's trigger, flipping it when it would leave the viewport, and keeps it shown until its exit animation has ended. Use an <hr> as a separator. For site navigation, use Navigation Menu: a menu is for actions in an application.

Size
11.9 kB

Example

Styling
MenuExample.tsx
import { Menu } from "menhir/menu";

const popup =
  "m-0 min-w-40 origin-(--transform-origin) rounded-lg border border-stone-200 bg-white p-1 text-sm text-inherit shadow-lg transition-[opacity,scale] duration-100 data-closed:scale-95 data-closed:opacity-0 dark:border-stone-700 dark:bg-stone-900 starting:data-open:scale-95 starting:data-open:opacity-0";
const item =
  "flex cursor-default items-center gap-2 rounded py-1.5 pr-2 pl-7 outline-none select-none data-active:bg-stone-100 data-disabled:opacity-50 dark:data-active:bg-stone-800";

export default function MenuExample() {
  return (
    <Menu.Root>
      <Menu.Trigger class="cursor-pointer rounded-md border border-stone-300 px-3 py-1.5 text-sm focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600">
        Edit
      </Menu.Trigger>
      <Menu.Popup sideOffset={4} class={popup}>
        <Menu.Item class={item}>Copy</Menu.Item>
        <Menu.Item class={item}>Cut</Menu.Item>
        <Menu.Item class={item} disabled>
          Paste
        </Menu.Item>
        <hr class="my-1 border-stone-200 dark:border-stone-700" />
        <Menu.CheckboxItem class={item} defaultChecked>
          <Menu.ItemIndicator class="-ml-5 w-3 data-unchecked:invisible">
            ✓
          </Menu.ItemIndicator>
          Word wrap
        </Menu.CheckboxItem>
        <Menu.Submenu>
          <Menu.SubmenuTrigger
            class={`${item} after:ml-auto after:content-['›']`}
          >
            Share
          </Menu.SubmenuTrigger>
          <Menu.Popup class={popup}>
            <Menu.Item class={item}>Email</Menu.Item>
            <Menu.Item class={item}>Messages</Menu.Item>
          </Menu.Popup>
        </Menu.Submenu>
      </Menu.Popup>
    </Menu.Root>
  );
}

Anatomy

<Menu.Root>
  <Menu.Trigger />
  <Menu.Popup>
    <Menu.Item />
    <Menu.CheckboxItem>
      <Menu.ItemIndicator />
    </Menu.CheckboxItem>
    <Menu.Group>
      <Menu.GroupLabel />
    </Menu.Group>
    <Menu.RadioGroup>
      <Menu.RadioItem>
        <Menu.ItemIndicator />
      </Menu.RadioItem>
    </Menu.RadioGroup>
    <hr />
    <Menu.Submenu>
      <Menu.SubmenuTrigger />
      <Menu.Popup />
    </Menu.Submenu>
  </Menu.Popup>
</Menu.Root>

API reference

Root

Holds the open state and renders no element. The Trigger and the Popup have data-open or data-closed.

PropTypeDefaultDescription
openboolean—Whether the menu is open, when controlled. Pair it with onOpenChange.
defaultOpenbooleanfalseWhether the menu is open at first, when uncontrolled.
onOpenChange(open: boolean, details: { reason, event }) => void—Called with the new value when the user opens or closes the menu, and why: "trigger-press", "item-press" (an item was activated), "escape-key", "outside-press", "focus-out" (Tab moved focus out) or "close-request" (such as another popover opening). A controlled menu changes only if open does.

Trigger

<button type="button" aria-haspopup="menu"> that opens and closes the menu, with aria-expanded and with popovertarget and aria-controls naming the Popup, which it labels. Name it with its content. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the trigger with your own button component. Read once.

Popup

<div popover="auto" role="menu">, placed against its trigger and labelled by it; without a Trigger, give it an aria-label. Its id is generated, for the trigger to name. Render it right after the Trigger, or right after a submenu's SubmenuTrigger.

PropTypeDefaultDescription
side"top" | "right" | "bottom" | "left""bottom", in a submenu the inline endThe side of its trigger it is placed against. It moves to the other side when it does not fit, and data-side says where it went.
align"start" | "center" | "end""start"Where along that side it sits. start and end follow the writing direction.
sideOffsetnumber0The gap between its trigger and the Popup, in pixels.
alignOffsetnumber0The shift along its trigger's side, in pixels.
collisionPaddingnumber5The space it keeps from the viewport's edges, in pixels.
anchorElement | VirtualElement | (() => Element | VirtualElement | undefined)—What it is placed against instead of its trigger: an element, a virtual element with a getBoundingClientRect(), such as a point, or a function returning one, for an element you hold in a ref.
returnFocusHTMLElement | ((focused) => HTMLElement | null | undefined)—Where focus goes once it has closed with focus inside, instead of its trigger. A function gets the element the browser focused, or null.
render(props) => JSX.Element—Renders the popup with your own component. Read once.

Item

<div role="menuitem">. A click, Enter or Space calls its onClick and closes every open menu. Render a link with render; Enter then follows it.

PropTypeDefaultDescription
disabledboolean—Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called.
labelstring—The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text.
render(props) => JSX.Element—Renders the item with your own component, such as a link. Read once.

CheckboxItem

<div role="menuitemcheckbox"> with aria-checked and data-checked or data-unchecked. A click and Enter change it and close the menu; Space changes it and keeps the menu open.

PropTypeDefaultDescription
checkedboolean—Whether it is checked, when controlled. Pair it with onCheckedChange.
defaultCheckedbooleanfalseWhether it is checked at first, when uncontrolled.
onCheckedChange(checked: boolean) => void—Called with the new value when the user changes it.
disabledboolean—Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called.
labelstring—The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text.
render(props) => JSX.Element—Renders the item with your own component. Read once.

RadioGroup

<div role="group"> that holds which of its RadioItems is checked, named by its GroupLabel.

PropTypeDefaultDescription
valuestring | null—The checked item's value, when controlled; null checks none. Pair it with onValueChange.
defaultValuestring | nullnullThe checked item's value at first, when uncontrolled.
onValueChange(value: string) => void—Called with the value of the item the user checks.
render(props) => JSX.Element—Renders the group with your own component. Read once.

RadioItem

<div role="menuitemradio"> with aria-checked and data-checked or data-unchecked. A click and Enter check it and close the menu; Space checks it and keeps the menu open.

PropTypeDefaultDescription
valuestring—The value its group holds while it is checked.
disabledboolean—Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called.
labelstring—The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text.
render(props) => JSX.Element—Renders the item with your own component. Read once.

ItemIndicator

aria-hidden <span> with its item's data-checked or data-unchecked, always rendered: show a check mark by state with CSS.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the indicator with your own component. Read once.

Group

<div role="group"> for items that belong together, named by its GroupLabel.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the group with your own component. Read once.

GroupLabel

<div> that names its Group or RadioGroup. The keys do not reach it. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the label with your own component. Read once.

Submenu

Holds a submenu's open state, as the Root does, and renders no element. Place it inside a Popup, around a SubmenuTrigger and the submenu's Popup.

PropTypeDefaultDescription
openboolean—Whether the submenu is open, when controlled. Pair it with onOpenChange.
defaultOpenbooleanfalseWhether the submenu is open at first, when uncontrolled.
onOpenChange(open: boolean, details: { reason, event }) => void—Called with the new value, and why: "trigger-hover", "trigger-press", "arrow-key" (Right or Left Arrow, or Up, Down, Home, End or typing moving through the menu around it), "item-press", "escape-key" or "outside-press".
delaynumber100How long the pointer rests on the SubmenuTrigger before the submenu opens, in milliseconds.
closeDelaynumber0How long after the pointer has left the SubmenuTrigger and the submenu it closes, in milliseconds. The pointer may cross to the submenu diagonally without a delay.

SubmenuTrigger

<div role="menuitem" aria-haspopup="menu"> with aria-expanded and data-open or data-closed: an item of the menu around it that opens its submenu, which it labels.

PropTypeDefaultDescription
disabledboolean—Whether it is disabled: the keys and the pointer still reach it, as the menu pattern asks, but it is not activated and its onClick is not called.
labelstring—The text typing a character matches, when the item's content is not its label, such as an icon and a shortcut beside the text.
render(props) => JSX.Element—Renders the item with your own component. Read once.

Keyboard

KeysAction
Enter, SpaceOn the Trigger, opens the menu with focus on its first item. On an item, activates it and closes every menu; Space on a checkbox or radio item changes it and keeps the menu open. On a SubmenuTrigger, opens the submenu with focus on its first item.
Down ArrowOn the Trigger, opens the menu with focus on its first item. In a menu, moves to the next item, from the last to the first.
Up ArrowOn the Trigger, opens the menu with focus on its last item. In a menu, moves to the previous item, from the first to the last.
Right ArrowOn a SubmenuTrigger, opens the submenu with focus on its first item. Left Arrow right to left.
Left ArrowIn a submenu, closes it and moves focus to its SubmenuTrigger. Right Arrow right to left.
Home, EndMoves to the first or last item.
A characterMoves to the next item whose label starts with it; characters typed within a second make one string.
EscapeCloses the menu that has focus, and returns focus to the Trigger or SubmenuTrigger that opened it.
Tab, Shift + TabCloses every menu; Tab moves focus to what follows the Trigger, Shift + Tab to the Trigger.