# Menubar

A row of menus that stays on screen, such as File, Edit and View across the top of an editor: the WAI-ARIA menubar pattern. A menubar is for an application's commands; for links to a site's pages, use Navigation Menu, as WAI-ARIA advises. The bar is one tab stop, and the arrow keys, Home, End and typed characters move along it, wrapping at the ends. Each menu is a Menu, opened from an item of the bar, so its items, submenus, keys and typeahead are Menu's, and an app with both components pays for them once. A press, Down Arrow, Enter or Space opens a menu with focus on its first item. Once one menu is open, moving along the bar, by a key or the pointer, opens the next one with focus left on its item in the bar, and Right or Left Arrow inside a menu does the same. Escape closes the open menu and returns focus to its item in the bar; Tab and Shift+Tab close it and leave the bar. Name the bar with aria-label.

- Entry point: `menhir/menubar`
- Size: 12.4 kB, minified and gzipped, Solid left out

> To add Menubar to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

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

## Example

### Tailwind CSS

`MenubarExample.tsx`:

```tsx
import { Menubar } from "menhir/menubar";

const trigger =
  "cursor-default rounded px-3 py-1 text-sm hover:bg-stone-100 data-open:bg-stone-100 dark:hover:bg-stone-800 dark:data-open:bg-stone-800";
const popup =
  "m-0 min-w-40 rounded-lg border border-stone-200 bg-white p-1 text-sm text-inherit shadow-lg transition-opacity duration-100 data-closed:opacity-0 dark:border-stone-700 dark:bg-stone-900 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";
const separator = "my-1 border-stone-200 dark:border-stone-700";
const indicator = "-ml-5 w-3 data-unchecked:invisible";

export default function MenubarExample() {
  return (
    <Menubar.Root
      aria-label="Editor"
      class="flex gap-0.5 rounded-lg border border-stone-200 p-1 dark:border-stone-700"
    >
      <Menubar.Menu>
        <Menubar.Trigger class={trigger}>File</Menubar.Trigger>
        <Menubar.Popup class={popup} sideOffset={4}>
          <Menubar.Item class={item}>New</Menubar.Item>
          <Menubar.Item class={item}>Open…</Menubar.Item>
          <hr class={separator} />
          <Menubar.Submenu>
            <Menubar.SubmenuTrigger
              class={`${item} after:ml-auto after:content-['›']`}
            >
              Share
            </Menubar.SubmenuTrigger>
            <Menubar.Popup class={popup}>
              <Menubar.Item class={item}>Email</Menubar.Item>
              <Menubar.Item class={item}>Messages</Menubar.Item>
            </Menubar.Popup>
          </Menubar.Submenu>
        </Menubar.Popup>
      </Menubar.Menu>
      <Menubar.Menu>
        <Menubar.Trigger class={trigger}>Edit</Menubar.Trigger>
        <Menubar.Popup class={popup} sideOffset={4}>
          <Menubar.Item class={item}>Undo</Menubar.Item>
          <Menubar.Item class={item} disabled>
            Redo
          </Menubar.Item>
          <hr class={separator} />
          <Menubar.Item class={item}>Copy</Menubar.Item>
          <Menubar.Item class={item}>Paste</Menubar.Item>
        </Menubar.Popup>
      </Menubar.Menu>
      <Menubar.Menu>
        <Menubar.Trigger class={trigger}>View</Menubar.Trigger>
        <Menubar.Popup class={popup} sideOffset={4}>
          <Menubar.CheckboxItem class={item} defaultChecked>
            <Menubar.ItemIndicator class={indicator}>✓</Menubar.ItemIndicator>
            Word wrap
          </Menubar.CheckboxItem>
          <Menubar.CheckboxItem class={item}>
            <Menubar.ItemIndicator class={indicator}>✓</Menubar.ItemIndicator>
            Minimap
          </Menubar.CheckboxItem>
        </Menubar.Popup>
      </Menubar.Menu>
    </Menubar.Root>
  );
}
```

### CSS

`MenubarExample.tsx`:

