Skip to content
flyingsalmon

Dropdown menu

A trigger opens a list of actions the traveller runs once: rename, duplicate, share, delete, or go somewhere.

Installation

npx shadcn@latest add @flyingsalmon/dropdown-menu

Usage

import { Button, ButtonVariant } from '@/components/ui/button'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'

interface DropdownMenuUsageProps {
  onRename: () => void
  onDuplicate: () => void
}

export function DropdownMenuUsage({
  onRename,
  onDuplicate,
}: DropdownMenuUsageProps) {
  return (
    <DropdownMenu>
      <DropdownMenuTrigger>
        <Button variant={ButtonVariant.Outline}>Trip actions</Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent>
        <DropdownMenuItem onSelect={onRename}>Rename</DropdownMenuItem>
        <DropdownMenuItem onSelect={onDuplicate}>Duplicate</DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  )
}

Examples

Without icons

An icon-free menu starts flush at the left edge, because the icon slot exists only on an item that gets an icon.

Labelled groups

A group with a heading puts a DropdownMenuLabel first. A separator divides groups; the group itself has no visual of its own.

A disabled item

The item stays in the list, dimmed, and the arrow keys skip it, so the menu never shifts between opens.

A navigation item

An action uses onSelect. An item that goes somewhere wraps your router’s Link with asChild, so it has a real href and middle-click works.

Placement

side and align choose where the panel opens. Aligning to the end keeps a menu on the right edge of the page from running off it.

Actions on a trip card

Duplicate adds a card straight away. Delete opens a confirmation, and the card disappears when it closes. Each result shows on the item that changed.

Kyoto in autumn

12 to 19 Oct, 3 travellers

Lisbon long weekend

3 to 6 Mar, 2 travellers

Guidelines

When to use

  • For the actions on one item, gathered behind an overflow button: rename, duplicate, share, delete.
  • For a short list of places to go from one trigger.

When not to use

  • Use Switch for settings that stay on or off. A menu closes on every choice, so a toggle belongs on the page.
  • Use Select to pick a value for a field. A menu runs an action and a select holds a value.
  • Use Button for one action with no siblings. A menu with a single item is a button with extra steps.

Do and don’t

  • Do
    Show the result on the item the action changed.The menu closes on select, so the traveller is looking at the item again. A duplicated trip appearing in the list is the confirmation. When the item has no visible home, a notice carries the result.
  • Do
    Open a confirmation dialog from a destructive item.The menu offers the choice and the dialog asks for the decision, so one stray click cannot delete a trip.
  • Do
    Put an overflow trigger at the trailing edge of its row, as a ghost icon button, and open the menu aligned to its end.The row reads first and its actions trail it. An end-aligned menu opens over the row rather than past the edge of the page.
  • Don’t
    Show an overflow trigger on an item with no actions.A trigger that opens nothing teaches the traveller to ignore every other one. Leave the trailing edge empty.
  • Do
    Give every item in a menu an icon, or none of them.Mixed rows leave the labels ragged.
  • Do
    Head the list with a quiet label for context, such as the trip name above the trip actions.The traveller sees which trip the actions apply to before they choose. Muted text never looks like something to press.
  • Don’t
    Add checkbox items, radio items, or a submenu.The menu is for actions only. A choice that persists belongs on the page as a field.

Accessibility

KeyDescription
EnterSpaceArrowDownOn the trigger, opens the menu with the first item highlighted. A pointer click opens it with no item highlighted.
ArrowUpArrowDownMoves the highlight. It stops at the first and last item rather than wrapping.
HomeEndJumps to the first or last item.
EnterSpaceOn an item, runs it and closes the menu.
EscapeCloses the menu and returns focus to the trigger.

Typing a letter jumps to the next item starting with it. Tab does nothing inside the menu, and disabled items are skipped. An outside click closes the menu and returns focus to the trigger, including after a link item navigates.

The trigger exposes aria-haspopup="menu" and aria-expanded. An icon-only trigger has no name of its own, so its aria-label names what the menu acts on. The highlight moves with focus and is the focus indicator, so an item draws no ring of its own.

API

DropdownMenu

The root. The menu is always modal.

PropTypeDefaultDescription
openboolean–The controlled open state.
defaultOpenbooleanfalseThe initial open state when uncontrolled.
onOpenChange(open: boolean) => void–Called when the menu opens or closes.

DropdownMenuTrigger

PropTypeDefaultDescription
childrenReactElementRequiredOne element, a registry Button, that opens the menu. An icon-only button takes an aria-label that names what the menu acts on, such as "Actions for Kyoto in autumn".

DropdownMenuContent

The panel. Everything else about its positioning is fixed.

PropTypeDefaultDescription
sidePanelSidePanelSide.BottomWhich edge of the trigger the panel opens from. It flips when there is no room.
alignPanelAlignPanelAlign.CenterHow the panel lines up along that edge.

DropdownMenuItem

PropTypeDefaultDescription
variantDropdownMenuItemVariantDropdownMenuItemVariant.DefaultDestructive marks an item that removes something.
iconReactNode–Fills the slot before the label. Give every item an icon or none.
onSelect(event: Event) => void–Runs the action. The menu closes after it.
asChildbooleanfalseRenders your element, such as a router Link, in place of the item.
disabledbooleanfalseDims the item, skips it in the arrow path, and ignores selection.

DropdownMenuGroup, DropdownMenuLabel, DropdownMenuSeparator, and DropdownMenuShortcut take the props of the element they render, typed as DropdownMenuGroupProps, DropdownMenuLabelProps, DropdownMenuSeparatorProps, and DropdownMenuShortcutProps. DropdownMenuShortcut displays the shortcut at the trailing edge and binds no key, so your app owns the shortcut.

  • Dialog

    Confirms a destructive item before it runs.
  • Select

    Picks a value for a field instead of running an action.
  • Button

    The trigger, and the home of a single action.
  • Principles

    Where a result belongs once the menu has closed.