oakoss/ui
Components

Dropdown Menu

A menu of actions or options opened from a button.

SourceReact Aria

Installation

npx shadcn@latest add @oakoss/dropdown-menu

This also installs Popover, whose panel the menu opens in.

Usage

import {
  DropdownMenu,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from '#/components/ui/overlays/dropdown-menu';

<DropdownMenuTrigger>
  <Button>Options</Button>
  <DropdownMenu onAction={(key) => run(key)}>
    <DropdownMenuItem id="edit" shortcut="⌘E">
      Edit
    </DropdownMenuItem>
    <DropdownMenuItem id="delete" intent="destructive">
      Delete
    </DropdownMenuItem>
  </DropdownMenu>
</DropdownMenuTrigger>;

An item's children are its label. shortcut and description sit beside the label and describe the item without joining its name. intent="destructive" colors an item that removes something.

Examples

Checkbox and radio groups

selectionMode on a DropdownMenuGroup makes its items checkboxes (multiple) or radios (single), marked with a check or a dot. Action groups and selection groups mix in one menu.

DropdownMenuSub wraps a DropdownMenuSubTrigger and the DropdownMenuSubContent it opens beside it.

description adds a line under the label, and href makes an item a link.

Accessibility

React Aria renders a button that opens a menu in a popover, and links them:

  • Name: the menu is named by its trigger. A shortcut and a description describe their item rather than joining its name, and a group is named by its DropdownMenuLabel.
  • Roles: items are menuitem, or menuitemcheckbox and menuitemradio in a group with selectionMode, with aria-checked. An item with href is a link.
  • Focus: opened from the keyboard or a screen reader, the menu focuses its first item; opened with a pointer, it focuses the menu itself, so ↓ moves to the first item. It keeps focus inside and returns it to the trigger on close.

Items in a selection group leave room at the start for the check or dot, which are hidden from assistive technology. In Windows High Contrast the focused item takes the system's selection colors.

KeyAction
Enter, Space, ↓Opens the menu on its first item
↑Opens the menu on its last item
↑, ↓Moves between items
TypingMoves to the next item that starts with the letters
Enter, SpaceRuns an item, or toggles it in a selection group
→, ←Opens or closes a submenu (reversed right to left)
EscapeCloses the menu, or only the submenu that's open

Follows the WAI-ARIA menu button pattern.

API

The popover and its menu. Renders our PopoverContent around React Aria's Menu. className applies to the panel, which is at least 8rem wide and grows with its items.

Props

Prop

Type

Also accepts all React Aria Menu props, such as onAction, selectionMode and disabledKeys. placement defaults to bottom start and offset to 4.

Data attributes

AttributePresent when
data-slotAlways: dropdown-menu-content (the panel), dropdown-menu-list
data-placementAlways; the side the panel opened on (top, bottom, left, right)
data-enteringThe panel is fading in
data-exitingThe panel is fading out

Extends React Aria's MenuItem. A text label, values in it included, is its typeahead text; any other label needs textValue, which the type requires. A wrapper that leaves out some of the item's props types them as Omit<MenuItemBaseProps, …> & MenuItemLabel, both from overlays/menu-item.tsx, since Omit on DropdownMenuItemProps would drop that requirement.

Props

Prop

Type

Data attributes

AttributePresent when
data-slotAlways dropdown-menu-item
data-intentAlways: neutral or destructive
data-focusedThe item has focus
data-selectedThe item is checked in a selection group
data-selection-modeThe item is in a selection group: single, multiple
data-openA submenu trigger's submenu is open
data-disabledThe item is disabled

CSS variables

VariableSet byUsed for
--menu-textintent="destructive"The item's text
--menu-subtleintent="destructive"The focused item's fill

Other parts

PartExtendsdata-slot
DropdownMenuTriggerReact Aria's MenuTriggernone
DropdownMenuGroupReact Aria's MenuSectiondropdown-menu-group
DropdownMenuLabelReact Aria's Header, with insetdropdown-menu-label
DropdownMenuSeparatorReact Aria's Separatordropdown-menu-separator
DropdownMenuSubReact Aria's SubmenuTriggernone
DropdownMenuSubTriggerDropdownMenuItem, with a chevrondropdown-menu-sub-trigger
DropdownMenuSubContentDropdownMenu, placed beside its triggerdropdown-menu-sub-content