shadcn.io is an independent project built on shadcn/ui
Components
Free
Context Menu for Svelte UI SDK
Right-click or long-press an area to open its actions at the pointer. A context menu for Svelte on UI SDK, with submenus, checkboxes and radio items.
On this page9 sections
A context menu opens at the pointer when someone right-clicks an area, or holds a finger on it on a touch screen, and lists the actions for whatever is under it: a file in a grid, a row in a table, a shape on a canvas. Inside that area it takes the place of the browser's own menu.
Nothing on screen says the menu is there, so put the same actions on a visible button too, usually a Dropdown Menu. An app's main commands belong in a Menubar.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
The menu has role="menu", focus moves into it when it opens, and checkbox and radio rows report aria-checked. Focus moves onto each item in turn, disabled ones included, and typing a letter jumps to the next item that starts with it. The trigger is a div that this file puts in the tab order, so Tab reaches it, and Shift + F10 or the Menu key opens the menu from there on Windows and Linux, as from any focusable element inside the area.
KeyAction
Shift+F10
When focus is on the trigger area or an element inside it, opens the menu on Windows and Linux, as the Menu key does, and moves focus to the first item.
Space
On an item, chooses it and closes the menu, while a checkbox or radio item toggles and the menu stays open. On a submenu trigger, opens the submenu.
Enter
On an item, chooses it and closes the menu, while a checkbox or radio item toggles and the menu stays open. On a submenu trigger, opens the submenu.
Arrow Down
Moves focus to the next item, and from the last one back to the first.
Arrow Up
Moves focus to the previous item, and from the first one to the last.
Arrow Right
On a submenu trigger, opens the submenu and moves focus into it. In a right-to-left menu, closes the submenu instead.
Arrow Left
In a submenu, closes it and moves focus back to its trigger. In a right-to-left menu, opens a submenu instead.
Home
Moves focus to the first item.
End
Moves focus to the last item.
Page Up
Moves focus to the first item, as Home does.
Page Down
Moves focus to the last item, as End does.
Tab
Closes the menu, submenus included.
EscEscape
Closes the menu, or only the submenu that has focus. A submenu returns focus to its trigger, and the whole menu to the trigger area when it can take focus, otherwise to where focus was before it opened.
Demos
Basic
Back, Forward and Reload in one short list, with disabled on Forward so it stays in view but can't be chosen. Start from this for a handful of actions on one object, such as a page or a file, where the whole list reads at a glance. Add groups and separators once it grows.
File list
Four files, each row its own ContextMenu with the row inside ContextMenuTrigger, so Star and Delete act on the file that was right-clicked and nothing has to be read back from the click. Every trigger is a tab stop in this file, so Shift + F10 opens a row's menu from the keyboard. Reach for this shape in a file grid, a table or a list of messages.
Submenu
Copy and Cut sit at the top, and More Tools opens a second panel with Save Page, Create Shortcut and Name Window, then Developer Tools and a destructive Delete. Move the actions people reach for less often into a submenu so the first list stays short. Hovering More Tools opens it, and so do ArrowRight and Enter.
Shortcuts
Back, Forward and Reload end in ⌘[, ⌘] and ⌘R, and Save and Save As below a separator end in ⌘S and ⇧⌘S. ContextMenuShortcut is a styled span that prints the hint and listens for nothing. Bind the keys yourself at the page level, where they work without opening the menu, and show a hint only for a shortcut that exists.
Groups
File and Edit are labeled groups, Cut, Copy and Paste follow in a group with no label, and Delete sits alone at the end, each row with its shortcut. A ContextMenuGroupLabel names a section and can't be chosen, while a ContextMenuSeparator divides groups that need no name. Group a menu once it holds more than one kind of action.
Icons
Copy, Cut and Paste each start with an icon, and Delete has a trash can below a separator. Every icon has aria-hidden, since the label already names the action. Icons pay off on familiar commands that people open the menu for again and again, and add little beside a label nobody would recognize from a picture.
Checkboxes
Show Bookmarks Bar, Show Full URLs and Show Developer Tools are three ContextMenuCheckboxItem rows, each with checked and onCheckedChange held in state outside the menu, so the marks are still right the next time it opens. The menu stays open while people toggle them, so several settings can change in one visit.
Radio
People offers Pedro Duarte and Colm Tuite, and Theme offers Light, Dark and System, each in its own ContextMenuRadioGroup with its value in state, so choosing a person leaves the theme alone. The menu stays open after a choice, so people can set both. Use radio rows for a mode with exactly one answer.
Destructive
Edit and Share come first with their icons, then Delete below a separator with variant set to destructive, which colors the row as a warning. The variant changes the color and nothing else, so it asks for no confirmation. When the action can't be undone, open an Alert Dialog from the item, and keep the dangerous row last.
Sides
Four dashed areas, labeled top, right, bottom and left, open the same Back, Forward and Reload list on that side of the pointer, through a placement in positioning on ContextMenu. Without one, the menu opens to the right of the pointer, and it flips to the other side when there's no room. Pin a side when the default would cover what people right-clicked.
RTL
Arabic labels on two submenus, two view checkboxes and a people radio group. dir is rtl on ContextMenu, which this file turns into an Arabic locale for every panel, so the text and chevrons turn around, submenus open to the left and ArrowLeft opens them. Use it on Arabic or Hebrew pages.
FAQ
Should I use a Context Menu or a Dropdown Menu?
Use a Context Menu for actions on the thing under the pointer, such as a file in a grid, a row in a table or a shape on a canvas, where a button on every one would crowd the layout. Use a Dropdown Menu when the actions hang off a button people can see.
Nothing on screen tells people a right-click menu exists, so treat it as a shortcut rather than the only way in. Most apps offer the same list both ways: an ellipsis button on the row opens it as a dropdown, and a right-click anywhere on the row opens it at the pointer.
How do I run code when a Context Menu item is chosen?
Give each ContextMenuItem an onSelect, since an onclick runs for the pointer alone. UI SDK calls it for a pointer click and for Enter or Space, then closes the menu, unless the handler calls preventDefault on the event it receives, which keeps the menu open. To branch in one place instead, give each item a value and set onSelect on ContextMenu, which receives that value first.
The handler acts on whatever was right-clicked, so it needs to know which row or file that was. Writing one menu per row, inside the row's own markup, puts that row's data in scope.
How do I know which row was right-clicked in a Context Menu?
Give each row its own ContextMenu, with the row inside ContextMenuTrigger, as the File list demo does. The item handlers are then written where that row's data is in scope, so Delete already knows which file to delete and nothing has to be read back from the click.
This doesn't make a long list heavy. Nothing inside a menu is rendered until it opens, so a table of a few hundred rows carries a few hundred triggers and one open menu at a time.
How do I keep the Context Menu open after toggling a checkbox item?
You don't need to do anything: UI SDK leaves the menu open when a ContextMenuCheckboxItem is toggled or a ContextMenuRadioItem is chosen, as the Checkboxes demo shows. Set closeOnSelect on the item, or on ContextMenu for every item, when you'd rather it close after each change. Plain items close the menu unless their closeOnSelect is false, or their select handler calls preventDefault on its event.
Does the Context Menu replace the browser's right-click menu?
Inside ContextMenuTrigger, yes. A right-click there cancels the browser's contextmenu event and opens your menu, so Inspect, Copy link and Open in new tab aren't offered in that area. Outside it, right-click works as usual.
Keep the trigger to the region that needs its own actions, such as a file grid or a canvas, and leave plain text, links and images out of it. People expect to copy a sentence or open a link in a new tab with a right-click, and inside the area they no longer can.
Does the Context Menu open with a long press on a phone?
Yes. UI SDK times the press itself and opens the menu where the finger is after 500 milliseconds, which also works in Safari on iOS, where no contextmenu event fires. Moving more than 10 pixels first cancels it, so a scroll that starts on the area doesn't open the menu.
A long press is even harder to discover than a right-click, so give touch users a visible way to the same actions. The demos swap their Right click here label for Long press here on touch screens.
Can keyboard users open a Context Menu?
Yes. ContextMenuTrigger renders a div that this file gives a tabindex of 0, so Tab reaches it, and Shift + F10 or the Menu key on Windows and Linux fire the browser's contextmenu event there or on a link or button inside the area, which opens the menu. Set tabindex to -1 on the trigger when the row inside it takes focus already.
Few people know these keys, so keep every action in the menu reachable from a visible control as well.
Why can't I scroll the page while a Context Menu is open?
Nothing locks it on UI SDK by default: modal on ContextMenu starts as "pointer", so the page keeps scrolling, and a click outside only closes the menu without reaching what it landed on. Set modal to true to also lock scroll, hold focus in the menu and make the rest of the page inert, or to false to let that click through. If scrolling stops anyway, something else has locked it, such as a modal dialog the menu sits in.
API Reference
ContextMenu
PropType
open?boolean
Whether the menu is open. Pass it to control the menu. Two-way with bind:open.
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
Type
DocumentFragment | Element | null
controller?MenuController
Opens, closes and inspects the menu from anywhere, and connects triggers outside it. Create one with createMenuController().
debug?boolean
Logs every state change with its reason to the console, in development builds.
delay?number
Milliseconds the pointer rests on a submenu trigger before its submenu opens.
Default
100
dismissible?boolean
Whether Escape, an outside press, focus or the window leaving, Android's back gesture and a sheet's swipe can close it. Triggers, Close parts and the controller always can. Pass false while it holds unsaved work, and ask in onDismissAttempt.
Default
true
items?MenuData
The menu as data. Renders the content for you, unless you place a content part of your own, and runs the items' shortcuts.
itemShortcuts?boolean
Whether a data menu runs its items' shortcuts while closed.
Default
true
loop?boolean
Whether arrow keys wrap from the last item to the first.
Default
true
modal?"pointer" | boolean
"pointer": an outside press only closes the menu. true: also locks scroll, traps focus and makes the rest of the page inert. false: outside presses reach what they hit.
Runs for every plain item, including items in submenus, with the item's value. Return a Promise to mark the item pending until it settles: it gets data-pending, aria-busy and aria-disabled, the menu stays open, and choosing it again waits. It closes once the Promise resolves, and a rejection keeps it open with the reason in the item's error state until the next try.
Runs when dismissible refused a dismissal made on purpose: Escape, an outside press, back or a swipe. Focus or the window leaving is refused without it. Ask "Discard changes?" here.
Runs when the payload the content shows changes: a trigger or context area with another payload opens the menu, or takes the open menu over, which fires no onOpenChange, and so does a controller's open({ payload }) on the open menu. details.cancel() keeps the previous payload.
What screen readers hear once an item has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
Default
"Loading"
presentation?PresentationSetting<"popover">
How the menu presents. "sheet" slides it up from the bottom of the screen, where it can be swiped away and submenus open in its place, and "auto" does that on small touch screens. Decided each time it opens; the server renders "popover" for "auto". A context menu and a menu in a menubar are always popovers.
Stops a right-click, a long press or Shift+F10 from opening the menu here. The browser's own menu shows instead.
Default
false
payload?unknown
Data for the content to render when this area opens the menu, like the row it belongs to.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-closed?string
Present on an area while the menu isn't open from it.
data-disabled?string
Present when the area is disabled.
data-open?string
Present on the area that opened the menu, while it's open.
ContextMenuContent
<div>data-slot="menu-content"
PropType
align?Align
How the popup lines up with the anchor along that side. start and end follow the reading direction. How the menu lines up with the trigger along that side.
Default
"start"
alignOffset?number
Shift along the anchor, in pixels. Shift along the trigger, in pixels. Submenus default to lining up their first item with the trigger.
Default
4
anchor?string | Anchor | null
Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.
Type
string | Anchor | null
arrowPadding?number
Space to keep between the arrow and the popup's corners, in pixels.
Default
6
collisionPadding?number
Space to keep from the viewport edges, in pixels.
Default
8
container?DocumentFragment | Element | null
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
Type
DocumentFragment | Element | null
finalFocus?PopupFocusTarget
Where focus goes when the menu closes while focus is inside it. "trigger" returns it to the trigger and "none" leaves it. A CSS selector, an element or a function picks a specific element.
Default
"trigger"
keepMounted?boolean
Whether the menu stays in the page, hidden, while closed. Its content keeps its state between opens.
Default
false
side?ContextMenuSide
The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.
Default
"inline-end"
sideOffset?number
Gap between the anchor and the popup, in pixels. Gap between the trigger and the menu, in pixels. UI SDK's default: 4.
Default
0
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-align"start" | "center" | "end"
How the menu lines up with its trigger.
Type
"start" | "center" | "end"
data-closed?string
Present while the menu is closed, including while it animates out.
data-ending-style?string
Present while the menu animates out. Style the state you animate to.
data-has-icons?string
Present while any item inside holds an icon part, so items without one can line up.
data-nested?string
Present on submenus.
data-open?string
Present while the menu is open. A submenu trigger has it while its submenu is open.
data-presentation"popover" | "sheet"
How the menu presents this time it's open. Submenus present like their root.
Type
"popover" | "sheet"
data-side"top" | "right" | "bottom" | "left"
Where the menu ended up, after flipping away from edges.
Type
"top" | "right" | "bottom" | "left"
data-starting-style?string
Present for the first frame. Style the state you animate from.
CSS variableType
--anchor-heightstring
Height of the anchor.
--anchor-widthstring
Width of the anchor, usually the trigger. Use it to match widths.
--arrow-offsetstring
Where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.
--available-heightstring
Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.
--available-widthstring
Space beside the popup, up to the viewport edge. Not inherited either.
--drawer-dimstring
While it presents as a sheet: how dim the page behind is, from 0 to 1. The backdrop's opacity follows it.
--drawer-expandstring
While it presents as a sheet: 0 at the second-largest snap point and below, 1 at the largest. With one snap point, the whole travel.
--drawer-progressstring
While it presents as a sheet: how far open the sheet is, from 0 closed to 1 at its largest snap point. Never past 1.
--drawer-stackstring
While it presents as a sheet: how many sheets opened from inside this one are open above it, rising smoothly as each one opens. Push it back with it.
--sheet-backdropstring
Set it yourself: the color the page dims to while it presents as a sheet. Black at 0.2 by default.
--transform-originstring
The point on the anchor the popup opens from. Use it to scale from the right place.
ContextMenuGroup
<div>
PropType
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
ContextMenuGroupLabel
<div>
PropType
inset?boolean
Indents it to line up with items that have an icon or indicator.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ContextMenuLabel
ContextMenuItem
<div>data-slot="menu-item"
PropType
closeOnSelect?boolean
Whether running the item closes the menu.
Default
true
destructive?boolean
Styles the item as dangerous, like Delete. Sets data-destructive.
disabled?boolean
Keeps the item focusable but stops it from running, as the menu pattern asks. Sets data-disabled and aria-disabled.
inset?boolean
Indents it to line up with items that have an icon or indicator.
label?string
The text typeahead matches, when the content isn't plain text. It isn't the accessible name: use aria-label for that.
onSelect?(event: Event) → unknown
Runs when the item is selected, by pointer or keyboard, with the event that selected it, or a new select Event when no DOM event did. Call event.preventDefault() to keep the menu open and skip onAction. Return a Promise to mark the item pending until it settles, like onAction.
Parameters
event – Event
Returns
unknown
shortcut?string
A keyboard shortcut like "mod+n". Shows ⌘N on Apple platforms and Ctrl+N elsewhere, and sets aria-keyshortcuts.
value?string
Passed to onAction when the item runs.
variant?"default" | "destructive"
The visual style.
Type
"default" | "destructive"
Default
"default"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when the item can't be chosen.
data-has-icon?string
Present on an item while it holds an icon part.
data-highlighted?string
Present on the item with keyboard focus or under the pointer.
ContextMenuSub
ContextMenuSubTrigger
<div>
PropType
disabled?boolean
Keeps the submenu trigger focusable but stops it from opening its submenu, as the menu pattern asks. Sets data-disabled and aria-disabled.
inset?boolean
Indents it to line up with items that have an icon or indicator.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
ContextMenuSubContent
<div>data-slot="menu-content"
PropType
align?Align
How the popup lines up with the anchor along that side. start and end follow the reading direction. How the menu lines up with the trigger along that side.
Default
"start"
alignOffset?number
Shift along the anchor, in pixels. Shift along the trigger, in pixels. Submenus default to lining up their first item with the trigger.
Default
4
anchor?string | Anchor | null
Positions the popup against this instead of its trigger: an element, a CSS selector, a point like { x, y }, or anything with getBoundingClientRect(). It's read on every check, so it can move. A controller's open({ anchor }) wins for that open.
Type
string | Anchor | null
arrowPadding?number
Space to keep between the arrow and the popup's corners, in pixels.
Default
6
collisionPadding?number
Space to keep from the viewport edges, in pixels.
Default
8
container?DocumentFragment | Element | null
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
Type
DocumentFragment | Element | null
finalFocus?PopupFocusTarget
Where focus goes when the menu closes while focus is inside it. "trigger" returns it to the trigger and "none" leaves it. A CSS selector, an element or a function picks a specific element.
Default
"trigger"
keepMounted?boolean
Whether the menu stays in the page, hidden, while closed. Its content keeps its state between opens.
Default
false
side?ContextMenuSide
The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.
Default
"inline-end"
sideOffset?number
Gap between the anchor and the popup, in pixels. Gap between the trigger and the menu, in pixels. UI SDK's default: 4.
Default
0
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-align"start" | "center" | "end"
How the menu lines up with its trigger.
Type
"start" | "center" | "end"
data-closed?string
Present while the menu is closed, including while it animates out.
data-ending-style?string
Present while the menu animates out. Style the state you animate to.
data-has-icons?string
Present while any item inside holds an icon part, so items without one can line up.
data-nested?string
Present on submenus.
data-open?string
Present while the menu is open. A submenu trigger has it while its submenu is open.
data-presentation"popover" | "sheet"
How the menu presents this time it's open. Submenus present like their root.
Type
"popover" | "sheet"
data-side"top" | "right" | "bottom" | "left"
Where the menu ended up, after flipping away from edges.
Type
"top" | "right" | "bottom" | "left"
data-starting-style?string
Present for the first frame. Style the state you animate from.
CSS variableType
--anchor-heightstring
Height of the anchor.
--anchor-widthstring
Width of the anchor, usually the trigger. Use it to match widths.
--arrow-offsetstring
Where the anchor's center falls along the popup's edge, kept arrowPadding from the corners. Position a ::before arrow with it.
--available-heightstring
Space between the anchor and the viewport edge. Use it for max-height. Not inherited, so a child that needs it copies it on the popup, like [--list-height:var(--available-height)], and reads max-h-(--list-height). The first popup registers both names for the whole page with CSS.registerProperty, so they don't inherit anywhere. A page that needs them to inherit calls it first, with inherits: true. An @property rule can't, since a registration in script wins.
--available-widthstring
Space beside the popup, up to the viewport edge. Not inherited either.
--drawer-dimstring
While it presents as a sheet: how dim the page behind is, from 0 to 1. The backdrop's opacity follows it.
--drawer-expandstring
While it presents as a sheet: 0 at the second-largest snap point and below, 1 at the largest. With one snap point, the whole travel.
--drawer-progressstring
While it presents as a sheet: how far open the sheet is, from 0 closed to 1 at its largest snap point. Never past 1.
--drawer-stackstring
While it presents as a sheet: how many sheets opened from inside this one are open above it, rising smoothly as each one opens. Push it back with it.
--sheet-backdropstring
Set it yourself: the color the page dims to while it presents as a sheet. Black at 0.2 by default.
--transform-originstring
The point on the anchor the popup opens from. Use it to scale from the right place.
ContextMenuCheckboxItem
<div>data-slot="menu-checkbox-item"
PropType
checked?boolean
Whether the item is checked. Pass it to control the item. Two-way with bind:checked.
defaultChecked?boolean
Whether the item starts checked, when uncontrolled.
Identifies the group in the menu's onValueChange. Defaults to a generated id, and the group then keeps its choice by its place among the menu's groups. Give a group that's shown only some of the time a name, so it keeps its own.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
ContextMenuRadioItem
<div>data-slot="menu-radio-item"
PropType
valuestring
Selected in the group when the item is chosen.
closeOnSelect?boolean
Whether choosing the item closes the menu.
Default
false
disabled?boolean
Keeps the item focusable but stops it from being chosen, as the menu pattern asks.
inset?boolean
Indents it to line up with items that have an icon or indicator.
label?string
The text typeahead matches, when the content isn't plain text. It isn't the accessible name: use aria-label for that.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-checked?string
Present on checked checkbox items, and on the radio item that's its group's value.
data-highlighted?string
Present on the item with keyboard focus or under the pointer.
data-unchecked?string
Present on items that aren't checked.
ContextMenuSeparator
<div>data-slot="menu-separator"
PropType
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.