Dropdown menu
A "more" button on a project card that opens a menu of actions, with icons, shortcut keys, dividers, a nested menu and a red delete. It works with the arrow keys and closes on Escape.
Website redesignexample.com
- Status
- Live
- Owner
- Maya Okafor
- Updated
- 2 hours ago
Options
Code
import { useEffect, useId, useRef, useState } from "react";
import "./dropdown-menu.css";
/* Filled 16px icons, as SVG paths. */
const icons = {
edit: "M11.5 1.8a1.6 1.6 0 0 1 2.3 0l.4.4a1.6 1.6 0 0 1 0 2.3L6.1 12.6a1 1 0 0 1-.4.25l-3 1a.6.6 0 0 1-.76-.76l1-3a1 1 0 0 1 .25-.4l8.3-8.3Z",
copy: "M5.5 1.5h6A2.5 2.5 0 0 1 14 4v6a1 1 0 0 1-1 1h-.5V5.5a2 2 0 0 0-2-2H4.5v-1a1 1 0 0 1 1-1ZM2 6.5A1.5 1.5 0 0 1 3.5 5h6A1.5 1.5 0 0 1 11 6.5v6A1.5 1.5 0 0 1 9.5 14h-6A1.5 1.5 0 0 1 2 12.5v-6Z",
share: "M14.3 1.7a.6.6 0 0 1 .1.63l-4.6 11.5a.6.6 0 0 1-1.1.03L6.6 9.4 2.14 7.3a.6.6 0 0 1 .03-1.1l11.5-4.6a.6.6 0 0 1 .63.1Z",
archive: "M1.5 3a1 1 0 0 1 1-1h11a1 1 0 0 1 1 1v1.5a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1V3Zm1 3.5h11v5A2.5 2.5 0 0 1 11 14H5a2.5 2.5 0 0 1-2.5-2.5v-5Zm3.750 1.750a.75.75 0 0 0 0 1.500h3.500a.75.75 0 0 0 0-1.500h-3.500Z",
trash: "M6.5 1.5h3a1 1 0 0 1 1 1V3H13a.75.75 0 0 1 0 1.5h-.3l-.55 8.2a1.5 1.5 0 0 1-1.5 1.400H5.350a1.5 1.5 0 0 1-1.500-1.400L3.300 4.500H3A.75.75 0 0 1 3 3h2.500v-.5a1 1 0 0 1 1-1Z",
link: "M6.5 4.5a.75.75 0 0 1 0 1.5H5a2 2 0 1 0 0 4h1.5a.75.75 0 0 1 0 1.5H5a3.5 3.5 0 1 1 0-7h1.5Zm3 0H11a3.5 3.5 0 1 1 0 7H9.5a.75.75 0 0 1 0-1.5H11a2 2 0 1 0 0-4H9.5a.75.75 0 0 1 0-1.5ZM5.250 8A.75.75 0 0 1 6 7.250h4a.75.75 0 0 1 0 1.500H6A.75.75 0 0 1 5.250 8Z",
mail: "M1.5 4.5a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v.3L8 8.600 1.500 4.800v-.3Zm0 2.050V11.500a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2V6.550L8.380 10.100a.75.75 0 0 1-.76 0L1.500 6.550Z",
invite: "M7 2a2.750 2.750 0 1 1 0 5.500A2.750 2.750 0 0 1 7 2ZM2 12.500C2 10.500 4.200 9 7 9c1 0 1.900.2 2.700.5v1.250a1.750 1.750 0 0 0 .8 3.250H3a1 1 0 0 1-1-1v-.5Zm10.250-3.500a.75.75 0 0 1 .75.75v1h1a.75.75 0 0 1 0 1.500h-1v1a.75.75 0 0 1-1.500 0v-1h-1a.75.75 0 0 1 0-1.500h1v-1a.75.75 0 0 1 .75-.75Z",
dots: "M3.250 6.500a1.500 1.500 0 1 1 0 3 1.500 1.500 0 0 1 0-3Zm4.750 0a1.500 1.500 0 1 1 0 3 1.500 1.500 0 0 1 0-3Zm4.750 0a1.500 1.500 0 1 1 0 3 1.500 1.500 0 0 1 0-3Z",
right: "M6.280 3.220a.75.75 0 0 0-1.060 1.060L8.940 8l-3.720 3.720a.75.75 0 1 0 1.060 1.060l4.250-4.250a.75.75 0 0 0 0-1.060L6.280 3.220Z",
};
function Icon({ name, className }) {
return (
<svg className={className} viewBox="0 0 16 16" aria-hidden="true">
<path fillRule="evenodd" d={icons[name]} />
</svg>
);
}
/* One menu, and any menus nested inside it. */
function Menu({ label, items, id, onDone }) {
const [openSub, setOpenSub] = useState(null);
const keys = (event, index) => {
const list = Array.from(event.currentTarget.closest(".dh-drop-menu").children)
.map((child) => (child.matches(".dh-drop-sub") ? child.firstElementChild : child))
.filter((child) => child.matches('[role="menuitem"]'));
const at = list.indexOf(event.currentTarget);
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault();
event.stopPropagation();
list[(at + (event.key === "ArrowDown" ? 1 : -1) + list.length) % list.length].focus();
}
if (event.key === "ArrowRight" && items[index].items) {
event.stopPropagation();
setOpenSub(index);
event.currentTarget.nextElementSibling.querySelector('[role="menuitem"]').focus();
}
};
return (
<div className="dh-drop-menu" id={id} role="menu" aria-label={label}>
{items.map((item, index) => {
if (item === "divider") return <hr key={index} />;
if (item.items) {
return (
<div
className="dh-drop-sub"
key={index}
onPointerEnter={(event) => event.pointerType === "mouse" && setOpenSub(index)}
onPointerLeave={(event) => event.pointerType === "mouse" && setOpenSub(null)}
onKeyDown={(event) => {
if (event.key !== "ArrowLeft" || event.target === event.currentTarget.firstElementChild) return;
event.stopPropagation();
setOpenSub(null);
event.currentTarget.firstElementChild.focus();
}}
>
<button
type="button"
role="menuitem"
tabIndex={-1}
aria-haspopup="menu"
aria-expanded={openSub === index}
onClick={() => setOpenSub(openSub === index ? null : index)}
onKeyDown={(event) => keys(event, index)}
>
<Icon name={item.icon} />
<span>{item.label}</span>
<Icon name="right" className="dh-drop-arrow" />
</button>
<Menu label={item.label} items={item.items} onDone={onDone} />
</div>
);
}
return (
<button
type="button"
role="menuitem"
tabIndex={-1}
key={index}
data-kind={item.danger ? "danger" : undefined}
onKeyDown={(event) => keys(event, index)}
onClick={() => {
if (item.onSelect) item.onSelect();
onDone();
}}
>
<Icon name={item.icon} />
<span>{item.label}</span>
{item.shortcut && <kbd>{item.shortcut}</kbd>}
</button>
);
})}
</div>
);
}
/* A "more" button that opens a menu of actions. The label is read out, not shown.
<DropdownMenu label="Project options" items={[{ label: "Rename", icon: "edit", shortcut: "R", onSelect: rename }, "divider", { label: "Delete", icon: "trash", danger: true }]} /> */
export function DropdownMenu({ label, items, defaultOpen = false }) {
const id = useId();
const drop = useRef(null);
const [open, setOpen] = useState(defaultOpen);
useEffect(() => {
const away = (event) => {
if (!drop.current.contains(event.target)) setOpen(false);
};
document.addEventListener("pointerdown", away);
return () => document.removeEventListener("pointerdown", away);
}, []);
const close = () => {
setOpen(false);
drop.current.querySelector(".dh-drop-trigger").focus();
};
return (
<div
className="dh-drop"
ref={drop}
onKeyDown={(event) => {
if (event.key === "Escape") close();
}}
>
<button
type="button"
className="dh-drop-trigger"
aria-label={label}
aria-haspopup="menu"
aria-expanded={open}
aria-controls={id}
onClick={() => setOpen(!open)}
onKeyDown={(event) => {
if (event.key !== "ArrowDown") return;
event.preventDefault();
setOpen(true);
drop.current.querySelector('[role="menuitem"]').focus();
}}
>
<Icon name="dots" />
</button>
{/* The key closes any nested menu when the menu itself closes. */}
<Menu key={open ? "open" : "closed"} id={id} label={label} items={items} onDone={close} />
</div>
);
}
export function Example() {
return (
<div className="dh-drop-demo">
<div className="dh-drop-card">
<header>
<i aria-hidden="true">W</i>
<p>
<b>Website redesign</b>
<span>example.com</span>
</p>
<DropdownMenu
label="Project options"
defaultOpen
items={[
{ label: "Rename", icon: "edit", shortcut: "R" },
{ label: "Duplicate", icon: "copy", shortcut: "⌘D" },
{
label: "Share",
icon: "share",
items: [
{ label: "Copy link", icon: "link" },
{ label: "Send by email", icon: "mail" },
{ label: "Invite people", icon: "invite" },
],
},
"divider",
{ label: "Archive", icon: "archive", shortcut: "E" },
"divider",
{ label: "Delete", icon: "trash", shortcut: "⌫", danger: true },
]}
/>
</header>
<dl>
<div>
<dt>Status</dt>
<dd>
<em>Live</em>
</dd>
</div>
<div>
<dt>Owner</dt>
<dd>Maya Okafor</dd>
</div>
<div>
<dt>Updated</dt>
<dd>2 hours ago</dd>
</div>
</dl>
</div>
</div>
);
}/* A "more" button that opens a menu of actions beside the card, with a nested menu.
A menu is open while the button before it has aria-expanded="true".
The card and the room beside it are only there to show the menu in use. */
.dh-drop-demo {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
container: dh-drop / inline-size;
width: min(524px, 100%);
min-height: 200px;
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-drop-demo :is(p, dl, dd) {
margin: 0;
}
.dh-drop-card {
width: min(300px, 100%);
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-drop-card header {
display: flex;
align-items: center;
gap: 10px;
padding: 12px 12px 12px 14px;
}
.dh-drop-card header > i {
display: grid;
flex: none;
place-items: center;
width: 28px;
height: 28px;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000);
color: var(--dh-on-accent, #fff);
font-size: 12px;
font-style: normal;
font-weight: 500;
}
.dh-drop-card header p {
display: grid;
flex: 1;
min-width: 0;
}
.dh-drop-card header b {
font-size: 14px;
font-weight: 500;
line-height: 18px;
letter-spacing: -0.015em;
}
.dh-drop-card header span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-drop-card dl {
padding: 2px 14px 6px;
border-top: 1px solid var(--line);
}
.dh-drop-card dl div {
display: flex;
justify-content: space-between;
padding: 7px 0;
}
.dh-drop-card dl div + div {
border-top: 1px solid var(--line);
}
.dh-drop-card dt {
color: var(--dh-muted, #5c5c5c);
}
.dh-drop-card dd em {
padding: 1px 5px;
border-radius: 4px;
background: color-mix(in srgb, #1fa750 14%, transparent);
color: color-mix(in srgb, #1fa750 70%, var(--dh-fg, #171717));
font-size: 11px;
font-style: normal;
font-weight: 500;
}
.dh-drop {
position: relative;
flex: none;
}
.dh-drop button {
border: 0;
background: none;
color: inherit;
font: inherit;
text-align: left;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
}
.dh-drop svg {
flex: none;
width: 14px;
height: 14px;
fill: currentColor;
}
.dh-drop .dh-drop-trigger {
display: grid;
place-items: center;
width: 24px;
height: 24px;
padding: 0;
border-radius: 7px;
color: var(--dh-faint, #8a8a8a);
transition:
transform 150ms var(--ease),
background-color 150ms var(--ease),
color 150ms var(--ease);
}
.dh-drop .dh-drop-trigger:active {
transform: scale(0.94);
}
.dh-drop .dh-drop-trigger[aria-expanded="true"] {
background: color-mix(in srgb, var(--dh-fg, #171717) 8%, transparent);
color: var(--dh-fg, #171717);
}
.dh-drop .dh-drop-trigger:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-drop-menu {
position: absolute;
top: -4px;
left: calc(100% + 20px);
z-index: 10;
display: grid;
gap: 1px;
width: 200px;
padding: 4px;
border-radius: 10px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent),
0 14px 28px -14px color-mix(in srgb, var(--dh-fg, #171717) 28%, transparent);
opacity: 0;
visibility: hidden;
transform: scale(0.97);
transform-origin: 0 0;
transition:
opacity 120ms var(--ease),
transform 120ms var(--ease),
visibility 0s 120ms;
}
.dh-drop [aria-expanded="true"] + .dh-drop-menu {
opacity: 1;
visibility: visible;
transform: none;
transition-duration: 150ms, 150ms, 0s;
transition-delay: 0s;
}
.dh-drop-menu [role="menuitem"] {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
height: 28px;
padding: 0 6px 0 8px;
border-radius: 6px;
letter-spacing: -0.01em;
white-space: nowrap;
transition: background-color 150ms var(--ease);
}
.dh-drop-menu [role="menuitem"] > svg:first-child {
color: var(--dh-faint, #8a8a8a);
}
.dh-drop-menu [role="menuitem"] span {
flex: 1;
}
.dh-drop-menu [role="menuitem"]:focus-visible,
.dh-drop-menu [role="menuitem"][aria-expanded="true"] {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
}
.dh-drop-menu [data-kind="danger"],
.dh-drop-menu [data-kind="danger"] > svg:first-child {
color: #d92d20;
}
.dh-drop-menu [data-kind="danger"]:focus-visible {
background: color-mix(in srgb, #d92d20 10%, transparent);
}
.dh-drop kbd {
display: grid;
place-items: center;
min-width: 18px;
height: 18px;
padding: 0 4px;
border-radius: 5px;
box-shadow: 0 0 0 1px var(--line);
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 1;
}
.dh-drop hr {
width: auto;
height: 0;
margin: 3px -4px;
border: 0;
border-top: 1px solid var(--line);
}
/* The nested menu opens beside its item, over the card. */
.dh-drop-sub {
position: relative;
}
.dh-drop-sub > .dh-drop-menu {
top: -4px;
right: calc(100% + 6px);
left: auto;
width: 164px;
transform-origin: 100% 0;
}
/* Covers the gap, so the pointer can cross to the nested menu without closing it. */
.dh-drop-sub > .dh-drop-menu::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 100%;
width: 10px;
}
.dh-drop .dh-drop-arrow {
width: 12px;
height: 12px;
color: var(--dh-faint, #8a8a8a);
}
/* With no room beside the card, the menu hangs from the button's right edge
and the nested menu opens under its item. */
@container dh-drop (max-width: 480px) {
.dh-drop > .dh-drop-menu {
top: calc(100% + 6px);
right: 0;
left: auto;
transform-origin: 100% 0;
}
.dh-drop-sub > .dh-drop-menu {
top: calc(100% + 2px);
right: 8px;
}
}
@media (hover: hover) and (pointer: fine) {
.dh-drop .dh-drop-trigger:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
color: var(--dh-fg, #171717);
}
.dh-drop-menu [role="menuitem"]:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
}
.dh-drop-menu [data-kind="danger"]:hover {
background: color-mix(in srgb, #d92d20 10%, transparent);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-drop-menu,
.dh-drop .dh-drop-trigger:active {
transform: none;
}
}<div class="dh-drop-demo">
<div class="dh-drop-card">
<header>
<i aria-hidden="true">W</i>
<p><b>Website redesign</b><span>example.com</span></p>
<div class="dh-drop">
<button type="button" class="dh-drop-trigger" aria-label="Project options" aria-haspopup="menu" aria-expanded="true" aria-controls="dh-drop-menu"><svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.250 6.500a1.500 1.500 0 1 1 0 3 1.500 1.500 0 0 1 0-3Zm4.750 0a1.500 1.500 0 1 1 0 3 1.500 1.500 0 0 1 0-3Zm4.750 0a1.500 1.500 0 1 1 0 3 1.500 1.500 0 0 1 0-3Z" /></svg></button>
<div class="dh-drop-menu" id="dh-drop-menu" role="menu" aria-label="Project options">
<button type="button" role="menuitem" tabindex="-1"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M11.5 1.8a1.6 1.6 0 0 1 2.3 0l.4.4a1.6 1.6 0 0 1 0 2.3L6.1 12.6a1 1 0 0 1-.4.25l-3 1a.6.6 0 0 1-.76-.76l1-3a1 1 0 0 1 .25-.4l8.3-8.3Z" /></svg><span>Rename</span><kbd>R</kbd></button>
<button type="button" role="menuitem" tabindex="-1"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M5.5 1.5h6A2.5 2.5 0 0 1 14 4v6a1 1 0 0 1-1 1h-.5V5.5a2 2 0 0 0-2-2H4.5v-1a1 1 0 0 1 1-1ZM2 6.5A1.5 1.5 0 0 1 3.5 5h6A1.5 1.5 0 0 1 11 6.5v6A1.5 1.5 0 0 1 9.5 14h-6A1.5 1.5 0 0 1 2 12.5v-6Z" /></svg><span>Duplicate</span><kbd>⌘D</kbd></button>
<div class="dh-drop-sub">
<button type="button" role="menuitem" tabindex="-1" aria-haspopup="menu" aria-expanded="false"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M14.3 1.7a.6.6 0 0 1 .1.63l-4.6 11.5a.6.6 0 0 1-1.1.03L6.6 9.4 2.14 7.3a.6.6 0 0 1 .03-1.1l11.5-4.6a.6.6 0 0 1 .63.1Z" /></svg><span>Share</span><svg class="dh-drop-arrow" viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M6.280 3.220a.75.75 0 0 0-1.060 1.060L8.940 8l-3.720 3.720a.75.75 0 1 0 1.060 1.060l4.250-4.250a.75.75 0 0 0 0-1.060L6.280 3.220Z" /></svg></button>
<div class="dh-drop-menu" role="menu" aria-label="Share">
<button type="button" role="menuitem" tabindex="-1"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M6.5 4.5a.75.75 0 0 1 0 1.5H5a2 2 0 1 0 0 4h1.5a.75.75 0 0 1 0 1.5H5a3.5 3.5 0 1 1 0-7h1.5Zm3 0H11a3.5 3.5 0 1 1 0 7H9.5a.75.75 0 0 1 0-1.5H11a2 2 0 1 0 0-4H9.5a.75.75 0 0 1 0-1.5ZM5.250 8A.75.75 0 0 1 6 7.250h4a.75.75 0 0 1 0 1.500H6A.75.75 0 0 1 5.250 8Z" /></svg><span>Copy link</span></button>
<button type="button" role="menuitem" tabindex="-1"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M1.5 4.5a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v.3L8 8.600 1.500 4.800v-.3Zm0 2.050V11.500a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2V6.550L8.380 10.100a.75.75 0 0 1-.76 0L1.500 6.550Z" /></svg><span>Send by email</span></button>
<button type="button" role="menuitem" tabindex="-1"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2a2.750 2.750 0 1 1 0 5.500A2.750 2.750 0 0 1 7 2ZM2 12.500C2 10.500 4.200 9 7 9c1 0 1.900.2 2.700.5v1.250a1.750 1.750 0 0 0 .8 3.250H3a1 1 0 0 1-1-1v-.5Zm10.250-3.500a.75.75 0 0 1 .75.75v1h1a.75.75 0 0 1 0 1.500h-1v1a.75.75 0 0 1-1.500 0v-1h-1a.75.75 0 0 1 0-1.500h1v-1a.75.75 0 0 1 .75-.75Z" /></svg><span>Invite people</span></button>
</div>
</div>
<hr />
<button type="button" role="menuitem" tabindex="-1"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M1.5 3a1 1 0 0 1 1-1h11a1 1 0 0 1 1 1v1.5a1 1 0 0 1-1 1h-11a1 1 0 0 1-1-1V3Zm1 3.5h11v5A2.5 2.5 0 0 1 11 14H5a2.5 2.5 0 0 1-2.5-2.5v-5Zm3.750 1.750a.75.75 0 0 0 0 1.500h3.500a.75.75 0 0 0 0-1.500h-3.500Z" /></svg><span>Archive</span><kbd>E</kbd></button>
<hr />
<button type="button" role="menuitem" tabindex="-1" data-kind="danger"><svg viewBox="0 0 16 16" aria-hidden="true"><path fill-rule="evenodd" d="M6.5 1.5h3a1 1 0 0 1 1 1V3H13a.75.75 0 0 1 0 1.5h-.3l-.55 8.2a1.5 1.5 0 0 1-1.5 1.400H5.350a1.5 1.5 0 0 1-1.500-1.400L3.300 4.500H3A.75.75 0 0 1 3 3h2.500v-.5a1 1 0 0 1 1-1Z" /></svg><span>Delete</span><kbd>⌫</kbd></button>
</div>
</div>
</header>
<dl>
<div><dt>Status</dt><dd><em>Live</em></dd></div>
<div><dt>Owner</dt><dd>Maya Okafor</dd></div>
<div><dt>Updated</dt><dd>2 hours ago</dd></div>
</dl>
</div>
</div>/* A "more" button that opens a menu of actions beside the card, with a nested menu.
A menu is open while the button before it has aria-expanded="true".
The card and the room beside it are only there to show the menu in use. */
.dh-drop-demo {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
container: dh-drop / inline-size;
width: min(524px, 100%);
min-height: 200px;
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-drop-demo :is(p, dl, dd) {
margin: 0;
}
.dh-drop-card {
width: min(300px, 100%);
border-radius: 14px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
}
.dh-drop-card header {
display: flex;
align-items: center;
gap: 10px;
padding: 12px 12px 12px 14px;
}
.dh-drop-card header > i {
display: grid;
flex: none;
place-items: center;
width: 28px;
height: 28px;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000);
color: var(--dh-on-accent, #fff);
font-size: 12px;
font-style: normal;
font-weight: 500;
}
.dh-drop-card header p {
display: grid;
flex: 1;
min-width: 0;
}
.dh-drop-card header b {
font-size: 14px;
font-weight: 500;
line-height: 18px;
letter-spacing: -0.015em;
}
.dh-drop-card header span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-drop-card dl {
padding: 2px 14px 6px;
border-top: 1px solid var(--line);
}
.dh-drop-card dl div {
display: flex;
justify-content: space-between;
padding: 7px 0;
}
.dh-drop-card dl div + div {
border-top: 1px solid var(--line);
}
.dh-drop-card dt {
color: var(--dh-muted, #5c5c5c);
}
.dh-drop-card dd em {
padding: 1px 5px;
border-radius: 4px;
background: color-mix(in srgb, #1fa750 14%, transparent);
color: color-mix(in srgb, #1fa750 70%, var(--dh-fg, #171717));
font-size: 11px;
font-style: normal;
font-weight: 500;
}
.dh-drop {
position: relative;
flex: none;
}
.dh-drop button {
border: 0;
background: none;
color: inherit;
font: inherit;
text-align: left;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
}
.dh-drop svg {
flex: none;
width: 14px;
height: 14px;
fill: currentColor;
}
.dh-drop .dh-drop-trigger {
display: grid;
place-items: center;
width: 24px;
height: 24px;
padding: 0;
border-radius: 7px;
color: var(--dh-faint, #8a8a8a);
transition:
transform 150ms var(--ease),
background-color 150ms var(--ease),
color 150ms var(--ease);
}
.dh-drop .dh-drop-trigger:active {
transform: scale(0.94);
}
.dh-drop .dh-drop-trigger[aria-expanded="true"] {
background: color-mix(in srgb, var(--dh-fg, #171717) 8%, transparent);
color: var(--dh-fg, #171717);
}
.dh-drop .dh-drop-trigger:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-drop-menu {
position: absolute;
top: -4px;
left: calc(100% + 20px);
z-index: 10;
display: grid;
gap: 1px;
width: 200px;
padding: 4px;
border-radius: 10px;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent),
0 14px 28px -14px color-mix(in srgb, var(--dh-fg, #171717) 28%, transparent);
opacity: 0;
visibility: hidden;
transform: scale(0.97);
transform-origin: 0 0;
transition:
opacity 120ms var(--ease),
transform 120ms var(--ease),
visibility 0s 120ms;
}
.dh-drop [aria-expanded="true"] + .dh-drop-menu {
opacity: 1;
visibility: visible;
transform: none;
transition-duration: 150ms, 150ms, 0s;
transition-delay: 0s;
}
.dh-drop-menu [role="menuitem"] {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
height: 28px;
padding: 0 6px 0 8px;
border-radius: 6px;
letter-spacing: -0.01em;
white-space: nowrap;
transition: background-color 150ms var(--ease);
}
.dh-drop-menu [role="menuitem"] > svg:first-child {
color: var(--dh-faint, #8a8a8a);
}
.dh-drop-menu [role="menuitem"] span {
flex: 1;
}
.dh-drop-menu [role="menuitem"]:focus-visible,
.dh-drop-menu [role="menuitem"][aria-expanded="true"] {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
}
.dh-drop-menu [data-kind="danger"],
.dh-drop-menu [data-kind="danger"] > svg:first-child {
color: #d92d20;
}
.dh-drop-menu [data-kind="danger"]:focus-visible {
background: color-mix(in srgb, #d92d20 10%, transparent);
}
.dh-drop kbd {
display: grid;
place-items: center;
min-width: 18px;
height: 18px;
padding: 0 4px;
border-radius: 5px;
box-shadow: 0 0 0 1px var(--line);
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 1;
}
.dh-drop hr {
width: auto;
height: 0;
margin: 3px -4px;
border: 0;
border-top: 1px solid var(--line);
}
/* The nested menu opens beside its item, over the card. */
.dh-drop-sub {
position: relative;
}
.dh-drop-sub > .dh-drop-menu {
top: -4px;
right: calc(100% + 6px);
left: auto;
width: 164px;
transform-origin: 100% 0;
}
/* Covers the gap, so the pointer can cross to the nested menu without closing it. */
.dh-drop-sub > .dh-drop-menu::before {
content: "";
position: absolute;
top: 0;
bottom: 0;
left: 100%;
width: 10px;
}
.dh-drop .dh-drop-arrow {
width: 12px;
height: 12px;
color: var(--dh-faint, #8a8a8a);
}
/* With no room beside the card, the menu hangs from the button's right edge
and the nested menu opens under its item. */
@container dh-drop (max-width: 480px) {
.dh-drop > .dh-drop-menu {
top: calc(100% + 6px);
right: 0;
left: auto;
transform-origin: 100% 0;
}
.dh-drop-sub > .dh-drop-menu {
top: calc(100% + 2px);
right: 8px;
}
}
@media (hover: hover) and (pointer: fine) {
.dh-drop .dh-drop-trigger:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
color: var(--dh-fg, #171717);
}
.dh-drop-menu [role="menuitem"]:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 6%, transparent);
}
.dh-drop-menu [data-kind="danger"]:hover {
background: color-mix(in srgb, #d92d20 10%, transparent);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-drop-menu,
.dh-drop .dh-drop-trigger:active {
transform: none;
}
}/* Opens and closes the menu and its nested menu. The arrow keys move through
the items, right and left go into and out of the nested menu, and Escape
or a click anywhere else closes everything. */
document.querySelectorAll(".dh-drop").forEach((drop) => {
const trigger = drop.querySelector(".dh-drop-trigger");
const menu = drop.querySelector(".dh-drop-menu");
const subs = Array.from(drop.querySelectorAll(".dh-drop-sub"));
const fine = matchMedia("(hover: hover) and (pointer: fine)");
const items = (list) => Array.from(list.children).map((child) => (child.matches(".dh-drop-sub") ? child.firstElementChild : child)).filter((child) => child.matches('[role="menuitem"]'));
const setSub = (sub, open) => sub.firstElementChild.setAttribute("aria-expanded", String(open));
const set = (open) => {
trigger.setAttribute("aria-expanded", String(open));
if (!open) subs.forEach((sub) => setSub(sub, false));
};
trigger.addEventListener("click", () => set(trigger.getAttribute("aria-expanded") !== "true"));
trigger.addEventListener("keydown", (event) => {
if (event.key !== "ArrowDown") return;
event.preventDefault();
set(true);
items(menu)[0].focus();
});
menu.addEventListener("click", (event) => {
const item = event.target.closest('[role="menuitem"]');
if (!item) return;
if (item.hasAttribute("aria-haspopup")) {
setSub(item.parentElement, item.getAttribute("aria-expanded") !== "true");
return;
}
set(false);
trigger.focus();
});
menu.addEventListener("keydown", (event) => {
const item = event.target.closest('[role="menuitem"]');
if (!item) return;
const list = items(item.closest(".dh-drop-menu"));
const at = list.indexOf(item);
const sub = item.closest(".dh-drop-sub");
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault();
list[(at + (event.key === "ArrowDown" ? 1 : -1) + list.length) % list.length].focus();
}
if (event.key === "ArrowRight" && item.hasAttribute("aria-haspopup")) {
setSub(sub, true);
items(item.nextElementSibling)[0].focus();
}
if (event.key === "ArrowLeft" && sub && item !== sub.firstElementChild) {
setSub(sub, false);
sub.firstElementChild.focus();
}
if (event.key === "Escape") {
set(false);
trigger.focus();
}
});
/* With a mouse, the nested menu opens while the pointer is over its item. */
subs.forEach((sub) => {
sub.addEventListener("pointerenter", () => fine.matches && setSub(sub, true));
sub.addEventListener("pointerleave", () => fine.matches && setSub(sub, false));
});
document.addEventListener("pointerdown", (event) => {
if (!drop.contains(event.target)) set(false);
});
});Dropdown menu is not in the Figma file yet.
Use the React or HTML code for now.