Skip to content

DropdownMenu ​

A menu opened from a button: actions, checkbox and radio rows, labels, separators, submenus. The keyboard model works on the rendered rows — their role and a data-nebula-item marker — never on a description of them, so rows you compose freely still behave as one menu.

Two behaviours are specific to a menu opened from a button. Opening it with the keyboard focuses the first row; opening it with the pointer does not, because pre-highlighting a row for a mouse user suggests it is about to be chosen. And ArrowDown on the trigger opens the menu and enters it, as every native menu button does.

DropdownMenu, ContextMenu and Menubar are the same menu opened three ways. Their rows come from one shared module, so the three cannot drift apart: what this page says about the rows holds for the other two.

sh
ream nebula:add dropdown-menu

Organism · organisms/DropdownMenu.ts · no dependencies

Usage ​

ts
import { html } from '@c9up/aurora'
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@c9up/nebula'

DropdownMenu({
  children: () => html`
    ${DropdownMenuTrigger({ children: 'Open' })}
    ${DropdownMenuContent({ children: () => html`
      ${DropdownMenuItem({ children: 'Profile', onSelect: () => openProfile() })}
      ${DropdownMenuItem({ children: 'Log out', onSelect: () => logOut() })}
    ` })}
  `,
})

DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuSub and DropdownMenuSubContent take their parts in a thunk — see composition. DropdownMenuContent has to be built during the menu's setup, directly in that thunk: the menu reads it once to set up its panel, and a content built later in an accessor leaves the menu with nothing to open.

Examples ​

A trigger that looks like a button ​

The trigger is a bare <button> with no classes of its own. Give it a button's look with buttonVariants:

ts
import { buttonVariants, DropdownMenuTrigger } from '@c9up/nebula'

DropdownMenuTrigger({ class: buttonVariants({ variant: 'outline' }), children: 'Account' })

Labels, groups, shortcuts ​

ts
import { html } from '@c9up/aurora'
import {
  DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel,
  DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuTrigger,
} from '@c9up/nebula'

DropdownMenu({
  children: () => html`
    ${DropdownMenuTrigger({ children: 'Menu' })}
    ${DropdownMenuContent({ children: () => html`
      ${DropdownMenuLabel({ children: 'My account' })}
      ${DropdownMenuGroup({ children: html`
        ${DropdownMenuItem({ children: html`Profile${DropdownMenuShortcut({ children: '⇧⌘P' })}` })}
        ${DropdownMenuItem({ children: html`Settings${DropdownMenuShortcut({ children: '⌘,' })}` })}
      ` })}
      ${DropdownMenuSeparator({})}
      ${DropdownMenuItem({ children: 'Archive', disabled: true })}
      ${DropdownMenuItem({ variant: 'destructive', children: 'Delete', onSelect: () => remove() })}
    ` })}
  `,
})

DropdownMenuShortcut only displays the keys: bind the real shortcut yourself. A disabled row stays in the menu, dimmed, and is skipped by the arrows, the pointer and the click.

Checkbox rows ​

A checkbox row holds no state of its own. It shows checked and reports the opposite in onCheckedChange, so a signal does both jobs:

ts
import { html, signal } from '@c9up/aurora'
import { DropdownMenu, DropdownMenuCheckboxItem, DropdownMenuContent, DropdownMenuTrigger } from '@c9up/nebula'

const statusBar = signal(true)
const activityBar = signal(false)

DropdownMenu({
  children: () => html`
    ${DropdownMenuTrigger({ children: 'View' })}
    ${DropdownMenuContent({ children: () => html`
      ${DropdownMenuCheckboxItem({ checked: statusBar, onCheckedChange: statusBar, children: 'Status bar' })}
      ${DropdownMenuCheckboxItem({ checked: activityBar, onCheckedChange: activityBar, children: 'Activity bar' })}
    ` })}
  `,
})

Choosing a checkbox row closes the menu, like any other row.

Radio rows ​

The same holds for a radio group: value says which row is checked, onValueChange reports the one chosen, and nothing changes until you write it back.

ts
import { html, signal } from '@c9up/aurora'
import {
  DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger,
} from '@c9up/nebula'

