Skip to content
GitHub

Collapsible

An interactive component which expands/collapses a panel.

Order #4189

StatusShipped
import { useState } from "react";
import { Button } from "@/components/ui/pxl/button";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/pxl/collapsible";
export default function CollapsibleDemo() {
const [isOpen, setIsOpen] = useState(false);
return (
<Collapsible
open={isOpen}
onOpenChange={setIsOpen}
className="flex w-87.5 flex-col gap-2"
>
<div className="flex items-center justify-between gap-4 px-4">
<h4 className="text-sm font-semibold">Order #4189</h4>
<CollapsibleTrigger
render={
<Button variant="ghost" size="icon" className="size-8">
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24"><path d="M13 20h-2v-2h2v2Zm-2-2H9v-2h2v2Zm4 0h-2v-2h2v2Zm-6-2H7v-2h2v2Zm8-2v2h-2v-2h2Zm-8-4H7V8h2v2Zm8 0h-2V8h2v2Zm-6-2H9V6h2v2Zm4 0h-2V6h2v2Zm-2-2h-2V4h2v2Z"/></svg>
<span className="sr-only">Toggle details</span>
</Button>
}
/>
</div>
<div className="flex items-center justify-between pixel-border px-4 py-2 text-sm">
<span className="text-muted-foreground">Status</span>
<span className="font-medium">Shipped</span>
</div>
<CollapsibleContent className="flex flex-col gap-2">
<div className="pixel-border px-4 py-2 text-sm">
<p className="font-medium">Shipping address</p>
<p className="text-muted-foreground">100 Market St, San Francisco</p>
</div>
<div className="pixel-border px-4 py-2 text-sm">
<p className="font-medium">Items</p>
<p className="text-muted-foreground">2x Studio Headphones</p>
</div>
</CollapsibleContent>
</Collapsible>
);
}
pnpm dlx shadcn@latest add pxl-ui/registry/collapsible
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/pxl/collapsible"
<Collapsible>
<CollapsibleTrigger>Can I use this in my project?</CollapsibleTrigger>
<CollapsibleContent>
Yes. Free to use for personal and commercial projects. No attribution
required.
</CollapsibleContent>
</Collapsible>

Use the following composition to build a Collapsible:

Collapsible
├── CollapsibleTrigger
└── CollapsibleContent

Use the open and onOpenChange props to control the state.

import { useState } from "react"
export function Example() {
const [open, setOpen] = useState(false)
return (
<Collapsible open={open} onOpenChange={setOpen}>
<CollapsibleTrigger>Toggle</CollapsibleTrigger>
<CollapsibleContent>Content</CollapsibleContent>
</Collapsible>
)
}

Use transition="easeOut" to enable animated transition.

import { Button } from "@/components/ui/pxl/button";
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@/components/ui/pxl/collapsible";
export default function CollapsibleTransition() {
return (
<Collapsible className="mx-auto w-full max-w-sm h-fit pixel-rounded data-open:bg-muted">
<CollapsibleTrigger
render={
<Button variant="ghost" className="w-full">
Product details
<svg
className="ml-auto group-data-panel-open/button:rotate-180"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 24 24"
>
<path d="M13 16h-2v-2h2v2Zm-2-2H9v-2h2v2Zm4 0h-2v-2h2v2Zm-6-2H7v-2h2v2Zm8 0h-2v-2h2v2ZM7 10H5V8h2v2Zm12 0h-2V8h2v2Z" />
</svg>
</Button>
}
/>
<CollapsibleContent
transition="easeOut"
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>
);
}

See the Base UI documentation for more information.