Accordion examples
नेटिव details और summary एलिमेंट्स से बनी एक collapsible लिस्ट, जिसमें किसी JavaScript की आवश्यकता नहीं होती।
नेटिव details और summary एलिमेंट्स से बनी एक collapsible लिस्ट। एक्सपैंड और कोलैप्स होना, कीबोर्ड सपोर्ट, और सही सेमांटिक्स सब कुछ ब्राउज़र से मिलता है, इसलिए इस कंपोनेंट के साथ कोई JavaScript नहीं आती है।
chevorn group-open वैरिएंट का उपयोग करके घूमता है, जिसका अर्थ है कि ओपन स्टेट को state में ट्रैक करने के बजाय 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 Accordion?
Accordion एक वर्टिकल लिस्ट है जहाँ प्रत्येक रो में एक हेडिंग होती है जिसे आप इसके कंटेंट को देखने के लिए क्लिक कर सकते हैं। यह एक डेंसिटी टूल है: यह एक पेज को तीन पैराग्राफ की जगह में बीस हेडिंग दिखाने देता है, और पाठकों को केवल वही खोलने देता है जो उनके काम की हैं। FAQ सेक्शन इसके सबसे आम उपयोग हैं, लेकिन यही पैटर्न सेटिंग पैनल, फ़िल्टर ग्रुप और ऐसी किसी भी लिस्ट के लिए काम आता है जहाँ अधिकांश पाठक अधिकांश कंटेंट को कोलैप्स रखना चाहते हैं।
इस पैटर्न को लगातार JavaScript में फिर से बनाया जाता है, जिसकी लगभग हमेशा कोई आवश्यकता नहीं होती। HTML में details और summary एलिमेंट्स होते हैं जो बिल्कुल यही काम करते हैं: summary क्लिक करने योग्य हेडिंग है, details के अंदर बाकी सब कुछ पैनल है, और ब्राउज़र टॉगलिंग, कीबोर्ड एक्टिवेशन, एक्सेसिबिलिटी ट्री में एक्सपैंडेड स्टेट, और ब्राउज़र के find-in-page द्वारा मैच दिखाने के लिए कोलैप्स सेक्शन को खोलने की क्षमता को संभालता है। JavaScript accordion को उन सभी को फिर से लागू (reimplement) करना पड़ता है, और आमतौर पर उनमें से अधिकांश को गलत तरीके से लागू करता है।
एक बात जो details डिफ़ॉल्ट रूप से नहीं करता है, वह यह है कि जब एक खुलता है तो अपने सिबलिंग्स (siblings) को बंद नहीं करता है। वह व्यवहार - जिसे अक्सर एक्सक्लूसिव accordion कहा जाता है - अब हर details एलिमेंट को एक ही name एट्रिब्यूट देकर उपलब्ध है, जिसे ब्राउज़र एक ग्रुप मानते हैं। आप इसे चाहते हैं या नहीं, यह एक तकनीकी सवाल से ज़्यादा एक वास्तविक डिज़ाइन सवाल है: एक्सक्लूसिव accordions पेज को छोटा रखते हैं लेकिन उनमें से किसी एक को दोबारा खोले बिना दो रो की तुलना करना असंभव बना देते हैं।
डिफ़ॉल्ट डिस्क्लोजर ट्रायंगल के कारण details को स्टाइल करना पहले अजीब था, जो summary पर एक लिस्ट मार्कर है। इसे हटाने के लिए Tailwind में marker:content-none की आवश्यकता होती है, साथ ही पुराने Safari पर webkit-specific वैरिएंट की। एक बार यह हट जाने के बाद, summary एक साधारण flex कंटेनर बन जाता है और कस्टम chevron को उसकी सही जगह पर रखा जा सकता है।
Anatomy
- Container
- रो (rows) को संभालने वाला रैपर, जिसमें डिवाइडर और मैक्स विड्थ होती है। पठनीयता (readability) के लिए विड्थ को सीमित रखें - फुल-विथ FAQ उत्तरों को स्कैन करना कठिन होता है।
- Details
- प्रति रो एक। यहाँ group क्लास वह है जो chevron को group-open वैरिएंट के माध्यम से ओपन स्टेट पर प्रतिक्रिया देने देती है।
- Summary
- क्लिक करने योग्य हेडिंग। यह पहले से ही फोकस करने योग्य और कीबोर्ड-एक्टिवेट करने योग्य है, इसलिए इसे किसी role, tabindex या की हैंडलर की आवश्यकता नहीं है।
- Indicator
- Chevron, जिसे group-open के साथ घुमाया गया है। इसे aria-hidden के रूप में चिह्नित किया गया है क्योंकि एक्सपैंडेड स्टेट की घोषणा पहले ही एलिमेंट द्वारा कर दी जाती है।
- Panel
- summary के बाद details के अंदर की हर चीज़। यहाँ कोई भी मार्कअप काम करता है - पैराग्राफ, लिस्ट, फॉर्म, अन्य कंपोनेंट्स।
How to Use
- 1
मार्कअप कॉपी करें
कोई भी वर्शन लें। React वाला वर्शन बिना state और बिना use client डायरेक्टिव के एक सादा फंक्शन कंपोनेंट है, इसलिए यह Next.js App Router में सर्वर कंपोनेंट के रूप में काम करता है।
- 2
रो बदलें
items अरे (array) को एडिट करें, या HTML वर्शन में details ब्लॉक को डुप्लीकेट करें। पैनल किसी भी मार्कअप को लेते हैं, केवल पैराग्राफ को नहीं।
- 3
तय करें कि क्या खुला शुरू हो
लोड होने पर जो भी रो एक्सपैंड होनी चाहिए, उसमें open एट्रिब्यूट जोड़ें। पहले वाले को खोलने से यह स्पष्ट हो जाता है कि रो इंटरैक्टिव हैं।
- 4
एक्सक्लूसिव चुनें या नहीं
यदि एक को खोलने से दूसरे बंद होने चाहिए, तो हर details को एक ही name एट्रिब्यूट दें। यदि पाठक रो की तुलना करना चाहें तो एट्रिब्यूट को छोड़ दें।
- 5
summary को स्टाइल करें
marker:content-none के साथ डिफ़ॉल्ट मार्कर को हटाएं, फिर लेबल और इंडिकेटर के लिए summary को एक सामान्य flex कंटेनर की तरह ट्रीट करें।
Features
कोई JavaScript नहीं
टॉगलिंग, कीबोर्ड एक्टिवेशन और स्टेट को ब्राउज़र द्वारा हैंडल किया जाता है, इसलिए कंपोनेंट हाइड्रेशन से पहले काम करता है और स्क्रिप्ट विफल होने पर भी काम करना जारी रखता है।
डिफ़ॉल्ट रूप से सही सेमेटिक्स
एक्पैंडेड स्टेट स्वचालित रूप से असिस्टिव टेक्नोलॉजी को एक्स्पोज़ की जाती है, इसमें सेट करने या सिंक में रखने के लिए किसी aria-expanded की आवश्यकता नहीं होती है।
कोलैप्स होने पर भी खोजने योग्य
ब्राउज़र का find-in-page मैच दिखाने के लिए एक कोलैप्सड पैनल को खोल सकता है, जो div से बने JavaScript अकॉर्डियन द्वारा नहीं किया जा सकता है।
CSS-संचालित इंडिकेटर
शेवरॉन रोटेशन group-open वेरिएंट से आता है, इसलिए ओपन स्टेट किसी स्टेट वेरिएबल के बजाय सीधे स्टाइलिंग को नियंत्रित करती है।
बिना कोड के एक्सक्लूसिव मोड
एक साझा name एट्रिब्यूट ग्रुप को एक समय में एक-खुले-रहने वाले अकॉर्डियन की तरह काम कराता है, जिसे पूरी तरह से ब्राउज़र द्वारा हैंडल किया जाता है।
सर्वर-कंपोनेंट फ्रेंडली
बिना किसी स्टेट और बिना किसी इफेक्ट के, React वर्शन Next.js ऐप राउटर में बिना किसी use client डायरेक्टिव के सर्वर पर रेंडर होता है।
Common Uses
FAQ सेक्शन
सबसे आम उपयोग। बीस प्रश्न एक स्क्रीन की हेडिंग्स में फिट आ जाते हैं, और रीडर्स केवल उन दो को खोलते हैं जो उनके काम के हैं।
फ़िल्टर ग्रुप
प्रोडक्ट साइडबार में कोलैप्सिबल कैटेगरी, प्राइस और एट्रिब्यूट ग्रुप्स, ताकि एक लंबा फ़िल्टर सेट छोटी स्क्रीन पर भी नेविगेट करने योग्य बना रहे।
सेटिंग्स पैनल
कोलैप्सड रो के पीछे एडवांस ऑप्शन्स को ग्रुप करने से ऑप्शन्स को पूरी तरह से छिपाए बिना सेटिंग्स पेज आसानी से समझने योग्य रहता है।
डॉक्यूमेंटेशन डिटेल्स
मुख्य फ़्लो को पठनीय बनाए रखने के लिए पूर्ण API टेबल, लंबे एरर मैसेज या वैकल्पिक सेटअप चरणों को डिस्क्लोज़र के पीछे छिपाना।
Accessibility
- summary फ़ोकस करने योग्य होता है और डिफ़ॉल्ट रूप से Enter तथा Space के साथ सक्रिय हो जाता है, इसके लिए किसी tabindex या की हैंडलर की आवश्यकता नहीं होती है।
- एक्पैंडेड और कोलैप्सड स्टेट एलिमेंट द्वारा ही असिस्टिव टेक्नोलॉजी को एक्स्पोज़ की जाती है, इसलिए मेंटेन करने के लिए कोई aria-expanded नहीं होता है।
- शेवरॉन को aria-hidden के रूप में चिह्नित किया जाता है क्योंकि यह उस स्टेट को डुप्लिकेट करता है जिसकी घोषणा पहले ही की जा चुकी है; इसके बिना, स्क्रीन रीडर उपयोगकर्ताओं को हर रो पर एक अर्थहीन ग्राफिक सुनाई देता है।
- कोलैप्सड कंटेंट डॉक्यूमेंट में रहता है और ब्राउज़र सर्च द्वारा खोजने योग्य बना रहता है, यही वह तरीका है जिससे कई लोग लंबे FAQ पेज पर कंटेंट ढूंढते हैं।
- यदि किसी रो की हेडिंग को एक डॉक्यूमेंट हेडिंग भी होना चाहिए, तो details के चारों ओर न रखकर summary के अंदर एक हेडिंग एलिमेंट रखें - इसके विपरीत नेस्टिंग अमान्य है।
- स्टाइलिंग करते समय 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 }[] | - | Accordion रो (rows)। इन्हें एक दर्जन या उससे कम रखें; इसके बाद पाठकों के लिए खोजने योग्य (searchable) लिस्ट या अलग-अलग पेजों का सेट बेहतर रहता है। |
| defaultOpenIndex | number | 0 | कौन सी रो (row) एक्सपैंडेड (expanded) शुरू होती है। पहले वाले को खोलना यह संकेत देता है कि रो इंटरैक्टिव हैं; सभी को बंद रखना एक स्टैटिक हेडिंग लिस्ट जैसा लग सकता है। |
Why Use These Accordions?
यह वह वर्शन है जिसमें गलत होने की संभावना सबसे कम है। कोई स्टेट नहीं, कोई डिपेंडेंसी नहीं, कोई हाइड्रेशन नहीं, सिंक में रखने के लिए कोई ARIA नहीं, और ऐसा व्यवहार जो JavaScript लोड होने से पहले और उसके विफल होने के बाद भी काम करता है।
यह खोजने योग्य रहता है। कोलैप्सड details के अंदर की सामग्री अभी भी ब्राउज़र के find-in-page द्वारा सामने लाई जा सकती है, जिससे कई रीडर्स वास्तव में एक लंबे FAQ को नेविगेट करते हैं - एक div-आधारित अकॉर्डियन उस सामग्री को सर्च से चुपचाप छिपा देता है।
React वर्शन को किसी use client डायरेक्टिव की आवश्यकता नहीं होती है, इसलिए यह सर्वर पर रेंडर होता है। प्रचलन में अधिकांश अकॉर्डियन कंपोनेंट विशुद्ध रूप से क्लाइंट कंपोनेंट होते हैं क्योंकि वे ओपन स्टेट को ट्रैक करते हैं जिसे ब्राउज़र पहले से ही ट्रैक कर रहा था।
Tips
- डिफ़ॉल्ट रूप से पहली रो खोलें। बंद हेडिंग्स का एक कॉलम एक स्टैटिक लिस्ट जैसा लग सकता है, और रीडर्स हमेशा उस पर क्लिक करने की कोशिश नहीं करेंगे।
- पैनल छोटे रखें। यदि कोई उत्तर कुछ पैराग्राफ से आगे निकल जाता है, तो पूर्ण पेज का लिंक दें - लंबे पैनल बाद की पंक्तियों को बहुत नीचे धकेल देते हैं और सूची को स्कैन करना कठिन बना देते हैं।
- एक्सक्लूसिव व्यवहार के लिए, JavaScript के बजाय साझा name एट्रिब्यूट का उपयोग करें। यह एक सिंगल एट्रिब्यूट है और बाकी का काम ब्राउज़र संभालता है।
- summary को उस प्रश्न के रूप में लिखें जिसे एक पाठक वास्तव में टाइप करेगा। FAQ पंक्तियाँ अक्सर पेज का वह हिस्सा होती हैं जो खोज परिणाम (search results) में दिखाई देती हैं, और इसकी शब्दावली ही यह तय करती है कि वे दिखाई देंगी या नहीं।
- details के चारों ओर एक हेडिंग एलिमेंट को नेस्ट (nest) न करें। यदि आप चाहते हैं कि प्रश्न एक डॉक्यूमेंट हेडिंग बने, तो हेडिंग को इसके बजाय summary के अंदर रखें।
- फोकस रिंग का ध्यान रखें। summary को स्टाइल करते समय इसे हटाना आसान है, और इसके बिना कीबोर्ड उपयोगकर्ताओं को पता नहीं चल पाता कि वे किस पंक्ति पर हैं।