Accordion
Questions on a card that fold open to show their answers, with a chevron that turns. One stays open at a time, or several if you prefer.
Yes. Use them in as many personal and client projects as you like. You only can't resell them as a design kit.
Every design comes as React and as plain HTML and CSS, so it drops into any stack.
No. A link back is welcome, but it is never required.
Write to [email protected] and Maya Okafor will reply within a working day.
Something else? Write to us
Options
Code
import { useId, useState } from "react";
import "./accordion.css";
/* Sections that fold open and shut. One stays open at a time, unless "many" is set.
<Accordion items={[{ title: "Can I use the designs in client work?", content: "Yes." }]} /> */
export function Accordion({ items, many = false, initial = [0] }) {
const id = useId();
const [open, setOpen] = useState(initial);
const toggle = (index) => {
if (open.includes(index)) setOpen(open.filter((other) => other !== index));
else setOpen(many ? [...open, index] : [index]);
};
return (
<div className="dh-acc" data-many={many ? "" : undefined}>
{items.map((item, index) => (
<div className="dh-acc-item" key={item.title}>
<h3>
<button
type="button"
id={id + "q" + index}
aria-expanded={open.includes(index)}
aria-controls={id + "a" + index}
onClick={() => toggle(index)}
>
{item.title}
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="M3.220 5.720a.75.75 0 0 1 1.060 0L8 9.440l3.720-3.720a.75.75 0 1 1 1.060 1.060L8.530 11.030a.75.75 0 0 1-1.060 0L3.220 6.780a.75.75 0 0 1 0-1.060Z" />
</svg>
</button>
</h3>
<div className="dh-acc-panel" id={id + "a" + index} role="region" aria-labelledby={id + "q" + index}>
<div>
<p>{item.content}</p>
</div>
</div>
</div>
))}
</div>
);
}
export function Example() {
return (
<div className="dh-acc-card">
<header>
<b>Licence questions</b>
<span>4</span>
</header>
<Accordion
items={[
{ title: "Can I use the designs in client work?", content: "Yes. Use them in as many personal and client projects as you like. You only can't resell them as a design kit." },
{ title: "Which frameworks do they work with?", content: "Every design comes as React and as plain HTML and CSS, so it drops into any stack." },
{ title: "Do I need to credit Logoipsum?", content: "No. A link back is welcome, but it is never required." },
{ title: "How do I get help?", content: "Write to [email protected] and Maya Okafor will reply within a working day." },
]}
/>
<p className="dh-acc-foot">
Something else? <a href="#">Write to us</a>
</p>
</div>
);
}/* Questions that fold open to show their answers, on a card. A question is
open while its button has aria-expanded="true"; the answer's height is
animated with a grid row that grows from 0fr to 1fr. */
.dh-acc-card {
--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);
overflow: hidden;
width: min(400px, 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;
}
.dh-acc-card :is(h3, p) {
margin: 0;
font: inherit;
}
.dh-acc-card > header {
display: flex;
align-items: center;
gap: 8px;
padding: 14px 16px 12px;
}
.dh-acc-card > header b {
font-size: 14px;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-acc-card > header span {
padding: 0 5px;
border-radius: 4px;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 16px;
}
.dh-acc-item {
border-top: 1px solid var(--line);
}
.dh-acc h3 button {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
width: 100%;
min-height: 40px;
padding: 10px 12px 10px 16px;
border: 0;
background: none;
color: inherit;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
text-align: left;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
transition: background-color 150ms var(--ease);
}
.dh-acc h3 button:focus-visible {
box-shadow: inset 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-acc h3 svg {
flex: none;
box-sizing: content-box;
width: 12px;
height: 12px;
padding: 4px;
border-radius: 6px;
fill: var(--dh-faint, #8a8a8a);
transition:
rotate 300ms var(--ease),
background-color 150ms var(--ease),
fill 150ms var(--ease);
}
.dh-acc h3 button[aria-expanded="true"] svg {
background: color-mix(in srgb, var(--accent) 12%, transparent);
fill: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
rotate: 180deg;
}
.dh-acc-panel {
display: grid;
grid-template-rows: 0fr;
visibility: hidden;
transition:
grid-template-rows 300ms var(--ease),
visibility 0s 300ms;
}
.dh-acc-panel > div {
overflow: hidden;
min-height: 0;
}
.dh-acc-panel p {
padding: 0 44px 14px 16px;
color: var(--dh-muted, #5c5c5c);
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
transition:
opacity 200ms var(--ease),
filter 200ms var(--ease),
transform 200ms var(--ease);
}
.dh-acc-item:has(h3 button[aria-expanded="true"]) .dh-acc-panel {
grid-template-rows: 1fr;
visibility: visible;
transition-delay: 0s;
}
.dh-acc-item:has(h3 button[aria-expanded="true"]) .dh-acc-panel p {
opacity: 1;
filter: none;
transform: none;
transition-duration: 300ms;
}
.dh-acc-foot {
padding: 10px 16px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, transparent);
color: var(--dh-muted, #5c5c5c);
font-size: 12px !important;
}
.dh-acc-foot a {
color: var(--dh-fg, #171717);
text-decoration: underline;
text-decoration-color: color-mix(in srgb, var(--dh-fg, #171717) 30%, transparent);
text-underline-offset: 3px;
border-radius: 3px;
outline: none;
}
.dh-acc-foot a:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
@media (hover: hover) and (pointer: fine) {
.dh-acc h3 button:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, transparent);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-acc-panel,
.dh-acc h3 svg {
transition: none;
}
.dh-acc-panel p {
filter: none;
transform: none;
}
}<div class="dh-acc-card">
<header><b>Licence questions</b><span>4</span></header>
<div class="dh-acc">
<div class="dh-acc-item">
<h3>
<button type="button" id="dh-acc-q1" aria-expanded="true" aria-controls="dh-acc-a1">
Can I use the designs in client work?
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.220 5.720a.75.75 0 0 1 1.060 0L8 9.440l3.720-3.720a.75.75 0 1 1 1.060 1.060L8.530 11.030a.75.75 0 0 1-1.060 0L3.220 6.780a.75.75 0 0 1 0-1.060Z" /></svg>
</button>
</h3>
<div class="dh-acc-panel" id="dh-acc-a1" role="region" aria-labelledby="dh-acc-q1">
<div><p>Yes. Use them in as many personal and client projects as you like. You only can't resell them as a design kit.</p></div>
</div>
</div>
<div class="dh-acc-item">
<h3>
<button type="button" id="dh-acc-q2" aria-expanded="false" aria-controls="dh-acc-a2">
Which frameworks do they work with?
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.220 5.720a.75.75 0 0 1 1.060 0L8 9.440l3.720-3.720a.75.75 0 1 1 1.060 1.060L8.530 11.030a.75.75 0 0 1-1.060 0L3.220 6.780a.75.75 0 0 1 0-1.060Z" /></svg>
</button>
</h3>
<div class="dh-acc-panel" id="dh-acc-a2" role="region" aria-labelledby="dh-acc-q2">
<div><p>Every design comes as React and as plain HTML and CSS, so it drops into any stack.</p></div>
</div>
</div>
<div class="dh-acc-item">
<h3>
<button type="button" id="dh-acc-q3" aria-expanded="false" aria-controls="dh-acc-a3">
Do I need to credit Logoipsum?
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.220 5.720a.75.75 0 0 1 1.060 0L8 9.440l3.720-3.720a.75.75 0 1 1 1.060 1.060L8.530 11.030a.75.75 0 0 1-1.060 0L3.220 6.780a.75.75 0 0 1 0-1.060Z" /></svg>
</button>
</h3>
<div class="dh-acc-panel" id="dh-acc-a3" role="region" aria-labelledby="dh-acc-q3">
<div><p>No. A link back is welcome, but it is never required.</p></div>
</div>
</div>
<div class="dh-acc-item">
<h3>
<button type="button" id="dh-acc-q4" aria-expanded="false" aria-controls="dh-acc-a4">
How do I get help?
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.220 5.720a.75.75 0 0 1 1.060 0L8 9.440l3.720-3.720a.75.75 0 1 1 1.060 1.060L8.530 11.030a.75.75 0 0 1-1.060 0L3.220 6.780a.75.75 0 0 1 0-1.060Z" /></svg>
</button>
</h3>
<div class="dh-acc-panel" id="dh-acc-a4" role="region" aria-labelledby="dh-acc-q4">
<div><p>Write to [email protected] and Maya Okafor will reply within a working day.</p></div>
</div>
</div>
</div>
<p class="dh-acc-foot">Something else? <a href="#">Write to us</a></p>
</div>/* Questions that fold open to show their answers, on a card. A question is
open while its button has aria-expanded="true"; the answer's height is
animated with a grid row that grows from 0fr to 1fr. */
.dh-acc-card {
--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);
overflow: hidden;
width: min(400px, 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;
}
.dh-acc-card :is(h3, p) {
margin: 0;
font: inherit;
}
.dh-acc-card > header {
display: flex;
align-items: center;
gap: 8px;
padding: 14px 16px 12px;
}
.dh-acc-card > header b {
font-size: 14px;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-acc-card > header span {
padding: 0 5px;
border-radius: 4px;
background: color-mix(in srgb, var(--accent) 12%, transparent);
color: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
line-height: 16px;
}
.dh-acc-item {
border-top: 1px solid var(--line);
}
.dh-acc h3 button {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
width: 100%;
min-height: 40px;
padding: 10px 12px 10px 16px;
border: 0;
background: none;
color: inherit;
font: inherit;
font-weight: 500;
letter-spacing: -0.01em;
text-align: left;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
transition: background-color 150ms var(--ease);
}
.dh-acc h3 button:focus-visible {
box-shadow: inset 0 0 0 2px var(--dh-faint, #8a8a8a);
}
.dh-acc h3 svg {
flex: none;
box-sizing: content-box;
width: 12px;
height: 12px;
padding: 4px;
border-radius: 6px;
fill: var(--dh-faint, #8a8a8a);
transition:
rotate 300ms var(--ease),
background-color 150ms var(--ease),
fill 150ms var(--ease);
}
.dh-acc h3 button[aria-expanded="true"] svg {
background: color-mix(in srgb, var(--accent) 12%, transparent);
fill: color-mix(in srgb, var(--accent) 80%, var(--dh-fg, #171717));
rotate: 180deg;
}
.dh-acc-panel {
display: grid;
grid-template-rows: 0fr;
visibility: hidden;
transition:
grid-template-rows 300ms var(--ease),
visibility 0s 300ms;
}
.dh-acc-panel > div {
overflow: hidden;
min-height: 0;
}
.dh-acc-panel p {
padding: 0 44px 14px 16px;
color: var(--dh-muted, #5c5c5c);
opacity: 0;
filter: blur(4px);
transform: translateY(-4px);
transition:
opacity 200ms var(--ease),
filter 200ms var(--ease),
transform 200ms var(--ease);
}
.dh-acc-item:has(h3 button[aria-expanded="true"]) .dh-acc-panel {
grid-template-rows: 1fr;
visibility: visible;
transition-delay: 0s;
}
.dh-acc-item:has(h3 button[aria-expanded="true"]) .dh-acc-panel p {
opacity: 1;
filter: none;
transform: none;
transition-duration: 300ms;
}
.dh-acc-foot {
padding: 10px 16px;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, transparent);
color: var(--dh-muted, #5c5c5c);
font-size: 12px !important;
}
.dh-acc-foot a {
color: var(--dh-fg, #171717);
text-decoration: underline;
text-decoration-color: color-mix(in srgb, var(--dh-fg, #171717) 30%, transparent);
text-underline-offset: 3px;
border-radius: 3px;
outline: none;
}
.dh-acc-foot a:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
@media (hover: hover) and (pointer: fine) {
.dh-acc h3 button:hover {
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, transparent);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-acc-panel,
.dh-acc h3 svg {
transition: none;
}
.dh-acc-panel p {
filter: none;
transform: none;
}
}/* Pressing a question opens its answer and closes the others.
With data-many on the accordion, the others stay as they are. */
document.querySelectorAll(".dh-acc").forEach((accordion) => {
const buttons = Array.from(accordion.querySelectorAll("h3 button"));
buttons.forEach((button) => {
button.addEventListener("click", () => {
const open = button.getAttribute("aria-expanded") !== "true";
if (!accordion.hasAttribute("data-many")) buttons.forEach((other) => other.setAttribute("aria-expanded", "false"));
button.setAttribute("aria-expanded", String(open));
});
});
});Accordion is not in the Figma file yet.
Use the React or HTML code for now.