Accordion examples
Eine eingeklappbare Liste auf Basis nativer details- und summary-Elemente, ganz ohne JavaScript.
Eine eingeklappbare Liste auf Basis nativer details- und summary-Elemente. Das Ein- und Ausklappen, die Tastaturbedienung und die korrekte Semantik kommen direkt vom Browser, sodass die Komponente komplett ohne JavaScript auskommt.
Das Chevron rotiert über die group-open-Variante, was bedeutet, dass der geöffnete Zustand in CSS gestylt wird, statt im State getrackt zu werden.
Accordion with dividers
The default FAQ layout: rows separated by hairlines, with a chevron that rotates when a row opens. No JavaScript.
export default function Accordion() {
return (
<div className="mx-auto max-w-2xl divide-y divide-gray-200">
<details className="group py-4" open>
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do I need to install anything?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Can I use these commercially?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do the components work without JavaScript?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Which frameworks are supported?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
</details>
</div>
);
}Single-open accordion
Every details element shares one name attribute, so opening a row closes the others. The browser handles it, with no script.
export default function ExclusiveAccordion() {
return (
<div className="mx-auto max-w-2xl divide-y divide-gray-200">
<details className="group py-4" name="faq" open>
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do I need to install anything?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
</details>
<details className="group py-4" name="faq">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Can I use these commercially?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
</details>
<details className="group py-4" name="faq">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do the components work without JavaScript?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
</details>
<details className="group py-4" name="faq">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Which frameworks are supported?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
</details>
</div>
);
}Accordion with bordered cards
Each row is its own rounded card that tints when open. Reads better when the questions are unrelated rather than one continuous list.
export default function CardAccordion() {
return (
<div className="mx-auto max-w-2xl space-y-3">
<details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50" open>
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do I need to install anything?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
</details>
<details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Can I use these commercially?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
</details>
<details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do the components work without JavaScript?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
</details>
<details className="group rounded-lg border border-gray-200 px-4 py-3 open:bg-gray-50">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Which frameworks are supported?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
</details>
</div>
);
}Accordion with plus and minus icons
A plus that becomes a minus using group-open. Clearer than a chevron when the open state needs to be obvious from a distance.
export default function PlusMinusAccordion() {
return (
<div className="mx-auto max-w-2xl divide-y divide-gray-200">
<details className="group py-4" open>
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do I need to install anything?
<svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
<svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Can I use these commercially?
<svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
<svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do the components work without JavaScript?
<svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
<svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Which frameworks are supported?
<svg className="h-5 w-5 shrink-0 text-gray-400 group-open:hidden" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M12 4.5v15m7.5-7.5h-15" /></svg>
<svg className="hidden h-5 w-5 shrink-0 text-gray-400 group-open:block" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="M19.5 12h-15" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
</details>
</div>
);
}FAQ section with heading beside the accordion
A heading and support line on the left and the accordion on the right at lg, stacked on smaller screens. Suits a full FAQ section on a landing page.
export default function FaqSection() {
return (
<section className="mx-auto grid max-w-5xl gap-10 px-4 py-12 lg:grid-cols-3">
<div className="bg-gray-900 p-4 rounded-lg">
<h2 className="text-2xl font-bold text-gray-100">Frequently asked questions</h2>
<p className="mt-3 text-sm text-white">Cannot find an answer? Email us and we will reply within a day.</p>
</div>
<div className="divide-y divide-gray-200 lg:col-span-2">
<details className="group py-4" open>
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do I need to install anything?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">No. Every component is plain markup you copy into your project.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Can I use these commercially?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Yes. No attribution is required and there is no license fee.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Do the components work without JavaScript?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Most do. Anything that needs state, like a menu or a dialog, uses a few lines of script.</p>
</details>
<details className="group py-4">
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 rounded-sm font-medium text-gray-900 outline-none focus-visible:ring-2 focus-visible:ring-gray-900 focus-visible:ring-offset-2 [&::-webkit-details-marker]:hidden">
Which frameworks are supported?
<svg className="h-5 w-5 shrink-0 text-gray-400 transition group-open:rotate-180" fill="none" stroke="currentColor" strokeWidth="1.5" viewBox="0 0 24 24" aria-hidden="true"><path strokeLinecap="round" strokeLinejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" /></svg>
</summary>
<p className="mt-3 text-sm text-gray-600">Every component comes as plain HTML and as a React component, both styled with Tailwind CSS.</p>
</details>
</div>
</section>
);
}What is a Accordion?
Ein Accordion ist eine vertikale Liste, bei der jede Zeile eine anklickbare Überschrift hat, um deren Inhalt einzublenden. Es ist ein Werkzeug zur Informationsverdichtung: Es ermöglicht einer Seite, zwanzig Überschriften im Platz von drei Absätzen anzuzeigen, und lässt Leser nur die Abschnitte öffnen, die für sie relevant sind. FAQ-Bereiche sind der häufigste Anwendungsfall, aber dasselbe Muster eignet sich auch für Einstellungs-Panels, Filtergruppen und jede Liste, bei der die meisten Leser die Inhalte standardmäßig eingeklappt sehen möchten.
Dieses Muster wird ständig in JavaScript nachgebaut, was fast immer unnötig ist. HTML bietet details- und summary-Elemente, die genau das tun: summary ist die anklickbare Überschrift, alles andere innerhalb von details ist das Panel, und der Browser kümmert sich um das Umschalten, die Tastaturaktivierung, den expandierten Zustand im Accessibility-Baum sowie die Fähigkeit der Browsersuche (Find-in-Page), einen eingeklappten Abschnitt bei einem Treffer automatisch zu öffnen. Ein JavaScript-Accordion muss all das und typischerweise das Meiste davon fehlerhaft neu implementieren.
Das Einzige, was details standardmäßig nicht tut, ist Geschwisterelemente zu schließen, wenn eines geöffnet wird. Dieses Verhalten - oft als exklusives Accordion bezeichnet - ist jetzt verfügbar, indem man jedem details-Element dasselbe name-Attribut gibt, welches Browser als Gruppe behandeln. Ob du das möchtest, ist eher eine Design- als eine technische Frage: Exklusive Accordions halten die Seite kurz, machen es aber unmöglich, zwei Zeilen miteinander zu vergleichen, ohne eine davon wieder zu öffnen.
Das Styling von details war früher unschön wegen des standardmäßigen Aufklappdreiecks, welches ein Listenmarker bei summary ist. Das Entfernen erfordert marker:content-none in Tailwind sowie die Webkit-spezifische Variante auf älteren Safari-Versionen. Sobald es verschwunden ist, ist summary ein ganznormaler Flex-Container und das benutzerdefinierte Chevron kann frei platziert werden.
Anatomy
- Container
- Der Wrapper, der die Zeilen umschließt und die Trennlinien sowie die maximale Breite bereitstellt. Begrenze die Breite für eine bessere Lesbarkeit - vollflächige FAQ-Antworten sind schwer zu erfassen.
- Details
- Eines pro Zeile. Die group-Klasse sorgt hier dafür, dass das Chevron über die group-open-Variante auf den geöffneten Zustand reagieren kann.
- Summary
- Die anklickbare Überschrift. Ist bereits fokussierbar und per Tastatur aktivierbar, weshalb sie weder eine role, noch ein tabindex oder einen Key-Handler benötigt.
- Indicator
- Das Chevron, rotiert mit group-open. Ist als aria-hidden markiert, da der erweiterte Zustand bereits vom Element selbst ausgegeben wird.
- Panel
- Alles innerhalb von details nach summary. Jedes Markup funktioniert hier - Absätze, Listen, Formulare, andere Komponenten.
How to Use
- 1
Markup kopieren
Nimm eine beliebige Version. Die React-Version ist eine einfache Funktionskomponente ohne State und ohne use client-Direktive, sodass sie als Server Component im Next.js App Router funktioniert.
- 2
Zeilen ersetzen
Bearbeite das items-Array oder dupliziere einen details-Block in der HTML-Version. Panels akzeptieren jedes Markup, nicht nur Absätze.
- 3
Festlegen, was geöffnet startet
Füge das open-Attribut der Zeile hinzu, die beim Laden erweitert sein soll. Das Öffnen der ersten Zeile macht sofort deutlich, dass die Zeilen interaktiv sind.
- 4
Exklusiv ja oder nein wählen
Gib jedem details dasselbe name-Attribut, wenn das Öffnen eines Elements die anderen schließen soll. Lass das Attribut weg, wenn Leser Zeilen miteinander vergleichen möchten.
- 5
Summary stylen
Entferne den Standardmarker mit marker:content-none und behandle summary anschließend als normalen Flex-Container für Beschriftung und Indikator.
Features
Kein JavaScript
Das Umschalten, die Tastaturaktivierung und der Zustand werden vom Browser verarbeitet, sodass die Komponente vor der Hydration funktioniert und auch dann weiterarbeitet, wenn Skripte fehlschlagen.
Korrekte Semantik standardmäßig
Der erweiterte Zustand wird automatisch an assistierende Technologien übergeben, ganz ohne aria-expanded, das gesetzt oder synchron gehalten werden muss.
Auffindbar im eingeklappten Zustand
Die browserspezifische Volltextsuche kann ein eingeklapptes Panel öffnen, um einen Treffer anzuzeigen – etwas, das ein aus Divs gebautes JavaScript-Akkordeon nicht leisten kann.
CSS-basierter Indikator
Die Drehung des Chevron-Symbols stammt von der group-open-Variante, sodass der geöffnete Zustand das Styling direkt steuert und nicht über eine Zustandsvariable läuft.
Exklusivmodus ohne Code
Ein gemeinsames name-Attribut sorgt dafür, dass sich die Gruppe wie ein Akkordeon verhält, bei dem immer nur ein Element geöffnet ist – komplett vom Browser gesteuert.
Server-Komponenten-freundlich
Ohne Zustand und ohne Effekte rendert die React-Version im Next.js App Router auf dem Server, ganz ohne use client-Direktive.
Common Uses
FAQ-Abschnitte
Der häufigste Anwendungsfall. Zwanzig Fragen passen auf eine Bildschirmseite an Überschriften, und Leser öffnen nur die zwei, die für sie relevant sind.
Filtergruppen
Einklappbare Kategorien-, Preis- und Attributgruppen in einer Produkt-Sidebar, damit ein langes Filterset auch auf kleinen Bildschirmen bedienbar bleibt.
Einstellungs-Panels
Das Gruppieren erweiterter Optionen hinter einer eingeklappten Zeile hält eine Einstellungsseite übersichtlich, ohne die Optionen komplett zu verstecken.
Dokumentations-Details
Das Verstecken vollständiger API-Tabellen, langer Fehlermeldungen oder optionaler Einrichtungsschritte hinter einer Disclosure, damit der Hauptfluss lesbar bleibt.
Accessibility
- Summary ist fokussierbar und wird standardmäßig mit Enter und Leertaste aktiviert, ohne dass tabindex oder ein Key-Handler erforderlich sind.
- Der erweiterte und eingeklappte Zustand wird vom Element selbst an assistierende Technologien übermittelt, sodass kein aria-expanded gepflegt werden muss.
- Das Chevron ist als aria-hidden markiert, da es einen Zustand dupliziert, der bereits angesagt wird; ohne dies würden Screenreader-Nutzer in jeder Zeile eine bedeutungslose Grafik hören.
- Eingeklappte Inhalte verbleiben im Dokument und bleiben über die Browsersuche auffindbar – genau so finden viele Menschen Inhalte auf einer langen FAQ-Seite.
- Wenn die Überschrift einer Zeile auch eine Dokumentenüberschrift sein soll, platzieren Sie ein Überschriftenelement innerhalb von summary anstatt um details herum – die umgekehrte Verschachtelung ist ungültig.
- Der Standard-Fokusring auf summary geht beim Styling oft versehentlich verloren. Behalten Sie einen sichtbaren focus-visible-Stil bei, da sonst Tastaturnutzer die Orientierung verlieren, in welcher Zeile sie sich befinden.
Customization
- Entfernen Sie das Standard-Dreieck mit marker:content-none und fügen Sie die webkit details-marker-Variante hinzu, falls Sie ältere Safari-Versionen unterstützen.
- Ersetzen Sie Trennlinien durch gerahmte Cards, indem Sie jedes details in einen eigenen abgerundeten Container mit eigenem Rahmen packen.
- Ändern Sie das Chevron in ein Plus und Minus, indem Sie beide Icons rendern und deren Sichtbarkeit mit group-open umschalten, was aus der Entfernung besser lesbar ist.
- Das Animieren der Panel-Höhe ist nach wie vor umständlich, da sich details sofort umschaltet. Der Ansatz mit interpolate-size und calc-size funktioniert im aktuellen Chrome; betrachten Sie die Animation eher als Verbesserung denn als Voraussetzung.
- Fügen Sie der geöffneten Zeile mit group-open am Container einen dezenten Hintergrund hinzu, was hilft, wenn mehrere Zeilen gleichzeitig geöffnet sind.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| items * | { question: string; answer: string }[] | - | Die Accordion-Zeilen. Beschränke dich auf maximal ein Dutzend; darüber hinaus sind eine durchsuchbare Liste oder separate Seiten für Leser besser geeignet. |
| defaultOpenIndex | number | 0 | Bestimmt, welche Zeile geöffnet startet. Das Öffnen der ersten Zeile signalisiert, dass die Zeilen interaktiv sind; alles geschlossen zu lassen kann wie eine statische Überschriftenliste wirken. |
Why Use These Accordions?
Es ist die Version, bei der am wenigsten schiefgehen kann. Kein Zustand, keine Abhängigkeit, keine Hydration, kein ARIA, das synchron gehalten werden muss, und ein Verhalten, das funktioniert, bevor JavaScript geladen ist und nachdem es fehlgeschlagen ist.
Es bleibt auffindbar. Inhalte innerhalb eines eingeklappten details können weiterhin über die Browsersuche (Find-in-Page) gefunden werden – so navigieren viele Leser tatsächlich durch ein langes FAQ –, während ein auf Divs basierendes Akkordeon diese Inhalte vor der Suche unsichtbar versteckt.
Die React-Version benötigt keine use client-Direktive und wird daher auf dem Server gerendert. Die meisten im Umlauf befindlichen Akkordeon-Komponenten sind rein deshalb Client-Komponenten, weil sie den offenen Zustand verfolgen, den der Browser ohnehin bereits verarbeitet hat.
Tips
- Öffnen Sie die erste Zeile standardmäßig. Eine Spalte geschlossener Überschriften kann wie eine statische Liste wirken, und Leser versuchen nicht immer von sich aus, darauf zu klicken.
- Halten Sie Panels kurz. Wenn eine Antwort mehr als ein paar Absätze umfasst, verlinken Sie auf eine vollständige Seite – lange Panels verschieben nachfolgende Zeilen weit nach unten und erschweren das Scannen der Liste.
- For exclusive behaviour, use the shared name attribute rather than JavaScript. It is a single attribute and the browser handles the rest.
- Write the summary as the question a reader would actually type. FAQ rows are frequently the part of a page that surfaces in search results, and the phrasing is what decides whether they do.
- Do not nest a heading element around details. If you want the question to be a document heading, put the heading inside summary instead.
- Watch the focus ring. It is easy to remove while styling summary, and without it keyboard users cannot tell which row they are on.