Search docs

Context Menu

A menu of actions that opens at the pointer on right click or long press.

A context menu is a Menu opened by Menu.ContextTrigger instead of Menu.Trigger. The items, groups, checkbox and radio items, submenus and keyboard interactions are those of Menu. The menu follows the Menu WAI-ARIA design pattern.

import { $, component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Menu } from '@leve-ui/menu';
import { LuChevronRight } from '@qwikest/icons/lucide';
import styles from './context.css?inline';

export default component$(() => {
  useStyles$(styles);
  const last = useSignal('Nothing yet');
  const select$ = $((action: string) => {
    last.value = action;
  });

  return (
    <div class="menu-context">
      <Menu.Root>
        {/* Right-click (or long-press) anywhere in the area. Keyboard users press Shift+F10 on the button. */}
        <Menu.ContextTrigger class="menu-context-area">
          <span>Right-click here</span>
          <button type="button" class="menu-context-button">
            Or focus me and press Shift+F10
          </button>
        </Menu.ContextTrigger>
        {/* There is no Trigger to take the name from, so name the menu with aria-label */}
        <Menu.Content aria-label="Page" class="menu-context-content">
          <Menu.Item onSelect$={() => select$('Back')} class="menu-context-item">
            Back
          </Menu.Item>
          <Menu.Item disabled class="menu-context-item">
            Forward
          </Menu.Item>
          <Menu.Item onSelect$={() => select$('Reload')} class="menu-context-item">
            Reload
          </Menu.Item>
          <Menu.Separator class="menu-context-separator" />
          <Menu.Sub>
            <Menu.SubTrigger class="menu-context-item">
              More tools
              <LuChevronRight class="menu-context-icon" aria-hidden="true" />
            </Menu.SubTrigger>
            <Menu.SubContent alignOffset={-5} class="menu-context-content">
              <Menu.Item onSelect$={() => select$('Save page as')} class="menu-context-item">
                Save page as…
              </Menu.Item>
              <Menu.Item onSelect$={() => select$('Developer tools')} class="menu-context-item">
                Developer tools
              </Menu.Item>
            </Menu.SubContent>
          </Menu.Sub>
        </Menu.Content>
      </Menu.Root>
      <p class="menu-context-status">Last action: {last.value}</p>
    </div>
  );
});

Installation#

Install the individual package, or use @leve-ui/core to get every component at once:

pnpm add @leve-ui/menu

Anatomy#

import { Menu } from '@leve-ui/menu'; // or '@leve-ui/core'

<Menu.Root>
  <Menu.ContextTrigger />
  <Menu.Content aria-label="...">
    <Menu.Item />
  </Menu.Content>
</Menu.Root>
PartElementDescription
Menu.Root<div>Wraps the parts and holds the open state.
Menu.ContextTrigger<div>The area that opens the menu.
Menu.Content<div>The menu, a popover="manual" element in the top layer.

The other parts are those of Menu. Use either Menu.Trigger or Menu.ContextTrigger in a Root, not both.

Behavior#

  • A right click in the area (Control + click on macOS) opens the menu at the pointer instead of the browser's menu. A right click elsewhere in the area while it is open moves it there.
  • Shift + F10 or the ContextMenu key on a focused element in the area opens the menu below that element. The area itself is not focusable, so put the focusable content (a list of files, a canvas with tabindex="0", ...) in it.
  • On touch screens, a long press (700ms, cancelled when the finger moves) opens the menu at the touch point. Add user-select: none and -webkit-touch-callout: none to the area so the browser does not select text or show its own callout.
  • The menu focuses its first item. Escape or selecting an item closes it; focus returns to the element focused before when it was opened with the keyboard.
  • With disabled, the area does not open the menu and the browser's menu is shown.

API Reference#

ContextTrigger#

PropDefaultType
disabledfalseboolean

Whether the area does not open the menu. The browser's own context menu is shown instead.

Also accepts all <div> attributes.

Data attributeValue
data-leve-menu-context-triggerAlways present
data-state"open" | "closed"
data-disabledPresent when disabled

The Content is placed at the pointer with side="bottom" and align="start", and flips or shifts to stay in the viewport. Its side, align and offset props work as with a Trigger. See Menu for the other parts.

Accessibility#

  • Menu.ContextTrigger renders no role: the area is your content, and its own semantics are kept.
  • There is no Trigger to name the menu, so give Menu.Content an aria-label (or aria-labelledby).
  • Keyboard users open the menu with Shift + F10 or the ContextMenu key on a focusable element in the area. Make sure the area has one, and offer the same actions elsewhere when you can, since the context menu is hard to discover.

Keyboard Interactions#

KeyDescription
Shift + F10 / ContextMenuOn a focused element in the area, opens the menu and focuses the first item.
EscapeCloses the menu and returns focus to the element focused before.

The keys inside the menu are those of Menu.