Collapsible
A disclosure panel that opens with its content's height.
Show more
Release 4.2
Shipped on Tuesday with no open issues.
"use client"
import * as React from "react"
import { Button } from "@/components/ui/button"
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"
export default function CollapsibleShowMore() {
const [open, setOpen] = React.useState(false)
return (
<Collapsible open={open} onOpenChange={setOpen} className="flex w-full max-w-sm flex-col gap-2 text-sm">
<div className="flex items-center justify-between gap-4">
<span className="font-medium">Release 4.2</span>
<CollapsibleTrigger render={<Button variant="ghost" size="sm" />}>
{open ? "Hide changes" : "Show 3 changes"}
</CollapsibleTrigger>
</div>
<p className="text-muted-foreground">Shipped on Tuesday with no open issues.</p>
<CollapsibleContent>
<ul className="flex flex-col gap-1 pt-1 text-muted-foreground">
<li>Invoices can be exported as PDF.</li>
<li>Task lists remember their sort order.</li>
<li>The project switcher opens faster.</li>
</ul>
</CollapsibleContent>
</Collapsible>
)
}
Open by default
Payment details
- Invoice
- INV-0412
- Due
- 30 November
- Amount
- 1,240.00
import { ChevronsUpDownIcon } from "@keyline-icons/react"
import { Button } from "@/components/ui/button"
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"
export default function CollapsibleOpenByDefault() {
return (
<Collapsible defaultOpen className="flex w-full max-w-sm flex-col gap-2 text-sm">
<div className="flex items-center justify-between gap-4">
<span className="font-medium">Payment details</span>
<CollapsibleTrigger render={<Button variant="ghost" size="icon-sm" aria-label="Payment details" />}>
<ChevronsUpDownIcon />
</CollapsibleTrigger>
</div>
<CollapsibleContent>
<dl className="grid grid-cols-2 gap-y-1 text-muted-foreground">
<dt>Invoice</dt>
<dd className="text-right tabular-nums">INV-0412</dd>
<dt>Due</dt>
<dd className="text-right tabular-nums">30 November</dd>
<dt>Amount</dt>
<dd className="text-right tabular-nums">1,240.00</dd>
</dl>
</CollapsibleContent>
</Collapsible>
)
}
Disabled
Archived tasks
import { Button } from "@/components/ui/button"
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"
export default function CollapsibleDisabled() {
return (
<Collapsible disabled className="flex w-full max-w-sm flex-col gap-2 text-sm">
<div className="flex items-center justify-between gap-4">
<span className="font-medium">Archived tasks</span>
<CollapsibleTrigger render={<Button variant="ghost" size="sm" />}>Show tasks</CollapsibleTrigger>
</div>
<CollapsibleContent>
<p className="text-muted-foreground">Nothing has been archived yet.</p>
</CollapsibleContent>
</Collapsible>
)
}
Without the height animation
Keyboard shortcuts
import { Button } from "@/components/ui/button"
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"
export default function CollapsibleWithoutTheHeightAnimation() {
return (
<Collapsible className="flex w-full max-w-sm flex-col gap-2 text-sm">
<div className="flex items-center justify-between gap-4">
<span className="font-medium">Keyboard shortcuts</span>
<CollapsibleTrigger render={<Button variant="ghost" size="sm" />}>Show shortcuts</CollapsibleTrigger>
</div>
<CollapsibleContent data-instant>
<p className="text-muted-foreground">
This panel carries <code>data-instant</code>, so it opens at once. Use it where a list is toggled often.
</p>
</CollapsibleContent>
</Collapsible>
)
}
Installation
npx shadcn@latest add @khaeli/collapsiblebunx --bun shadcn@latest add @khaeli/collapsibleInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- npm packages
- @base-ui/react ^1.7.0
Keyboard
EnterSpace- Opens or closes the panel from the trigger.
Tab- Moves to the trigger, then into the open panel. With
disabledon the root the trigger is a native disabled button and is skipped.
Accessible name
- The trigger is a button named by its text and exposes
aria-expanded. Say what it shows, and change the text when it changes what it does: Show 3 changes, Hide changes. - An icon-only trigger needs an
aria-labelthat names what it opens. Do not put the state in the label:aria-expandedalready says it.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/collapsible.json