codefast/ui

Command Palette

Search for a command to run...

Source
Overlay

Dropdown Menu

Contextual action menu with keyboard navigation, shortcuts, checkboxes, and radio groups.

Examples

Checkbox items

A column-toggle menu — checkbox items drive state, with a live count below.

38 lines
import { Button } from "@codefast/ui/button";
import {
  DropdownMenu,
  DropdownMenuCheckboxItem,
  DropdownMenuContent,
  DropdownMenuGroup,
  DropdownMenuLabel,
  DropdownMenuTrigger,
} from "@codefast/ui/dropdown-menu";
import * as React from "react";

export function DropdownMenuCheckboxes() {
  const [showStatusBar, setShowStatusBar] = React.useState(true);
  const [showActivityBar, setShowActivityBar] = React.useState(false);
  const [showPanel, setShowPanel] = React.useState(false);

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button variant="outline">Open</Button>
      </DropdownMenuTrigger>
      <DropdownMenuContent className="w-40">
        <DropdownMenuGroup>
          <DropdownMenuLabel>Appearance</DropdownMenuLabel>
          <DropdownMenuCheckboxItem checked={showStatusBar ?? false} onCheckedChange={setShowStatusBar}>
            Status Bar
          </DropdownMenuCheckboxItem>
          <DropdownMenuCheckboxItem checked={showActivityBar} onCheckedChange={setShowActivityBar} disabled>
            Activity Bar
          </DropdownMenuCheckboxItem>
          <DropdownMenuCheckboxItem checked={showPanel} onCheckedChange={setShowPanel}>
            Panel
          </DropdownMenuCheckboxItem>
        </DropdownMenuGroup>
      </DropdownMenuContent>
    </DropdownMenu>
  );
}

Anatomy

How the parts nest — every slot the component exposes, in composition order.

DropdownMenu
├── DropdownMenuTrigger
└── DropdownMenuContent
└── DropdownMenuItem

Features

  • variant="destructive" on DropdownMenuItem tints the text/icon and the focus background for irreversible actions.
  • inset on Item/SubTrigger/Label aligns text with icon-led sibling items that don't have their own icon.
  • DropdownMenuSub/SubTrigger/SubContent nest a secondary menu; DropdownMenuShortcut right-aligns a keyboard-hint string.
  • DropdownMenuCheckboxItem and DropdownMenuRadioGroup/RadioItem add toggleable or exclusive-choice items inside the menu.

API reference

Props for each part of the component. All native element props are also forwarded.

DropdownMenuCheckboxItem

A menu item with a checkmark you control.

checkedboolean | "indeterminate"

The controlled checked state.

onCheckedChange(checked: boolean) => void

Called when the checked state changes.

DropdownMenuRadioGroup

Single-choice section inside the menu.

valuestring

The controlled chosen RadioItem value.

onValueChange(value: string) => void

Called when the chosen RadioItem value changes.

DropdownMenuRadioItem

One choice within a DropdownMenuRadioGroup.

valuestring

This item's value, matched against the group's controlled value (required).

DropdownMenuContent

side"top" | "right" | "bottom" | "left"

The preferred edge of the trigger to render against.

Default"bottom"

align"start" | "center" | "end"

The preferred alignment against the trigger.

Default"center"

Accessibility

Built to be keyboard-navigable and screen-reader friendly out of the box.

KeyFunction
SpaceOpens the menu from the trigger.
Arrow+DownMoves to the next item.
Arrow+UpMoves to the previous item.
EnterActivates the focused item.
EscCloses the menu and restores focus.
  • Implements the ARIA menu pattern with type-ahead and roving focus.
  • Checkbox and radio items expose aria-checked so their state is announced.
  • Use a Select (listbox) when the goal is choosing a form value, not running an action.

Guidelines

Conventions that keep usage consistent across an app.

Do

  • Group related actions and label the groups.
  • Use checkbox items for toggles and a radio group for one-of-many.

Don’t

  • Don’t nest more than one level of submenus.
  • Don’t put primary page actions only in a dropdown.

Explore further

Ready to integrate?

Follow the Getting Started guide to install @codefast/ui, or browse the full component gallery.