> A guide to [@fabiocv/ui](https://ui.fabio.cv/). Every guide is listed at https://ui.fabio.cv/llms.txt.

# Menu

A [Base UI Menu](https://base-ui.com/react/components/menu) built from an
`options` list, exported from `@fabiocv/ui/menu`. Base UI handles focus,
keyboard navigation, typeahead, disabled items, dismissal, and collision-aware
positioning.

```tsx
import { Button } from '@fabiocv/ui/button';
import { ChevronDown, Grid, Moon, Sun } from '@fabiocv/ui/icons';
import { Menu } from '@fabiocv/ui/menu';

<Menu
  aria-label="Actions"
  trigger={<Button leadingIcon={<ChevronDown />}>Actions</Button>}
  options={[
    { label: 'Duplicate', onClick: duplicate },
    { label: 'Share', options: [{ label: 'Copy link', onClick: copy }] },
    { type: 'separator' },
    {
      type: 'group',
      label: 'Help',
      options: [{ label: 'Docs', href: '/docs' }],
    },
    { type: 'checkbox', label: 'Show dates', checked, onCheckedChange },
    {
      type: 'radio-group',
      value: sort,
      onValueChange: setSort,
      options: [
        { label: 'Latest', value: 'latest' },
        { label: 'Oldest', value: 'oldest' },
      ],
    },
    {
      type: 'toggle-group',
      label: 'Theme',
      value: theme,
      onValueChange: setTheme,
      options: [
        { label: 'Light', value: 'light', icon: <Sun /> },
        { label: 'Dark', value: 'dark', icon: <Moon /> },
      ],
    },
    {
      type: 'toggle',
      label: 'Grid',
      icon: <Grid empty={!grid} />,
      pressed: grid,
      onPressedChange: setGrid,
    },
  ]}
/>;
```

| Option       | Shape                                                                                                    |
| ------------ | -------------------------------------------------------------------------------------------------------- |
| Item         | `{ label, icon?, leadingIconColor?, onClick?, disabled?, badge?, interactive? }`                         |
| Link         | `{ label, icon?, leadingIconColor?, href, onClick?, trailingIconOnHover?, trailingIconColor? }`          |
| Submenu      | `{ label, icon?, leadingIconColor?, options, onOpenChange?, trailingIconColor?, disabled?, className? }` |
| Checkbox     | `{ type: 'checkbox', label, checked?, defaultChecked?, onCheckedChange?, disabled? }`                    |
| Radio group  | `{ type: 'radio-group', value?, defaultValue?, onValueChange?, options }`                                |
| Radio item   | `{ label, value, icon?, disabled? }`, in a radio group's `options`                                       |
| Toggle group | `{ type: 'toggle-group', label, value?, defaultValue?, onValueChange?, options, disabled? }`             |
| Toggle       | `{ type: 'toggle', label, icon, pressed?, defaultPressed?, onPressedChange?, disabled? }`                |
| Group        | `{ type: 'group', label, options, notch? }`                                                              |
| Separator    | `{ type: 'separator', notch? }`                                                                          |

- The trigger is a `Button`, which takes the small size, its label `14px` like
  the items. The menu opens `4px` below it, aligned to its
  start, and the trigger keeps its hover background while the menu is open.
- The menu opens out of its trigger. Its box starts as the edge it shares
  with the trigger, as wide as the trigger but never wider than the list,
  then spreads across and unrolls away from it on `UNFOLD`, a response of
  `0.05s` and a damping ratio of `2`, quick off the mark and easing into
  place. Its corners stay round throughout, `8px` as soon as it is tall enough
  for them.
  The box reveals the items, which stay in place. It closes as the highlight
  goes, fading out and shrinking to `0.9` towards the trigger, on the quicker
  `TRACK`. With reduced motion it appears in place.
- A list is Surface subtle with `8px` corners and `2px` padding. Items are
  `32px` tall in `Text.Menu`, `14px` on a `20px` line, in Regular and
  Foreground secondary, with `6px` corners so they stay concentric with the
  list.
- Under the pointer, one Surface sunken highlight slides between the items
  of a list, on the quicker
  `TRACK` spring. It fades in along the pointer's path on the same spring,
  and out along it on `APPEAR`. It grows from a scale of `0.9` and back on
  `APPEAR`. The item's text turns Foreground on `TRACK`.
- Keyboard focus paints Surface sunken on the item itself and adds
  `focusRing`.
- A disabled item is Foreground disabled, and takes no highlight.
- An item with `interactive: false` still highlights, but keeps the default
  cursor and leaves the menu open when chosen.
- A link to another site, an `http` or `https` address, opens in a new tab and
  ends in an Arrow up right that says so. With `trailingIconOnHover`, the
  arrow shows only while the item is highlighted, fading in on the same spring
  as its text.
- An icon's ink is `text` or `muted`. A `text` icon is drawn in its item's
  text colour and turns Foreground with it. A `muted` icon stays Foreground
  muted, and turns Foreground disabled with a disabled item.
  `leadingIconColor` sets a leading icon's, and `trailingIconColor` a link's
  arrow or a submenu's chevron. Icons default to `text`.
- Icons are `24px`, drawn in their middle `16px`, which sits `8px` from the
  item's edge. A leading icon sits `12px` from its label, and a trailing one
  `8px`. Checkbox and radio items keep that box for
  their Check, so their labels line up whether checked or not. A radio item
  with an `icon` leads with it instead, and keeps the box for its Check at
  its end.
- A toggle group item leads with its `label` and ends in a `ToggleGroup` in
  the small size, with the clear appearance, so only its thumb shows. The
  group is flush with the item's top, bottom and end: as tall as the item,
  with its `6px` corners, and its toggles take `4px`, so they stay
  concentric. Its last icon is drawn `8px` from the end, where a trailing icon
  is. The item highlights like any other, and pressing a toggle keeps the menu
  open and the toggle focused. Clicking the item, or Enter or Space on it,
  presses the next toggle, round to the first.
- A toggle item ends in a `Toggle` the same way, small and clear, showing its
  `icon`. Pressed, it fills Separator. Clicking the item, or Enter or Space on
  it, turns the toggle on or off.
- The menu closes after a link, as after an item, since a link through the app's
  [router](https://ui.fabio.cv/router/index.md) leaves the page loaded under it. A link to
  another site opens in a new tab and leaves the menu open.
- The menu locks the page's scroll while it is open, unless `modal` is `false`.
- A submenu's `className` goes to its list, such as to set its width.
- The arrow keys reach a toggle group item like any other. ArrowRight moves
  into its group, Left and Right move between toggles, Space or Enter
  presses one, and Up and Down move on through the menu. Inside the group,
  Left never closes a submenu. ArrowRight moves into a toggle item's toggle
  too, and the other keys work on it as on the item.
- A group label is `Text.GroupLabel` in Medium and Foreground muted, lined
  up with the items' icons. With `notch`, the list is notched level with its
  text, as at a notched separator.
- A separator is a `1px` line of Separator across the full width of the list,
  with `4px` above and below. With `notch`, it is `8px` of space instead, with
  a notch cut into each side of the list at its middle. A notch is a circle of
  `3px` radius and meets the side in a `4px` fillet. It follows the separator
  as the list scrolls.
- A submenu reaches `16px` over its trigger. With the cutout gap and the
  list's padding, that is `20px` of room, which a submenu trigger keeps at its
  end. Its chevron sits at the edge like any item's icon.
- Beside a cut at the end, the open trigger's chevron springs `12px` into its
  room. Every other row whose label is level with the cut keeps its layout,
  and is masked where the trigger's shrunk background ends: the cut covers
  its trailing icon, and a label that runs that far fades out over the `24px`
  before it. The mask follows the background as it shrinks.
- Beside a cut at the start, leading icons and labels spring `20px` in, which
  keeps them `8px` inside the trigger's shrunk background. A label gives that
  room up from its own width and ends in an ellipsis where it no longer fits.
  The list keeps its width.
- Rows move on `TRACK`, with the submenu.
- Each list is a `Cutout` surface. A submenu opens over the end of its
  trigger, and the parent list is cut around it with the cutout gap. The cut
  runs along the end of the parent's items and follows the submenu wherever
  Base UI places it. The submenu's first item lines up with its trigger, which
  ends in a Chevron right. Where there is no room after its trigger, it opens
  over the trigger's start instead, and the list is cut on that side. The
  submenu also takes the pointer in the gap around it, so the pointer moves
  from the trigger straight into it.
- A submenu opens out of its trigger as the menu does. Its box starts as the
  edge it shares with the trigger, as tall as the trigger, then spreads
  across and unrolls away from it on `UNFOLD`, revealing the items, which
  stay in place. It also slides in `8px` from the side it opens on, so one on
  the right comes in from the right, on the quick `TRACK` spring. Its cut
  follows its box every frame, the gap opening with it. With reduced motion
  it appears in place.
- While its submenu is open, the trigger stays highlighted. Its background
  shrinks from full width to fit beside the cut, keeping the list's `2px`
  padding from it, on the quick `TRACK` spring.

`open`, `defaultOpen`, `onOpenChange` and `modal` go to Base UI's `Root`.
`anchor`, `side`, `align`, `sideOffset`, `alignOffset` and `positionMethod` go
to the `Positioner`, which defaults to bottom/start, `4px` from the trigger.
Other props go to the root list's `Popup`, such as `aria-label`, `className`,
`ref` and `finalFocus`.

For a menu opened by a canvas gesture, leave out `trigger`, control `open` with
`modal={false}`, pass an element, ref or virtual `anchor`, and set `finalFocus`
to the canvas.

## Types of `@fabiocv/ui/menu`

```ts
/** An icon's ink: the item's text colour, or Foreground muted. */
export type MenuIconColor = 'text' | 'muted';

type OptionBase = {
    label: string;
    /** Drawn before the label. */
    icon?: ReactNode;
    /** The leading icon's ink. Defaults to `text`. */
    leadingIconColor?: MenuIconColor;
};

export type MenuItemOption = OptionBase & {
    /** Called when the item is chosen. The menu closes after it. */
    onClick?: (event: MouseEvent<HTMLElement>) => void;
    disabled?: boolean;
    /** A short word at the end, such as Soon, shown while highlighted. */
    badge?: string;
    /**
     * `false` for an item that does nothing yet: it still highlights, keeps the
     * default cursor and leaves the menu open when chosen. Defaults to `true`.
     */
    interactive?: boolean;
};

export type MenuLinkOption = OptionBase & {
    /** The menu closes after it, unless it opens another site in a new tab. */
    href: string;
    onClick?: (event: MouseEvent<HTMLElement>) => void;
    /** Shows the trailing arrow only while the item is highlighted. */
    trailingIconOnHover?: boolean;
    /** The trailing arrow's ink. Defaults to `text`. */
    trailingIconColor?: MenuIconColor;
};

export type MenuSubmenuOption = OptionBase & {
    /** The submenu's own options. It opens beside this list. */
    options: readonly MenuOption[];
    /**
     * Called as the submenu opens and as it closes, with what did it, such as
     * `trigger-hover` when the pointer rests on its item.
     */
    onOpenChange?: (open: boolean, details: {
        reason: string;
    }) => void;
    disabled?: boolean;
    /** The chevron's ink. Defaults to `text`. */
    trailingIconColor?: MenuIconColor;
    /** Given to the submenu's list, such as to set its width. */
    className?: string;
};

export type MenuCheckboxOption = {
    type: 'checkbox';
    label: string;
    checked?: boolean;
    defaultChecked?: boolean;
    onCheckedChange?: (checked: boolean) => void;
    disabled?: boolean;
};

export type MenuRadioGroupOption = {
    type: 'radio-group';
    /** The chosen item's `value`. */
    value?: string;
    defaultValue?: string;
    onValueChange?: (value: string) => void;
    options: readonly {
        label: string;
        value: string;
        /** Drawn before the label, which moves the Check to the end. */
        icon?: ReactNode;
        disabled?: boolean;
    }[];
};

export type MenuToggleGroupOption = Pick<ToggleGroupProps, 'options' | 'value' | 'defaultValue' | 'onValueChange' | 'disabled'> & {
    type: 'toggle-group';
    /** Leads the row, and names the group. */
    label: string;
};

export type MenuToggleOption = Pick<ToggleProps, 'pressed' | 'defaultPressed' | 'onPressedChange' | 'disabled'> & {
    type: 'toggle';
    /** Leads the row, and names the toggle. */
    label: string;
    /** Shown in the toggle. */
    icon: ReactNode;
};

export type MenuGroupOption = {
    type: 'group';
    /** Shown above the group's options. */
    label: string;
    /** Notches both sides of the list level with the label. */
    notch?: boolean;
    options: readonly MenuOption[];
};

export type MenuSeparatorOption = {
    type: 'separator';
    /** Notches both sides of the list instead of drawing a line. */
    notch?: boolean;
};

export type MenuOption = MenuItemOption | MenuLinkOption | MenuSubmenuOption | MenuCheckboxOption | MenuRadioGroupOption | MenuToggleGroupOption | MenuToggleOption | MenuGroupOption | MenuSeparatorOption;

type ListProps = Omit<BaseMenu.Popup.Props, 'children' | 'className' | 'render'> & {
    options: readonly MenuOption[];
    className?: string;
};

export type MenuProps = ListProps & Pick<BaseMenu.Root.Props, 'open' | 'defaultOpen' | 'onOpenChange' | 'modal'> & Pick<BaseMenu.Positioner.Props, 'anchor' | 'side' | 'align' | 'sideOffset' | 'alignOffset' | 'positionMethod'> & {
    /**
     * The button that opens the menu. A `Button` takes the small size. Leave
     * it out to open the menu at `anchor`.
     */
    trigger?: ReactElement;
};

/**
 * A Base UI menu built from `options`: items, links, checkbox items, radio
 * groups, toggle groups, toggles, groups, separators and submenus. Other props
 * go to the popup, such as `aria-label` and `finalFocus`.
 */
export declare function Menu({ trigger, open, defaultOpen, onOpenChange, modal, anchor, side, align, sideOffset, alignOffset, positionMethod, className, ...list }: MenuProps): import("react").JSX.Element;
```

## Examples

### Items

```tsx
<Menu
  aria-label="Projects"
  trigger={<Button leadingIcon={<ChevronDown />}>Latest projects</Button>}
  options={[
    { label: 'Selected projects' },
    { label: 'Latest' },
    { label: 'I am feeling lucky' },
    { label: 'Archived', disabled: true },
  ]}
/>
```

### Links with icons

```tsx
<Menu
  aria-label="More"
  trigger={<Button leadingIcon={<More />}>More</Button>}
  options={[
    { label: 'E-mail', icon: <Copy /> },
    {
      label: 'LinkedIn',
      icon: <LinkedIn />,
      href: 'https://www.linkedin.com/',
      trailingIconOnHover: true,
    },
    {
      label: 'GitHub',
      icon: <GitHub />,
      href: 'https://github.com/',
      trailingIconOnHover: true,
    },
  ]}
/>
```

### Groups

```tsx
<Menu
  aria-label="More"
  trigger={<Button leadingIcon={<More />}>More</Button>}
  options={[
    {
      type: 'group',
      label: 'Contact',
      notch: true,
      options: [{ label: 'E-mail' }, { label: 'Résumé' }],
    },
    {
      type: 'group',
      label: 'On social',
      notch: true,
      options: [{ label: 'LinkedIn' }, { label: 'X' }],
    },
  ]}
/>
```

### Radio items

```tsx
<Menu
  aria-label="Theme"
  trigger={<Button leadingIcon={<Theme />}>Theme</Button>}
  options={[
    {
      type: 'radio-group',
      defaultValue: 'system',
      options: [
        { label: 'System', value: 'system', icon: <Display /> },
        { label: 'Light', value: 'light', icon: <Sun /> },
        { label: 'Dark', value: 'dark', icon: <Moon /> },
      ],
    },
  ]}
/>
```

### Checkbox items

```tsx
<Menu
  aria-label="Show projects"
  trigger={<Button leadingIcon={<ChevronDown />}>Projects</Button>}
  options={[
    { type: 'checkbox', label: 'Client work', defaultChecked: true },
    { type: 'checkbox', label: 'Side projects', defaultChecked: true },
    { type: 'checkbox', label: 'Experiments' },
  ]}
/>
```

### Toggles

```tsx
<Menu
  aria-label="Settings"
  trigger={<Button leadingIcon={<More />}>Settings</Button>}
  options={[
    {
      type: 'toggle-group',
      label: 'Theme',
      defaultValue: 'system',
      options: [
        { label: 'System', value: 'system', icon: <Display /> },
        { label: 'Light', value: 'light', icon: <Sun /> },
        { label: 'Dark', value: 'dark', icon: <Moon /> },
      ],
    },
    { type: 'toggle', label: 'Sound', icon: <Sound /> },
    { type: 'toggle', label: 'Grid', icon: <Grid /> },
  ]}
/>
```

### Submenus

```tsx
<Menu
  aria-label="Project"
  trigger={<Button leadingIcon={<More />}>Project</Button>}
  options={[
    { label: 'Open case study' },
    { label: 'Live site', href: 'https://example.com/' },
    { type: 'separator', notch: true },
    {
      label: 'Share',
      options: [
        { label: 'Copy link', icon: <Copy /> },
        { label: 'X', icon: <X />, href: 'https://x.com/' },
      ],
    },
    {
      label: 'Download',
      options: [
        { label: 'Case study as PDF' },
        { label: 'Images as ZIP' },
      ],
    },
    { label: 'Credits' },
  ]}
/>
```

See it at https://ui.fabio.cv/menu/.
