Skip to content
Browse documentation

Actions behind a button: the menu rises off the trigger, one highlight glides between rows, and checkbox rows draw their tick in place.

Used forRow actionsAccount menusView optionsCreate menus

Install with the shadcn CLI

$ npx shadcn@latest add @hairline/dropdown-menu

Older shadcn CLI, or a network that blocks ui.shadcn.com? Install by URL: shadcn add https://hairlineui.com/r/dropdown-menu.json. More

Used in 2 blocks

All blocks

Templates

Need the whole page?

65 Next.js landing pages, each designed from scratch. Try any of them live; one is $39, all of them $159, and 8 are free.

Where it’s used

  • Row actions

    Edit, duplicate, delete on a "…" button.

  • Account menus

    Profile, settings, sign out under an avatar.

  • View options

    Checkable columns or display settings.

  • Create menus

    New doc, new folder, upload.

What it handles for you

Menus need roving focus, typeahead, Home/End and focus return; the gliding highlight is what makes it feel made rather than assembled.

Instead of wiring upRadix DropdownMenufloating-uiroving tabindex
  1. Gliding highlight

    One fill moves between rows (220ms) for pointer and keys alike.

  2. Keyboard

    Enter/Space/↓ open on the first row, ↑ on the last; arrows wrap; typing jumps.

  3. Checkable rows

    A drawn tick, aria-checked, menu stays open.

  4. Plain data

    items: rows, labels and separators as objects.

Overview

Menus usually repaint each row's background as you move, so the highlight blinks from row to row. Here one highlight glides, typing jumps to a row, checkbox rows draw their tick and keep the menu open, and destructive rows are red. It rises off its trigger and flips above when there is no room.

States

open
Rises off the trigger (320ms); flips above if needed.
move
The highlight glides to the row under the pointer or the key.
pick
Closes (180ms) and focus returns to the trigger; checkbox rows stay open and draw their tick (280ms).

Usage

usage.tsx
import { DropdownMenu } from "@/components/dropdown-menu";<DropdownMenu  trigger={<button aria-label="More">…</button>}  items={[    { id: "rename", label: "Rename", shortcut: "F2", onSelect: rename },    { type: "separator" },    { id: "delete", label: "Delete", destructive: true, onSelect: remove },  ]}/>

Depends on lucide-react, installed for you by the CLI along with the keyframes it needs.

Works with

Props

trigger
ReactElement
The button that opens it.
items
MenuEntry[]
Rows ({ id, label, icon, shortcut, destructive, disabled, onSelect }), { type: "checkbox", checked, onCheckedChange }, { type: "label" } and { type: "separator" }.
side / align= "bottom" / "start"
Side / Align
Where it sits.
open / onOpenChange
boolean / (open) => void
Control it.
minWidth= 200
number
Never narrower than the trigger.
label
string
Accessible name of the menu.

Accessibility and motion

  • Placement, presence and dismissal live in floating.ts, shared by tooltip, popover, dropdown and context menus: they all flip, shift, enter and leave the same way.
  • role="menu" with menuitem and menuitemcheckbox rows.
  • Reduced motion: the menu fades; the highlight jumps.

Requirements

React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

Source

"use client";import * as React from "react";import { createPortal } from "react-dom";import { enterStyle, themeVars, useDismiss, useFloating, usePresence, type Align, type Side } from "./floating";import { MenuList, type MenuEntry } from "./menu-list";import { useInteractionReducedMotion } from "./ui-interaction-motion";/* ───────────────────────────────────────────────────────── * DROPDOWN MENU: actions behind a button * *   open     the menu rises off the trigger out of a blur, from *            the point on the trigger nearest to it; it flips *            above when there's no room below *   move     one highlight glides between rows *   pick     the menu closes and focus goes back to the trigger; *            checkbox rows draw their tick and stay open * * Opening from the keyboard (Enter, Space, ↓) focuses the first * row; ↑ focuses the last. Escape or a press outside closes it. * ───────────────────────────────────────────────────────── */export type { MenuEntry } from "./menu-list";export interface DropdownMenuProps {  /** The button that opens it. It gets aria-haspopup, aria-expanded and the click. */  trigger: React.ReactElement<React.HTMLAttributes<HTMLElement>>;  items: MenuEntry[];  side?: Side;  align?: Align;  open?: boolean;  onOpenChange?: (open: boolean) => void;  /** Accessible name of the menu. */  label?: string;  /** Minimum width in px; never narrower than the trigger. */  minWidth?: number;}export function DropdownMenu({ trigger, items, side = "bottom", align = "start", open: controlled, onOpenChange, label, minWidth = 200 }: DropdownMenuProps) {  const reduced = useInteractionReducedMotion();  const anchor = React.useRef<HTMLSpanElement>(null);  const surface = React.useRef<HTMLDivElement>(null);  const [own, setOwn] = React.useState(false);  const [focusOn, setFocusOn] = React.useState<"first" | "last" | "none">("none");  const open = controlled ?? own;  const { mounted, shown } = usePresence(open, 180);  const place = useFloating({ open: mounted, anchor, floating: surface, side, align, offset: 6 });  const [vars, setVars] = React.useState<React.CSSProperties>({});  const id = React.useId();  const triggerId = React.useId();  const setOpen = (next: boolean, refocus = false) => {    if (controlled === undefined) setOwn(next);    onOpenChange?.(next);    // By id rather than a ref: this runs from handlers that are created while rendering.    if (!next && refocus) document.getElementById(trigger.props.id ?? triggerId)?.focus();  };  useDismiss(open, (reason) => setOpen(false, reason === "escape"), [anchor, surface]);  const [width, setWidth] = React.useState(minWidth);  React.useLayoutEffect(() => {    if (!mounted) return;    setVars(themeVars(anchor.current));    setWidth(Math.max(minWidth, anchor.current?.offsetWidth ?? 0));  }, [mounted, minWidth]);  return (    <>      <span ref={anchor} className="inline-flex">        {React.cloneElement(trigger, {          id: trigger.props.id ?? triggerId,          "aria-haspopup": "menu",          "aria-expanded": open,          "aria-controls": open ? id : undefined,          onClick: (e: React.MouseEvent<HTMLElement>) => {            trigger.props.onClick?.(e);            setFocusOn(e.detail === 0 ? "first" : "none");            setOpen(!open);          },          onKeyDown: (e: React.KeyboardEvent<HTMLElement>) => {            trigger.props.onKeyDown?.(e);            if (e.key === "ArrowDown" || e.key === "ArrowUp") {              e.preventDefault();              setFocusOn(e.key === "ArrowDown" ? "first" : "last");              setOpen(true);            }          },        })}      </span>      {mounted &&        createPortal(          <div            ref={surface}            id={id}            className="fixed left-0 top-0 z-[55] overflow-hidden rounded-[14px] bg-popover text-popover-foreground shadow-[0_0_0_1px_var(--border)]"            style={{ ...vars, left: place.x, top: place.y, minWidth: width, transformOrigin: place.origin, ...enterStyle(shown, place.side, reduced) }}          >            <MenuList items={items} label={label} autoFocus={focusOn} onClose={() => setOpen(false, true)} />          </div>,          document.body,        )}    </>  );}export default DropdownMenu;

Registry item: /r/dropdown-menu.json