Dropdown Menu

The Dropdown Menu component provides a list of actions or options that appear when a button is clicked. It’s perfect for compact navigation, displaying options, or executing actions from a menu.

Usage

<script>
  import { 
    DropdownMenu,
    DropdownMenuTrigger,
    DropdownMenuContent,
    DropdownMenuItem
  } from "$lib/components/ui/dropdown-menu";
  import { Button } from "$lib/components/ui/button";
</script>

<DropdownMenu>
  <DropdownMenuTrigger>
    <Button>Open Menu</Button>
  </DropdownMenuTrigger>
  <DropdownMenuContent>
    <DropdownMenuItem>Profile</DropdownMenuItem>
    <DropdownMenuItem>Settings</DropdownMenuItem>
    <DropdownMenuItem>Logout</DropdownMenuItem>
  </DropdownMenuContent>
</DropdownMenu>

Examples

Basic Dropdown

A simple dropdown menu with a few items.

With Label and Groups

A dropdown menu with labels and grouped items.

With Checkbox Items

A dropdown menu with checkbox items that can be toggled.

With Radio Items

A dropdown menu with radio items for selecting a single option from a group.

With Sub-Menu

A dropdown menu with a nested sub-menu.

Disabled Items

A dropdown menu with disabled items.

With Inset Items

A dropdown menu with inset items that have consistent indentation.

API Reference

Dropdown Menu Components

ComponentDescription
DropdownMenuThe root component that manages the dropdown menu state.
DropdownMenuTriggerThe button that toggles the dropdown menu.
DropdownMenuContentThe floating content that contains the dropdown menu items.
DropdownMenuItemA selectable menu item that performs an action.
DropdownMenuCheckboxItemA checkbox item that can be toggled on or off.
DropdownMenuRadioItemA radio item that can be selected from a group.
DropdownMenuLabelA non-interactive label to group menu items.
DropdownMenuSeparatorA visual separator between menu items.
DropdownMenuShortcutDisplays keyboard shortcut hints.
DropdownMenuGroupGroups related menu items together.
DropdownMenuSubCreates a submenu within a dropdown menu.
DropdownMenuSubTriggerThe trigger button for a submenu.
DropdownMenuSubContentThe content wrapper for a submenu.
DropdownMenuRadioGroupA container for radio items where only one can be selected.

DropdownMenu Properties

PropertyTypeDefaultDescription
openbooleanfalseWhether the dropdown menu is open.
onOpenChangefunctionundefinedCallback fired when the open state changes.
modalbooleantrueWhether clicking outside closes the dropdown.

DropdownMenuContent Properties

PropertyTypeDefaultDescription
sideOffsetnumber4Distance from the trigger.
classstringundefinedCustom CSS classes.
portalPropsobjectundefinedProps for the portal component.

DropdownMenuItem Properties

PropertyTypeDefaultDescription
disabledbooleanfalseWhether the item is disabled.
insetbooleanfalseWhether to add consistent indentation.
classstringundefinedCustom CSS classes.

DropdownMenuCheckboxItem Properties

PropertyTypeDefaultDescription
checkedbooleanfalseWhether the checkbox is checked.
disabledbooleanfalseWhether the checkbox is disabled.
indeterminatebooleanfalseWhether the checkbox is in indeterminate state.

DropdownMenuRadioItem Properties

PropertyTypeDefaultDescription
valuestringRequiredThe value of the radio item.
disabledbooleanfalseWhether the radio item is disabled.

DropdownMenuRadioGroup Properties

PropertyTypeDefaultDescription
valuestringundefinedThe value of the selected radio item.
onValueChangefunctionundefinedCallback fired when the selected value changes.

Implementation

The Dropdown Menu component is built on top of bits-ui primitives. Here’s a look at how the various components are styled:

<!-- DropdownMenuContent styling -->
<DropdownMenuPrimitive.Content
  class={cn(
    "bg-popover text-popover-foreground z-50 min-w-[8rem] overflow-hidden rounded-md border p-1 shadow-md",
    "data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 outline-none"
  )}
/>

<!-- DropdownMenuItem styling -->
<DropdownMenuPrimitive.Item
  class={cn(
    "data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground relative flex cursor-default select-none items-center gap-2 rounded-sm px-2 py-1.5 text-sm outline-none transition-colors data-[disabled]:pointer-events-none data-[disabled]:opacity-50 [&>svg]:size-4 [&>svg]:shrink-0",
    inset && "pl-8"
  )}
/>

The component is built with accessibility in mind, supporting keyboard navigation with arrow keys, proper ARIA attributes, and focus management for a seamless user experience.