Collapsible
Togglable content section with animated expand/collapse. Controlled or uncontrolled.
Examples
Basic
An interactive component which expands/collapses a panel.
import { Button } from "@codefast/ui/button";
import { Card, CardContent } from "@codefast/ui/card";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@codefast/ui/collapsible";
import { ChevronDownIcon } from "lucide-react";
export function CollapsibleBasic() {
return (
<Card className="mx-auto w-full max-w-sm">
<CardContent>
<Collapsible className="rounded-md data-[state=open]:bg-muted">
<CollapsibleTrigger asChild>
<Button variant="ghost" className="group w-full">
Product details
<ChevronDownIcon className="ms-auto group-data-[state=open]:rotate-180" />
</Button>
</CollapsibleTrigger>
<CollapsibleContent className="flex flex-col items-start gap-2 p-2.5 pt-0 text-sm">
<div>This panel can be expanded or collapsed to reveal additional content.</div>
<Button size="xs">Learn More</Button>
</CollapsibleContent>
</Collapsible>
</CardContent>
</Card>
);
}
File Tree
Use nested collapsibles to build a file tree.
import { Button } from "@codefast/ui/button";
import { Card, CardContent, CardHeader } from "@codefast/ui/card";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@codefast/ui/collapsible";
import { Tabs, TabsList, TabsTrigger } from "@codefast/ui/tabs";
import { ChevronRightIcon, FileIcon, FolderIcon } from "lucide-react";
type FileTreeItem = { name: string } | { name: string; items: Array<FileTreeItem> };
export function CollapsibleFileTree() {
const fileTree: Array<FileTreeItem> = [
{
name: "components",
items: [
{
name: "ui",
items: [
{ name: "button.tsx" },
{ name: "card.tsx" },
{ name: "dialog.tsx" },
{ name: "input.tsx" },
{ name: "select.tsx" },
{ name: "table.tsx" },
],
},
{ name: "login-form.tsx" },
{ name: "register-form.tsx" },
],
},
{
name: "lib",
items: [{ name: "utils.ts" }, { name: "cn.ts" }, { name: "api.ts" }],
},
{
name: "hooks",
items: [{ name: "use-media-query.ts" }, { name: "use-debounce.ts" }, { name: "use-local-storage.ts" }],
},
{
name: "types",
items: [{ name: "index.d.ts" }, { name: "api.d.ts" }],
},
{
name: "public",
items: [{ name: "favicon.ico" }, { name: "logo.svg" }, { name: "images" }],
},
{ name: "app.tsx" },
{ name: "layout.tsx" },
{ name: "globals.css" },
{ name: "package.json" },
{ name: "tsconfig.json" },
{ name: "README.md" },
{ name: ".gitignore" },
];
const renderItem = (fileItem: FileTreeItem) => {
if ("items" in fileItem) {
return (
<Collapsible key={fileItem.name}>
<CollapsibleTrigger asChild>
<Button
variant="ghost"
size="sm"
className="group w-full justify-start transition-none hover:bg-accent hover:text-accent-foreground"
>
<ChevronRightIcon className="transition-transform group-data-[state=open]:rotate-90" />
<FolderIcon />
{fileItem.name}
</Button>
</CollapsibleTrigger>
<CollapsibleContent className="style-lyra:ms-4 ms-5 mt-1">
<div className="flex flex-col gap-1">{fileItem.items.map((child) => renderItem(child))}</div>
</CollapsibleContent>
</Collapsible>
);
}
return (
<Button key={fileItem.name} variant="link" size="sm" className="w-full justify-start gap-2 text-foreground">
<FileIcon />
<span>{fileItem.name}</span>
</Button>
);
};
return (
<Card className="mx-auto w-full max-w-64 gap-2" size="sm">
<CardHeader>
<Tabs defaultValue="explorer">
<TabsList className="w-full">
<TabsTrigger value="explorer">Explorer</TabsTrigger>
<TabsTrigger value="settings">Outline</TabsTrigger>
</TabsList>
</Tabs>
</CardHeader>
<CardContent>
<div className="flex flex-col gap-1">{fileTree.map((item) => renderItem(item))}</div>
</CardContent>
</Card>
);
}
RTL
Right-to-left layout support for languages such as Arabic and Hebrew.
Translations are AI-generated for demonstration and may be imperfect.
الطلب #4189
import { Button } from "@codefast/ui/button";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@codefast/ui/collapsible";
import { ChevronsUpDown } from "lucide-react";
import * as React from "react";
import type { Translations } from "#/features/components-catalog/components/detail/language";
import { useTranslation } from "#/features/components-catalog/components/detail/language-context";
const translations: Translations = {
en: {
dir: "ltr",
values: {
orderNumber: "Order #4189",
status: "Status",
shipped: "Shipped",
shippingAddress: "Shipping address",
address: "100 Market St, San Francisco",
items: "Items",
itemsDescription: "2x Studio Headphones",
},
},
ar: {
dir: "rtl",
values: {
orderNumber: "الطلب #4189",
status: "الحالة",
shipped: "تم الشحن",
shippingAddress: "عنوان الشحن",
address: "100 Market St, San Francisco",
items: "العناصر",
itemsDescription: "2x سماعات الاستوديو",
},
},
he: {
dir: "rtl",
values: {
orderNumber: "הזמנה #4189",
status: "סטטוס",
shipped: "נשלח",
shippingAddress: "כתובת משלוח",
address: "100 Market St, San Francisco",
items: "פריטים",
itemsDescription: "2x אוזניות סטודיו",
},
},
};
export function CollapsibleRtl() {
const { dir, t } = useTranslation(translations, "ar");
const [isOpen, setIsOpen] = React.useState(false);
return (
<Collapsible open={isOpen} onOpenChange={setIsOpen} className="flex w-87.5 flex-col gap-2" dir={dir}>
<div className="flex items-center justify-between gap-4 px-4">
<h4 className="text-sm font-semibold">{t.orderNumber}</h4>
<CollapsibleTrigger asChild>
<Button variant="ghost" size="icon" className="size-8">
<ChevronsUpDown />
<span className="sr-only">Toggle details</span>
</Button>
</CollapsibleTrigger>
</div>
<div className="flex items-center justify-between rounded-md border px-4 py-2 text-sm">
<span className="text-muted-foreground">{t.status}</span>
<span className="font-medium">{t.shipped}</span>
</div>
<CollapsibleContent className="flex flex-col gap-2">
<div className="rounded-md border px-4 py-2 text-sm">
<p className="font-medium">{t.shippingAddress}</p>
<p className="text-muted-foreground">{t.address}</p>
</div>
<div className="rounded-md border px-4 py-2 text-sm">
<p className="font-medium">{t.items}</p>
<p className="text-muted-foreground">{t.itemsDescription}</p>
</div>
</CollapsibleContent>
</Collapsible>
);
}
Settings Panel
Use a trigger button to reveal additional settings.
import { Button } from "@codefast/ui/button";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@codefast/ui/card";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@codefast/ui/collapsible";
import { Field, FieldGroup, FieldLabel } from "@codefast/ui/field";
import { Input } from "@codefast/ui/input";
import { MaximizeIcon, MinimizeIcon } from "lucide-react";
import * as React from "react";
export function CollapsibleSettings() {
const [isOpen, setIsOpen] = React.useState(false);
return (
<Card className="mx-auto w-full max-w-xs" size="sm">
<CardHeader>
<CardTitle>Radius</CardTitle>
<CardDescription>Set the corner radius of the element.</CardDescription>
</CardHeader>
<CardContent>
<Collapsible open={isOpen} onOpenChange={setIsOpen} className="flex items-start gap-2">
<FieldGroup className="grid w-full grid-cols-2 gap-2">
<Field>
<FieldLabel htmlFor="radius-x" className="sr-only">
Radius X
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue={0} />
</Field>
<Field>
<FieldLabel htmlFor="radius-y" className="sr-only">
Radius Y
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue={0} />
</Field>
<CollapsibleContent className="col-span-full grid grid-cols-subgrid gap-2">
<Field>
<FieldLabel htmlFor="radius-x" className="sr-only">
Radius X
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue={0} />
</Field>
<Field>
<FieldLabel htmlFor="radius-y" className="sr-only">
Radius Y
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue={0} />
</Field>
</CollapsibleContent>
</FieldGroup>
<CollapsibleTrigger asChild>
<Button variant="outline" size="icon">
{isOpen ? <MinimizeIcon /> : <MaximizeIcon />}
</Button>
</CollapsibleTrigger>
</Collapsible>
</CardContent>
</Card>
);
}
Usage
The minimal import and composition — see Examples below for styled, real-world variants.
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@codefast/ui/collapsible";
export function CollapsibleUsage() {
return (
<Collapsible>
<CollapsibleTrigger>Can I use this in my project?</CollapsibleTrigger>
<CollapsibleContent>Yes. Free to use for personal and commercial projects.</CollapsibleContent>
</Collapsible>
);
}
Anatomy
How the parts nest — every slot the component exposes, in composition order.
Features
- open/onOpenChange for controlled state, or defaultOpen uncontrolled — the same pattern as Accordion and Dialog.
- CollapsibleTrigger exposes aria-expanded automatically; pair with asChild to make any element (a Button, a row) the trigger.
- For several independent, mutually-exclusive sections, reach for Accordion instead — Collapsible is a single region.
API reference
Props for each part of the component. All native element props are also forwarded.
Collapsible
A single togglable region.
openbooleanThe controlled open state.
defaultOpenbooleanThe open state when initially rendered (uncontrolled).
Default
falseonOpenChange(open: boolean) => voidCalled when the open state changes.
disabledbooleanPrevents toggling.
Default
false
Accessibility
Built to be keyboard-navigable and screen-reader friendly out of the box.
| Key | Function |
|---|---|
| Tab | Moves focus to the trigger. |
| Space | Toggles the content. |
| Enter | Toggles the content. |
- The trigger exposes aria-expanded and controls the content region.
- For several independent sections, use an Accordion instead.
- Keep the always-visible summary meaningful on its own.
Guidelines
Conventions that keep usage consistent across an app.
Do
- Use to hide secondary detail behind a single toggle.
- Show a clear summary of what’s hidden.
Don’t
- Don’t hide content users need at a glance.
- Don’t use for many sections — that’s an Accordion.
Explore further
Ready to integrate?
Follow the Getting Started guide to install @codefast/ui, or browse the full component gallery.