Skip to content
← Components

Collapsible

Collapsible with thoughtful defaults, semantic styling, and editable source.

Open ↗

Make it yours.

Install the editable source and its dependencies into your React + Tailwind project.

Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/collapsible.json

Use it

Scroll horizontally for long lines
"use client";
import { Collapsible } from "@/components/jez-ui/ui/collapsible";
import { FileCode2 } from "lucide-react";

export default function Example() {
  return (
    <>
      <div className="w-full max-w-md">
        <Collapsible
          title="Changed files · 3"
          defaultOpen
        >
          {[
            "components/button.tsx",
            "styles/theme.css",
            "tests/controls.spec.ts",
          ].map((file, i) => (
            <div
              key={file}
              className="flex items-center gap-2 py-2 text-sm"
            >
              <FileCode2
                size={15}
                className="shrink-0 text-muted-foreground"
              />
              <span className="min-w-0 flex-1 truncate font-mono text-xs">
                {file}
              </span>
              <span className="text-xs text-primary">+{[24, 8, 16][i]}</span>
            </div>
          ))}
        </Collapsible>
      </div>
    </>
  );
}

Compose your own

These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.

Scroll horizontally for long lines
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from "@/components/jez-ui/ui/collapsible";
Read the composition guide →

Props & types

Scroll horizontally for long lines
Omit<React.ComponentProps<typeof P.Root>, "title"> & {
  title?: React.ReactNode;
}

Source

View 2 source files

registry/ui/collapsible.tsx

Scroll horizontally for long lines
"use client";
import * as React from "react";
import { Collapsible as P } from "radix-ui";
import { ChevronDown } from "lucide-react";
import { cn } from "./utils";
export function Collapsible({
  title,
  children,
  className,
  ...props
}: Omit<React.ComponentProps<typeof P.Root>, "title"> & {
  title?: React.ReactNode;
}) {
  return (
    <P.Root
      className={cn(
        "w-full overflow-hidden rounded-lg border border-border",
        className,
      )}
      {...props}
    >
      {title !== undefined ? (
        <>
          <CollapsibleTrigger>{title}</CollapsibleTrigger>
          <CollapsibleContent>{children}</CollapsibleContent>
        </>
      ) : (
        children
      )}
    </P.Root>
  );
}
export function CollapsibleTrigger({
  children,
  className,
  ...props
}: React.ComponentProps<typeof P.Trigger>) {
  return (
    <P.Trigger
      className={cn(
        "group flex w-full items-center justify-between gap-4 px-4 py-3.5 text-left text-sm font-medium outline-none hover:bg-muted/40 focus-visible:ring-2 focus-visible:ring-primary",
        className,
      )}
      {...props}
    >
      {children}
      <ChevronDown
        size={16}
        className="shrink-0 text-muted-foreground transition-transform group-data-[state=open]:rotate-180 motion-reduce:transition-none"
      />
    </P.Trigger>
  );
}
export function CollapsibleContent({
  children,
  className,
  ...props
}: React.ComponentProps<typeof P.Content>) {
  return (
    <P.Content className="jez-collapse overflow-hidden" {...props}>
      <div
        className={cn(
          "border-t border-border px-4 py-4 text-sm leading-relaxed text-muted-foreground",
          className,
        )}
      >
        {children}
      </div>
    </P.Content>
  );
}

registry/ui/utils.ts

Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
  return twMerge(clsx(values));
}

Dependencies

radix-ui@1.6.7 · lucide-react@0.577.0 · clsx@2.1.1 · tailwind-merge@3.5.0

Accessibility & behaviour

Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.

Read the accessibility and performance guide →