Skip to Content
Getting startedFoundationsComponentsPackagesTools

Menu

A list of actions opened from a trigger.

Examples

Playground

States

An item at rest, hovered or focused (one highlight for both), pressed, disabled, and destructive.

Content

Items with an icon and a shortcut, a disabled item (skipped by the arrows, still announced), a separator, and a destructive item. One menu is open at a time: focus moves into a menu when it opens, and a menu closes when focus leaves it.

In context

An account menu at the end of a top bar: aligned to the end, opening with who is signed in.

Keyboard

Open with Enter, move with the arrows (Home and End jump), choose with Enter, or press Escape: focus goes back to Options every time.

Install

npx shadcn add @hipuku/menu

Usage

import { Menu, MenuItem, MenuSeparator } from '@/components/haus/components/Menu'

const trigger = useRef<HTMLButtonElement>(null)

<div style={{ position: 'relative' }}>
  <Button ref={trigger} aria-haspopup="menu" aria-expanded={open} onClick={() => setOpen(o => !o)}>
    Actions
  </Button>
  <Menu open={open} onClose={() => setOpen(false)} triggerRef={trigger} aria-label="Actions">
    <MenuItem onSelect={rename}>Rename</MenuItem>
    <MenuSeparator />
    <MenuItem tone="error" onSelect={remove}>Delete</MenuItem>
  </Menu>
</div>

API

Menu also takes its element's own attributes and forwards its ref. It is a Popover, so placement, alignment and width work as they do there.

PropTypeDescription
onSelectReact.ReactEventHandler<HTMLDivElement>
openrequiredboolean
onCloserequired() => void
triggerRefrequiredReact.RefObject<HTMLElement | null>The button that opens the menu. Focus returns here when it closes.
aria-labelrequiredstringThe menu's name, which a screen reader announces with it.
align"start" | "end" | "stretch"
placement"bottom" | "top"
width"auto" | "trigger" | "sm" | "md" | "lg" | "xl"
portalboolean

Parts

  • MenuItem: one action, with an optional icon and shortcut. tone="error" for an action that destroys something. Choosing it runs onSelect and closes the menu.
  • MenuSeparator: a rule between groups.
  • MenuLabel: a heading over a group. Not an action and not focusable.

shortcut only displays a key. It does not bind one: wire the shortcut yourself.

Accessibility

  • aria-label is required, so the menu is announced with a name.
  • Focus moves to the first item on open. The arrow keys move between items and skip disabled ones, Home and End jump to the ends, and Enter or Space chooses.
  • Escape or a click outside closes it, and focus returns to the trigger.

On this page