Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Accordion for Vue UI SDK

Stacked headings that each open the section under them, one at a time or several at once. An accordion for Vue on UI SDK, for FAQs and settings.

On this page9 sections

An accordion stacks headings in a column, and each heading opens the section under it. One section is open at a time unless you allow several. It suits an FAQ or a long settings page, where most of the content can stay folded away until someone needs it.

For one section that opens and closes on its own, use a Collapsible. When every section matters as much as the next and people move between them, Tabs keep one in view at all times.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Accordion anatomy: root, item, item trigger, item content, item indicator

Usage

<script setup lang="ts">
import {
  Accordion, AccordionContent, AccordionItem, AccordionTrigger,
} from "@/components/ui/accordion";
</script>
<template>
  <Accordion class="w-full max-w-sm" :defaultValue='["shipping"]'>
    <AccordionItem value="shipping">
      <AccordionTrigger>What are your shipping options?</AccordionTrigger>
      <AccordionContent>
        We offer standard (5-7 days), express (2-3 days), and overnight
        shipping. Free shipping on international orders.
      </AccordionContent>
    </AccordionItem>
    <AccordionItem value="returns">
      <AccordionTrigger>What is your return policy?</AccordionTrigger>
      <AccordionContent>
        Returns accepted within 30 days. Items must be unused and in original
        packaging. Refunds processed within 5-7 business days.
      </AccordionContent>
    </AccordionItem>
    <AccordionItem value="support">
      <AccordionTrigger>How can I contact customer support?</AccordionTrigger>
      <AccordionContent>
        Reach us via email, live chat, or phone. We respond within 24 hours
        during business days.
      </AccordionContent>
    </AccordionItem>
  </Accordion>
</template>

Accessibility

Each trigger is a <button> that reports its state with aria-expanded, so a screen reader says whether a section is open and Enter or Space toggles it. The button sits inside an h3, which puts every section in the page's heading outline. If the page calls for another level, change that heading in components/ui/accordion.

Space
When focus is on a trigger, opens its section, or closes it where the accordion allows that.
Enter
When focus is on a trigger, opens its section, or closes it where the accordion allows that.
Tab
Moves focus to the next focusable element, which is the next trigger unless an open section holds one. Disabled triggers stay in the tab order.
Shift+Tab
Moves focus to the previous focusable element.
Arrow Down
Moves focus to the next trigger, passing over disabled ones, and from the last one back to the first.
Arrow Up
Moves focus to the previous trigger, and from the first one to the last.
Home
Moves focus to the first trigger.
End
Moves focus to the last trigger.
Arrow Right
With orientation="horizontal", moves focus to the next trigger, or to the previous one right to left.
Arrow Left
With orientation="horizontal", moves focus to the previous trigger, or to the next one right to left.

Demos

Basic

Account questions, one open at a time, with the password item starting open. The rows come from a list, so another question is another entry rather than a fresh block of markup. Use this for a short FAQ where two answers on screen at once would be noise. If people compare sections, let more than one stay open.

Preview width

Multiple

Three groups of account settings, with notifications open from the start and privacy or billing free to open beside it. Allow several sections at once when people compare them or check related settings in one pass. In a single-open list, opening the next group folds away the one they were still reading.

Preview width

Disabled

Premium feature information, the middle of three account questions, has disabled on its AccordionItem, so its trigger does nothing while the questions about account history and email open as usual. Leave a disabled row when the heading should stay visible as a locked feature and the upgrade path lives elsewhere on the page. If the plan will never include it, omit the row.

Preview width

Borders

The root gets a border and a radius. Each item gets a bottom border and horizontal padding, and the last item drops its border so it does not double up with the outline. Use the bordered list when the accordion is the surface, sitting on a plain page. Skip the extra edges when a card already supplies them.

Preview width

Card

The questions sit in CardContent, under a title and a short description about subscription and billing. Plans starts open. Use the card when this FAQ is one block among others, on a pricing or billing screen, and it needs a heading that is not itself a trigger. A bare accordion is enough when the page heading already names the topic.

Preview width

RTL

