Context Menu
A right-click menu.
Anatomy
<script>
import { ContextMenu } from '@shardsui/svelte/context-menu'
</script>
<ContextMenu.Root>
<ContextMenu.Trigger />
<ContextMenu.Portal>
<ContextMenu.Backdrop />
<ContextMenu.Positioner>
<ContextMenu.Popup>
<ContextMenu.Arrow />
<ContextMenu.Item />
<ContextMenu.LinkItem />
<ContextMenu.Separator />
<ContextMenu.SubmenuRoot>
<ContextMenu.SubmenuTrigger />
</ContextMenu.SubmenuRoot>
<ContextMenu.Group>
<ContextMenu.GroupLabel />
</ContextMenu.Group>
<ContextMenu.RadioGroup>
<ContextMenu.RadioItem>
<ContextMenu.RadioItemIndicator />
</ContextMenu.RadioItem>
</ContextMenu.RadioGroup>
<ContextMenu.CheckboxItem>
<ContextMenu.CheckboxItemIndicator />
</ContextMenu.CheckboxItem>
</ContextMenu.Popup>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>Most parts (Backdrop, Popup, Item, etc.) are re-exported from the Menu component, so they share the same props and data attributes. Positioner is the exception: its align, offset, arrowPadding and positionMethod defaults follow the pointer instead of a trigger element, and are documented below.
Usage guidelines
- Use context menus as an enhancement: they may be undiscoverable on touch devices or with assistive technology, so always pair them with a visible control for the same actions.
Examples
The Menu page has more demos — most of its patterns carry over to the context menu.
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.
Nested menu
Nest <ContextMenu.SubmenuRoot> inside the popup to create a submenu, and use <ContextMenu.SubmenuTrigger> for the menu item that opens the nested menu.
API reference
Root
Opens on right click or long press. Doesn't render its own HTML element.
Trigger
An area that opens the menu on right click or long press.
Renders a <div> element.
Positioner
Positions the context menu popup at the press point.
Renders a <div> element.
Other parts
Portal, Backdrop, Popup, Arrow, Item, LinkItem, Group, GroupLabel, RadioGroup, RadioItem, RadioItemIndicator, CheckboxItem, CheckboxItemIndicator, SubmenuRoot, SubmenuTrigger, Separator — see Menu for their props and data attributes.