Context Menu

A menu that appears at the pointer on right click or long press. The popup has no stroke. Highlighted rows show the accent dot, and checked marks use the accent. Items are Regular and group labels are Light.

Right click here

Usage guidelines

  • Use context menus as an enhancement: Don’t make a context menu the only way to perform actions. Users may not discover or be able to open a context menu, especially on touch devices or with assistive technology. Always provide visible controls for the actions that are available in the context menu.

Anatomy

<UiContextMenuRoot>
  <UiContextMenuTrigger />
  <UiContextMenuPortal>
    <UiContextMenuBackdrop />
    <UiContextMenuPositioner>
      <UiContextMenuPopup>
        <UiContextMenuArrow />
        <UiContextMenuItem />
        <UiContextMenuLinkItem href="/" />
        <UiContextMenuSeparator />

        <UiContextMenuSubmenuRoot>
          <UiContextMenuSubmenuTrigger />
        </UiContextMenuSubmenuRoot>

        <UiContextMenuGroup>
          <UiContextMenuGroupLabel />
        </UiContextMenuGroup>

        <UiContextMenuRadioGroup>
          <UiContextMenuRadioItem value="a">
            <UiContextMenuRadioItemIndicator />
          </UiContextMenuRadioItem>
        </UiContextMenuRadioGroup>

        <UiContextMenuCheckboxItem>
          <UiContextMenuCheckboxItemIndicator />
        </UiContextMenuCheckboxItem>
      </UiContextMenuPopup>
    </UiContextMenuPositioner>
  </UiContextMenuPortal>
</UiContextMenuRoot>

Examples

Menu displays additional demos, many of which apply to the context menu as well.

Using with Menu

A context menu should supplement a primary way to perform the same actions. This image card exposes actions through a visible menu button and reuses them in the context menu for right-click and long-press users.

Station Hofplein

JPG, 2.4 MB

<div class="card">
  <UiContextMenuRoot>
    <UiContextMenuTrigger><!-- image --></UiContextMenuTrigger>
    <UiContextMenuPortal><!-- shared items --></UiContextMenuPortal>
  </UiContextMenuRoot>
  <UiMenuRoot>
    <UiMenuTrigger aria-label="Image actions"><!-- ⋮ --></UiMenuTrigger>
    <UiMenuPortal><!-- shared items --></UiMenuPortal>
  </UiMenuRoot>
</div>

Nested menu

To create a submenu, put UiContextMenuSubmenuRoot inside the parent popup. UiContextMenuSubmenuTrigger is the item that opens the nested menu. Hover or ArrowRight opens it to the right; ArrowLeft closes it.

Right click here
<UiContextMenuPopup>
  <UiContextMenuItem>Add to Library</UiContextMenuItem>
  <UiContextMenuSubmenuRoot>
    <UiContextMenuSubmenuTrigger>Add to Playlist</UiContextMenuSubmenuTrigger>
    <UiContextMenuPortal>
      <UiContextMenuPositioner :align-offset="-4" :side-offset="-4">
        <UiContextMenuPopup>
          <UiContextMenuItem>Get Up!</UiContextMenuItem>
          <UiContextMenuItem>New playlist…</UiContextMenuItem>
        </UiContextMenuPopup>
      </UiContextMenuPositioner>
    </UiContextMenuPortal>
  </UiContextMenuSubmenuRoot>
</UiContextMenuPopup>

API reference

Root

Creates a context menu activated by right click or long press. Doesn’t render its own HTML element.

PropTypeDefaultDescription
defaultOpenbooleanfalseUncontrolled initial open.
openboolean—Controlled open (v-model:open).
disabledbooleanfalseIgnore interaction.
highlightItemOnHoverbooleantrueHighlight items on pointer move.
loopFocusbooleantrueArrow keys wrap at the ends of the list.
EventTypeDescription
@update:open(open: boolean) => voidMenu opened or closed.

Trigger

Area that opens the menu on right click or long press. Renders a <div>.

AttributeDescription
data-popup-openPresent when the menu is open.
data-pressedPresent when the menu is open or a long-press is held.
data-disabledPresent when disabled.

Portal

Teleports to body while the menu is present.

PropTypeDefaultDescription
hiddenboolean—Skip rendering even while present.
AttributeDescription
data-openPresent when open.
data-closedPresent while present but not open.
data-starting-stylePresent while animating in.
data-ending-stylePresent while animating out.

Backdrop

Optional overlay behind the menu. Pointer-events none in this port.

Positioner

Pins the popup to the pointer, or to the submenu trigger when nested. Sets --transform-origin.

PropTypeDefaultDescription
sideOffsetnumber0 / -4 nestedGap from the anchor.
alignOffsetnumber0Shift along the trigger for nested menus.

Menu surface (role="menu"). Enter and leave use data-starting-style / data-ending-style (scale 0.98, 100ms). No animation library. No drop-shadow. Arrow keys, Enter, and Escape.

AttributeDescription
data-openPresent when open.
data-closedPresent when closed.
data-starting-stylePresent while animating in.
data-ending-stylePresent while animating out.

Arrow

Optional arrow. Hidden in the default styles.

Item

One action. Renders role="menuitem". data-highlighted shows the accent dot.

PropTypeDefaultDescription
disabledbooleanfalseIgnore presses. Sets data-disabled.
closeOnClickbooleantrueClose the menu when clicked.
labelstring—Override for typeahead.
AttributeDescription
data-highlightedPresent when highlighted.
data-disabledPresent when disabled.

LinkItem

Menu item that renders an <a>.

PropTypeDefaultDescription
hrefstring—Link destination.
disabledbooleanfalseIgnore presses.
closeOnClickbooleantrueClose the menu when clicked.

Group

Section of items. Renders role="group".

GroupLabel

Heading for a group.

Separator

Visual divider. Renders role="separator".

Owns a nested menu. Opens to the right of its trigger.

PropTypeDefaultDescription
defaultOpenbooleanfalseUncontrolled initial open.
disabledbooleanfalseIgnore interaction.
sideOffsetnumber-4Gap from the parent item.

Item that opens the nested menu. Renders role="menuitem" with aria-haspopup="menu". Hover or ArrowRight opens.

AttributeDescription
data-popup-openPresent when the submenu is open.
data-highlightedPresent when highlighted.

RadioGroup

Groups radio items. value / defaultValue / @update:value.

RadioItem

role="menuitemradio". Stays open on click by default.

PropTypeDefaultDescription
valuestring—Value selected when this item is chosen.
disabledbooleanfalseIgnore presses.
closeOnClickbooleanfalseClose the menu when clicked.

RadioItemIndicator

Check mark for a selected radio item.

CheckboxItem

role="menuitemcheckbox". Toggles checked. Stays open on click by default.

PropTypeDefaultDescription
defaultCheckedbooleanfalseUncontrolled initial tick.
checkedboolean—Controlled tick (v-model:checked).
disabledbooleanfalseIgnore presses.
closeOnClickbooleanfalseClose the menu when clicked.

CheckboxItemIndicator

Check mark while the checkbox item is ticked.