Context menu
A context menu: a panel opened by right-click or long-press on its trigger area. Render ContextMenu.Trigger and ContextMenu.Content inside it.
Built on bits-ui ContextMenu.
The item parts are the same components as Menu’s, so ContextMenu.Item, ContextMenu.CheckboxItem, and the rest are documented on the Menu page.
<ContextMenu bind:open> <ContextMenu.Trigger>Right-click here</ContextMenu.Trigger> <ContextMenu.Content> <ContextMenu.Item onclick={copy}>Copy</ContextMenu.Item> <ContextMenu.Item onclick={paste}>Paste</ContextMenu.Item> </ContextMenu.Content></ContextMenu>- Opens at the pointer, on right-click or long-press.
- Shares every item part with
Menu, so the item kinds, submenus, and shortcuts are documented there. openis bindable, two-way or as a getter and setter. State & binding explains both modes.disabledon the trigger lets the browser’s own menu show.- A context menu is a pointer shortcut. Keyboard and screen reader users cannot open it, so every action in it needs a reachable path elsewhere, such as a
Menuor a toolbar.
Anatomy
Section titled “Anatomy”Every part is a property on ContextMenu. ContextMenu renders no element. ContextMenu.Content portals itself to the body.
<ContextMenu> <ContextMenu.Trigger /> <ContextMenu.Content> <ContextMenu.Item /> <ContextMenu.Separator /> </ContextMenu.Content></ContextMenu>Put the area’s content in ContextMenu.Trigger and the items in ContextMenu.Content.
open: false
Code
<script lang="ts"> import { ContextMenu } from "@viamrobotics/prime-ui";
let open = $state(false);</script>
<div class="not-content"> <ContextMenu bind:open> <ContextMenu.Trigger class="flex h-24 items-center justify-center rounded-lg border border-dashed border-medium text-sm text-subtle-2" > Right-click or long-press </ContextMenu.Trigger> <ContextMenu.Content> <ContextMenu.Item>Copy</ContextMenu.Item> <ContextMenu.Item>Paste</ContextMenu.Item> </ContextMenu.Content> </ContextMenu>
<p class="mt-3 text-sm text-subtle-2">open: <code>{open}</code></p></div>States
Section titled “States”Plain area
Disabled
Styling
Section titled “Styling”Every part that renders an element takes class on that element. ContextMenu.Trigger renders a <div> with no look of its own, so give the area its own size and border. ContextMenu.Content wears z-menu; see z layers. The data attributes are the same as on Menu.
Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Right-click or long-press | Opens the menu at the pointer. |
| ArrowDown / ArrowUp | Moves focus to the next or previous item. |
| Home / End | Moves focus to the first or last item. |
| ArrowRight / ArrowLeft | Opens a submenu, or closes it and returns to its parent. |
| Typed characters | Focuses the next item whose text starts with them. |
| Enter / Space | On an item, activates it and closes the menu. |
| Escape | Closes the menu. |
| Prop | Type | Default | Description |
|---|---|---|---|
bind:open | boolean | Whether the menu is open. Two-way bindable. | |
children | Snippet | The ContextMenu.* parts. |
ContextMenu.CheckboxItem
A toggle in a Menu.Content. Bind checked two-way, or as a getter and setter to
veto or transform a change. A check icon leads the label while checked. The menu closes after a toggle.
<Menu.CheckboxItem bind:checked={showHidden}>Show hidden</Menu.CheckboxItem>| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Element id. null is not accepted. | |
bind:checked | boolean | false | Checked state. Two-way bindable. |
disabled | boolean | false | Prevent the item from being toggled. |
children | Snippet<[{ checked: boolean }]> | The item's content. Receives { checked }. |
Plus everything in Omit<HTMLAttributes<HTMLDivElement>, 'children'>, spread onto the element.
ContextMenu.Content
The panel of a ContextMenu, portaled to the body and opened at the pointer. It closes on Escape, on a press outside, and when an item is activated.
Takes extraClasses, children, portal, ...restProps }: Props as its props.
ContextMenu.Group
A set of related items in a Menu.Content, labeled by its Menu.GroupHeading.
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Element id. null is not accepted. |
Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.
ContextMenu.GroupHeading
A non-interactive heading that labels the Menu.Group or
Menu.RadioGroup it sits in. It must be a child of one of those, and
rendering it directly in the content throws when the menu opens.
Takes extraClasses, children, ...restProps }: Props as its props.
ContextMenu.Item
An action in a Menu.Content. Activation is the native onclick, which fires for a click, Enter, and Space.
The menu closes after the item is activated. A leading icon is part of the children.
<Menu.Item onclick={rename}> <Icon icon={mdiPencil} size="sm" /> Rename</Menu.Item>| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Element id. null is not accepted. | |
variant | 'default' | 'danger' | 'default' | Visual treatment. danger marks a destructive action. |
disabled | boolean | false | Prevent the item from being activated. |
Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.
ContextMenu.RadioGroup
A set of Menu.RadioItems sharing one value. Bind value two-way, or as a getter
and setter to veto or transform a change.
<Menu.RadioGroup bind:value={size}> <Menu.RadioItem value="small">Small</Menu.RadioItem> <Menu.RadioItem value="large">Large</Menu.RadioItem></Menu.RadioGroup>| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Element id. null is not accepted. | |
bind:value | string | The selected Menu.RadioItem's value. Two-way bindable. |
Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.
ContextMenu.RadioItem
A choice in a Menu.RadioGroup. Selecting it sets the group’s value to this item’s
value. A check icon leads the label while selected. The menu closes after a selection.
<Menu.RadioItem value="small">Small</Menu.RadioItem>Takes extraClasses, children: content, ...restProps }: Props as its props.
ContextMenu.Separator
A horizontal rule between groups of items in a Menu.Content.
Takes extraClasses, children, ...restProps }: Props as its props.
ContextMenu.Shortcut
A keyboard shortcut hint at the end of a Menu.Item. It is decorative and hidden from
assistive technology, so the item’s accessible name is its text alone. Binding the key is the app’s job.
<Menu.Item onclick={save}> Save <Menu.Shortcut>⌘S</Menu.Shortcut></Menu.Item>Takes extraClasses, children, ...restProps }: Props as its props.
ContextMenu.Sub
A nested menu inside a Menu.Content. It holds one Menu.SubTrigger and one Menu.SubContent.
<Menu.Sub bind:open> <Menu.SubTrigger>Export</Menu.SubTrigger> <Menu.SubContent> <Menu.Item onclick={exportPdf}>PDF</Menu.Item> </Menu.SubContent></Menu.Sub>| Prop | Type | Default | Description |
|---|---|---|---|
bind:open | boolean | Whether the submenu is open. Two-way bindable. | |
children | Snippet | A Menu.SubTrigger and a Menu.SubContent. |
ContextMenu.SubContent
The floating panel of a Menu.Sub. It opens beside its Menu.SubTrigger, closes on ArrowLeft, and returns focus to the trigger.
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Element id. null is not accepted. | |
dir | 'ltr' | 'rtl' | Text direction of the panel. auto is not accepted, because positioning needs a concrete direction. | |
sideOffset | number | 4 | Gap in pixels between the trigger and the panel. |
alignOffset | number | -4 | Shift in pixels along the trigger's edge, so the first item lines up with the trigger. |
Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.
ContextMenu.SubTrigger
The item in a Menu.Content that opens a Menu.Sub. It opens on hover, Enter, Space, and ArrowRight.
A trailing chevron marks it as a submenu.
Takes extraClasses, children, ...restProps }: Props as its props.
ContextMenu.Trigger
The area of a ContextMenu that opens it on right-click or long-press. It renders a <div> around its children. With disabled, right-clicks and long-presses
are ignored and the browser’s own menu shows.
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | Element id. null is not accepted. | |
disabled | boolean | false | Ignore right-clicks and long-presses. |
Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.