context-menu

Right-click context menu with keyboard navigation and viewport-aware positioning.

Description

The <context-menu> component provides a custom right-click menu that opens at the cursor position. It supports keyboard navigation, separators, danger items, and disabled states. Shares menu styling patterns with <drop-down>.

Basic Usage

Structure

ElementRequiredDescription
any element with data-triggeryesElement defining the right-click target area
<menu>yesContext menu with <li> items holding <button> actions

Child Attributes

AttributeOnValuesDescription
data-trigger*booleanElement defining the right-click target area
data-shortcutbuttonstringKeyboard shortcut to bind, e.g. 'meta+c'. Displayed as formatted hint.

Disabled Items

With Icons

Add <icon-wc> or inline <svg> inside buttons. The existing flex layout handles alignment automatically.

Keyboard Shortcuts

Add data-shortcut to any button to display a keyboard shortcut badge and bind a real global keyboard shortcut. Pressing the shortcut anywhere on the page triggers the button's click. The format is platform-aware — it shows symbols on Mac and Ctrl text on other platforms. Shortcuts are automatically cleaned up when the component disconnects.

Group Labels

Use <li data-group> to create labeled sections within the menu. Group labels are non-interactive and visually separated from items. Non-first groups automatically get a top border.

Keyboard Navigation

Key Action
Arrow DownMove to next item
Arrow UpMove to previous item
HomeMove to first item
EndMove to last item
Enter / SpaceActivate focused item
EscapeClose menu

JavaScript API

Methods

Method Description
close() Close the context menu

Events

Event Description
context-menu:open Fired when the menu opens
context-menu:close Fired when the menu closes
context-menu:select Fired when an item is selected (detail.item)

Accessibility

  • Full keyboard navigation (Arrow keys, Home/End, Enter, Escape)
  • Menu items have role="menuitem"
  • Separators have role="separator"
  • Closes on click outside, Escape, or scroll
  • Menu is positioned within viewport bounds