

# Menubar

A horizontal bar of menu triggers (File, Edit, View, Help) like the menu of a desktop application. Each `Menubar.Menu` is a trigger plus a popup that you fill with the same `Menu.Item`, `Menu.Section` and `Menu.Separator` parts as [`Menu`](/components/menu). Use `Menu` for a single action menu opened by a button; use `Menubar` when several related menus sit side by side and people move between them.

```tsx
<Menubar aria-label="Application">
  <Menubar.Menu label="File">
    <Menu.Item id="new" onAction={newFile}>
      New
    </Menu.Item>
    <Menu.Separator />
    <Menu.Item id="quit" onAction={quit}>
      Quit
    </Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="Edit">...</Menubar.Menu>
</Menubar>
```

Only one menu is open at a time. While one is open, hovering another top-level item or pressing Left / Right switches to that menu.

## Examples

### Application menu

Items take `onAction`, `disabled` and `textValue` (typeahead text when the children are not a plain string). Group items with `Menu.Section` (optional `title`) and `Menu.Separator`. Shortcuts are ordinary children: put the label and a `Kbd` in the item.

```tsx
import { Kbd, Menu, Menubar } from '@var-ui/react';

function Row({ label, shortcut }: { label: string; shortcut: string }) {
  return (
    <>
      <span style={{ flex: 1 }}>{label}</span>
      <Kbd>{shortcut}</Kbd>
    </>
  );
}

<Menubar aria-label="Application">
  <Menubar.Menu label="File">
    <Menu.Item id="new" textValue="New file">
      <Row label="New file" shortcut="⌘N" />
    </Menu.Item>
    <Menu.Item id="open" textValue="Open">
      <Row label="Open…" shortcut="⌘O" />
    </Menu.Item>
    <Menu.Separator />
    <Menu.Section title="Recent">
      <Menu.Item id="r1">report.md</Menu.Item>
      <Menu.Item id="r2">notes.txt</Menu.Item>
    </Menu.Section>
    <Menu.Separator />
    <Menu.Item id="quit" textValue="Quit">
      <Row label="Quit" shortcut="⌘Q" />
    </Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="Edit">
    <Menu.Item id="undo" textValue="Undo">
      <Row label="Undo" shortcut="⌘Z" />
    </Menu.Item>
    <Menu.Item id="redo" textValue="Redo">
      <Row label="Redo" shortcut="⇧⌘Z" />
    </Menu.Item>
    <Menu.Separator />
    <Menu.Item id="cut" textValue="Cut">
      <Row label="Cut" shortcut="⌘X" />
    </Menu.Item>
    <Menu.Item id="copy" textValue="Copy">
      <Row label="Copy" shortcut="⌘C" />
    </Menu.Item>
    <Menu.Item id="paste" textValue="Paste">
      <Row label="Paste" shortcut="⌘V" />
    </Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="View">
    <Menu.Item id="zoom-in">Zoom in</Menu.Item>
    <Menu.Item id="zoom-out">Zoom out</Menu.Item>
    <Menu.Item id="fullscreen">Enter full screen</Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="Help">
    <Menu.Item id="docs">Documentation</Menu.Item>
    <Menu.Item id="about">About</Menu.Item>
  </Menubar.Menu>
</Menubar>
```

```astro
<!-- No Astro binding: Menubar holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Disabled items and menus

A disabled item cannot be focused or activated. A disabled `Menubar.Menu` is a disabled trigger: arrow keys skip it.

```tsx
import { Menu, Menubar } from '@var-ui/react';

<Menubar aria-label="Editor">
  <Menubar.Menu label="File">
    <Menu.Item id="new">New</Menu.Item>
    <Menu.Item id="save" disabled>
      Save
    </Menu.Item>
    <Menu.Item id="export" disabled>
      Export…
    </Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="Edit">
    <Menu.Item id="undo" disabled>
      Undo
    </Menu.Item>
    <Menu.Item id="paste">Paste</Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="Plugins" disabled>
    <Menu.Item id="manage">Manage plugins</Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="Help">
    <Menu.Item id="about">About</Menu.Item>
  </Menubar.Menu>
</Menubar>
```

```astro
<!-- No Astro binding: Menubar holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### In an app shell header

The bar is an inline-flex row, so it drops into a header or [`TopNav`](/components/top-nav). The `size` prop (or the surrounding control size) sets the density.

```tsx
import { AppShell, Menu, Menubar, Text, TopNav } from '@var-ui/react';

<div
  style={{
    height: '16.25rem',
    border: '0.0625rem solid var(--var-ui-color-border-subtle)',
    borderRadius: '0.5rem',
    overflow: 'hidden',
  }}
>
  <AppShell
    topNav={
      <TopNav>
        <Menubar aria-label="Application" size="sm">
          <Menubar.Menu label="File">
            <Menu.Item id="new">New</Menu.Item>
            <Menu.Item id="open">Open…</Menu.Item>
            <Menu.Separator />
            <Menu.Item id="close">Close window</Menu.Item>
          </Menubar.Menu>
          <Menubar.Menu label="Edit">
            <Menu.Item id="undo">Undo</Menu.Item>
            <Menu.Item id="redo">Redo</Menu.Item>
          </Menubar.Menu>
          <Menubar.Menu label="View">
            <Menu.Item id="sidebar">Toggle sidebar</Menu.Item>
          </Menubar.Menu>
        </Menubar>
      </TopNav>
    }
  >
    <Text size="sm">Main content</Text>
  </AppShell>
</div>
```

