Shows a menu of actions or settings when a trigger is pressed. Compose it from DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, and DropdownMenuItem, toggle settings with DropdownMenuCheckboxItem and DropdownMenuRadioItem, and nest menus with DropdownMenuSub.
<script lang="ts">
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuGroup, DropdownMenuLabel, DropdownMenuItem, DropdownMenuCheckboxItem, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuShortcut, DropdownMenuSub, DropdownMenuSubTrigger, DropdownMenuSubContent } from "#lib/components/ui/dropdown-menu/index.js";
let showStatusBar = $state(true);
let showActivityBar = $state(false);
let panelPosition = $state("bottom");
</script>
<DropdownMenu>
<DropdownMenuTrigger>Open</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuGroup>
<DropdownMenuLabel>My Account</DropdownMenuLabel>
<DropdownMenuItem>Profile <DropdownMenuShortcut>⇧⌘P</DropdownMenuShortcut></DropdownMenuItem>
<DropdownMenuItem>Billing <DropdownMenuShortcut>⌘B</DropdownMenuShortcut></DropdownMenuItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuLabel inset>Appearance</DropdownMenuLabel>
<DropdownMenuCheckboxItem bind:checked={showStatusBar}>Status Bar</DropdownMenuCheckboxItem>
<DropdownMenuCheckboxItem bind:checked={showActivityBar}>Activity Bar</DropdownMenuCheckboxItem>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuGroup>
<DropdownMenuLabel inset>Panel Position</DropdownMenuLabel>
<DropdownMenuRadioGroup bind:value={panelPosition}>
<DropdownMenuRadioItem value="top">Top</DropdownMenuRadioItem>
<DropdownMenuRadioItem value="bottom">Bottom</DropdownMenuRadioItem>
</DropdownMenuRadioGroup>
</DropdownMenuGroup>
<DropdownMenuSeparator />
<DropdownMenuSub>
<DropdownMenuSubTrigger>Invite users</DropdownMenuSubTrigger>
<DropdownMenuSubContent>
<DropdownMenuGroup>
<DropdownMenuLabel>Invite via</DropdownMenuLabel>
<DropdownMenuItem>Email</DropdownMenuItem>
<DropdownMenuItem>Message</DropdownMenuItem>
</DropdownMenuGroup>
</DropdownMenuSubContent>
</DropdownMenuSub>
</DropdownMenuContent>
</DropdownMenu>pnx remote0 add https://plainsv.com/r/dropdown-menu.jsonDropdownMenu holds the open state and the style settings, and renders no element of its own. DropdownMenuTrigger renders an outline Button that toggles the menu. DropdownMenuContent renders the role="menu" panel beside the trigger.
Inside the content, DropdownMenuItem runs an action, DropdownMenuCheckboxItem toggles a setting with a check mark, and DropdownMenuRadioItem picks one value from its DropdownMenuRadioGroup with a dot. DropdownMenuGroup wraps related items and is named by its DropdownMenuLabel, DropdownMenuSeparator divides groups, and DropdownMenuShortcut shows a muted keyboard hint at the end of an item.
DropdownMenuSub nests a menu. Its DropdownMenuSubTrigger is an item with a chevron, and its DropdownMenuSubContent opens beside it on hover, click, or the right arrow. Hovering another item closes it.
Menus open in the top layer with the native popover API, so no ancestor's overflow or z-index can clip them. They flip to the other side of the trigger when there is not enough room, and shift to stay inside the viewport.
Enter, Space, and the down arrow open the menu from the trigger and focus the first item, and the up arrow focuses the last. In a menu, the up and down arrows move between items and wrap at the ends, and Home and End jump to the first and last. Type the start of an item's text to jump to it. Enter or Space selects the focused item. The right arrow opens a submenu, and the left arrow closes it. Escape closes the current menu, and Tab or clicking outside closes them all. Disabled items are skipped.
To open the menu from any other element, pass a trigger snippet to DropdownMenuTrigger. It receives props with the ARIA attributes, the handlers, and an attachment that registers the element the menu is placed against. Spread them onto the element, such as an icon button.
<script lang="ts">
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from "#lib/components/ui/dropdown-menu/index.js";
import { Button } from "#lib/components/ui/button/index.js";
import { EllipsisIcon } from "@lucide/svelte";
</script>
<DropdownMenu align="end">
<DropdownMenuTrigger>
{#snippet trigger({ props })}
<Button variant="ghost" size="icon-md" aria-label="More actions" {...props}>
<EllipsisIcon />
</Button>
{/snippet}
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>Rename</DropdownMenuItem>
<DropdownMenuItem>Duplicate</DropdownMenuItem>
<DropdownMenuItem>Archive</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>Pass onSelect to run code when an item is selected with a click, Enter, or Space, or set href to render the item as a link that a click, Enter, or Space follows. Icons placed before the text are sized to match and muted. Set variant="destructive" for a dangerous action, and disabled to block an item. Set inset on an item or label to line its text up with checkbox and radio items. Place a DropdownMenuShortcut after the text to show a keyboard hint at the end of the item. It only displays the hint, so bind the keys yourself.
Items close the menu when selected, while checkbox and radio items keep it open so several settings can be changed at once. Set closeOnSelect on any item to change this.
Last action: none
<DropdownMenu>
<DropdownMenuTrigger>Actions</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem onSelect={() => edit()}><PencilIcon /> Edit <DropdownMenuShortcut>⌘E</DropdownMenuShortcut></DropdownMenuItem>
<DropdownMenuItem onSelect={() => copy()}><CopyIcon /> Copy <DropdownMenuShortcut>⌘C</DropdownMenuShortcut></DropdownMenuItem>
<DropdownMenuItem href="/docs"><BookOpenIcon /> Docs</DropdownMenuItem>
<DropdownMenuItem disabled><SettingsIcon /> Settings</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem variant="destructive" onSelect={() => remove()}><Trash2Icon /> Delete <DropdownMenuShortcut>⌘⌫</DropdownMenuShortcut></DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>Use bind:open or onOpenChange on DropdownMenu to read and set the open state from your own code. Use bind:checked or onCheckedChange on a checkbox item, and bind:value or onValueChange on a radio group, for their state.
Open: false
<script lang="ts">
import { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuItem } from "#lib/components/ui/dropdown-menu/index.js";
import { Button } from "#lib/components/ui/button/index.js";
let isOpen = $state(false);
</script>
<Button variant="outline" onclick={() => (isOpen = true)}>Open from outside</Button>
<DropdownMenu bind:open={isOpen}>
<DropdownMenuTrigger>Account</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>Profile</DropdownMenuItem>
<DropdownMenuItem>Billing</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>Set side on DropdownMenu to top, right, bottom, or left to choose the preferred side of the trigger. It defaults to bottom. Set align to start, center, or end to line the menu up along the trigger's edge, and sideOffset to change the gap in pixels.
<DropdownMenu side="right" align="start">
<DropdownMenuTrigger>Open</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>Profile</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>Set size on DropdownMenu to xs, sm, md, or lg to change the default trigger, the item padding, the text, and the icons. It defaults to lg, the item padding of the shadcn dropdown menu, and submenus use the same size.
<DropdownMenu size="sm">
<DropdownMenuTrigger>Open</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>Profile</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>Set rounded on DropdownMenu to xs through 4xl or pill to change the radius. It defaults to md, an 8 pixel menu with 6 pixel items like shadcn, instead of following size. Items use one step smaller so the corners nest. pill makes the menu a true pill, which clips its items, so use 3xl or 4xl for a round menu with items that line up.
<DropdownMenu rounded="lg">
<DropdownMenuTrigger>Open</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>Profile</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>The default DropdownMenuTrigger button takes variant, size, rounded, and class. Its size follows the menu's size when omitted. DropdownMenuContent and DropdownMenuSubContent take size and rounded for that menu only. DropdownMenuItem, DropdownMenuCheckboxItem, DropdownMenuRadioItem, and DropdownMenuSubTrigger take rounded to set that item's exact radius.
<DropdownMenu>
<DropdownMenuTrigger variant="ghost" size="sm">Open</DropdownMenuTrigger>
<DropdownMenuContent size="md" rounded="lg">
<DropdownMenuItem rounded="pill">Profile</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>DropdownMenuContent, DropdownMenuSubContent, every item, group, label, and separator extend the div attributes, DropdownMenuShortcut extends the span attributes, and extra attributes pass through.
interface Props {
/** Whether the menu is shown; supports `bind:open` */
open?: boolean;
/** Called when the menu opens or closes */
onOpenChange?: (open: boolean) => void;
/** Preferred side; flips to the opposite side when there is not enough room; defaults to `bottom` */
side?: "top" | "right" | "bottom" | "left";
/** Alignment along the trigger's edge; defaults to `start` */
align?: "start" | "center" | "end";
/** Gap in pixels between the trigger and the menu; defaults to `4` */
sideOffset?: number;
/** Item size on the shared scale; defaults to `lg`, the shadcn item spacing */
size?: UiSize;
/** Corner radius of the menus, with items one step smaller; defaults to `md`, like shadcn. `pill` makes the panels pills too, which can clip their items, so use `3xl` or `4xl` for round panels */
rounded?: UiRounded;
/** The trigger and content parts */
children: Snippet;
}
interface TriggerProps {
/** Label for the default trigger button */
children?: Snippet;
/** Custom trigger; spread `props` onto the element that opens the menu */
trigger?: Snippet<[{ props: TriggerAttrs }]>;
/** Style of the default trigger button; defaults to `outline` */
variant?: ButtonProps["variant"];
/** Size of the default trigger button, including the square `icon-*` sizes; follows the menu's `size` when omitted */
size?: ButtonProps["size"];
/** Corner radius of the default trigger button; follows its size when omitted */
rounded?: UiRounded;
}
interface ItemProps extends Omit<CompPropsWithChild<"div">, "role"> {
/** Blocks selecting the item; the arrow keys skip it */
disabled?: boolean;
/** Visual style of the item; `destructive` marks a dangerous action */
variant?: "default" | "destructive";
/** Indents the item to line up with checkbox and radio item text */
inset?: boolean;
/** Whether selecting the item closes the menu; defaults to `true` */
closeOnSelect?: boolean;
/** Called when the item is selected with a click, Enter, or Space */
onSelect?: () => void;
/** Renders the item as a link to this URL; a click, Enter, or Space follows it */
href?: string;
/** Corner radius of this item with this exact value; overrides the menu's item radius */
rounded?: UiRounded;
}
interface CheckboxItemProps extends Omit<CompPropsWithChild<"div">, "role"> {
/** Whether the item is checked; supports `bind:checked` */
checked?: boolean;
/** Called with the new state when the item is toggled */
onCheckedChange?: (checked: boolean) => void;
/** Blocks toggling the item; the arrow keys skip it */
disabled?: boolean;
/** Whether toggling the item closes the menu; defaults to `false` */
closeOnSelect?: boolean;
/** Corner radius of this item with this exact value; overrides the menu's item radius */
rounded?: UiRounded;
}
interface RadioGroupProps extends CompPropsWithChild<"div"> {
/** Value of the checked radio item; supports `bind:value` */
value?: string;
/** Called with the new value when a radio item is checked */
onValueChange?: (value: string) => void;
}
interface RadioItemProps extends Omit<CompPropsWithChild<"div">, "role"> {
/** Value set on the radio group when this item is checked */
value: string;
/** Blocks checking the item; the arrow keys skip it */
disabled?: boolean;
/** Whether checking the item closes the menu; defaults to `false` */
closeOnSelect?: boolean;
/** Corner radius of this item with this exact value; overrides the menu's item radius */
rounded?: UiRounded;
}
interface LabelProps extends CompPropsWithChild<"div"> {
/** Indents the label to line up with checkbox and radio item text */
inset?: boolean;
}
interface SubProps {
/** Whether the submenu is shown; supports `bind:open` */
open?: boolean;
/** The sub trigger and sub content parts */
children: Snippet;
}
interface SubTriggerProps extends Omit<CompPropsWithChild<"div">, "role"> {
/** Blocks opening the submenu; the arrow keys skip it */
disabled?: boolean;
/** Indents the item to line up with checkbox and radio item text */
inset?: boolean;
/** Corner radius of this item with this exact value; overrides the menu's item radius */
rounded?: UiRounded;
}
interface ContentProps extends CompPropsWithChild<"div"> {
/** Item size of this menu; overrides the dropdown menu's `size` */
size?: UiSize;
/** Corner radius of this menu, with items one step smaller; overrides the dropdown menu's `rounded`. `pill` makes it a pill too, which can clip its items, so use `3xl` or `4xl` to stay round */
rounded?: UiRounded;
}
interface SubContentProps extends CompPropsWithChild<"div"> {
/** Item size of this submenu; overrides the dropdown menu's `size` */
size?: UiSize;
/** Corner radius of this submenu, with items one step smaller; overrides the dropdown menu's `rounded`. `pill` makes it a pill too, which can clip its items, so use `3xl` or `4xl` to stay round */
rounded?: UiRounded;
}