Back
Forward
Reload
More tools
Save page as…
Developer tools
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>
);
});.menu-context {
display: flex;
flex-direction: column;
gap: 12px;
font-family: inherit;
}
/* The area that opens the menu; the long press must not select text or show the iOS callout */
.menu-context-area {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 12px;
box-sizing: border-box;
width: 320px;
height: 160px;
border: 2px dashed light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
user-select: none;
-webkit-user-select: none;
-webkit-touch-callout: none;
}
.menu-context-area[data-state="open"] {
border-color: #009dfd;
}
.menu-context-button {
padding: 6px 10px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 13px;
color: light-dark(#0f172a, #ffffff);
}
.menu-context-button:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
/* The Content is a popover in the top layer; the library places it at the pointer */
.menu-context-content {
box-sizing: border-box;
min-width: 180px;
padding: 4px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font-family: inherit;
box-shadow: 0 8px 24px rgb(15 23 42 / 0.18);
}
.menu-context-content:focus-visible {
outline: none;
}
.menu-context-item {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 6px 8px;
border-radius: 4px;
font-size: 14px;
cursor: default;
outline: none;
}
/* The focused item: arrow keys, typeahead and the pointer move focus */
.menu-context-item:focus {
background: #009dfd;
color: #ffffff;
}
.menu-context-item[data-disabled] {
opacity: 0.5;
}
/* The SubTrigger stays highlighted while its submenu is open */
.menu-context-item[data-state="open"] {
background: light-dark(#f1f5f9, #ffffff14);
}
.menu-context-separator {
height: 1px;
margin: 4px 0;
background: light-dark(#e2e8f0, #ffffff1f);
}
.menu-context-icon {
width: 14px;
height: 14px;
}
.menu-context-status {
margin: 0;
font-size: 14px;
color: light-dark(#475569, #c9ddf0);
}Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreimport { Menu } from '@leve-ui/menu'; // or '@leve-ui/core'
<Menu.Root>
<Menu.ContextTrigger />
<Menu.Content aria-label="...">
<Menu.Item />
</Menu.Content>
</Menu.Root>
| Part | Element | Description |
|---|---|---|
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.
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.user-select: none and -webkit-touch-callout: none to the area so the browser does not select text or show its own callout.disabled, the area does not open the menu and the browser's menu is shown.| Prop | Default | Type |
|---|---|---|
| disabled | false | boolean Whether the area does not open the menu. The browser's own context menu is shown instead. |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-menu-context-trigger | Always present |
data-state | "open" | "closed" |
data-disabled | Present 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.
Menu.ContextTrigger renders no role: the area is your content, and its own semantics are kept.Menu.Content an aria-label (or aria-labelledby).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.| Key | Description |
|---|---|
Shift + F10 / ContextMenu | On a focused element in the area, opens the menu and focuses the first item. |
Escape | Closes the menu and returns focus to the element focused before. |
The keys inside the menu are those of Menu.