shadcn.io is an independent project built on shadcn/ui
Components
Free
Dropdown Menu for Svelte UI SDK
A Svelte dropdown menu on UI SDK: a button opens a list of actions, with submenus, checkbox and radio rows, and shortcut hints.
On this page9 sections
A dropdown menu is a list of actions that opens from a button: the ellipsis at the end of a table row, the avatar in an app header, a More button in a toolbar. Choosing an item runs it and closes the list. Rows can also open a submenu, or carry a checkbox or radio mark for a view setting.
For a value that a form submits, use a Select. A menu that opens with a right-click on an area of the page is a Context Menu.
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 trigger is a button with aria-haspopup="menu" and aria-expanded, and the list is a role="menu" labelled by the trigger. Focus moves onto each item in turn, disabled ones included, typing a letter jumps to the next item that starts with it, and checkbox and radio rows report aria-checked. Choosing an item or pressing Escape returns focus to the trigger, and Tab closes the menu. Give an icon-only trigger an aria-label, as the ellipsis in the Actions demo has.
KeyAction
Space
When focus is on the trigger, opens the menu on its first item. 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
When focus is on the trigger, opens the menu on its first item. 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
When focus is on the trigger, opens the menu on its first item. In the menu, moves focus to the next item, and from the last one back to the first.
Arrow Up
When focus is on the trigger, opens the menu on its last item. In the menu, 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 layout, closes the submenu instead.
Arrow Left
In a submenu, closes it and moves focus back to its trigger. In a right-to-left layout, opens a submenu instead.
Home
Moves focus to the first item, as PageUp does.
End
Moves focus to the last item, as PageDown does.
EscEscape
Closes the menu, or only the submenu that has focus, and moves focus back to the trigger that opened it.
Tab
Closes the menu.
Demos
Basic
Profile, Billing and Settings under a My Account label, then a separator, GitHub, Support and a disabled API row. Start here for a short account menu. The label names the group but isn't an item, so it can't be chosen and the arrow keys pass over it, while the disabled row stays in view for something people can't use yet.
Submenu
Invite users opens a second panel with Email and Message, and More options inside it opens a third with Calendly, Slack and Webhook. Reach for a submenu when a few related actions would crowd the first list. Keep to two levels where you can, since every extra panel is one more hover path to hold and one more key press back out.
Shortcuts
Profile, Billing, Settings and Log out each end in a key hint, such as ⇧⌘P. DropdownMenuShortcut is a styled span: it prints the hint and listens for nothing. Bind the keys yourself at the page level, where they work while the menu is closed, and only show a hint for a shortcut that exists.
Icons
A person, a card and a gear in front of Profile, Billing and Settings, and a destructive Log out below a separator. Each icon has aria-hidden, because the label already names the action and a screen reader shouldn't read it twice. Add icons to a menu people open often, where a familiar picture is quicker to find than a word.
Checkboxes
An Appearance group where Status Bar starts checked, Activity Bar is disabled and Panel starts off. Each row is its own on or off view setting, held in state outside the menu so the marks are still there the next time it opens. When only one row may be on at a time, use a radio group instead.
Checkboxes icons
A Notifications button opens email, SMS and push channels, each with its own icon, and email and push start on. Here the icons earn their place: three channels with similar labels are easier to tell apart at a glance in a narrow menu. The check mark sits at the end of the row, so it never competes with the icon at the start.
Radio group
Panel Position offers Top, Bottom and Right, with Bottom chosen. Exactly one row is marked, and choosing another moves the mark, so the menu reads as a setting rather than a list of commands. Hold the chosen value in state and start from a real default, since an empty radio group looks unfinished.
Radio icons
A Payment Method button opens Credit Card, PayPal and Bank Transfer, each with an icon, and Credit Card chosen. Use it when the options are a familiar set of brands or kinds that people recognize by their picture. The words still name each option, and the icons carry aria-hidden.
Destructive
Edit and Share, 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: it doesn't ask for confirmation. When the action can't be undone, open a confirmation from the item, as the Actions demo does, and keep the dangerous row last.
Avatar
A round avatar is the trigger, opening Account, Billing and Notifications, then Sign Out below a separator. Use it for the account menu in an app header, where people already look for their own picture. The button's only text is the image's alt, so make that the person's name, or give the button an aria-label such as Account menu.
Complex
File, View and Account sections in one menu, with shortcut hints, submenus two levels deep, view checkboxes, a theme radio group and a destructive Sign Out. Read it as a reference for how the parts combine rather than a menu to ship. A list this long hides what people came for, and an app's main commands belong in a Menubar.
Actions
An ellipsis button on a Project Alpha row, labelled Actions for Project Alpha, opens Edit, Duplicate and Delete. One select handler on DropdownMenu reads the chosen item's value. Edit and Duplicate write the result into a live region under the row, and Delete opens an alert dialog first. Copy this for row actions in a table or list.
RTL
Arabic labels from an account submenu through view checkboxes, a position radio group and a destructive log out. dir is rtl on DropdownMenu, which passes the direction to 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
How do I open or close a Dropdown Menu from my own state in Svelte?
Pass open to DropdownMenu and update your state in onOpenChange, which receives the new boolean first and an object describing the cause second. UI SDK calls it for every change it makes itself, when an item is chosen, on Escape and on a click outside.
With the state in your hands, a keyboard shortcut elsewhere on the page can open the menu, and code that finishes later can close it.
How do I run code when a Dropdown Menu item is chosen?
Set onSelect on DropdownMenu: it receives the chosen item's value first, so one handler can branch on it, as the Actions demo does. The menu closes after a choice. Set closeOnSelect to false on DropdownMenu, or on one item, to keep it open.
How do I add checkbox or radio items to a Dropdown Menu?
Use DropdownMenuCheckboxItem, with checked and onCheckedChange, for settings that can be on together, and DropdownMenuRadioItem rows in a DropdownMenuRadioGroup, with value and onValueChange, for one choice out of several, and give every radio item a value of its own. Both kinds leave the menu open when chosen, so people can change several in a row. Set closeOnSelect on them to close it after each.
The Checkboxes and Radio group demos show both, with the state held outside the menu.
To split one choice into sections, put a DropdownMenuGroup with its own DropdownMenuLabel for each inside the DropdownMenuRadioGroup: each label names its own section, and the value stays one for the whole group.
How do I change where a Dropdown Menu opens and how wide it is?
Pass positioning to DropdownMenu with a placement such as bottom-start, or set side and align on DropdownMenuContent, which win over it. This file opens the menu 4px below the trigger, lined up with its end edge, and UI SDK moves it to the other side when there's no room. The content takes the trigger's width, though never less than its minimum, so add a width class such as w-56 when the items need more room.
How do I open a Dialog from a Dropdown Menu item?
Render the dialog beside the menu, not inside DropdownMenuContent, and open it from state that the item sets. Choosing the item closes the menu and unmounts what's inside it, so a dialog placed there would disappear with it.
In the Actions demo, the select handler on DropdownMenu sets confirming when the chosen value is delete, and the AlertDialog under the row takes it as open.
How do I use a router link as a Dropdown Menu item?
Give DropdownMenuItem a render snippet with an <a> and its href, spreading the props it receives onto it, the way the demos render the trigger's button. The anchor takes the item's menuitem role and highlight, and Enter on the highlighted link clicks it, so SvelteKit routes it like a pointer click, and the menu closes.
Why can't I scroll the page while a Dropdown Menu is open?
Nothing locks it on UI SDK: the menu's modal setting defaults to "pointer", so the page keeps scrolling while the menu is open, and a click outside only closes the menu, without reaching what's under it. Set modal to true on DropdownMenu to lock scroll and make the rest of the page inert as well, or to false to let that click through.
Should I use a Dropdown Menu, a Select or a Context Menu?
Use a Dropdown Menu when a visible button opens a list of actions, such as Edit, Duplicate and Delete on a row, or Profile and Sign out under an avatar. Each item does something when chosen, and only checkbox and radio rows keep a state.
Use a Select when the choice is a value a form keeps and the closed control should show it. A Context Menu holds the same kind of actions but opens with a right-click on an area, so keep a visible button for anything people need to find.
API Reference
DropdownMenu
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 the trigger from opening the menu. A native button can't be focused or pressed, unless focusableWhenDisabled is set.
Default
false
focusableWhenDisabled?boolean
Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.
Default
false
payload?unknown
Data for the content to render when this trigger opens the menu, like the row it belongs to.
shortcut?string
A keyboard shortcut that opens the menu from anywhere, like "mod+j". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts.
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 }]>
AttributeType
data-closed?string
Present on a trigger while the menu isn't open from it.
data-open?string
Present on the trigger that opened the menu, while it's open.
DropdownMenuContent
<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
0
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?DropdownMenuSide
The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.
Default
"bottom"
sideOffset?number
Gap between the anchor and the popup, in pixels. Gap between the trigger and the menu, in pixels.
Default
4
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
The anchor's width.
--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
The height left between the anchor and the viewport's edge.
--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
Where the content scales from, on its trigger's side.
DropdownMenuGroup
<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 }]>
DropdownMenuGroupLabel
<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.
DropdownMenuItem
<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.
DropdownMenuSub
PropType
open?boolean
Whether the submenu is open. Pass it to control the submenu. Two-way with bind:open.
defaultOpen?boolean
Whether the submenu starts open, when uncontrolled.
Opens, closes and inspects the submenu. Create one with createMenuController(). Triggers outside the menu can't connect to it.
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
loop?boolean
Whether arrow keys wrap from the last item to the first.
Default
true
onOpenChangeComplete?(open: boolean) → void
Runs once the menu has finished opening or closing, after any animation.
Parameters
open – boolean
Returns
void
DropdownMenuSubTrigger
<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.
DropdownMenuSubContent
<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
-3
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?DropdownMenuSide
The side of the trigger it opens on. It flips when there's no room, unless avoidCollisions is off.
Default
"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
The height left between the anchor and the viewport's edge.
--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
Where the content scales from, on its trigger's side.
DropdownMenuCheckboxItem
<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 }]>
DropdownMenuRadioItem
<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.
DropdownMenuSeparator
<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.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
DropdownMenuShortcut
<span>data-slot="dropdown-menu-shortcut"
PropType
class?string
Merged with the part's classes.
DropdownMenuLabel
<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.