Skip to content

Menubar ​

The application menu bar: File, Edit, View, each a menu. There is one panel for the whole bar, not one per menu — only one menu is ever open, and moving between them changes the panel's anchor and contents rather than closing one and opening another. That is what makes the arrows feel like walking a bar instead of dismissing and summoning.

Once a menu is open, the pointer moving onto another trigger switches to it; while none is open, the pointer passing over the bar opens nothing. Each MenubarMenu takes its place in the bar as it is built, and that order is the order the arrows walk.

The rows are the DropdownMenu's, built from the same shared module: checkbox and radio rows, labels, shortcuts and submenus all work here the same way.

sh
ream nebula:add menubar

Organism · organisms/Menubar.ts · no dependencies

Usage ​

ts
import { html } from '@c9up/aurora'
import { Menubar, MenubarContent, MenubarItem, MenubarMenu, MenubarTrigger } from '@c9up/nebula'

Menubar({
  children: () => html`
    ${MenubarMenu({ children: () => html`
      ${MenubarTrigger({ children: 'File' })}
      ${MenubarContent({ children: () => html`
        ${MenubarItem({ children: 'New tab', onSelect: () => newTab() })}
        ${MenubarItem({ children: 'Print…', onSelect: () => print() })}
      ` })}
    ` })}
    ${MenubarMenu({ children: () => html`
      ${MenubarTrigger({ children: 'Edit' })}
      ${MenubarContent({ children: () => html`
        ${MenubarItem({ children: 'Undo', onSelect: () => undo() })}
        ${MenubarItem({ children: 'Redo', onSelect: () => redo() })}
      ` })}
    ` })}
  `,
})

Menubar, MenubarMenu, MenubarContent, MenubarRadioGroup, MenubarSub and MenubarSubContent take their parts in a thunk — see composition.

Examples ​

A full bar ​

ts
import { html, signal } from '@c9up/aurora'
import {
  Menubar, MenubarCheckboxItem, MenubarContent, MenubarItem, MenubarMenu, MenubarRadioGroup,
  MenubarRadioItem, MenubarSeparator, MenubarShortcut, MenubarSub, MenubarSubContent,
  MenubarSubTrigger, MenubarTrigger,
} from '@c9up/nebula'

const bookmarksBar = signal(false)
const profile = signal<string | undefined>('benoit')

Menubar({
  children: () => html`
    ${MenubarMenu({ children: () => html`
      ${MenubarTrigger({ children: 'File' })}
      ${MenubarContent({ children: () => html`
        ${MenubarItem({ children: html`New tab${MenubarShortcut({ children: '⌘T' })}` })}
        ${MenubarItem({ disabled: true, children: 'New incognito window' })}
        ${MenubarSeparator({})}
        ${MenubarSub({ children: () => html`
          ${MenubarSubTrigger({ children: 'Share' })}
          ${MenubarSubContent({ children: () => html`
            ${MenubarItem({ children: 'Email link' })}
            ${MenubarItem({ children: 'Messages' })}
          ` })}
        ` })}
      ` })}
    ` })}
    ${MenubarMenu({ children: () => html`
      ${MenubarTrigger({ children: 'View' })}
      ${MenubarContent({ children: () =>
        MenubarCheckboxItem({ checked: bookmarksBar, onCheckedChange: bookmarksBar, children: 'Always show bookmarks bar' }),
      })}
    ` })}
    ${MenubarMenu({ children: () => html`
      ${MenubarTrigger({ children: 'Profiles' })}
      ${MenubarContent({ children: () =>
        MenubarRadioGroup({
          value: profile,
          onValueChange: profile,
          children: () => html`
            ${MenubarRadioItem({ value: 'andy', children: 'Andy' })}
            ${MenubarRadioItem({ value: 'benoit', children: 'Benoit' })}
          `,
        }),
      })}
    ` })}
  `,
})

Checkbox and radio rows hold no state of their own: they show checked or value and report the change, and a signal passed to both sides does the rest — see DropdownMenu.

A menu that cannot open ​

A disabled trigger keeps its place in the bar but its menu never opens, by click, key or hover:

ts
MenubarMenu({ children: () => html`
  ${MenubarTrigger({ disabled: true, children: 'History' })}
  ${MenubarContent({ children: () => MenubarItem({ children: 'Show full history' }) })}
` })

The arrows do not step over it: walking the bar, from a trigger or from an open menu, stops at the menu before it.

The bar's thunk is called once, during setup, so it can map over a list:

ts
import { html } from '@c9up/aurora'
import { Menubar, MenubarContent, MenubarItem, MenubarMenu, MenubarTrigger } from '@c9up/nebula'

const menus = [
  { label: 'File', entries: [{ label: 'New', run: newTab }] },
  { label: 'Edit', entries: [{ label: 'Undo', run: undo }] },
]