const layout = signal<string | undefined>('list')

DropdownMenu({
  children: () => html`
    ${DropdownMenuTrigger({ children: 'Layout' })}
    ${DropdownMenuContent({ children: () =>
      DropdownMenuRadioGroup({
        value: layout,
        onValueChange: layout,
        children: () => html`
          ${DropdownMenuRadioItem({ value: 'list', children: 'List' })}
          ${DropdownMenuRadioItem({ value: 'grid', children: 'Grid' })}
        `,
      }),
    })}
  `,
})

DropdownMenuSub pairs a row with a panel. The panel opens beside the row as soon as the pointer reaches it, or on ArrowRight, Enter or Space, overlapping the parent slightly so the pointer can travel diagonally into it.

ts
import { html } from '@c9up/aurora'
import {
  DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuSub,
  DropdownMenuSubContent, DropdownMenuSubTrigger, DropdownMenuTrigger,
} from '@c9up/nebula'

DropdownMenu({
  children: () => html`
    ${DropdownMenuTrigger({ children: 'Share' })}
    ${DropdownMenuContent({ children: () => html`
      ${DropdownMenuItem({ children: 'Copy link' })}
      ${DropdownMenuSub({ children: () => html`
        ${DropdownMenuSubTrigger({ children: 'Send to' })}
        ${DropdownMenuSubContent({ children: () => html`
          ${DropdownMenuItem({ children: 'Email' })}
          ${DropdownMenuItem({ children: 'Messages' })}
        ` })}
      ` })}
    ` })}
  `,
})

Choosing a row in a submenu closes the whole stack.

Opening it from your code ​

Pass open to hold the state yourself. onOpenChange reports every request — the trigger, Escape, a click outside, a chosen row — and the menu follows open, so write the request back:

ts
import { html, signal } from '@c9up/aurora'
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@c9up/nebula'

const menuOpen = signal(false)

DropdownMenu({
  open: menuOpen,
  onOpenChange: menuOpen,
  children: () => html`
    ${DropdownMenuTrigger({ children: 'Actions' })}
    ${DropdownMenuContent({ children: () => DropdownMenuItem({ children: 'Rename' }) })}
  `,
})

menuOpen(true)

Without open, the menu holds its own state, starting from defaultOpen.

Placement ​

The panel opens below the trigger, aligned on its start edge, 4 px away. side, align and sideOffset move it; it flips and shifts on its own when the viewport has no room.

ts
DropdownMenuContent({ side: 'top', align: 'end', sideOffset: 8, children: () => DropdownMenuItem({ children: 'Sign out' }) })

API ​

PropTypeDefaultDescription
childrenParts—The trigger and the content.
openReactive<boolean>—Controls the state. Present means the menu follows it.
defaultOpenbooleanfalseThe starting state when open is absent.
onOpenChange(open: boolean) => void—Every request to open or close.
PropTypeDefaultDescription
childrenSlot—The button's content.
classReactive<string>—The button's classes. It has none of its own.

Renders nothing where it is written: the menu portals the panel when it opens.

PropTypeDefaultDescription
childrenParts—The rows.
sideSide'bottom''top', 'right', 'bottom' or 'left' of the trigger.
alignAlign'start''start', 'center' or 'end' along that side.
sideOffsetnumber4Distance from the trigger, in pixels.
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 DropdownMenuSubTrigger and one DropdownMenuSubContent.
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. DropdownMenuContent does not need it: the menu portals its own panel.

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

Accessibility ​

The trigger carries aria-haspopup="menu", aria-expanded, and aria-controls while the menu is open. The panel is role="menu", named by the 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 first row; opened with the pointer, the panel itself takes focus, so the arrows and Escape reach the menu rather than the page behind it. When the menu closes while focus is in its top-level panel, focus goes back to the trigger. The pointer moving onto a row focuses it.

KeyAction
Enter, Space, ArrowDownOn the trigger: open the menu and focus its first row.
ArrowDown, ArrowUpNext or previous row, wrapping at the ends. Disabled rows are skipped.
Home, EndFirst or last row.
Enter, SpaceChoose the focused row, or open its submenu.
ArrowRightOn a submenu row: open the submenu and focus its first row.
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.