```tsx
import { Menubar } from "menhir/menubar";

import "./menubar.css";

export default function MenubarExample() {
  return (
    <Menubar.Root class="menubar" aria-label="Editor">
      <Menubar.Menu>
        <Menubar.Trigger class="menubar-trigger">File</Menubar.Trigger>
        <Menubar.Popup class="menubar-popup" sideOffset={4}>
          <Menubar.Item class="menubar-item">New</Menubar.Item>
          <Menubar.Item class="menubar-item">Open…</Menubar.Item>
          <hr class="menubar-separator" />
          <Menubar.Submenu>
            <Menubar.SubmenuTrigger class="menubar-item menubar-submenu-trigger">
              Share
            </Menubar.SubmenuTrigger>
            <Menubar.Popup class="menubar-popup">
              <Menubar.Item class="menubar-item">Email</Menubar.Item>
              <Menubar.Item class="menubar-item">Messages</Menubar.Item>
            </Menubar.Popup>
          </Menubar.Submenu>
        </Menubar.Popup>
      </Menubar.Menu>
      <Menubar.Menu>
        <Menubar.Trigger class="menubar-trigger">Edit</Menubar.Trigger>
        <Menubar.Popup class="menubar-popup" sideOffset={4}>
          <Menubar.Item class="menubar-item">Undo</Menubar.Item>
          <Menubar.Item class="menubar-item" disabled>
            Redo
          </Menubar.Item>
          <hr class="menubar-separator" />
          <Menubar.Item class="menubar-item">Copy</Menubar.Item>
          <Menubar.Item class="menubar-item">Paste</Menubar.Item>
        </Menubar.Popup>
      </Menubar.Menu>
      <Menubar.Menu>
        <Menubar.Trigger class="menubar-trigger">View</Menubar.Trigger>
        <Menubar.Popup class="menubar-popup" sideOffset={4}>
          <Menubar.CheckboxItem class="menubar-item" defaultChecked>
            <Menubar.ItemIndicator class="menubar-indicator">
              ✓
            </Menubar.ItemIndicator>
            Word wrap
          </Menubar.CheckboxItem>
          <Menubar.CheckboxItem class="menubar-item">
            <Menubar.ItemIndicator class="menubar-indicator">
              ✓
            </Menubar.ItemIndicator>
            Minimap
          </Menubar.CheckboxItem>
        </Menubar.Popup>
      </Menubar.Menu>
    </Menubar.Root>
  );
}
```

`menubar.css`:

```css
/* The least CSS that makes the Menubar look like one: a row of items, each
   with a card below it, and a submenu beside it. Menhir places each popup;
   an item of the bar whose menu is open has data-open, and the item the
   keys or the pointer reached in a menu has data-active, and a disabled one
   data-disabled. A checkbox item's indicator shows by its data-checked. The
   popups fade in on data-open and out on data-closed. Colours are
   Tailwind's stone palette; light-dark() picks the second one in dark mode
   (color-scheme: dark). */
.menubar {
  display: flex;
  gap: 0.125rem;
  padding: 0.25rem;
  border: 1px solid
    light-dark(
      oklch(92.3% 0.003 48.717) /* stone-200 */,
      oklch(37.4% 0.01 67.558) /* stone-700 */
    );
  border-radius: 0.5rem;
}

.menubar-trigger {
  padding: 0.25rem 0.75rem;
  border: 0;
  border-radius: 0.25rem;
  background: transparent;
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
  cursor: default;
}

.menubar-trigger:hover,
.menubar-trigger[data-open] {
  background: light-dark(
    oklch(97% 0.001 106.424) /* stone-100 */,
    oklch(26.8% 0.007 34.298) /* stone-800 */
  );
}

.menubar-popup {
  --border: light-dark(
    oklch(92.3% 0.003 48.717) /* stone-200 */,
    oklch(37.4% 0.01 67.558) /* stone-700 */
  );
  min-width: 10rem;
  margin: 0;
  padding: 0.25rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: light-dark(white, oklch(21.6% 0.006 56.043) /* stone-900 */);
  color: inherit;
  font:
    0.875rem system-ui,
    sans-serif;
  box-shadow: 0 4px 12px oklch(0% 0 0 / 10%);
}

.menubar-popup[data-open] {
  animation: menubar-in 100ms ease-out;
}

.menubar-popup[data-closed] {
  animation: menubar-in 100ms ease-in reverse;
}

@keyframes menubar-in {
  from {
    opacity: 0;
  }
}

.menubar-item {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  padding: 0.375rem 0.5rem 0.375rem 1.75rem;
  border-radius: 0.25rem;
  outline: none;
  cursor: default;
  user-select: none;
}

.menubar-item[data-active] {
  background: light-dark(
    oklch(97% 0.001 106.424) /* stone-100 */,
    oklch(26.8% 0.007 34.298) /* stone-800 */
  );
}

.menubar-item[data-disabled] {
  opacity: 0.5;
}

/* The check mark takes the item's left padding. */
.menubar-indicator {
  margin-left: -1.25rem;
  width: 0.75rem;
}

.menubar-indicator[data-unchecked] {
  visibility: hidden;
}

.menubar-submenu-trigger::after {
  content: "›";
  margin-left: auto;
}

.menubar-separator {
  margin: 0.25rem 0;
  border: 0;
  border-top: 1px solid var(--border);
}
```

