Skip to content

ContextMenu ​

The menu a right-click opens. It opens at the pointer, not against a control: a zero-size anchor is moved to the click's coordinates and the panel is placed against it, so it flips and shifts off a viewport edge exactly as a button's menu would. The browser's own menu is suppressed inside the region.

Touch has no right button, so a long press — half a second — opens it instead. Moving the finger cancels the press, so a scroll that starts on the region does not open a menu under the thumb.

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 context-menu

Organism · organisms/ContextMenu.ts · no dependencies

Usage ​

ts
import { html } from '@c9up/aurora'
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from '@c9up/nebula'

ContextMenu({
  children: () => html`
    ${ContextMenuTrigger({ class: 'rounded-md border border-dashed p-12 text-sm', children: 'Right-click here' })}
    ${ContextMenuContent({ children: () => html`
      ${ContextMenuItem({ children: 'Cut', onSelect: () => cut() })}
      ${ContextMenuItem({ children: 'Copy', onSelect: () => copy() })}
      ${ContextMenuItem({ children: 'Paste', onSelect: () => paste() })}
    ` })}
  `,
})

ContextMenu, ContextMenuContent, ContextMenuRadioGroup, ContextMenuSub and ContextMenuSubContent take their parts in a thunk — see composition. ContextMenuContent has to be built during the menu's setup, directly in that thunk: the menu reads it once to set up its panel.

Examples ​

The region ​

ContextMenuTrigger is a <div> with no classes of its own: it is whatever area should answer to the right-click, styled as you need. It carries data-state="open" while its menu is showing, which is how a row stays highlighted while the menu opened on it is up:

ts
ContextMenuTrigger({ class: 'rounded-sm px-2 py-1 data-[state=open]:bg-accent', children: fileName })

Every kind of row ​

ts
import { html, signal } from '@c9up/aurora'
import {
  ContextMenu, ContextMenuCheckboxItem, ContextMenuContent, ContextMenuItem, ContextMenuLabel,
  ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuSeparator, ContextMenuShortcut,
  ContextMenuSub, ContextMenuSubContent, ContextMenuSubTrigger, ContextMenuTrigger,
} from '@c9up/nebula'

const showBookmarks = signal(true)
const person = signal<string | undefined>('pedro')

ContextMenu({
  children: () => html`
    ${ContextMenuTrigger({ children: 'Right-click here' })}
    ${ContextMenuContent({ class: 'w-52', children: () => html`
      ${ContextMenuItem({ inset: true, children: html`Back${ContextMenuShortcut({ children: '⌘[' })}` })}
      ${ContextMenuItem({ inset: true, disabled: true, children: html`Forward${ContextMenuShortcut({ children: '⌘]' })}` })}
      ${ContextMenuSub({ children: () => html`
        ${ContextMenuSubTrigger({ inset: true, children: 'More tools' })}
        ${ContextMenuSubContent({ children: () => html`
          ${ContextMenuItem({ children: 'Save page…' })}
          ${ContextMenuItem({ children: 'Developer tools' })}
        ` })}
      ` })}
      ${ContextMenuSeparator({})}
      ${ContextMenuCheckboxItem({ checked: showBookmarks, onCheckedChange: showBookmarks, children: 'Show bookmarks' })}
      ${ContextMenuSeparator({})}
      ${ContextMenuLabel({ inset: true, children: 'People' })}
      ${ContextMenuRadioGroup({
        value: person,
        onValueChange: person,
        children: () => html`
          ${ContextMenuRadioItem({ value: 'pedro', children: 'Pedro Duarte' })}
          ${ContextMenuRadioItem({ value: 'colm', children: 'Colm Tuite' })}
        `,
      })}
    ` })}
  `,
})

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.

Knowing when it opens ​

There is no open prop: only a right-click or a long press opens a context menu. onOpenChange reports both directions:

ts
import { html } from '@c9up/aurora'
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from '@c9up/nebula'

ContextMenu({
  onOpenChange: (open) => (open ? pauseAutoRefresh() : resumeAutoRefresh()),
  children: () => html`
    ${ContextMenuTrigger({ children: 'Right-click here' })}
    ${ContextMenuContent({ children: () => ContextMenuItem({ children: 'Refresh' }) })}
  `,
})

API ​

ContextMenu ​

PropTypeDefaultDescription
childrenParts—The trigger and the content.
onOpenChange(open: boolean) => void—Called when the menu opens and when it closes.

ContextMenuTrigger ​

The region that answers to the right-click and the long press, and the place the menu's anchor lives.

PropTypeDefaultDescription
childrenSlot—The region's content.
classReactive<string>—The region's classes. It has none of its own.

ContextMenuContent ​

Renders nothing where it is written: the menu portals the panel when it opens, 2 px below the pointer and starting at it.

PropTypeDefaultDescription
childrenParts—The rows.
classReactive<string>—Merged into the panel's classes.

ContextMenuItem ​

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.

ContextMenuCheckboxItem ​

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.

ContextMenuRadioGroup ​

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.

ContextMenuRadioItem ​

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.

ContextMenuLabel ​

PropTypeDefaultDescription
childrenSlot—The heading's text.
insetbooleanfalseIndents it to line up with checkbox and radio rows.
classReactive<string>—Merged into the label's classes.

ContextMenuGroup ​

PropTypeDefaultDescription
childrenSlot—The rows it gathers.
classReactive<string>—The group's classes.

ContextMenuSeparator ​

PropTypeDefaultDescription
classReactive<string>—Merged into the rule's classes.

ContextMenuShortcut ​

PropTypeDefaultDescription
childrenSlot—The keys, right-aligned in the row. Display only.
classReactive<string>—Merged into its classes.

ContextMenuSub ​

PropTypeDefaultDescription
childrenParts—One ContextMenuSubTrigger and one ContextMenuSubContent.

ContextMenuSubTrigger ​

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.

ContextMenuSubContent ​

PropTypeDefaultDescription
childrenParts—The submenu's rows.
classReactive<string>—Merged into the submenu panel's classes.

ContextMenuPortal ​

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

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

Accessibility ​

The panel is role="menu" with aria-orientation="vertical". 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. The trigger region has no role: it is whatever content you put in it.

The menu listens to the contextmenu event, which browsers also fire for the Menu key and Shift+F10 when focus is inside the region — so put something focusable in it if keyboard users need the menu. On open, the panel takes focus, so the arrows and Escape reach the menu rather than the page behind it. The pointer moving onto a row focuses it.

KeyAction
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.