The same account questions, in Arabic, with the first item open and the accordion inside a Direction provider set to right to left, whose dir UI SDK reads. Triggers, text, and the chevron follow the inline direction, so the icon is not stuck on the physical right. Use this when the page language is Arabic or Hebrew. Only the direction of the row changes.

Preview width

FAQ

How do I let only one accordion item open at a time in Vue?

That is how it behaves with no props: opening an item closes the one that was open, and pressing the open item again closes it too. Add multiple to Accordion when several should stay open together, as the Multiple demo does. Set collapsible to false to stop the open item from closing when it is pressed again.

With collapsible off, the open trigger reports aria-disabled, so a screen reader hears that it can't close, but it keeps its usual look and stays in the tab order.

How do I make an accordion item open by default?

List its value in defaultValue on Accordion, which takes an array even for one item: ["shipping"] opens the item whose value is shipping. Every item starts closed when you leave the prop out. To open several from the start, add multiple and list each one.

The Basic demo opens its first item this way.

How do I open or close accordion items from my own code?

Hold the open items in a ref and bind it with v-model, which reads and writes an array of the open items' values.

Once the state is yours, anything can change it. A link to one question can open that item by reading the hash from the URL, and where several may be open, an Expand all button lists every item at once.

Do form fields inside a closed accordion item still submit?

Not as it ships. A closed panel is left out of the DOM, so its fields aren't part of the form. Add keepMounted to Accordion, or to one AccordionContent, to render it hidden instead, and its fields submit.

A hidden field can't show the browser's own validation message, though. If a closed section holds a required field, validate it yourself and open that section when it fails.

Why can't Ctrl+F find text inside a closed accordion section?

The browser's find in page only searches what is rendered and visible, and a closed panel isn't in the DOM. Add hiddenUntilFound to Accordion: the panels stay in the page with hidden="until-found", and a browser that supports it opens the section that holds the match.

Is the UI SDK accordion accessible with the keyboard?

Yes. Every trigger is a button in the tab order, Enter and Space toggle its section, and the arrow keys, Home and End move between triggers without tabbing through the open content.

The Accessibility section lists each key. Keep links and buttons out of the trigger itself, because a button can't hold other controls.

How do I change the accordion's chevron icon?

Edit AccordionTrigger in components/ui/accordion, which is your code once installed. It renders two icons, ChevronDownIcon while the section is closed and ChevronUpIcon while it is open, and classes keyed on aria-expanded show one and hide the other.

Swap both imports for the pair you want, a plus and a minus for example. For a single icon that rotates, keep one of the two and turn it with a rotate class under the same aria-expanded variant.

When should I use an accordion instead of tabs or a collapsible?

Use an accordion when the sections are read one after another on the same page and most can stay closed: an FAQ, a filter sidebar, a long settings page. Headings stay visible, so people scan them and open what they need.

Tabs fit when there are few sections, their labels fit on one row and one must always show. A Collapsible is one section with its own trigger, with no group around it and nothing else closing when it opens.

API Reference

Accordion

<div> data-slot="accordion"

PropType
value?T[]

The open items' values. Pass it to control the accordion. Two-way with v-model.

defaultValue?T[]

The items open at first, when uncontrolled.

Default
[]
collapsible?boolean

Whether the open item can close when only one may be open. With false, one item always stays open and its trigger reports it can't be pressed.

Default
true
controller?{ /** The open items' values. */ getValue: () → string[]; /** Sets the open items. Only the first counts unless `multiple` is set. Returns false when canceled. */ setValue: (value: string[], options?: AccordionChangeOptions) → boolean; /** Whether an item is open. */ isOpen: (value: string) → boolean; /** Opens an item. With one item open at a time, closes the others. */ open: (value: string, options?: AccordionChangeOptions) → boolean; /** Closes an item, unless it's the one item that must stay open. */ close: (value: string, options?: AccordionChangeOptions) → boolean; /** Opens a closed item, or closes an open one. */ toggle: (value: string, options?: AccordionChangeOptions) → boolean; /** Opens every enabled item. Only when `multiple` is set. */ openAll: (options?: AccordionChangeOptions) → boolean; /** Closes every item that may close. */ closeAll: (options?: AccordionChangeOptions) → boolean; /** Moves focus to an item's trigger, or the first trigger. */ focus: (value?: string) → void; }

