DropdownMenu
ActionsA menu of actions or options opened from a button: <DropdownMenu.Root> holds a DropdownMenu.Trigger and a DropdownMenu.Content of DropdownMenu.Items (onSelect, variant="error", DropdownMenu.Shortcut for a key hint). It also takes CheckboxItems, a RadioGroup of RadioItems, Groups with a GroupHeading, Labels, Separators and submenus (Sub, SubTrigger, SubContent). Arrow keys move, Enter or Space chooses, typing jumps to an item, Escape closes and returns focus.
import { DropdownMenu } from '@limerdev/zest'; Select. Navigation between pages: use links or NavigationMenu. The only way to reach an important action: show it as a Button.Examples
Actions
A row's actions. variant="error" marks the dangerous one; Shortcut shows a key hint.
Chose: nothing yet
<script lang="ts">
import Copy from '@lucide/svelte/icons/copy';
import Ellipsis from '@lucide/svelte/icons/ellipsis';
import Pencil from '@lucide/svelte/icons/pencil';
import Trash from '@lucide/svelte/icons/trash-2';
import { DropdownMenu } from '@limerdev/zest';
let last = $state('nothing yet');
</script>
<DropdownMenu.Root>
<DropdownMenu.Trigger size="icon" variant="outline" aria-label="Actions for order 1042">
<Ellipsis />
</DropdownMenu.Trigger>
<DropdownMenu.Content align="end" class="w-48">
<DropdownMenu.Item onSelect={() => (last = 'Edit')}>
<Pencil /> Edit
<DropdownMenu.Shortcut>⌘E</DropdownMenu.Shortcut>
</DropdownMenu.Item>
<DropdownMenu.Item onSelect={() => (last = 'Duplicate')}>
<Copy /> Duplicate
</DropdownMenu.Item>
<DropdownMenu.Separator />
<DropdownMenu.Item variant="error" onSelect={() => (last = 'Delete')}>
<Trash /> Delete
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Root>
<p class="text-sm text-muted">Chose: {last}</p> View options
Checkbox items toggle settings, a radio group picks one; both keep the menu open.
Without archived, billable only, by name
<script lang="ts">
import { DropdownMenu } from '@limerdev/zest';
let archived = $state(false);
let billableOnly = $state(true);
let sort = $state('name');
</script>
<DropdownMenu.Root>
<DropdownMenu.Trigger variant="outline">View</DropdownMenu.Trigger>
<DropdownMenu.Content class="w-52">
<DropdownMenu.Label>Show</DropdownMenu.Label>
<DropdownMenu.CheckboxItem bind:checked={archived}>Archived projects</DropdownMenu.CheckboxItem>
<DropdownMenu.CheckboxItem bind:checked={billableOnly}>Billable only</DropdownMenu.CheckboxItem>
<DropdownMenu.Separator />
<DropdownMenu.Label>Sort by</DropdownMenu.Label>
<DropdownMenu.RadioGroup bind:value={sort}>
<DropdownMenu.RadioItem value="name">Name</DropdownMenu.RadioItem>
<DropdownMenu.RadioItem value="updated">Last updated</DropdownMenu.RadioItem>
<DropdownMenu.RadioItem value="hours">Hours logged</DropdownMenu.RadioItem>
</DropdownMenu.RadioGroup>
</DropdownMenu.Content>
</DropdownMenu.Root>
<p class="text-sm text-muted">
{archived ? 'With' : 'Without'} archived, {billableOnly ? 'billable only' : 'all'}, by {sort}
</p> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
DropdownMenu.Root
| Prop | Type | Default | Description |
|---|---|---|---|
| dir | "ltr" | "rtl" | "ltr" | The direction of the site. |
| onOpenChange | OnChangeFn<boolean> | – | A callback that is called when the menu is opened or closed. |
| onOpenChangeComplete | OnChangeFn<boolean> | – | A callback that is called when the menu is opened or closed. |
| open bindable | boolean | false | The open state of the menu. |
DropdownMenu.Trigger
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | null | false | true | false | Whether the trigger is disabled. |
| size | "default" | "sm" | "lg" | "icon" | "icon-sm" | "icon-lg" | 'default' | The height and padding, from the theme's field size; the icon sizes are square. |
| variant | "link" | "primary" | "secondary" | "neutral" | "error" | "outline" | "ghost" | 'primary' | The look, by the theme's colour roles: primary for the main action, error for deleting, secondary, neutral and outline for the rest, ghost in toolbars, link for a navigation that looks like text. |
DropdownMenu.Content
| Prop | Type | Default | Description |
|---|---|---|---|
| align | "start" | "center" | "end" | – | The preferred alignment of the anchor to render against when open. This may change when collisions occur. |
| alignOffset | number | – | An offset in pixels from the "start" or "end" alignment options. |
| arrowPadding | number | – | This describes the padding between the arrow and the edges of the floating element. If your floating element has border-radius, this will prevent it from overflowing the corners. |
| avoidCollisions | boolean | true | When true, overrides the side and align options to prevent collisions with the boundary edges. |
| collisionBoundary | Arrayable<Boundary> | – | A boundary element or array of elements to check for collisions against. |
| collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> | 8 | The amount in pixels of virtual padding around the viewport edges to check for overflow which will cause a collision. |
| customAnchor | string | HTMLElement | Measurable | null | – | Use an element other than the trigger to anchor the content to. If provided, the content will be anchored to the provided element instead of the trigger. You can pass a selector string or an HTMLElement. |
| dir | "ltr" | "rtl" | – | The text direction of the content. |
| escapeKeydownBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Escape behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to its parent component that has an escape keydown handler. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an escape key press. |
| forceMount | boolean | – | Whether to force mount the component. |
| hideWhenDetached | boolean | – | |
| interactOutsideBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Interact outside behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to the parent element. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an outside interaction. |
| loop | boolean | false | When true, the menu will loop through items when navigating with the keyboard. |
| onCloseAutoFocus | EventCallback | – | Event handler called when auto-focusing on close. Can be prevented. |
| onEscapeKeydown | ((e: KeyboardEvent) => void) | – | Callback fired when escape is pressed. |
| onFocusOutside | ((event: FocusEvent) => void) | – | Callback fired when focus leaves the dismissible layer. |
| onInteractOutside | InteractOutsideEventHandler | – | Callback fired when an outside interaction event completes, which is either a pointerup, mouseup, or touchend event, depending on the user's input device. |
| onOpenAutoFocus | EventCallback | – | Event handler called when auto-focusing on open. Can be prevented. |
| portalProps | PortalProps | – | Props for the portal the content renders in: to (the element to render in, the body by default) and disabled (render in place). |
| preventOverflowTextSelection | boolean | true | Passing true will prevent the overflow of text selection outside the element, provided the element is the top layer. |
| preventScroll | boolean | true | Whether to prevent scrolling the body when the content is open. Whether to prevent body scrolling when the content is open. |
| side | "top" | "right" | "bottom" | "left" | – | The preferred side of the anchor to render against when open. Will be reversed when collisions occur. |
| sideOffset | number | 4 | The distance in pixels from the anchor to the floating element. |
| sticky | "partial" | "always" | – | |
| strategy | "absolute" | "fixed" | – | The positioning strategy to use for the floating element. |
| trapFocus | boolean | false | Whether focus is trapped within the focus scope. |
| updatePositionStrategy | "always" | "optimized" | "optimized" | "optimized" will only update the position when necessary, while "always" will update the position on each animation frame, which is useful if the floating content is following something like a mouse cursor. |
DropdownMenu.Portal
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | Disable portalling and render the component inline |
| to | PortalTarget | document.body | Where to portal the content to. |
DropdownMenu.Item
| Prop | Type | Default | Description |
|---|---|---|---|
| closeOnSelect | boolean | true | Whether or not the menu item should close when selected. |
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
| variant | "error" | "default" | 'default' | error colours the item for deleting. |
DropdownMenu.CheckboxItem
| Prop | Type | Default | Description |
|---|---|---|---|
| checked bindable | boolean | false | The checked state of the checkbox. It can be one of: - true for checked - false for unchecked |
| closeOnSelect | boolean | false | Whether or not the menu item should close when selected. |
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| indeterminate bindable | boolean | false | Whether the checkbox is in an indeterminate state or not. |
| onCheckedChange | OnChangeFn<boolean> | – | A callback that is fired when the checked state changes. |
| onIndeterminateChange | OnChangeFn<boolean> | – | A callback function called when the indeterminate state changes. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
| value | string | – | The value of the checkbox item when used in a checkbox group. |
DropdownMenu.CheckboxGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| onValueChange | OnChangeFn<string[]> | – | A callback that is fired when the selected checkbox items change. |
| value bindable | string[] | [] | The values of the selected checkbox items. Supports two-way binding with bind:value. |
DropdownMenu.RadioGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| onValueChange | OnChangeFn<string> | – | A callback that is fired when the selected radio item changes. |
| value bindable | string | '' | The value of the selected radio item. Supports two-way binding with bind:value. |
DropdownMenu.RadioItem
| Prop | Type | Default | Description |
|---|---|---|---|
| closeOnSelect | boolean | false | Whether or not the menu item should close when selected. |
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
| value required | string | – | The value of the radio item. |
DropdownMenu.Group
No props of its own beyond the common ones.
DropdownMenu.GroupHeading
| Prop | Type | Default | Description |
|---|---|---|---|
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
DropdownMenu.Label
| Prop | Type | Default | Description |
|---|---|---|---|
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
DropdownMenu.Separator
No props of its own beyond the common ones.
DropdownMenu.Shortcut
No props of its own beyond the common ones.
DropdownMenu.Sub
| Prop | Type | Default | Description |
|---|---|---|---|
| onOpenChange | OnChangeFn<boolean> | – | A callback that is called when the menu is opened or closed. |
| onOpenChangeComplete | OnChangeFn<boolean> | – | A callback that is called when the menu finishes opening/closing animations. |
| open bindable | boolean | false | The open state of the menu. |
DropdownMenu.SubTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | When true, the user will not be able to interact with the menu item. |
| inset | boolean | – | Indent the text to line up with items that show an icon, check or dot. |
| onSelect | ((event: Event) => void) | – | A callback fired when the menu item is selected. Prevent default behavior of selection with event.preventDefault(). |
| openDelay | number | 100 | The amount of time in ms from when the mouse enters the subtrigger until the submenu opens. This is useful for preventing the submenu from opening as a user is moving their mouse through the menu without a true intention to open that submenu. To disable the behavior, set it to 0. |
| textValue | string | undefined | Optional text to use for typeahead filtering. By default, typeahead will use the .textContent of the menu item. When the content is more complex, you can provide a string here instead. |
DropdownMenu.SubContent
| Prop | Type | Default | Description |
|---|---|---|---|
| align | "start" | "center" | "end" | – | The preferred alignment of the anchor to render against when open. This may change when collisions occur. |
| alignOffset | number | – | An offset in pixels from the "start" or "end" alignment options. |
| arrowPadding | number | – | This describes the padding between the arrow and the edges of the floating element. If your floating element has border-radius, this will prevent it from overflowing the corners. |
| avoidCollisions | boolean | true | When true, overrides the side and align options to prevent collisions with the boundary edges. |
| collisionBoundary | Arrayable<Boundary> | – | A boundary element or array of elements to check for collisions against. |
| collisionPadding | number | Partial<Record<"top" | "right" | "bottom" | "left", number>> | 8 | The amount in pixels of virtual padding around the viewport edges to check for overflow which will cause a collision. |
| customAnchor | string | HTMLElement | Measurable | null | – | Use an element other than the trigger to anchor the content to. If provided, the content will be anchored to the provided element instead of the trigger. You can pass a selector string or an HTMLElement. |
| dir | "ltr" | "rtl" | – | The text direction of the content. |
| escapeKeydownBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Escape behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to its parent component that has an escape keydown handler. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an escape key press. |
| forceMount | boolean | – | Whether to force mount the component. |
| hideWhenDetached | boolean | – | |
| interactOutsideBehavior | "close" | "defer-otherwise-close" | "defer-otherwise-ignore" | "ignore" | close | Interact outside behavior type. close: Closes the element immediately. defer-otherwise-close: Delegates the action to the parent element. If no parent is found, it closes the element. defer-otherwise-ignore: Delegates the action to the parent element. If no parent is found, nothing is done. ignore: Prevents the element from closing and also blocks the parent element from closing in response to an outside interaction. |
| loop | boolean | false | When true, the menu will loop through items when navigating with the keyboard. |
| onCloseAutoFocus | EventCallback | – | Event handler called when auto-focusing on close. Can be prevented. |
| onEscapeKeydown | ((e: KeyboardEvent) => void) | – | Callback fired when escape is pressed. |
| onFocusOutside | ((event: FocusEvent) => void) | – | Callback fired when focus leaves the dismissible layer. |
| onInteractOutside | InteractOutsideEventHandler | – | Callback fired when an outside interaction event completes, which is either a pointerup, mouseup, or touchend event, depending on the user's input device. |
| onOpenAutoFocus | EventCallback | – | Event handler called when auto-focusing on open. Can be prevented. |
| preventOverflowTextSelection | boolean | true | Passing true will prevent the overflow of text selection outside the element, provided the element is the top layer. |
| side | "top" | "right" | "bottom" | "left" | – | The preferred side of the anchor to render against when open. Will be reversed when collisions occur. |
| sideOffset | number | – | The distance in pixels from the anchor to the floating element. |
| sticky | "partial" | "always" | – | |
| strategy | "absolute" | "fixed" | – | The positioning strategy to use for the floating element. |
| trapFocus | boolean | false | Whether focus is trapped within the focus scope. |
| updatePositionStrategy | "always" | "optimized" | "optimized" | "optimized" will only update the position when necessary, while "always" will update the position on each animation frame, which is useful if the floating content is following something like a mouse cursor. |