suiss/ui · Component
Dropdown Menu
A menu of actions or options that opens from a button.
Dropdown Menu is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Dropdown menu wraps the menu primitive of the chosen base. The DropdownMenu root manages the open state and DropdownMenuTrigger opens it. DropdownMenuContent renders the menu in a portal, positioned below the trigger. Inside it, use DropdownMenuItem for actions, DropdownMenuCheckboxItem for toggles, and DropdownMenuRadioGroup with DropdownMenuRadioItem for a single choice. Organize items with DropdownMenuGroup, DropdownMenuLabel and DropdownMenuSeparator, and nest a submenu with DropdownMenuSub, DropdownMenuSubTrigger and DropdownMenuSubContent. DropdownMenuShortcut displays a keyboard shortcut at the end of an item.
Use a dropdown menu for actions and settings behind a visible button. For actions on an object opened with a right-click, use a context menu; to choose a value for a form field, use a select.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
-
Add the pieces it builds on:
use-platform. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a DropdownMenu:
Examples
Checkboxes
Controlled checkbox items that toggle interface elements, including a disabled item. Checkbox items keep the menu open so several choices can be made in a row.
Radio group
A controlled radio group for choosing a single panel position. Each radio item needs a value.
Inset
Set inset on items and labels without an icon to align their text with items that have one. Each item here also has a keyboard shortcut.
Accessibility
- The trigger sets
aria-expanded. The menu has themenurole; items have themenuitem,menuitemcheckboxormenuitemradiorole, and checkbox and radio items setaria-checked. - Groups have the
grouprole and take their name fromDropdownMenuLabel. - Opening the menu from the keyboard focuses the first item. Arrow keys move between items, and disabled items set
aria-disabled. - On a submenu trigger, the Right Arrow opens the submenu and moves focus into it, and the Left Arrow returns to the trigger. The arrow directions follow the reading direction.
- Escape closes the menu and returns focus to the trigger.
DropdownMenuShortcutonly displays a shortcut; it does not register one. It is hidden on small screens.
API Reference
DropdownMenu
The root that manages the menu's open state.
Also accepts the props of the primitive part it wraps.
The menu is uncontrolled by default; set defaultOpen to start open, or control it with open and onOpenChange.
The menu is uncontrolled by default; set defaultOpen to start open, or control it with isOpen and onOpenChange. The Button placed first inside DropdownMenu opens it.
DropdownMenuPortal
Renders its children into a portal outside the current DOM hierarchy.
Also accepts the props of the primitive part it wraps.
DropdownMenuTrigger
The button that opens the menu.
Also accepts the props of the primitive part it wraps.
Render the trigger as another component, such as a Button, with the render prop.
Set asChild to render your own Button as the trigger.
DropdownMenuContent
The menu panel, rendered in a portal and positioned relative to the trigger.
| Prop | Type | Default |
|---|---|---|
side | "top" | "right" | "bottom" | "left" | "inline-start" | "inline-end" | "bottom" |
sideOffset | number | 4 |
align | "start" | "center" | "end" | "start" |
alignOffset | number | 0 |
| Prop | Type | Default |
|---|---|---|
side | "top" | "right" | "bottom" | "left" | "bottom" |
sideOffset | number | 4 |
align | "start" | "center" | "end" | "start" |
alignOffset | number | 0 |
| Prop | Type | Default |
|---|---|---|
placement | Placement | "bottom start" |
offset | number | 4 |
crossOffset | number | 0 |
Also accepts the props of the primitive parts it wraps.
side is the side of the trigger the menu opens on, and align how it lines up with the trigger along that side. sideOffset and alignOffset are in pixels.
placement sets where the menu opens relative to the trigger. offset and crossOffset are in pixels.
By default the menu opens below the trigger, aligned to start, and matches the trigger's width with a minimum of 12rem. Override the width with className.
DropdownMenuGroup
Groups related items.
Also accepts the props of the primitive part it wraps.
DropdownMenuLabel
A heading for a group of items.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
Also accepts the props of the primitive part it wraps.
DropdownMenuItem
An action in the menu.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
variant | "default" | "destructive" | "default" |
Also accepts the props of the primitive part it wraps.
Selecting a regular item closes the menu. inset adds leading padding so the item aligns with items that have an icon or indicator; labels, sub triggers, checkbox and radio items take it too. variant="destructive" colors the item and its icon with the destructive color; use it on items that delete or remove something. Items set data-inset and data-variant, and disabled items get data-disabled.
DropdownMenuSub
The root of a nested submenu.
Also accepts the props of the primitive part it wraps.
DropdownMenuSubTrigger
The item that opens a submenu, shown with a trailing caret.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
Also accepts the props of the primitive part it wraps.
An open submenu trigger gets data-popup-open.
DropdownMenuSubContent
The panel of a submenu, opened beside its trigger.
| Prop | Type | Default |
|---|---|---|
side | "top" | "right" | "bottom" | "left" | "inline-start" | "inline-end" | "inline-end" |
sideOffset | number | 0 |
align | "start" | "center" | "end" | "start" |
alignOffset | number | -3 |
| Prop | Type | Default |
|---|---|---|
sideOffset | number | 0 |
alignOffset | number | -3 |
| Prop | Type | Default |
|---|---|---|
placement | Placement | "end top" |
offset | number | 0 |
crossOffset | number | -3 |
Also accepts the props of the primitive part it wraps.
The placement props work as on DropdownMenuContent, measured against the submenu trigger.
DropdownMenuCheckboxItem
An item that toggles a setting on or off and shows a check mark when on.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
| Prop | Type | Default |
|---|---|---|
defaultChecked | boolean | false |
closeOnClick | boolean | false |
| Prop | Type | Default |
|---|---|---|
isSelected | boolean | - |
defaultSelected | boolean | false |
onChange | (isSelected: boolean) => void | - |
Also accepts the props of the primitive part it wraps.
Checkbox items are uncontrolled with defaultChecked, or controlled with checked and onCheckedChange.
Set closeOnClick to close the menu when the item is selected.
Checkbox items are uncontrolled with defaultSelected, or controlled with isSelected and onChange.
Checkbox and radio items keep the menu open so several choices can be made in a row.
DropdownMenuRadioGroup
Groups radio items so that only one can be selected.
| Prop | Type | Default |
|---|---|---|
value | string | - |
defaultValue | string | - |
onChange | (value: string) => void | - |
Also accepts the props of the primitive part it wraps.
Radio groups take defaultValue, or value and onValueChange.
Radio groups take defaultValue, or value and onChange, which is called with the value of the newly selected item.
DropdownMenuRadioItem
An item in a radio group that shows a check mark when selected.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
| Prop | Type | Default |
|---|---|---|
closeOnClick | boolean | false |
| Prop | Type | Default |
|---|---|---|
value | string | - |
Also accepts the props of the primitive part it wraps.
Each radio item needs a value, which the group reports when it is selected.
Set closeOnClick to close the menu when the item is selected.
DropdownMenuSeparator
A divider between groups of items.
Also accepts the props of the primitive part it wraps.
DropdownMenuShortcut
Displays a keyboard shortcut at the end of an item.
| Prop | Type | Default |
|---|---|---|
mod | boolean | - |
Also accepts the props of React.ComponentProps<"span">.
Set mod to prefix the shortcut with the platform modifier key, ⌘ on Apple platforms and Ctrl+ elsewhere.
Each part sets a data-slot, such as dropdown-menu-item or dropdown-menu-checkbox-item-indicator, for styling.
Web preview
DropdownMenu opens DropdownMenuContent next to a DropdownMenuTrigger when it is pressed. The content renders through a portal (inside a full-window overlay on iOS) as a raised, rounded card positioned below the trigger by default.
Items can be plain actions, checkbox items, radio items in a group, or a submenu. DropdownMenuSubContent expands in place under its trigger, and the caret turns to point down while it is open. For a menu that opens on long press at the touch point, use Context Menu.
Installation
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/dropdown-menu.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/dropdown-menu.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Copy the animated view into
components/ui/animated-view.tsx:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/dropdown-menu.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Default
Web preview
Checkboxes
Checkbox items that toggle panels on and off.
Use DropdownMenuCheckboxItem with checked and onCheckedChange.
Web preview
Radio group
Radio items that pick one value.
Put DropdownMenuRadioItems inside a DropdownMenuRadioGroup with value and onValueChange.
Web preview
Inset
Use inset on items without an icon to align them with items that have one.
Web preview
Accessibility
- The trigger has the
buttonrole. - The popup has the
menurole; items usemenuitem,checkboxandradioroles, and radio groups useradiogroup. - Groups are labelled by their
DropdownMenuLabel, and separators have theseparatorrole.
API Reference
DropdownMenu
The root that manages the open state.
| Prop | Type | Default |
|---|---|---|
open | boolean | - |
defaultOpen | boolean | false |
onOpenChange | (open: boolean, details) => void | - |
modal | boolean | true |
disabled | boolean | false |
Use open with onOpenChange for controlled state, or defaultOpen for uncontrolled. modal blocks interaction with the rest of the screen while open, and disabled prevents the menu from opening. Use render on DropdownMenuTrigger to render it as a Button.
DropdownMenuContent
Floating menu card.
| Prop | Type | Default |
|---|---|---|
portalHost | string | - |
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.Popup>, PositionerProps.
Position the content with side, align, sideOffset and alignOffset. portalHost names the portal host to render into. The fade-in respects the system Reduce Motion setting.
DropdownMenuSubTrigger
Row that expands the submenu.
| Prop | Type | Default |
|---|---|---|
children | React.ReactNode | - |
iconClassName | string | - |
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.SubmenuTrigger>.
iconClassName adds classes to the caret icon. An open submenu trigger uses bg-accent.
DropdownMenuSubContent
Items of the submenu.
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.Popup>.
DropdownMenuItem
Action row.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
variant | "default" | "destructive" | "default" |
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.Item>.
Items close the menu when pressed unless closeOnPress={false}. Set variant="destructive" on an item that deletes or removes. inset adds start padding; the sub trigger, checkbox, radio and label parts take it too. A highlighted item fades to opacity-80 and a disabled one to opacity-50.
DropdownMenuCheckboxItem
Row with a check mark that toggles.
| Prop | Type | Default |
|---|---|---|
children | React.ReactNode | - |
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.CheckboxItem>.
DropdownMenuRadioItem
Row that selects one value in its group.
| Prop | Type | Default |
|---|---|---|
children | React.ReactNode | - |
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.RadioItem>.
DropdownMenuLabel
Heading for a group.
| Prop | Type | Default |
|---|---|---|
inset | boolean | - |
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.GroupLabel>.
Group items with DropdownMenuGroup and a DropdownMenuLabel, and divide sections with DropdownMenuSeparator.
DropdownMenuSeparator
Line between sections.
Also accepts the props of React.ComponentProps<typeof MenuPrimitive.Separator>.
DropdownMenu
The root that manages the open state.
| Parameter | Type | Default |
|---|---|---|
isPresented | Binding<Bool>? | nil |
side | Edge | .bottom |
align | PopupAlignment | .start |
sideOffset | CGFloat | 4 |
alignOffset | CGFloat | 0 |
content | () -> Content | - |
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
Overloads: init(isPresented:side:align:sideOffset:alignOffset:content:label:), init(_:isPresented:side:align:content:).
DropdownMenuItem
Action row.
| Parameter | Type | Default |
|---|---|---|
variant | Variant: .default | .destructive | .default |
inset | Bool | false |
action | () -> Void | - |
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
icon | Image | - |
Overloads: init(variant:inset:action:label:), init(_:variant:inset:action:), init(_:icon:variant:action:).
DropdownMenuCheckboxItem
Row with a check mark that toggles.
| Parameter | Type | Default |
|---|---|---|
isOn | Binding<Bool> | - |
inset | Bool | false |
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
Overloads: init(isOn:inset:label:), init(_:isOn:inset:).
DropdownMenuRadioGroup
Groups radio items so that only one can be selected.
| Parameter | Type | Default |
|---|---|---|
selection | Binding<Value> | - |
content | () -> Content | - |
DropdownMenuRadioItem
Row that selects one value in its group.
| Parameter | Type | Default |
|---|---|---|
value | Value | - |
inset | Bool | false |
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
Overloads: init(value:inset:label:), init(_:value:inset:).
DropdownMenuLabel
Heading for a group.
| Parameter | Type | Default |
|---|---|---|
_Â title | LocalizedStringKey | - |
inset | Bool | false |
DropdownMenuGroup
Groups related items.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
DropdownMenuSeparator
Line between sections.
Takes no parameters.
DropdownMenuSub
The root of a nested submenu.
| Parameter | Type | Default |
|---|---|---|
inset | Bool | false |
content | () -> Content | - |
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
icon | Image | - |
Overloads: init(inset:content:label:), init(_:inset:content:), init(_:icon:content:).
MenuItemVariant
Values: Default, Destructive.
DropdownMenu
The root that manages the open state.
| Parameter | Type | Default |
|---|---|---|
expanded | Boolean | - |
onExpandedChange | (Boolean) -> Unit | - |
trigger | @Composable () -> Unit | - |
modifier | Modifier | Modifier |
side | PopupSide | PopupSide.Bottom |
align | PopupAlign | PopupAlign.Start |
sideOffset | Dp | 4.dp |
alignOffset | Dp | 0.dp |
content | @Composable ColumnScope.() -> Unit | - |
DropdownMenuItem
Action row.
| Parameter | Type | Default |
|---|---|---|
onClick | () -> Unit | - |
modifier | Modifier | Modifier |
variant | MenuItemVariant: Default | Destructive | MenuItemVariant.Default |
enabled | Boolean | true |
inset | Boolean | false |
closeOnClick | Boolean | true |
content | @Composable RowScope.() -> Unit | - |
text | String | - |
icon | ImageVector? | null |
Overloads: DropdownMenuItem(onClick, content), DropdownMenuItem(text, onClick, icon).
DropdownMenuCheckboxItem
Row with a check mark that toggles.
| Parameter | Type | Default |
|---|---|---|
checked | Boolean | - |
onCheckedChange | (Boolean) -> Unit | - |
modifier | Modifier | Modifier |
enabled | Boolean | true |
inset | Boolean | false |
closeOnClick | Boolean | false |
content | @Composable RowScope.() -> Unit | - |
text | String | - |
Overloads: DropdownMenuCheckboxItem(checked, onCheckedChange, closeOnClick, content), DropdownMenuCheckboxItem(text, checked, onCheckedChange).
DropdownMenuRadioGroup
Groups radio items so that only one can be selected.
| Parameter | Type | Default |
|---|---|---|
value | String? | - |
onValueChange | (String) -> Unit | - |
modifier | Modifier | Modifier |
content | @Composable ColumnScope.() -> Unit | - |
DropdownMenuRadioItem
Row that selects one value in its group.
| Parameter | Type | Default |
|---|---|---|
value | String | - |
modifier | Modifier | Modifier |
enabled | Boolean | true |
inset | Boolean | false |
closeOnClick | Boolean | false |
content | @Composable RowScope.() -> Unit | - |
text | String | - |
Overloads: DropdownMenuRadioItem(value, closeOnClick, content), DropdownMenuRadioItem(value, text).
DropdownMenuLabel
Heading for a group.
| Parameter | Type | Default |
|---|---|---|
text | String | - |
modifier | Modifier | Modifier |
inset | Boolean | false |
DropdownMenuSeparator
Line between sections.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
DropdownMenuGroup
Groups related items.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable ColumnScope.() -> Unit | - |
DropdownMenuSub
The root of a nested submenu.
| Parameter | Type | Default |
|---|---|---|
text | String | - |
modifier | Modifier | Modifier |
icon | ImageVector? | null |
enabled | Boolean | true |
inset | Boolean | false |
content | @Composable ColumnScope.() -> Unit | - |
trigger | @Composable RowScope.() -> Unit | - |
Overloads: DropdownMenuSub(text, icon, content), DropdownMenuSub(trigger, content).