Opens and closes items from anywhere. Create one with createAccordionController().

Type
{ /** The open items' values. */ getValue: () → string[]; /** Sets the open items. Only the first counts unless `multiple` is set. Returns false when canceled. */ setValue: (value: string[], options?: AccordionChangeOptions) → boolean; /** Whether an item is open. */ isOpen: (value: string) → boolean; /** Opens an item. With one item open at a time, closes the others. */ open: (value: string, options?: AccordionChangeOptions) → boolean; /** Closes an item, unless it's the one item that must stay open. */ close: (value: string, options?: AccordionChangeOptions) → boolean; /** Opens a closed item, or closes an open one. */ toggle: (value: string, options?: AccordionChangeOptions) → boolean; /** Opens every enabled item. Only when `multiple` is set. */ openAll: (options?: AccordionChangeOptions) → boolean; /** Closes every item that may close. */ closeAll: (options?: AccordionChangeOptions) → boolean; /** Moves focus to an item's trigger, or the first trigger. */ focus: (value?: string) → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Stops every item from opening or closing. Triggers stay focusable.

Default
false
hiddenUntilFound?boolean

Keeps closed panels in the page as hidden="until-found", so the browser's find in page can match their text and open them.

Default
false
keepMounted?boolean

Whether closed panels stay in the page, hidden.

Default
false
loopFocus?boolean

Whether the arrow keys wrap from the last trigger to the first.

Default
true
multiple?boolean

Whether several items can be open at once. Otherwise opening one closes the others.

Default
false
orientation?Orientation

Which arrow keys move between triggers: Up and Down, or Left and Right.

Default
"vertical"
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@value-change(value: T[], details: ChangeDetails<AccordionValueChangeReason>) → void

Runs when items want to open or close. details.reason says why, and details.cancel() stops it.

Type
(value: T[], details: ChangeDetails<AccordionValueChangeReason>) → void
Parameters

value – T[]

details – ChangeDetails<AccordionValueChangeReason>

Returns
void
AttributeType
data-disabled?string

Present when the whole accordion is disabled.

data-orientation?string

Mirrors orientation.

AccordionItem

<div> data-slot="accordion-item"

PropType
disabled?boolean

Stops the item from opening or closing. Its trigger stays focusable.

Default
false
value?T

Identifies the item. The accordion's value lists the open items' values. Defaults to a generated id.

#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@open-change(open: boolean, details: ChangeDetails<AccordionValueChangeReason>) → void

Runs before the item opens or closes, before the accordion's onValueChange. details.cancel() stops it.

Type
(open: boolean, details: ChangeDetails<AccordionValueChangeReason>) → void
Parameters

open – boolean

details – ChangeDetails<AccordionValueChangeReason>

Returns
void
@open-change-complete(open: boolean) → void

Runs once the item's panel has finished opening or closing, after any animation.

Parameters

open – boolean

Returns
void
AttributeType
data-closed?string

Present while the item is closed, including while its panel animates out.

data-disabled?string

Present when the item can't open or close.

data-open?string

Present while the item is open.

AccordionTrigger

<button> data-slot="accordion-trigger-icon"

PropType
disabled?boolean

Stops this trigger from opening or closing the item. It stays focusable.

Default
false
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the item is closed, including while its panel animates out.

data-disabled?string

Present when the item can't open or close.

data-open?string

Present while the item is open.

AccordionContent

<div> data-slot="accordion-panel"

PropType
hiddenUntilFound?boolean

Whether the panel stays findable with hidden="until-found" while closed. Overrides the accordion's.

keepMounted?boolean

Whether the panel stays in the page, hidden, while closed. Overrides the accordion's.

#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-closed?string

Present while the item is closed, including while its panel animates out.

data-disabled?string

Present when the item can't open or close.

data-ending-style?string

Present while a panel animates closed. Style the state you animate to.

data-open?string

Present while the item is open.

data-starting-style?string

Present for a panel's first frame. Style the state you animate from.

CSS variableType
--accordion-panel-heightstring

The panel's content height in pixels while it animates, auto once open and 0px while closed.

--accordion-panel-widthstring

The panel's content width, the same way. For accordions that open sideways.