Skip to content

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>
Right-click here
  • 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.
  • open is bindable, two-way or as a getter and setter. State & binding explains both modes.
  • disabled on 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 Menu or a toolbar.

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.

Right-click or long-press

open: false

Code
Basic.svelte
<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>

Plain area

Right-click here

Disabled

The browser's own menu shows here

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.

KeyAction
Right-click or long-pressOpens the menu at the pointer.
ArrowDown / ArrowUpMoves focus to the next or previous item.
Home / EndMoves focus to the first or last item.
ArrowRight / ArrowLeftOpens a submenu, or closes it and returns to its parent.
Typed charactersFocuses the next item whose text starts with them.
Enter / SpaceOn an item, activates it and closes the menu.
EscapeCloses the menu.
PropTypeDefaultDescription
bind:openbooleanWhether the menu is open. Two-way bindable.
childrenSnippetThe 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>
PropTypeDefaultDescription
idstringElement id. null is not accepted.
bind:checkedbooleanfalseChecked state. Two-way bindable.
disabledbooleanfalsePrevent the item from being toggled.
childrenSnippet<[{ 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.

PropTypeDefaultDescription
idstringElement 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>
PropTypeDefaultDescription
idstringElement id. null is not accepted.
variant'default' | 'danger''default'Visual treatment. danger marks a destructive action.
disabledbooleanfalsePrevent 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>
PropTypeDefaultDescription
idstringElement id. null is not accepted.
bind:valuestringThe 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>
PropTypeDefaultDescription
bind:openbooleanWhether the submenu is open. Two-way bindable.
childrenSnippetA 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.

PropTypeDefaultDescription
idstringElement id. null is not accepted.
dir'ltr' | 'rtl'Text direction of the panel. auto is not accepted, because positioning needs a concrete direction.
sideOffsetnumber4Gap in pixels between the trigger and the panel.
alignOffsetnumber-4Shift 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.

PropTypeDefaultDescription
idstringElement id. null is not accepted.
disabledbooleanfalseIgnore right-clicks and long-presses.

Plus everything in HTMLAttributes<HTMLDivElement>, spread onto the element.