アコーディオン examples
JavaScriptを使わず、ネイティブの details 要素と summary 要素で構築された折りたたみ式リスト。
ネイティブの details 要素と summary 要素で構築された折りたたみ式リスト。開閉の動作、キーボード操作、正しいセマンティクスはすべてブラウザによって処理されるため、JavaScriptを一切含まずに提供されます。
シェブロンは group-open バリアントを使用して回転するため、オープン状態はステートで追跡されるのではなく、CSSでスタイリングされます。
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 アコーディオン?
アコーディオンは、各行をクリック可能な見出しを持ち、クリックするとコンテンツが表示される垂直方向のリストです。これは情報密度を調整するツールであり、3段落分のスペースに20個の見出しを表示し、読者が必要なものだけを開くことができます。FAQセクションが最も一般的な用途ですが、設定パネル、フィルターグループ、およびほとんどの読者にほとんどのコンテンツを折りたたんでおいてほしいあらゆるリストで同じパターンが役立ちます。
このパターンはJavaScriptで頻繁に再実装されますが、ほとんどの場合それは不要です。HTMLにはまさにこれを行う details 要素と summary 要素があります。summary はクリック可能な見出しであり、details 内の他のすべてがパネルとなり、トグル、キーボードによるアクティブ化、アクセシビリティツリーでの expanded 状態、およびブラウザのページ内検索機能による折りたたまれたセクションの自動展開とマッチ箇所の表示をブラウザが処理します。JavaScript製のアコーディオンはこれらすべてを再実装する必要があり、通常はそのほとんどが正しく実装されません。
details がデフォルトで行わない唯一のことは、1つが開いたときに他の兄弟要素を閉じることです。この動作(排他的アコーディオンと呼ばれることが多い)は、すべての details 要素に同じ name attribute を付与することで利用できるようになり、ブラウザはこれらをグループとして扱います。これを必要とするかどうかは技術的な問題ではなく、実際のデザイン上の問題です。排他的アコーディオンはページを短く保ちますが、一方をもう一度開き直さずに2つの行を比較することを不可能にします。
details のスタイリングは、summary のリストマーカーであるデフォルトのディスクロージャー三角形があるため、かつては面倒でした。これを削除するには、Tailwind の marker:content-none と、古い Safari 用の webkit 固有のバリアントが必要です。これがなくなると、summary は通常の flex コンテナとなり、カスタムシェブロンを適切な位置に配置できます。
Anatomy
- Container
- 行を保持し、区切り線と最大幅を適用するラッパー。可読性を高めるために幅を制限してください。全幅のFAQの回答はスキャンしづらいです。
- Details
- 行ごとに1つ。ここの group クラスは、group-open バリアントを通じてシェブロンがオープン状態に応答できるようにするものです。
- Summary
- クリック可能な見出し。すでにフォーカス可能でキーボード操作に対応しているため、role、tabindex、キーハンドラーは不要です。
- Indicator
- group-open で回転するシェブロン。展開状態は要素自体によってすでにアナウンスされるため、aria-hidden がマークされています。
- Panel
- summary の後にある details 内のすべて。段落、リスト、フォーム、その他のコンポーネントなど、任意のマークアップが機能します。
How to Use
- 1
マークアップをコピー
どちらのバージョンでも使用できます。React版はステートや use client ディレクティブを持たないプレーンな関数コンポーネントであるため、Next.js App Router のサーバーコンポーネントとして動作します。
- 2
行を置き換える
items 配列を編集するか、HTML版の details ブロックを複製します。パネルには段落だけでなく、任意のマークアップを使用できます。
- 3
最初に開くものを決める
読み込み時に展開状態にしたい行に open attribute を追加します。最初の行を開いておくと、行がインタラクティブであることが一目でわかります。
- 4
排他的にするかどうかを選択する
1つを開いたときに他のものを閉じるようにしたい場合は、すべての details に同じ name attribute を付与します。読者が行を比較できるようにしたい場合は、属性を省略します。
- 5
summary をスタイリングする
marker:content-none でデフォルトのマーカーを削除し、summary をラベルとインジケーターのための通常の flex コンテナとして扱います。
Features
JavaScriptなし
トグル、キーボード操作、ステートはブラウザによって処理されるため、ハイドレーション前でも動作し、スクリプトの読み込みに失敗した場合でも動作し続けます。
デフォルトで正しいセマンティクス
展開状態は支援技術に自動的に公開されるため、aria-expandedを設定したり同期させたりする必要はありません。
折りたたみ時でも検索可能
ブラウザのページ内検索によって折りたたまれたパネルを開き、一致箇所を表示させることができます。これはdivで作られたJavaScriptのアコーディオンにはできない動作です。
CSS駆動のインジケーター
シェブロンの回転はgroup-openバリアントによって処理されるため、ステート変数を介さず、オープン状態が直接スタイリングを制御します。
コードなしのエクスクルーシブモード(排他制御)
共有のname属性により、グループはブラウザによって完全に制御される「一度に1つだけ開く」アコーディオンとして動作します。
Server Component対応
ステートもエフェクトもないため、React版はuse clientディレクティブなしでNext.jsのApp Router上でサーバーサイドレンダリングされます。
Common Uses
FAQセクション
最も一般的なユースケースです。1つの画面に収まる数の見出しの中に20個の質問があり、読者は自分に関係のある2つだけを開きます。
フィルターグループ
プロダクトのサイドバーにおける折りたたみ可能なカテゴリ、価格、属性グループ。これにより、長いフィルターセットが小さな画面でもナビゲートしやすくなります。
設定パネル
折りたたまれた行の後ろに高度なオプションをグループ化することで、オプションを完全に隠すことなく、設定ページを親しみやすく保ちます。
ドキュメントの詳細
完全なAPIテーブル、長いエラーメッセージ、またはオプションのセットアップ手順をディスクロージャーの背後に隠すことで、メインのフローを読みやすく保ちます。
Accessibility
- summaryはデフォルトでフォーカス可能であり、tabindexやキーハンドラーなしでEnterやSpaceキーでアクティブ化できます。
- 展開および折りたたみ状態は要素自体によって支援技術に公開されるため、維持すべきaria-expandedはありません。
- シェブロンには、すでにアナウンスされているステートを重複して伝えないようにするため、aria-hiddenマークが付けられています。これがなければ、スクリーンリーダーのユーザーは行ごとに意味のないグラフィックを聞かされることになります。
- 折りたたまれたコンテンツはドキュメント内に留まり、ブラウザの検索で見つけられる状態を維持します。これは、長いFAQページで多くのユーザーがコンテンツを探す方法です。
- 行の見出しをドキュメントの見出しとしても機能させたい場合は、detailsの外側ではなくsummaryの内側にheading要素を配置してください。逆のネストは無効です。
- summaryのデフォルトのフォーカスリングは、スタイリングの際に誤って削除されることがよくあります。目に見えるfocus-visibleスタイルを維持しないと、キーボードユーザーが自分がどの行にいるのか分からなくなります。
Customization
- marker:content-noneでデフォルトの三角形を削除し、古いSafariバージョンをサポートする場合はwebkit details-markerバリアントを追加します。
- それぞれのdetailsを独自のボーダーを持つ独自の丸みを帯びたコンテナでラップすることで、区切り線をボーダー付きカードに置き換えます。
- 両方のアイコンを描画し、group-openで表示・非表示を切り替えることで、シェブロンをプラスとマイナスに変更します。これにより、離れた場所からでもより明確に読み取れるようになります。
- detailsは即座にトグルするため、パネルの高さをアニメーション化することは依然として難易度が高いです。interpolate-sizeとcalc-sizeのアプローチは現在のChromeで機能しますが、アニメーションは必須要件ではなく、あくまでエンハンスメントとして扱ってください。
- コンテナにgroup-openを使用してオープン行にさりげない背景色を追加します。これにより、複数の行が一度に開いている場合に視認性が向上します。
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| items * | { question: string; answer: string }[] | - | アコーディオンの行。12個以下に抑えてください。それ以上になる場合は、検索可能なリストや別々のページのセットの方が読者にとって使いやすくなります。 |
| defaultOpenIndex | number | 0 | 最初に展開状態にする行。最初の行を開いておくことで、行がインタラクティブであることを伝えられます。すべて閉じたままにすると、静的な見出しのリストに見えてしまう可能性があります。 |
Why Use These アコーディオンs?
最もトラブルの少ないバージョンです。ステートも、依存関係も、ハイドレーションも、同期すべきARIAもなく、JavaScriptの読み込み前でも失敗した後でも機能する挙動を備えています。
検索可能です。折りたたまれたdetails内のコンテンツは、ブラウザのページ内検索で引き続き見つけることができます。これは多くの読者が実際に長いFAQをナビゲートする方法であり、divベースのアコーディオンはそのようなコンテンツを検索から密かに隠してしまいます。
React版はuse clientディレクティブを必要としないため、サーバー側でレンダリングされます。世の中に存在するほとんどのアコーディオンコンポーネントがクライアントコンポーネントである理由は、単にブラウザがすでに追跡していたオープン状態を自分たちで追跡しているためです。
Tips
- デフォルトで最初の行を開いておきます。閉じられた見出しの列は静的なリストのように読まれてしまい、読者が必ずしもクリックを試みるとは限りません。
- パネルを短く保ちます。回答が数段落を超える場合は、フルページへのリンクを設定してください。長いパネルは後続の行を遠くへ押し下げ、リストのスキャンを困難にします。
- 排他的な挙動を実現するには、JavaScriptではなく、共有の name 属性を使用します。これは単一の属性であり、ブラウザが残りの処理を行います。
- summary には、読者が実際に検索窓に入力するような質問文を記述します。FAQの行は検索結果によく表示されるパーツであり、その文言が実際に表示されるかどうかを左右します。
- details 要素の周囲に見出し要素をネストしないでください。質問を文書の見出しにしたい場合は、代わりに summary の中見出しを配置してください。
- フォーカスリングに注意してください。summary のスタイリング中に削除しやすく、フォーカスリングがないとキーボードユーザーがどの行にいるのか把握できません。