Select
A select with a search box and grouped options that picks one option or several. Type to filter, move with the arrow keys, press Enter to choose and Escape to close.
Request a quote
Service
Extras
Options
Code
import { useEffect, useId, useRef, useState } from "react";
import "./select.css";
/* A select with a search box and grouped options. With multiple, value is a
list and the menu stays open while you pick.
<Select label="Service" groups={[{ label: "Design", options: ["Brand identity"] }]} value={value} onChange={setValue} /> */
export function Select({
label,
groups,
value,
onChange,
multiple = false,
placeholder = "Choose…",
searchLabel = "Search",
defaultOpen = false,
}) {
const id = useId();
const root = useRef(null);
const button = useRef(null);
const search = useRef(null);
const list = useRef(null);
const [open, setOpen] = useState(defaultOpen);
const [words, setWords] = useState("");
const [active, setActive] = useState(multiple ? null : value);
const picked = multiple ? value : value ? [value] : [];
const match = (option) => option.toLowerCase().includes(words.trim().toLowerCase());
const shown = groups
.map((group) => ({ ...group, options: group.options.filter(match) }))
.filter((group) => group.options.length);
const items = shown.flatMap((group) => group.options);
const optionId = (option) => id + "-" + option.replace(/[^a-z0-9]+/gi, "-");
const show = () => {
setWords("");
setActive(picked[0] ?? groups[0]?.options[0] ?? null);
setOpen(true);
};
const hide = (refocus) => {
setOpen(false);
if (refocus) button.current.focus();
};
const choose = (option) => {
if (multiple) {
onChange(picked.includes(option) ? picked.filter((other) => other !== option) : [...picked, option]);
search.current.focus();
} else {
onChange(option);
hide(true);
}
};
/* Focus goes to the search box when the menu opens by hand. */
const opened = useRef(defaultOpen);
useEffect(() => {
if (open && !opened.current) search.current.focus();
opened.current = open;
}, [open]);
/* A press anywhere else closes the menu. */
useEffect(() => {
const away = (event) => {
if (!root.current.contains(event.target)) setOpen(false);
};
document.addEventListener("pointerdown", away);
return () => document.removeEventListener("pointerdown", away);
}, []);
/* Keeps the option the keys act on in view. */
useEffect(() => {
const option = active && document.getElementById(optionId(active));
if (!option || !open) return;
const box = list.current;
const top = option.offsetTop;
const bottom = top + option.offsetHeight;
if (top < box.scrollTop) box.scrollTop = top - 6;
else if (bottom > box.scrollTop + box.clientHeight) box.scrollTop = bottom - box.clientHeight + 6;
}, [active, open]);
const keys = (event) => {
const at = items.indexOf(active);
if (event.key === "ArrowDown") {
event.preventDefault();
setActive(items[(at + 1) % items.length] ?? null);
} else if (event.key === "ArrowUp") {
event.preventDefault();
setActive(items[(at - 1 + items.length) % items.length] ?? null);
} else if (event.key === "Enter") {
event.preventDefault();
if (items.includes(active)) choose(active);
} else if (event.key === "Escape") {
hide(true);
} else if (event.key === "Tab") {
hide(false);
}
};
return (
<div className="dh-select" ref={root} data-open={open ? "" : undefined}>
<span id={id + "-label"}>{label}</span>
<button
type="button"
ref={button}
aria-haspopup="listbox"
aria-expanded={open}
aria-labelledby={id + "-label " + id + "-value"}
onClick={() => (open ? hide(true) : show())}
>
<span id={id + "-value"} data-empty={picked.length ? undefined : ""}>
{picked.join(", ") || placeholder}
</span>
<svg viewBox="0 0 12 12" aria-hidden="true">
<path d="M3 4.500 6 7.500l3-3" />
</svg>
</button>
<div className="dh-select-menu">
<label>
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="M7 2a5 5 0 0 1 3.960 8.050l2.750 2.740a.75.75 0 1 1-1.060 1.060l-2.750-2.740A5 5 0 1 1 7 2Zm0 1.500a3.500 3.500 0 1 0 0 7 3.500 3.500 0 0 0 0-7Z" />
</svg>
<input
type="text"
ref={search}
role="combobox"
aria-expanded="true"
aria-autocomplete="list"
aria-controls={id + "-list"}
aria-activedescendant={items.includes(active) ? optionId(active) : undefined}
aria-label={searchLabel}
placeholder="Search…"
autoComplete="off"
value={words}
onChange={(event) => {
setWords(event.target.value);
const next = groups.flatMap((group) => group.options);
const typed = event.target.value.trim().toLowerCase();
setActive(next.find((option) => option.toLowerCase().includes(typed)) ?? null);
}}
onKeyDown={keys}
/>
</label>
<div
role="listbox"
id={id + "-list"}
ref={list}
aria-labelledby={id + "-label"}
aria-multiselectable={multiple || undefined}
>
{shown.map((group) => (
<div role="group" aria-labelledby={optionId(group.label) + "-group"} key={group.label}>
<p id={optionId(group.label) + "-group"}>{group.label}</p>
{group.options.map((option) => (
<div
role="option"
id={optionId(option)}
key={option}
aria-selected={picked.includes(option)}
data-active={option === active ? "" : undefined}
onPointerMove={() => setActive(option)}
onClick={() => choose(option)}
>
{option}
</div>
))}
</div>
))}
{items.length === 0 && <p className="dh-select-empty">No matches</p>}
</div>
</div>
</div>
);
}
const services = [
{ label: "Design", options: ["Brand identity", "Website design", "Illustration"] },
{ label: "Build", options: ["Website build", "Online shop", "Web app"] },
{ label: "Care", options: ["Hosting", "Maintenance"] },
];
const extras = [
{ label: "Content", options: ["Copywriting", "Photography"] },
{ label: "Growth", options: ["Search set-up", "Analytics", "Newsletter"] },
];
export function Example() {
const [service, setService] = useState("Website design");
const [chosen, setChosen] = useState(["Copywriting", "Analytics"]);
return (
<section className="dh-select-card">
<h3>Request a quote</h3>
<div className="dh-select-row">
<Select
label="Service"
placeholder="Choose a service"
searchLabel="Search services"
groups={services}
value={service}
onChange={setService}
/>
<Select
label="Extras"
placeholder="Choose extras"
searchLabel="Search extras"
groups={extras}
value={chosen}
onChange={setChosen}
multiple
defaultOpen
/>
</div>
<footer>
<span>Most projects start within two weeks.</span>
<button type="button">Continue</button>
</footer>
</section>
);
}/* A select with a search box and grouped options, shown in a small form card.
The same piece picks one option or several: add aria-multiselectable="true"
to the list for several. Add data-open to the outer element to show the menu. */
.dh-select-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
width: 440px;
max-width: 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);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
/* Room for the open menu in a preview. Not needed on a real page. */
.dh-select-card:has(.dh-select[data-open]) {
margin-bottom: 176px;
}
.dh-select-card h3 {
margin: 0;
padding: 14px 16px 12px;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-select-row {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 12px;
padding: 0 16px 16px;
}
.dh-select-card footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 10px 10px 16px;
border-top: 1px solid var(--line);
border-radius: 0 0 14px 14px;
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
}
.dh-select-card footer button {
flex: none;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
font: inherit;
font-size: 13px;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select-card footer button:active {
transform: scale(0.97);
}
.dh-select-card footer button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
.dh-select {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
position: relative;
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 5px;
min-width: 0;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-select > span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-select > button {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
height: 32px;
padding: 0 8px 0 10px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
text-align: left;
outline: none;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select > button:focus-visible,
.dh-select[data-open] > button {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-select > button span {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-select > button span[data-empty] {
color: var(--dh-faint, #8a8a8a);
}
.dh-select > button svg {
flex: none;
width: 12px;
height: 12px;
fill: none;
stroke: var(--dh-faint, #8a8a8a);
stroke-width: 1.4;
stroke-linecap: round;
stroke-linejoin: round;
transition: rotate 250ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select[data-open] > button svg {
rotate: 180deg;
}
/* The menu grows out of the field and fades away faster than it came. */
.dh-select-menu {
position: absolute;
top: calc(100% + 6px);
left: 0;
z-index: 10;
width: 100%;
min-width: 180px;
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);
transform-origin: top;
visibility: hidden;
opacity: 0;
scale: 0.97;
transition:
opacity 120ms cubic-bezier(0.23, 1, 0.32, 1),
scale 120ms cubic-bezier(0.23, 1, 0.32, 1),
visibility 0s 120ms;
}
.dh-select[data-open] .dh-select-menu {
visibility: visible;
opacity: 1;
scale: 1;
transition:
opacity 150ms cubic-bezier(0.23, 1, 0.32, 1),
scale 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select-menu label {
display: flex;
align-items: center;
gap: 7px;
padding: 0 10px;
border-bottom: 1px solid var(--line);
}
.dh-select-menu label svg {
flex: none;
width: 12px;
height: 12px;
fill: var(--dh-faint, #8a8a8a);
}
.dh-select-menu input {
flex: 1;
width: 0;
height: 32px;
padding: 0;
border: 0;
background: none;
color: inherit;
font: inherit;
outline: none;
}
.dh-select-menu input::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-select [role="listbox"] {
position: relative;
max-height: 212px;
padding: 4px;
overflow-y: auto;
overscroll-behavior: contain;
scrollbar-width: thin;
}
.dh-select [role="group"] p {
margin: 0;
padding: 6px 8px 2px;
color: var(--dh-faint, #8a8a8a);
font-size: 11px;
line-height: 16px;
}
.dh-select [role="option"] {
display: flex;
align-items: center;
gap: 8px;
height: 28px;
margin-top: 1px;
padding: 0 8px;
border-radius: 6px;
color: var(--dh-muted, #5c5c5c);
letter-spacing: -0.005em;
cursor: pointer;
transition: background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select [hidden] {
display: none;
}
/* The option under the pointer or the arrow keys. */
.dh-select [role="option"][data-active] {
background: color-mix(in srgb, var(--dh-fg, #171717) 5%, transparent);
color: var(--dh-fg, #171717);
}
/* With one choice, the chosen option is tinted with the accent. */
.dh-select [role="listbox"]:not([aria-multiselectable]) [aria-selected="true"] {
background: color-mix(in srgb, var(--accent) 10%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
}
/* With several, each option has a box that fills when chosen. */
.dh-select [aria-multiselectable] [role="option"]::before {
content: "";
flex: none;
width: 14px;
height: 14px;
border-radius: 4px;
background: var(--dh-raised, #fff) center / 10px no-repeat;
box-shadow:
0 0 0 1px color-mix(in srgb, var(--dh-fg, #171717) 16%, transparent),
0 1px 1px color-mix(in srgb, var(--dh-fg, #171717) 8%, transparent);
transition: background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select [aria-multiselectable] [aria-selected="true"] {
color: var(--dh-fg, #171717);
}
.dh-select [aria-multiselectable] [aria-selected="true"]::before {
background-color: var(--accent);
background-image:
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.500 6.300 5 8.700l4.500-5.200' fill='none' stroke='%23fff' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"),
linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
background-size: 10px, auto;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000);
}
.dh-select-empty {
margin: 0;
padding: 14px 8px;
color: var(--dh-faint, #8a8a8a);
font-size: 12px;
text-align: center;
}
@media (pointer: coarse) {
.dh-select > button,
.dh-select [role="option"] {
height: 44px;
}
.dh-select-menu input {
height: 44px;
font-size: 16px;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-select-menu,
.dh-select[data-open] .dh-select-menu {
scale: 1;
transition: opacity 150ms, visibility 0s;
}
.dh-select > button svg,
.dh-select-card footer button {
transition: none;
}
}<section class="dh-select-card">
<h3>Request a quote</h3>
<div class="dh-select-row">
<div class="dh-select">
<span id="dh-select-service-label">Service</span>
<button type="button" aria-haspopup="listbox" aria-expanded="false" aria-labelledby="dh-select-service-label dh-select-service-value">
<span id="dh-select-service-value" data-placeholder="Choose a service">Website design</span>
<svg viewBox="0 0 12 12" aria-hidden="true"><path d="M3 4.500 6 7.500l3-3" /></svg>
</button>
<div class="dh-select-menu">
<label>
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M7 2a5 5 0 0 1 3.960 8.050l2.750 2.740a.75.75 0 1 1-1.060 1.060l-2.750-2.740A5 5 0 1 1 7 2Zm0 1.500a3.500 3.500 0 1 0 0 7 3.500 3.500 0 0 0 0-7Z" /></svg>
<input type="text" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-controls="dh-select-service-list" aria-label="Search services" placeholder="Search…" autocomplete="off" />
</label>
<div role="listbox" id="dh-select-service-list" aria-labelledby="dh-select-service-label">
<div role="group" aria-labelledby="dh-select-service-group-1">
<p id="dh-select-service-group-1">Design</p>
<div role="option" aria-selected="false">Brand identity</div>
<div role="option" aria-selected="true">Website design</div>
<div role="option" aria-selected="false">Illustration</div>
</div>
<div role="group" aria-labelledby="dh-select-service-group-2">
<p id="dh-select-service-group-2">Build</p>
<div role="option" aria-selected="false">Website build</div>
<div role="option" aria-selected="false">Online shop</div>
<div role="option" aria-selected="false">Web app</div>
</div>
<div role="group" aria-labelledby="dh-select-service-group-3">
<p id="dh-select-service-group-3">Care</p>
<div role="option" aria-selected="false">Hosting</div>
<div role="option" aria-selected="false">Maintenance</div>
</div>
<p class="dh-select-empty" hidden>No matches</p>
</div>
</div>
</div>
<div class="dh-select" data-open>
<span id="dh-select-extras-label">Extras</span>
<button type="button" aria-haspopup="listbox" aria-expanded="true" aria-labelledby="dh-select-extras-label dh-select-extras-value">
<span id="dh-select-extras-value" data-placeholder="Choose extras">Copywriting, Analytics</span>
<svg viewBox="0 0 12 12" aria-hidden="true"><path d="M3 4.500 6 7.500l3-3" /></svg>
</button>
<div class="dh-select-menu">
<label>
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M7 2a5 5 0 0 1 3.960 8.050l2.750 2.740a.75.75 0 1 1-1.060 1.060l-2.750-2.740A5 5 0 1 1 7 2Zm0 1.500a3.500 3.500 0 1 0 0 7 3.500 3.500 0 0 0 0-7Z" /></svg>
<input type="text" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-controls="dh-select-extras-list" aria-label="Search extras" placeholder="Search…" autocomplete="off" />
</label>
<div role="listbox" id="dh-select-extras-list" aria-labelledby="dh-select-extras-label" aria-multiselectable="true">
<div role="group" aria-labelledby="dh-select-extras-group-1">
<p id="dh-select-extras-group-1">Content</p>
<div role="option" aria-selected="true">Copywriting</div>
<div role="option" aria-selected="false">Photography</div>
</div>
<div role="group" aria-labelledby="dh-select-extras-group-2">
<p id="dh-select-extras-group-2">Growth</p>
<div role="option" aria-selected="false">Search set-up</div>
<div role="option" aria-selected="true">Analytics</div>
<div role="option" aria-selected="false">Newsletter</div>
</div>
<p class="dh-select-empty" hidden>No matches</p>
</div>
</div>
</div>
</div>
<footer>
<span>Most projects start within two weeks.</span>
<button type="button">Continue</button>
</footer>
</section>/* A select with a search box and grouped options, shown in a small form card.
The same piece picks one option or several: add aria-multiselectable="true"
to the list for several. Add data-open to the outer element to show the menu. */
.dh-select-card {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
width: 440px;
max-width: 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);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 13px;
line-height: 20px;
text-align: left;
}
/* Room for the open menu in a preview. Not needed on a real page. */
.dh-select-card:has(.dh-select[data-open]) {
margin-bottom: 176px;
}
.dh-select-card h3 {
margin: 0;
padding: 14px 16px 12px;
font-size: 14px;
font-weight: 500;
line-height: 20px;
letter-spacing: -0.015em;
}
.dh-select-row {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
gap: 12px;
padding: 0 16px 16px;
}
.dh-select-card footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 10px 10px 10px 16px;
border-top: 1px solid var(--line);
border-radius: 0 0 14px 14px;
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, var(--dh-raised, #fff));
color: var(--dh-muted, #5c5c5c);
font-size: 12px;
}
.dh-select-card footer button {
flex: none;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 8px;
background: linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
color: var(--dh-on-accent, #fff);
font: inherit;
font-size: 13px;
font-weight: 500;
letter-spacing: -0.01em;
cursor: pointer;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 22%, transparent);
transition: transform 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select-card footer button:active {
transform: scale(0.97);
}
.dh-select-card footer button:focus-visible {
outline: 2px solid var(--dh-faint, #8a8a8a);
outline-offset: 2px;
}
.dh-select {
--accent: var(--dh-accent, #2563ff);
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
position: relative;
display: grid;
grid-template-columns: minmax(0, 1fr);
gap: 5px;
min-width: 0;
color: var(--dh-fg, #171717);
font-size: 13px;
line-height: 20px;
text-align: left;
}
.dh-select > span {
color: var(--dh-muted, #5c5c5c);
font-size: 11px;
line-height: 16px;
}
.dh-select > button {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
height: 32px;
padding: 0 8px 0 10px;
border: 0;
border-radius: 7px;
background: var(--dh-well, #ededeb);
color: inherit;
font: inherit;
text-align: left;
outline: none;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
transition: box-shadow 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select > button:focus-visible,
.dh-select[data-open] > button {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-select > button span {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-select > button span[data-empty] {
color: var(--dh-faint, #8a8a8a);
}
.dh-select > button svg {
flex: none;
width: 12px;
height: 12px;
fill: none;
stroke: var(--dh-faint, #8a8a8a);
stroke-width: 1.4;
stroke-linecap: round;
stroke-linejoin: round;
transition: rotate 250ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select[data-open] > button svg {
rotate: 180deg;
}
/* The menu grows out of the field and fades away faster than it came. */
.dh-select-menu {
position: absolute;
top: calc(100% + 6px);
left: 0;
z-index: 10;
width: 100%;
min-width: 180px;
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);
transform-origin: top;
visibility: hidden;
opacity: 0;
scale: 0.97;
transition:
opacity 120ms cubic-bezier(0.23, 1, 0.32, 1),
scale 120ms cubic-bezier(0.23, 1, 0.32, 1),
visibility 0s 120ms;
}
.dh-select[data-open] .dh-select-menu {
visibility: visible;
opacity: 1;
scale: 1;
transition:
opacity 150ms cubic-bezier(0.23, 1, 0.32, 1),
scale 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select-menu label {
display: flex;
align-items: center;
gap: 7px;
padding: 0 10px;
border-bottom: 1px solid var(--line);
}
.dh-select-menu label svg {
flex: none;
width: 12px;
height: 12px;
fill: var(--dh-faint, #8a8a8a);
}
.dh-select-menu input {
flex: 1;
width: 0;
height: 32px;
padding: 0;
border: 0;
background: none;
color: inherit;
font: inherit;
outline: none;
}
.dh-select-menu input::placeholder {
color: var(--dh-faint, #8a8a8a);
}
.dh-select [role="listbox"] {
position: relative;
max-height: 212px;
padding: 4px;
overflow-y: auto;
overscroll-behavior: contain;
scrollbar-width: thin;
}
.dh-select [role="group"] p {
margin: 0;
padding: 6px 8px 2px;
color: var(--dh-faint, #8a8a8a);
font-size: 11px;
line-height: 16px;
}
.dh-select [role="option"] {
display: flex;
align-items: center;
gap: 8px;
height: 28px;
margin-top: 1px;
padding: 0 8px;
border-radius: 6px;
color: var(--dh-muted, #5c5c5c);
letter-spacing: -0.005em;
cursor: pointer;
transition: background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select [hidden] {
display: none;
}
/* The option under the pointer or the arrow keys. */
.dh-select [role="option"][data-active] {
background: color-mix(in srgb, var(--dh-fg, #171717) 5%, transparent);
color: var(--dh-fg, #171717);
}
/* With one choice, the chosen option is tinted with the accent. */
.dh-select [role="listbox"]:not([aria-multiselectable]) [aria-selected="true"] {
background: color-mix(in srgb, var(--accent) 10%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
}
/* With several, each option has a box that fills when chosen. */
.dh-select [aria-multiselectable] [role="option"]::before {
content: "";
flex: none;
width: 14px;
height: 14px;
border-radius: 4px;
background: var(--dh-raised, #fff) center / 10px no-repeat;
box-shadow:
0 0 0 1px color-mix(in srgb, var(--dh-fg, #171717) 16%, transparent),
0 1px 1px color-mix(in srgb, var(--dh-fg, #171717) 8%, transparent);
transition: background-color 150ms cubic-bezier(0.23, 1, 0.32, 1);
}
.dh-select [aria-multiselectable] [aria-selected="true"] {
color: var(--dh-fg, #171717);
}
.dh-select [aria-multiselectable] [aria-selected="true"]::before {
background-color: var(--accent);
background-image:
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.500 6.300 5 8.700l4.500-5.200' fill='none' stroke='%23fff' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E"),
linear-gradient(color-mix(in srgb, var(--accent) 84%, #fff), var(--accent));
background-size: 10px, auto;
box-shadow:
inset 0 1px 0 rgb(255 255 255 / 0.3),
0 0 0 1px color-mix(in srgb, var(--accent) 78%, #000);
}
.dh-select-empty {
margin: 0;
padding: 14px 8px;
color: var(--dh-faint, #8a8a8a);
font-size: 12px;
text-align: center;
}
@media (pointer: coarse) {
.dh-select > button,
.dh-select [role="option"] {
height: 44px;
}
.dh-select-menu input {
height: 44px;
font-size: 16px;
}
}
@media (prefers-reduced-motion: reduce) {
.dh-select-menu,
.dh-select[data-open] .dh-select-menu {
scale: 1;
transition: opacity 150ms, visibility 0s;
}
.dh-select > button svg,
.dh-select-card footer button {
transition: none;
}
}/* Opens the menu, filters it as you type and moves through it with the
arrow keys. Enter chooses, Escape closes. */
let dhSelectCount = 0;
document.querySelectorAll(".dh-select").forEach((root) => {
const button = root.querySelector("button");
const value = button.querySelector("span");
const search = root.querySelector("input");
const list = root.querySelector("[role=listbox]");
const options = [...list.querySelectorAll("[role=option]")];
const empty = root.querySelector(".dh-select-empty");
const many = list.getAttribute("aria-multiselectable") === "true";
let active = null;
options.forEach((option) => {
dhSelectCount += 1;
option.id = option.id || "dh-select-option-" + dhSelectCount;
});
const chosen = (option) => option.getAttribute("aria-selected") === "true";
const shown = () => options.filter((option) => !option.hidden);
/* Marks one option as the one the keys act on, and keeps it in view. */
const point = (option) => {
if (active) active.removeAttribute("data-active");
active = option || null;
if (!active) return search.removeAttribute("aria-activedescendant");
active.setAttribute("data-active", "");
search.setAttribute("aria-activedescendant", active.id);
const top = active.offsetTop;
const bottom = top + active.offsetHeight;
if (top < list.scrollTop) list.scrollTop = top - 6;
else if (bottom > list.scrollTop + list.clientHeight) list.scrollTop = bottom - list.clientHeight + 6;
};
const write = () => {
const names = options.filter(chosen).map((option) => option.textContent.trim());
value.textContent = names.join(", ") || value.dataset.placeholder;
value.toggleAttribute("data-empty", names.length === 0);
};
const filter = () => {
const words = search.value.trim().toLowerCase();
options.forEach((option) => {
option.hidden = !option.textContent.toLowerCase().includes(words);
});
list.querySelectorAll("[role=group]").forEach((group) => {
group.hidden = !group.querySelector("[role=option]:not([hidden])");
});
empty.hidden = shown().length > 0;
point(shown()[0]);
};
const open = () => {
root.setAttribute("data-open", "");
button.setAttribute("aria-expanded", "true");
search.value = "";
filter();
point(options.find(chosen) || shown()[0]);
search.focus();
};
const close = (refocus) => {
root.removeAttribute("data-open");
button.setAttribute("aria-expanded", "false");
if (refocus) button.focus();
};
const choose = (option) => {
if (many) option.setAttribute("aria-selected", String(!chosen(option)));
else options.forEach((other) => other.setAttribute("aria-selected", String(other === option)));
write();
if (!many) close(true);
};
button.addEventListener("click", () => (root.hasAttribute("data-open") ? close(true) : open()));
search.addEventListener("input", filter);
search.addEventListener("keydown", (event) => {
const items = shown();
const at = items.indexOf(active);
if (event.key === "ArrowDown") {
event.preventDefault();
point(items[(at + 1) % items.length]);
} else if (event.key === "ArrowUp") {
event.preventDefault();
point(items[(at - 1 + items.length) % items.length]);
} else if (event.key === "Enter") {
event.preventDefault();
if (active) choose(active);
} else if (event.key === "Escape") {
close(true);
} else if (event.key === "Tab") {
close(false);
}
});
list.addEventListener("pointermove", (event) => {
const option = event.target.closest("[role=option]");
if (option && option !== active) point(option);
});
list.addEventListener("click", (event) => {
const option = event.target.closest("[role=option]");
if (!option) return;
choose(option);
if (many) search.focus();
});
document.addEventListener("pointerdown", (event) => {
if (!root.contains(event.target)) close(false);
});
write();
if (root.hasAttribute("data-open")) point(options.find(chosen));
});Select is not in the Figma file yet.
Use the React or HTML code for now.