Liking cljdoc? Tell your friends :D

x-context-menu

A positioned dropdown overlay that opens on a trigger click. Items are placed in the default slot. The panel auto-flips when it would overflow the viewport.


Tag

<x-context-menu></x-context-menu>

Attributes

AttributeTypeDefaultDescription
openbooleanfalseWhether the menu is visible
disabledbooleanfalseDisables trigger and interactions
placementenum"bottom-start"Preferred panel placement: bottom-start bottom-end top-start top-end right-start left-start
offsetnumber8Distance in pixels between anchor and panel
z-indexnumber1000CSS z-index of the floating panel

Properties

PropertyTypeReflects attribute
openbooleanopen
disabledbooleandisabled
placementstringplacement
offsetnumberoffset
zIndexnumberz-index

Events

EventCancelableDetailDescription
x-context-menu-open-requestyes{}Fired before opening
x-context-menu-openno{}Fired after opening
x-context-menu-close-requestyes{}Fired before closing
x-context-menu-closeno{}Fired after closing
x-context-menu-selectno{ item }Fired when a menu item is selected

Slots

SlotDescription
triggerThe element that opens the menu
(default)Menu item elements

Slotted items with role="menuitem" are keyboard-navigable via ArrowUp / ArrowDown and selectable with Enter / Space.


Auto-flip

The panel is positioned using pure DOM geometry. When the preferred placement overflows the viewport, the panel flips to the opposite side. The position is clamped to viewport margins.


Keyboard

KeyAction
ArrowDownFocus next menu item
ArrowUpFocus previous menu item
EnterSelect focused item, close menu
SpaceSelect focused item, close menu
EscapeClose menu
TabClose menu

Accessibility

  • The panel has role="menu".
  • Each role="menuitem" item is keyboard navigable.
  • The trigger receives aria-haspopup="menu" and aria-expanded.

Examples

Basic

<x-context-menu>
  <button slot="trigger">Actions ▾</button>
  <div role="menuitem">Edit</div>
  <div role="menuitem">Duplicate</div>
  <div role="menuitem" style="color:red">Delete</div>
</x-context-menu>

Custom placement

<x-context-menu placement="bottom-end" offset="4">
  <button slot="trigger">⋯</button>
  <div role="menuitem">Rename</div>
  <div role="menuitem">Move</div>
</x-context-menu>

Listening to select

document.querySelector('x-context-menu')
  .addEventListener('x-context-menu-select', e => {
    console.log('selected', e.detail.item);
  });

ClojureScript (hiccup renderer)

[:x-context-menu
 [:button {:slot "trigger"} "Actions ▾"]
 [:div {:role "menuitem"} "Edit"]
 [:div {:role "menuitem"} "Duplicate"]
 [:div {:role "menuitem" :style "color:red"} "Delete"]]

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close