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.

Licence questions4

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

Screen
Colour

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>
  );
}
Licence questions4

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