```astro
<!-- No Astro binding: Menubar holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Submenus

Put `Menu.Submenu` inside a `Menubar.Menu` like in [Menu](/components/menu). Left and Right are shared between the bar and the submenus: inside a submenu, the key that closes it (Left, Right in RTL) closes only that submenu; on a submenu trigger item, the key that opens it opens it; on any other item Left and Right move to the previous or next top-level menu.

```tsx
import { Menu, Menubar } from '@var-ui/react';

<Menubar aria-label="Application">
  <Menubar.Menu label="File">
    <Menu.Item id="new">New file</Menu.Item>
    <Menu.Submenu label="Open recent">
      <Menu.Item id="r1">report.md</Menu.Item>
      <Menu.Item id="r2">notes.txt</Menu.Item>
      <Menu.Separator />
      <Menu.Item id="clear">Clear list</Menu.Item>
    </Menu.Submenu>
    <Menu.Separator />
    <Menu.Item id="quit">Quit</Menu.Item>
  </Menubar.Menu>
  <Menubar.Menu label="Edit">
    <Menu.Item id="undo">Undo</Menu.Item>
    <Menu.Submenu label="Transform">
      <Menu.Item id="upper">Uppercase</Menu.Item>
      <Menu.Item id="lower">Lowercase</Menu.Item>
    </Menu.Submenu>
  </Menubar.Menu>
  <Menubar.Menu label="Help">
    <Menu.Item id="about">About</Menu.Item>
  </Menubar.Menu>
</Menubar>
```

```astro
<!-- No Astro binding: Menubar holds client state, use @var-ui/react -->
```

```html
<!-- No HTML demo yet — use @var-ui/react -->
```

### Watching a menu open and close

`Menubar.Menu` takes `onOpenChange(open, details)`. `details.reason` is `trigger-press`, `escape-key`, `outside-press`, `focus`, `hover` (the menu opened or closed because the pointer moved to another top-level item) or `imperative` (the menu changed with the arrow keys). `details.cancel()` vetoes the change.

## Props

| Name | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| children | ReactNode | no |  | The `Menubar.Menu`s. |
| className | string \| undefined | no |  |  |
| ref | Ref<HTMLDivElement> \| undefined | no |  | Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref). Ref to the `role="menubar"` element. Name it with `aria-label` or `aria-labelledby`. |
| size | "lg" \| "md" \| "sm" \| undefined | no | "md" | Control density. Falls back to the surrounding control size, then `md`. |
| style | CSSProperties \| undefined | no |  |  |

`Menubar.Menu` takes `label` (the trigger content), `children` (the menu items), `disabled`, `placement`, `offset`, `onOpenChange`, and everything a `<button>` takes (it goes onto the trigger).

## Accessibility

- Pattern: the APG [menubar](https://www.w3.org/WAI/ARIA/apg/patterns/menubar/) pattern. The root is `role="menubar"` (name it with `aria-label` or `aria-labelledby`); each top-level trigger is a native `<button type="button">` with `role="menuitem"`, `aria-haspopup="menu"` and `aria-expanded`. The popups are `role="menu"` elements named by their trigger.
- Focus: the bar is a single Tab stop (roving tabindex, on the last focused item or else the first). Disabled triggers are skipped. The Tab stop is applied on the client, so server markup is plain buttons.
- Focus return: closing a menu with Escape, or by activating an item, puts focus back on that menu's trigger. After switching menus with the arrow keys it returns to the new trigger.
- RTL: Left and Right are mirrored (read from the computed `direction`), the first item sits at the inline start, and popups align to the trigger's inline start.
- Forced colors: the open trigger and the focused menu item are drawn with `Highlight` / `HighlightText`, not only a background tint.
- Typeahead inside an open menu, and Up / Down / Home / End between items, are react-aria's menu behaviour, shared with `Menu`.

| Key                                                | Action                                                                |
| -------------------------------------------------- | --------------------------------------------------------------------- |
| Tab / Shift+Tab                                    | Moves focus into or out of the bar (one Tab stop)                     |
| Right / Left arrow                                 | Next / previous top-level item, wrapping (mirrored in RTL)            |
| Home / End                                         | First / last enabled top-level item                                   |
| Down arrow, Enter, Space                           | Opens the menu and focuses its first item                             |
| Up arrow                                           | Opens the menu and focuses its last item                              |
| Right / Left arrow (in menu)                       | Closes the menu and opens the next / previous one, first item focused |
| Right arrow (on a submenu item)                    | Opens its submenu, first item focused (Left in RTL)                   |
| Left arrow (in a submenu)                          | Closes the submenu and focuses its trigger item (Right in RTL)        |
| Right / Left arrow (in a submenu, on a plain item) | Moves to the next / previous top-level menu                           |
| Down / Up arrow (in menu)                          | Next / previous item, skipping disabled ones                          |
| Home / End (in menu)                               | First / last item                                                     |
| Letters (in menu)                                  | Typeahead to the next item that starts with them                      |
| Enter / Space (on an item)                         | Runs `onAction`, closes the menu, focuses the trigger                 |
| Escape                                             | Closes the menu and focuses its top-level item                        |

## Limitations

- Checkbox and radio menu items (`menuitemcheckbox`, `menuitemradio`) are not supported, as in `Menu`.
- Moving the pointer onto another top-level item while a menu is open switches to it; clicking an item that is already open closes it, as in desktop menu bars.
- The popups are rendered next to their triggers inside the bar (native popovers in the top layer), so the `role="dialog"` shell react-aria puts around each `role="menu"` is a child of the `role="menubar"` element while open.
- There is no Astro component: the bar keeps client state (which menu is open).


> Also accepts all standard HTML attributes (ARIA attributes, event handlers, styles, …). Run `var-ui component menubar --all-props` for the complete list.
