Dropdown menu

Actions, with a submenu that opens at once.

Pick an action

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/dropdown.json

This copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.

Code

components/fabricui/dropdown.tsx
"use client";

import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { DropdownMenu as DropdownEngine } from "@/lib/fabricui/dropdown-engine";
import "./dropdown.css";

const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;

/** An action. `icon` is the inner markup of a 24×24 stroke icon (Hugeicons' `<path d="…" />`s). */
export type DropdownMenuAction = {
  id: string;
  text: string;
  icon?: string;
  /** A shortcut shown at the row's end, like "⌘ ,". */
  kbd?: string;
components/fabricui/dropdown.css
/*
 * dropdown.css — Dropdown menu (dropdown.js). Moved out of dropdown.html.
 */
/*
 * Dropdown menu. The trigger + overlay panel pair holding a menu of actions:
 * icons, shortcuts, a group label, separators, a submenu, a danger item.
 * The menu drops open as cloth (cloth-drop.js, overlay mode) and collapses
 * plainly; the submenu hangs off its item and drops in as its own small
 * cloth (no trigger, tuck 0).
 */
/* room for the menu (260) + gap + submenu (200), so the submenu stays in frame */
.frame__slot:where(.fui-dropdown, .fui-dropdown *) { width: 466px; height: 330px; }

.dd:where(.fui-dropdown, .fui-dropdown *) { position: relative; width: 260px; height: 38px; }
.dd__panel:where(.fui-dropdown, .fui-dropdown *) { position: absolute; inset: 0 0 auto 0; z-index: 10; }
lib/fabricui/dropdown-engine.js
import { ClothDrop } from "./cloth-drop";
import { inTriangle } from "./context-menu-engine";

/*
 * dropdown-engine.js — Dropdown menu (dropdown.css). Moved out of dropdown.html so it can be used anywhere.
 */

// (on the original page these two came from the page's script; an item's
// `icon` is now its SVG's inner markup, like the context menu's)
const MORE = `<path d="M9.00005 6C9.00005 6 15 10.4189 15 12C15 13.5812 9 18 9 18" />`;
const svg = (d, cls = "icon") => `<svg class="${cls}" viewBox="0 0 24 24" aria-hidden="true">${d}</svg>`;
const esc = s => String(s).replace(/[&<>"]/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
let menus = 0;   // each menu's items get ids of their own (two menus can share item ids)

/**
 * DropdownMenu: a trigger-anchored menu of actions with one level of

Usage

Usage
import { DropdownMenu } from "@/components/fabricui/dropdown";

const ITEMS = [
  { id: "settings", text: "Settings", kbd: "⌘ ," },
  { id: "share", text: "Share", items: [{ id: "link", text: "Copy link", kbd: "⌘ L" }, { id: "email", text: "Email" }] },
  { sep: true as const },
  { id: "logout", text: "Log out", danger: true, kbd: "⇧ ⌘ Q" },
];

export function Workspace() {
  return <DropdownMenu label="Acme Studio" subLabel="Share" items={ITEMS} trigger="Acme Studio" onSelect={item => run(item.id)} />;
}

Props

PropTypeDefault
items

The rows. icon is the inner markup of a 24×24 stroke icon (Hugeicons <path>s); kbd a shortcut shown at the end; items a submenu (one level) that opens at once on hover; sep a line.

({ id, text, icon?, kbd?, danger?, items? } | { sep: true })[]—
trigger

What the trigger shows, before its chevron.

ReactNode—
onSelect

Called with the chosen action.

(item) => void—
label

The menu's accessible name.

string—
subLabel

The submenu's accessible name.

string—
className

Added to the root. Takes any other <div> prop too.

string—