Menubar({
  children: () =>
    menus.map((menu) =>
      MenubarMenu({
        children: () => html`
          ${MenubarTrigger({ children: menu.label })}
          ${MenubarContent({ children: () => menu.entries.map((entry) => MenubarItem({ children: entry.label, onSelect: entry.run })) })}
        `,
      }),
    ),
})

API ​

PropTypeDefaultDescription
childrenParts—The menus.
classReactive<string>—Merged into the bar's classes.

One menu in the bar. It provides its place to the trigger and the content inside it, so neither has to be told which menu it belongs to.

PropTypeDefaultDescription
childrenParts—One MenubarTrigger and one MenubarContent.
PropTypeDefaultDescription
childrenSlot—The button's content.
disabledbooleanfalseThe menu never opens.
classReactive<string>—Merged into the button's classes.

Renders nothing where it is written: the bar's panel shows it below the trigger, aligned on its start edge, 4 px away.

PropTypeDefaultDescription
childrenParts—The rows.
classReactive<string>—Merged into the panel's classes.
PropTypeDefaultDescription
childrenSlot—The row's content.
onSelect() => void—Runs when the row is chosen; the menu then closes.
variant'default' | 'destructive''default'destructive draws the row in the destructive colour.
disabledbooleanfalseDims the row and makes it unreachable.
insetbooleanfalseIndents the row to line up with checkbox and radio rows.
classReactive<string>—Merged into the row's classes.
PropTypeDefaultDescription
childrenSlot—The row's content.
checkedReactive<boolean>falseWhether the check mark shows.
onCheckedChange(checked: boolean) => void—Called with the opposite of checked.
disabledbooleanfalseDims the row and makes it unreachable.
classReactive<string>—Merged into the row's classes.
PropTypeDefaultDescription
childrenParts—The radio rows.
valueReactive<string | undefined>—The checked row's value.
onValueChange(value: string) => void—The row chosen.
classReactive<string>—Merged into the group's classes.
PropTypeDefaultDescription
valuestring—Required. Compared with the group's value.
childrenSlot—The row's content.
disabledbooleanfalseDims the row and makes it unreachable.
classReactive<string>—Merged into the row's classes.
PropTypeDefaultDescription
childrenSlot—The heading's text.
insetbooleanfalseIndents it to line up with checkbox and radio rows.
classReactive<string>—Merged into the label's classes.
PropTypeDefaultDescription
childrenSlot—The rows it gathers.
classReactive<string>—The group's classes.
PropTypeDefaultDescription
classReactive<string>—Merged into the rule's classes.
PropTypeDefaultDescription
childrenSlot—The keys, right-aligned in the row. Display only.
classReactive<string>—Merged into its classes.
PropTypeDefaultDescription
childrenParts—One MenubarSubTrigger and one MenubarSubContent.
PropTypeDefaultDescription
childrenSlot—The row's content. A chevron is added after it.
disabledbooleanfalseKeeps the arrows and the pointer off the row, so its submenu never opens. Unlike the other rows, it is not dimmed and not marked aria-disabled.
insetbooleanfalseIndents the row.
classReactive<string>—Merged into the row's classes.
PropTypeDefaultDescription
childrenParts—The submenu's rows.
classReactive<string>—Merged into the submenu panel's classes.

Mounts its children at the end of container, outside the component's own place in the page. MenubarContent does not need it: the menu portals its own panel.

PropTypeDefaultDescription
childrenSlot—What to mount.
container() => Element | nulldocument.bodyWhere to mount it.

Accessibility ​

The bar is role="menubar" and each trigger a <button> with role="menuitem", aria-haspopup="menu", aria-expanded, and aria-controls while its menu is open. The panel is role="menu", named by the open trigger through aria-labelledby. Rows are menuitem, menuitemcheckbox or menuitemradio, the last two with aria-checked; a disabled item, checkbox or radio row has aria-disabled="true". A submenu row has aria-haspopup="menu", and aria-expanded with aria-controls while its submenu is open.

Opened with the keyboard, focus lands on the menu's first row; opened with the pointer, the panel itself takes focus, so the arrows and Escape reach it rather than the page behind. When the menu closes while focus is in its top-level panel, focus goes back to its trigger. Each trigger is its own tab stop. ArrowRight on a submenu row is taken by the bar too, which walks to the next menu: open a submenu with Enter, Space or the pointer.

KeyAction
Enter, Space, ArrowDownOn a trigger: open its menu and focus the first row.
ArrowRight, ArrowLeftOn a trigger: focus the next or previous trigger, wrapping. In an open menu: open the next or previous menu and focus its first row. A disabled trigger is not skipped.
ArrowDown, ArrowUpIn a menu: next or previous row, wrapping. Disabled rows are skipped.
Home, EndFirst or last row.
Enter, SpaceChoose the focused row, or open its submenu.
ArrowLeftIn a submenu: close it and return to its row.
EscapeClose the menu, from a submenu too.
TabClose the menu.
LettersFocus the next row whose text starts with what was typed; keys typed within a second build one search.