Collapsible for Astro
One expandable Astro panel for optional detail or secondary controls.
Installation
bunx --bun shadcn@latest add @gnostic/collapsiblenpx shadcn@latest add @gnostic/collapsiblepnpm dlx shadcn@latest add @gnostic/collapsibleComplete the Installation guide first. Then copy the component files below.
Install component packages
bun add @data-slot/collapsibleCopy the files
src/components/ui/collapsible/collapsible-content.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"div">
const { class: className, hidden: _hidden, ...props } = Astro.props
---
<div
data-slot="collapsible-content"
class={cn(
"h-(--collapsible-panel-height) overflow-hidden transition-[height] duration-200 data-ending-style:h-0 data-starting-style:h-0 data-[state=closed]:h-0",
className,
)}
{...props}
>
<slot />
</div>
src/components/ui/collapsible/collapsible-trigger.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"button">
const { class: className, type = "button", ...props } = Astro.props
---
<button
data-slot="collapsible-trigger"
type={type}
class={cn(className)}
{...props}
>
<slot />
</button>
src/components/ui/collapsible/collapsible.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import type { CollapsibleProps } from "./types.ts"
type Props = HTMLAttributes<"div"> & CollapsibleProps
const {
class: className,
defaultOpen,
hiddenUntilFound,
closeOnEscape,
closeOnClickOutside,
...props
} = Astro.props
---
<div
data-slot="collapsible"
data-default-open={defaultOpen}
data-hidden-until-found={hiddenUntilFound}
data-close-on-escape={closeOnEscape}
data-close-on-click-outside={closeOnClickOutside}
class={cn(className)}
{...props}
>
<slot />
</div>
<script>
import { create } from "@data-slot/collapsible"
import type { CollapsibleController } from "@data-slot/collapsible"
// Destroy bindings before a view transition so the next page-load binds once.
const controllers: CollapsibleController[] = []
const initialize = () => controllers.push(...create())
initialize()
document.addEventListener("astro:page-load", initialize)
document.addEventListener("astro:before-swap", () => {
for (const controller of controllers) controller.destroy()
controllers.length = 0
})
</script>
src/components/ui/collapsible/index.ts
export { default as Collapsible } from './collapsible.astro'
export { default as CollapsibleContent } from './collapsible-content.astro'
export { default as CollapsibleTrigger } from './collapsible-trigger.astro'
export type { CollapsibleProps } from './types.ts'
src/components/ui/collapsible/types.ts
import type { CollapsibleOptions } from '@data-slot/collapsible'
/**
* Collapsible props.
*/
export type CollapsibleProps = Pick<
CollapsibleOptions,
'defaultOpen' | 'hiddenUntilFound' | 'closeOnEscape' | 'closeOnClickOutside'
>
Usage
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/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>
Composition
Arrange the parts as shown for a Collapsible:
Collapsible
├── CollapsibleTrigger
└── CollapsibleContent
Default Open
Use defaultOpen to render a collapsible expanded on first load.
<Collapsible defaultOpen>
<CollapsibleTrigger>Toggle</CollapsibleTrigger>
<CollapsibleContent>Content</CollapsibleContent>
</Collapsible>
Examples
Basic
Settings Panel
Use a trigger button to reveal additional settings.
File Tree
Use nested collapsibles to build a file tree.
Notes
- Use
Collapsiblefor progressive disclosure inside an existing layout. - Keep
CollapsibleTriggeras the single control that opens and closes the content. - For stacked FAQ patterns with multiple items, prefer Accordion.
API Reference
CollapsibleProps
defaultOpen?boolean
Initial open state
booleanhiddenUntilFound?boolean
Use hidden="until-found" when closed so browser find-in-page can reveal content.
booleanfalsecloseOnEscape?boolean
Close when pressing Escape. Focus returns to the trigger when it was inside the collapsible.
booleanfalsecloseOnClickOutside?boolean
Close on a pointer press outside the collapsible.
booleanfalseEvents
Dispatch these events on the collapsible root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
collapsible:change |
{ open: boolean } |
Inbound events
| Event | Detail |
|---|---|
collapsible:set |
{ open: boolean } |
When to use Collapsible
Use Collapsible when a single piece of supporting content can be hidden. Place its trigger and content under the same root. Keep the trigger text useful in both open and closed states.
Keyboard and behavior
The data-slot script changes expanded state and panel visibility. A button trigger supports keyboard activation. Do not hide the only route to an essential action behind an unexplained icon.
Theme and layout
The panel reads its height from the controller for the open and close transition. Put padding on an inner child if clipping during motion affects the layout. Theme styles come from the content you place inside it.