Collapsible
An interactive component which expands/collapses a panel.
Order #4189
StatusShipped
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { createSignal } from "solid-js"
import { Button } from "~/components/ui/button"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger
} from "~/components/ui/collapsible"
export default function CollapsibleDemo() {
const [isOpen, setIsOpen] = createSignal(false)
return (
<Collapsible
open={isOpen()}
onOpenChange={setIsOpen}
class="flex w-[350px] flex-col gap-2"
>
<div class="flex items-center justify-between gap-4 px-4">
<h4 class="text-sm font-semibold">Order #4189</h4>
<CollapsibleTrigger
render={(partProps) => (
<Button {...partProps} variant="ghost" size="icon" />
)}
class="size-8"
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="m7 15 5 5 5-5" />
<path d="m7 9 5-5 5 5" />
</svg>
<span class="sr-only">Toggle details</span>
</CollapsibleTrigger>
</div>
<div class="flex items-center justify-between rounded-md border px-4 py-2 text-sm">
<span class="text-muted-foreground">Status</span>
<span class="font-medium">Shipped</span>
</div>
<CollapsibleContent class="flex flex-col gap-2">
<div class="rounded-md border px-4 py-2 text-sm">
<p class="font-medium">Shipping address</p>
<p class="text-muted-foreground">100 Market St, San Francisco</p>
</div>
<div class="rounded-md border px-4 py-2 text-sm">
<p class="font-medium">Items</p>
<p class="text-muted-foreground">2x Studio Headphones</p>
</div>
</CollapsibleContent>
</Collapsible>
)
}Installation
Install the following dependencies:
Copy and paste the following code into your project.
import { type ComponentProps } from "@solidjs/web"
import {
Collapsible as SdkCollapsible,
CollapsibleTrigger as SdkCollapsibleTrigger,
CollapsiblePanel as SdkCollapsiblePanel
} from "@ui-sdk/solid/collapsible"
import { omit } from "solid-js"
type CollapsibleProps = Omit<ComponentProps<typeof SdkCollapsible>, "class"> & {
class?: string
}
const Collapsible = (props: CollapsibleProps) => (
<SdkCollapsible {...props} class={props.class} />
)
type CollapsibleTriggerProps = Omit<
ComponentProps<typeof SdkCollapsibleTrigger>,
"class"
> & {
class?: string
}
const CollapsibleTrigger = (props: CollapsibleTriggerProps) => {
const others = omit(props, "class", "children")
return (
<SdkCollapsibleTrigger class={props.class} {...others}>
{props.children}
</SdkCollapsibleTrigger>
)
}
type CollapsibleContentProps = Omit<
ComponentProps<typeof SdkCollapsiblePanel>,
"class"
> & {
class?: string
}
const CollapsibleContent = (props: CollapsibleContentProps) => {
const others = omit(props, "class", "children")
return (
<SdkCollapsiblePanel class={props.class} {...others}>
{props.children}
</SdkCollapsiblePanel>
)
}
export { Collapsible, CollapsibleContent, CollapsibleTrigger }Update the import paths to match your project setup.
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
Use the following composition to build a Collapsible:
Collapsible
โโโ CollapsibleTrigger
โโโ CollapsibleContentControlled State
Use open and onOpenChange to fully control the expanded state.
const [open, setOpen] = createSignal(false)
<Collapsible open={open()} onOpenChange={setOpen}>
<CollapsibleTrigger>Toggle details</CollapsibleTrigger>
<CollapsibleContent>Controlled content</CollapsibleContent>
</Collapsible>Basic
Yes. It is built with UI SDK components, so it supports keyboard navigation and controlled or uncontrolled state.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger
} from "~/components/ui/collapsible"
export default function CollapsibleBasic() {
return (
<Collapsible class="w-full max-w-sm rounded-md border" defaultOpen>
<CollapsibleTrigger class="group/collapsible-trigger flex w-full items-center justify-between px-4 py-3 text-left text-sm font-medium hover:bg-muted/50">
<span>Can I use this component in my project?</span>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4 shrink-0 text-muted-foreground transition-transform group-data-[expanded]/collapsible-trigger:rotate-180"
>
<path d="m6 9 6 6 6-6" />
</svg>
</CollapsibleTrigger>
<CollapsibleContent class="px-4 pb-4 text-sm text-muted-foreground">
Yes. It is built with UI SDK components, so it supports keyboard
navigation and controlled or uncontrolled state.
</CollapsibleContent>
</Collapsible>
)
}Settings Panel
Use a trigger button to reveal additional settings.
Radius
Set the corner radius of the element.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { createSignal, Show } from "solid-js"
import { Button } from "~/components/ui/button"
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle
} from "~/components/ui/card"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger
} from "~/components/ui/collapsible"
import { Field, FieldGroup, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"
export default function CollapsibleSettings() {
const [isOpen, setIsOpen] = createSignal(false)
return (
<Card class="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}
class="flex items-start gap-2"
>
<FieldGroup class="grid w-full grid-cols-2 gap-2">
<Field>
<FieldLabel for="radius-x" class="sr-only">
Radius X
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue="0" />
</Field>
<Field>
<FieldLabel for="radius-y" class="sr-only">
Radius Y
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue="0" />
</Field>
<CollapsibleContent class="col-span-full grid grid-cols-subgrid gap-2">
<Field>
<FieldLabel for="radius-x" class="sr-only">
Radius X
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue="0" />
</Field>
<Field>
<FieldLabel for="radius-y" class="sr-only">
Radius Y
</FieldLabel>
<Input id="radius" placeholder="0" defaultValue="0" />
</Field>
</CollapsibleContent>
</FieldGroup>
<CollapsibleTrigger
render={(partProps) => (
<Button {...partProps} variant="outline" size="icon" />
)}
>
<Show
when={isOpen()}
fallback={
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M8 3H5a2 2 0 0 0-2 2v3" />
<path d="M21 8V5a2 2 0 0 0-2-2h-3" />
<path d="M3 16v3a2 2 0 0 0 2 2h3" />
<path d="M16 21h3a2 2 0 0 0 2-2v-3" />
</svg>
}
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="M8 3v3a2 2 0 0 1-2 2H3" />
<path d="M21 8h-3a2 2 0 0 1-2-2V3" />
<path d="M3 16h3a2 2 0 0 1 2 2v3" />
<path d="M16 21v-3a2 2 0 0 1 2-2h3" />
</svg>
</Show>
</CollapsibleTrigger>
</Collapsible>
</CardContent>
</Card>
)
}File Tree
Use nested collapsibles to build a file tree.
// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import type { JSX } from "@solidjs/web"
import { For } from "solid-js"
import { Button } from "~/components/ui/button"
import { Card, CardContent, CardHeader } from "~/components/ui/card"
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger
} from "~/components/ui/collapsible"
import { Tabs, TabsList, TabsTrigger } from "~/components/ui/tabs"
type FileTreeItem = { name: string } | { name: string; items: FileTreeItem[] }
const fileTree: 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 hasChildren = (
item: FileTreeItem
): item is { name: string; items: FileTreeItem[] } => {
return "items" in item
}
const renderItem = (item: FileTreeItem): JSX.Element => {
if (hasChildren(item)) {
return (
<Collapsible>
<CollapsibleTrigger
render={(partProps) => (
<Button {...partProps} size="sm" variant="ghost" />
)}
class="group/collapsible-trigger w-full justify-start transition-none hover:bg-accent hover:text-accent-foreground"
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4 transition-transform group-data-[expanded]/collapsible-trigger:rotate-90"
>
<path d="m9 18 6-6-6-6" />
</svg>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z" />
</svg>
{item.name}
</CollapsibleTrigger>
<CollapsibleContent class="mt-1 ml-5">
<div class="flex flex-col gap-1">
<For each={item.items}>{(child) => renderItem(child)}</For>
</div>
</CollapsibleContent>
</Collapsible>
)
}
return (
<Button
class="w-full justify-start gap-2 text-foreground"
size="sm"
variant="link"
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
class="size-4"
>
<path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z" />
<path d="M14 2v5a1 1 0 0 0 1 1h5" />
</svg>
<span>{item.name}</span>
</Button>
)
}
export default function CollapsibleFileTree() {
return (
<Card class="mx-auto w-full max-w-[16rem] gap-2" size="sm">
<CardHeader>
<Tabs defaultValue="explorer">
<TabsList class="w-full">
<TabsTrigger value="explorer">Explorer</TabsTrigger>
<TabsTrigger value="outline">Outline</TabsTrigger>
</TabsList>
</Tabs>
</CardHeader>
<CardContent>
<div class="flex flex-col gap-1 text-sm">
<For each={fileTree}>{(item) => renderItem(item)}</For>
</div>
</CardContent>
</Card>
)
}