Chuyển tới nội dung
Component

Accordion

A stack of sections that open in place, one at a time or several: questions and answers, groups of settings, long details. Each section is a row with its title and a caret that turns, and a divider under it.

Xem trước

Yes, within 14 days of your purchase.

Props

  • type
  • collapsible
  • defaultValue
  • value
  • disabled
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"

<Accordion defaultValue="refunds" className="max-w-md">
  <AccordionItem value="refunds">
    <AccordionTrigger>Can I get a refund?</AccordionTrigger>
    <AccordionContent>Yes, within 14 days of your purchase.</AccordionContent>
  </AccordionItem>
  <AccordionItem value="seats">
    <AccordionTrigger>Can I add more people later?</AccordionTrigger>
    <AccordionContent>Add or remove seats at any time from Billing.</AccordionContent>
  </AccordionItem>
  <AccordionItem value="cancel">
    <AccordionTrigger>What happens when I cancel?</AccordionTrigger>
    <AccordionContent>Your projects stay readable for 30 days.</AccordionContent>
  </AccordionItem>
</Accordion>

Cách dùng

Khi nào dùng

  • Long pages of questions and answers, or groups of settings the user opens one at a time.
  • Details most people skip, kept on the page for those who need them.

Khi nào không dùng

  • Switching between views of the same content. Use Tabs.
  • Content everyone needs. Keep it on the page, open.
  • A section inside another accordion section.

Ví dụ

Questions

Yes, within 14 days of your purchase.

import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"

<Accordion defaultValue="refunds" className="max-w-md">
  <AccordionItem value="refunds">
    <AccordionTrigger>Can I get a refund?</AccordionTrigger>
    <AccordionContent>Yes, within 14 days of your purchase.</AccordionContent>
  </AccordionItem>
  <AccordionItem value="seats">
    <AccordionTrigger>Can I add more people later?</AccordionTrigger>
    <AccordionContent>Add or remove seats at any time from Billing.</AccordionContent>
  </AccordionItem>
</Accordion>

Several open

type multiple keeps every section the user opened.

import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"

<Accordion type="multiple" headingLevel={4} className="max-w-md">
  <AccordionItem value="email">
    <AccordionTrigger>Email</AccordionTrigger>
    <AccordionContent>Mentions, replies and weekly digests.</AccordionContent>
  </AccordionItem>
  <AccordionItem value="push">
    <AccordionTrigger>Push</AccordionTrigger>
    <AccordionContent>Mentions and replies only.</AccordionContent>
  </AccordionItem>
  <AccordionItem value="sms" disabled>
    <AccordionTrigger>Text messages</AccordionTrigger>
    <AccordionContent>Not available on your plan.</AccordionContent>
  </AccordionItem>
</Accordion>

Nên và không nên

Nên

  • Write titles the user scans for: a question, or the name of a group of settings.
  • Set headingLevel to one below the heading the accordion sits under.
  • Use type multiple when users compare sections side by side.

Không nên

  • Don't hide what everyone needs; keep it on the page.
  • Don't use it to switch between views of the same content; use Tabs.
  • Don't put an accordion inside an accordion section.

Props

TênKiểuGiá trịMặc địnhMô tả
typeenumsingle | multiplesingleSet on Accordion. single opens one section at a time; multiple lets several stay open, for settings the user compares.
collapsibleboolean-trueSet on Accordion, with type single. The open section closes when its title is pressed again.
defaultValuestring--Set on Accordion. The value of the section open on first render (an array of values with type multiple).
valuestring--Set on Accordion. The open section, when you control it. Pair it with onValueChange.
onValueChangefunction--Set on Accordion. Called with the open value (or values) when a section opens or closes.
headingLevelenum2 | 3 | 43Set on Accordion. The heading level of every title: one below the heading the accordion sits under.
disabledboolean-falseSet on AccordionItem, or on Accordion for every section. The title can't be pressed and dims.

Slot

TênBắt buộcMô tả
itemcóOne AccordionItem per section, each with a unique value, an AccordionTrigger and an AccordionContent.

Cấu tạo

PhầnMô tả
rootAccordion: a column as wide as its parent.
itemAccordionItem: one section, with a divider under it.
triggerAccordionTrigger: the title row, a button inside a heading. At least the md control height, 14px medium text, the caret at the end.
iconTabler ChevronDown, 16px, secondary, at the end of the title; half a turn when open.
contentAccordionContent: the part that opens, 13px secondary text.
bodyThe padded block inside the content, pb-md, so nothing shows while it grows.

Khả năng truy cập

Role: region

PhímHành động
Space, EnterOpens or closes the focused section.
ArrowDown, ArrowUpMoves focus to the next or previous section title.
Home, EndMoves focus to the first or last section title.
TabMoves from a title into the open content, then on.
  • Each title is a button with aria-expanded inside a real heading, h3 by default; set headingLevel so it fits the page's outline.
  • An open section is a region named by its title.
  • The caret is decorative; aria-expanded says whether it is open.
  • It opens with a short grow and fade only when the user allows motion, and closes at once.

Cài đặt

npx shadcn add https://werewolf-ui.vercel.app/r/accordion.json

Lệnh này thêm luôn styles/werewolf.css, bộ token mà mọi component dùng, và các component khác mà nó cần. Import file đó một lần trong CSS gốc, ngay sau @import "tailwindcss", với đường dẫn tính từ file CSS đó (từ app/globals.css là @import "../styles/werewolf.css").