## API reference

### Root

<div role="menubar">, the bar, around its Menus. Name it with aria-label or aria-labelledby. It is horizontal.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the bar with your own component. Read once. |

### Menu

One menu of the bar: holds its open state and renders no element. Its Trigger and Popup have data-open or data-closed. Opening another menu of the bar closes this one.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | Whether the menu is open, when controlled. Pair it with onOpenChange. |
| `defaultOpen` | `boolean` | `false` | Whether 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", "trigger-hover" (the pointer moved onto another item of the bar while a menu was open), "arrow-key" (a key moved along the bar), "item-press" (an item was activated), "escape-key", "outside-press", "focus-out" (Tab or Shift + Tab moved focus out) or "close-request". A controlled menu changes only if open does. |

### Trigger

<button type="button" role="menuitem"> in the bar, with aria-haspopup="menu" and aria-expanded, that opens and closes its menu. The item that last had focus is the bar's tab stop, else the first. Render it inside a Menu, right before its Popup.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the item 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `side` | `"top" \| "right" \| "bottom" \| "left"` | `"bottom", in a submenu the inline end` | The 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. |
| `sideOffset` | `number` | `0` | The gap between its trigger and the Popup, in pixels. |
| `alignOffset` | `number` | `0` | The shift along its trigger's side, in pixels. |
| `collisionPadding` | `number` | `5` | The space it keeps from the viewport's edges, in pixels. |
| `anchor` | `Element \| 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. |
| `returnFocus` | `HTMLElement \| ((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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | — | 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. |
| `label` | `string` | — | 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `checked` | `boolean` | — | Whether it is checked, when controlled. Pair it with onCheckedChange. |
| `defaultChecked` | `boolean` | `false` | Whether it is checked at first, when uncontrolled. |
| `onCheckedChange` | `(checked: boolean) => void` | — | Called with the new value when the user changes it. |
| `disabled` | `boolean` | — | 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. |
| `label` | `string` | — | 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string \| null` | — | The checked item's value, when controlled; null checks none. Pair it with onValueChange. |
| `defaultValue` | `string \| null` | `null` | The 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | The value its group holds while it is checked. |
| `disabled` | `boolean` | — | 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. |
| `label` | `string` | — | 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | Whether the submenu is open, when controlled. Pair it with onOpenChange. |
| `defaultOpen` | `boolean` | `false` | Whether 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". |
| `delay` | `number` | `100` | How long the pointer rests on the SubmenuTrigger before the submenu opens, in milliseconds. |
| `closeDelay` | `number` | `0` | How 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.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | — | 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. |
| `label` | `string` | — | 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

| Keys | Action |
| --- | --- |
| `Right Arrow, Left Arrow` | In the bar, moves to the next or previous item, wrapping; with a menu open, opens that item's menu, focus staying on the item. In a menu, on an item with no submenu, closes the menus and does the same; Left Arrow in a submenu closes it instead, and Right Arrow on a SubmenuTrigger opens its submenu. Mirrored right to left. |
| `Home, End` | Moves to the first or last item, of the bar or of the menu that has focus. |
| `A character` | Moves to the next item, of the bar or of the menu that has focus, whose label starts with it; characters typed within a second make one string. |
| `Down Arrow` | On an item of the bar, opens its menu with focus on its first item. In a menu, moves to the next item, from the last to the first. |
| `Up Arrow` | On an item of the bar, opens its menu with focus on its last item. In a menu, moves to the previous item, from the first to the last. |
| `Enter, Space` | On an item of the bar, opens its menu, or moves into it if open, with focus on its first item. On an item, activates it and closes the 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. |
| `Escape` | Closes the menu that has focus, or the open menu of the focused item of the bar, and returns focus to the item it opened from. |
| `Tab, Shift + Tab` | Closes every menu and moves focus out of the bar. |
