Accordion examples
รายการที่พับเก็บได้ซึ่งสร้างจากแท็ก details และ summary ของ HTML โดยไม่ต้องใช้ JavaScript
รายการที่พับเก็บได้ซึ่งสร้างจากแท็ก details และ summary ดั้งเดิม การเปิด-ปิด การรองรับคีย์บอร์ด และ semantics ที่ถูกต้องล้วนมาจากเบราว์เซอร์ ทำให้คอมโพเนนต์นี้ไม่ต้องใช้ JavaScript เลยแม้แต่น้อย
ไอคอนลูกศรจะหมุนโดยใช้ variant แบบ group-open ซึ่งหมายความว่าสถานะเปิดถูกจัดสไตล์ด้วย CSS แทนที่จะติดตามผ่าน state
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 คือพาเนล และเบราว์เซอร์จะจัดการเรื่องการสลับสถานะ การกดใช้งานด้วยคีย์บอร์ด สถานะ expanded ใน accessibility tree รวมถึงความสามารถในการค้นหาในหน้า (find-in-page) ของเบราว์เซอร์ที่จะเปิดส่วนที่ถูกยุบไว้เพื่อแสดงผลลัพธ์ที่ตรงกัน Accordion ที่เขียนด้วย JavaScript ต้องสร้างสิ่งเหล่านี้ขึ้นมาใหม่หมด ซึ่งมักจะทำได้ไม่สมบูรณ์
สิ่งเดียวที่ details ไม่ได้ทำเป็นค่าเริ่มต้นคือการปิดแถวพี่น้องเมื่อมีแถวอื่นถูกเปิด พฤติกรรมนี้มักเรียกว่า exclusive accordion ซึ่งปัจจุบันทำได้โดยการกำหนดแอตทริบิวต์ name ให้กับแท็ก details ทุกอันเหมือนกัน ซึ่งเบราว์เซอร์จะมองว่าเป็นกลุ่มเดียวกัน การจะใช้งานหรือไม่นั้นเป็นเรื่องของการออกแบบมากกว่าเรื่องทางเทคนิค เพราะ exclusive accordion จะช่วยให้หน้าเว็บสั้นลง แต่จะทำให้ไม่สามารถเปรียบเทียบข้อมูลสองแถวพร้อมกันได้เว้นแต่จะเปิดแถวใดแถวหนึ่งขึ้นมาใหม่
การจัดสไตล์ details ในอดีตค่อนข้างยุ่งยากเนื่องจากมีสามเหลี่ยม disclosure เริ่มต้นที่เป็น list marker ของ summary การซ่อนมันทำได้โดยใช้ marker:content-none ใน Tailwind รวมถึง variant สำหรับ webkit บน Safari เวอร์ชันเก่า เมื่อเอาออกแล้ว summary จะกลายเป็น flex container ปกติ และสามารถจัดตำแหน่งลูกศร custom ได้ตามต้องการ
Anatomy
- Container
- ส่วนห่อหุ้มแถวต่างๆ ที่มีเส้นคั่นและกำหนดความกว้างสูงสุด ควรจำกัดความกว้างเพื่อให้อ่านง่าย เพราะคำตอบ FAQ ที่กว้างเต็มหน้าจอจะกวาดสายตาลำบาก
- Details
- มีหนึ่งอันต่อแถว คลาส group ในที่นี้คือสิ่งที่ทำให้ลูกศรตอบสนองต่อสถานะเปิดผ่าน variant แบบ group-open
- Summary
- หัวข้อที่คลิกได้ โฟกัสและกดใช้งานด้วยคีย์บอร์ดได้ทันที จึงไม่ต้องใส่ role, tabindex หรือ key handler ใดๆ
- Indicator
- ไอคอนลูกศร หมุนด้วย group-open กำหนดเป็น aria-hidden เพราะสถานะ expanded ถูกประกาศโดยตัวแท็กเองอยู่แล้ว
- Panel
- ทุกอย่างที่อยู่ภายใน details ต่อจาก summary สามารถใช้มาร์กอัปใดก็ได้ เช่น ย่อหน้า, รายการ, ฟอร์ม หรือคอมโพเนนต์อื่นๆ
How to Use
- 1
คัดลอกมาร์กอัป
เลือกใช้เวอร์ชันใดก็ได้ ฝั่ง React เป็นฟังก์ชันคอมโพเนนต์ธรรมดาที่ไม่มี state และไม่มีไดเรกทิล use client จึงทำงานเป็น Server Component ใน Next.js App Router ได้
- 2
แทนที่แถว
แก้ไขอาเรย์ items หรือทำซ้ำบล็อก details ในเวอร์ชัน HTML พาเนลสามารถใส่เนื้อหาอะไรก็ได้ ไม่ใช่แค่ย่อหน้า
- 3
กำหนดว่าจะให้เปิดอันไหนตั้งแต่เริ่ม
เพิ่มแอตทริบิวต์ open ให้กับแถวที่ต้องการให้ขยายแสดงผลตอนโหลดหน้าเว็บ การเปิดแถวแรกช่วยให้เห็นชัดเจนว่าแถวเหล่านี้โต้ตอบได้
- 4
เลือกใช้แบบ exclusive หรือไม่
ใส่แอตทริบิวต์ name ที่เหมือนกันให้กับ details ทุกอันหากต้องการให้การเปิดอันหนึ่งเป็นการปิดอันอื่น ละเว้นแอตทริบิวต์นี้หากผู้อ่านอาจต้องการเปรียบเทียบข้อมูลระหว่างแถว
- 5
จัดสไตล์ summary
ลบมาร์กเกอร์เริ่มต้นด้วย marker:content-none จากนั้นจัดการ summary เป็น flex container ปกติสำหรับป้ายข้อความและไอคอน
Features
ไม่ต้องใช้ JavaScript
การเปิด-ปิด การใช้งานผ่านคีย์บอร์ด และสถานะได้รับการจัดการโดยเบราว์เซอร์ คอมโพเนนต์จึงทำงานได้ก่อน hydration และยังคงทำงานได้ต่อไปหากสคริปต์ล้มเหลว
ความหมายเชิงความหมาย (Semantics) ที่ถูกต้องเป็นค่าเริ่มต้น
สถานะ expanded จะถูกเปิดเผยไปยังเทคโนโลยีสิ่งอำนวยความสะดวก (Assistive Technology) โดยอัตโนมัติ โดยไม่ต้องตั้งค่าหรือคอยซิงค์ aria-expanded
ค้นหาเจอแม้ในขณะพับอยู่
ฟังก์ชัน find-in-page ของเบราว์เซอร์สามารถเปิด panel ที่ถูกพับอยู่เพื่อแสดงผลลัพธ์ที่ตรงกันได้ ซึ่ง accordion ที่สร้างจาก div ด้วย JavaScript ไม่สามารถทำได้
อินดิเคเตอร์ที่ขับเคลื่อนด้วย CSS
การหมุนของ chevron มาจาก variant group-open ดังนั้นสถานะ open จึงเป็นตัวควบคุมสไตล์โดยตรง แทนที่จะผ่านตัวแปร state
โหมด Exclusive โดยไม่ต้องเขียนโค้ด
แอตทริบิวต์ name ที่ใช้ร่วมกันทำให้กลุ่มทำงานเป็นแบบเปิดได้ทีละหนึ่งรายการ ซึ่งจัดการทั้งหมดโดยเบราว์เซอร์
รองรับ Server-component
ด้วยความที่ไม่มี state และไม่มี effects เวอร์ชัน React จึงเรนเดอร์บนเซิร์ฟเวอร์ใน Next.js App Router ได้โดยไม่ต้องมีไดเรกทีฟ use client
Common Uses
ส่วน FAQ
การใช้งานที่พบบ่อยที่สุด คำถามยี่สิบคําถามพอดีกับพื้นที่หัวข้อหนึ่งหน้าจอ และผู้อ่านจะเปิดเฉพาะสองข้อที่เกี่ยวข้องกับพวกเขาเท่านั้น
กลุ่มฟิลเตอร์
หมวดหมู่ ช่วงราคา และกลุ่มแอตทริบิวต์ที่พับได้ใน sidebar ของสินค้า เพื่อให้ชุดฟิลเตอร์ที่มีความยาวสามารถนำทางได้บนหน้าจอขนาดสั้น
แพเนลการตั้งค่า
การจัดกลุ่มตัวออปชันขั้นสูงไว้หลังแถวที่พับอยู่ช่วยให้หน้าการตั้งค่าดูเข้าถึงง่ายโดยไม่ต้องซ่อนออปชันเหล่านั้นไปทั้งหมด
รายละเอียดเอกสาร
การซ่อนตาราง API เต็มรูปแบบ, ข้อความแสดงข้อผิดพลาดที่มีความยาว, หรือขั้นตอนการตั้งค่าเสริม ไว้หลัง disclosure เพื่อให้ฟังก์ชันหลักยังคงอ่านง่าย
Accessibility
- Summary สามารถโฟกัสได้และเปิดใช้งานด้วย Enter และ Space เป็นค่าเริ่มต้น โดยไม่ต้องใช้ tabindex หรือคีย์เฮนเดอร์
- สถานะ expanded และ collapsed ถูกเปิดเผยไปยังเทคโนโลยีสิ่งอำนวยความสะดวกโดยตัวอิลิเมนต์เอง ดังนั้นจึงไม่มี aria-expanded ที่ต้องคอยดูแลรักษา
- Chevron ถูกทำเครื่องหมายเป็น aria-hidden เพราะมันซ้ำซ้อนกับสถานะที่มีการประกาศไปแล้ว หากไม่มีสิ่งนี้ ผู้ใช้โปรแกรมอ่านหน้าจอจะได้ยินกราฟิกที่ไม่มีความหมายในทุกๆ แถว
- คอนเทนต์ที่ถูกพับจะยังคงอยู่ในด็อกเมนต์และสามารถค้นหาได้โดยการค้นหาของเบราว์เซอร์ ซึ่งเป็นวิธีที่หลายคนใช้หาคอนเทนต์บนหน้า FAQ ยาวๆ
- หากหัวข้อของแถวควรเป็นหัวข้อของด็อกเมนต์ด้วย ให้ใส่ heading element ไว้ข้างใน summary แทนที่จะอยู่รอบ details ซึ่งการซ้อนกลับด้านนั้นไม่ถูกต้อง
- โฟกัสดำเนินการเริ่มต้นบน summary มักจะถูกลบออกโดยบังเอิญตอนแต่งสไตล์ คงสไตล์ focus-visible ที่มองเห็นได้ไว้ ไม่อย่างนั้นผู้ใช้คีย์บอร์ดจะสูญเสียการติดตามว่าตนเองอยู่แถวไหน
Customization
- ลบสามเหลี่ยมเริ่มต้นด้วย marker:content-none และเพิ่ม variant webkit details-marker หากคุณรองรับ Safari เวอร์ชันเก่า
- สลับตัวคั่นเป็นแบบการ์ดมีกรอบโดยการห่อ details แต่ละอันในคอนเทนต์โค้งมนของตัวเองพร้อมกับเส้นขอบของตัวเอง
- เปลี่ยน chevron เป็นเครื่องหมายบวกและลบโดยการเรนเดอร์ไอคอนทั้งสองและสลับการแสดงผลด้วย group-open ซึ่งจะอ่านได้ง่ายกว่าเมื่อมองจากระยะไกล
- การทำแอนิเมชันความสูงของ panel ยังคงทำได้ยากเนื่องจาก details สลับสถานะทันที แนวทาง interpolate-size และ calc-size สามารถใช้งานได้ใน Chrome ปัจจุบัน ให้มองแอนิเมชันนี้เป็นส่วนเสริมแทนที่จะเป็นข้อกำหนดบังคับ
- เพิ่มพื้นหลังแบบจางๆ บนแถวที่เปิดด้วย group-open บนคอนเทนต์ ซึ่งช่วยได้เมื่อมีหลายแถวเปิดพร้อมกัน
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| items * | { question: string; answer: string }[] | - | แถวของ accordion ควรมีประมาณสิบสองแถวหรือน้อยกว่านั้น หากมากกว่านั้น การใช้รายการที่ค้นหาได้หรือหน้าเพจแยกต่างหากจะตอบโจทย์ผู้อ่านได้ดีกว่า |
| defaultOpenIndex | number | 0 | ระบุว่าแถวใดควรเปิดไว้ตั้งแต่เริ่มต้น การเปิดแถวแรกไว้ช่วยให้รู้ว่าแถวเหล่านี้สามารถกดโต้ตอบได้ การปล่อยให้ปิดทั้งหมดอาจดูเหมือนเป็นรายการหัวข้อธรรมดา |
Why Use These Accordions?
นี่คือเวอร์ชันที่มีโอกาสพังน้อยที่สุด ไม่มี state, ไม่มี dependency, ไม่มี hydration, ไม่มี ARIA ที่ต้องคอยซิงค์ และพฤติกรรมที่ทำงานได้ก่อน JavaScript จะโหลดและหลังจากที่โหลดไม่สำเร็จ
มันยังคงค้นหาได้ คอนเทนต์ภายใน details ที่ถูกพับยังคงสามารถแสดงขึ้นมาได้โดยการค้นหาในหน้าของเบราว์เซอร์ ซึ่งเป็นวิธีที่ผู้อ่านหลายคนใช้งานจริงบนหน้า FAQ ยาวๆ - accordion ที่สร้างจาก div จะซ่อนคอนเทนต์นั้นจากการค้นหาอย่างเงียบๆ
เวอร์ชัน React ไม่ต้องการไดเรกทีฟ use client ดังนั้นจึงเรนเดอร์บนเซิร์ฟเวอร์ คอมโพเนนต์ accordion ส่วนใหญ่ที่มีอยู่ทั่วไปเป็น client components ก็เพราะพวกมันติดตามสถานะการเปิดซึ่งเบราว์เซอร์ติดตามไว้อยู่แล้ว
Tips
- เปิดแถวแรกเป็นค่าเริ่มต้น คอลัมน์ของหัวข้อที่ปิดไว้อาจดูเหมือนรายการแบบสถิต และผู้อ่านอาจจะไม่ลองคลิกดูทุกครั้ง
- รักษา panel ให้สั้น หากคำตอบยาวเกินกว่าสองสามย่อหน้า ให้ลิงก์ไปยังหน้าเต็ม - panel ที่ยาวจะดันแถวถัดไปให้ลงไปไกลและทำให้รายการยากแก่การกวาดสายตาดู
- สำหรับการทำงานแบบเอ็กซ์คลูซีฟ ให้ใช้แอททริบิวต์ name ที่ใช้ร่วมกันแทน JavaScript เป็นแอททริบิวต์เดียวและเบราว์เซอร์จะจัดการส่วนที่เหลือให้เอง
- เขียน summary เป็นคำถามที่ผู้อ่านมักจะพิมพ์ค้นหาจริงๆ แถว FAQ มักจะเป็นส่วนของหน้าที่ปรากฏในผลการค้นหา และการใช้ถ้อยคำคือสิ่งที่จะเป็นตัวตัดสินว่าจะปรากฏหรือไม่
- อย่าใส่เฮดดิงอิลิเมนต์ครอบทับ details หากคุณต้องการให้คำถามเป็นเฮดดิงของเอกสาร ให้ใส่เฮดดิงไว้ข้างใน summary แทน
- ระวังเรื่อง focus ring มันง่ายมากที่จะเผลอลบออกตอนจัดสไตล์ให้ summary และหากไม่มีมัน ผู้ใช้ที่ใช้คีย์บอร์ดจะไม่รู้ว่าตอนนี้ตัวเองอยู่ที่แถวไหน