Show or hide a single section of content.
Order details
Sizes
Chevron only
Shipping address
Controlled and persistent
Find in page and disabled
Purpose
A collapsible lets people show or hide one section, such as order details, an address, or a set of optional settings.
Parts & Composition
Place the trigger and panel inside the same root. The trigger can fill the row or sit beside a heading as a chevron button.
| Part | Role |
|---|---|
Collapsible | Root that owns panel visibility. |
CollapsibleTrigger | Button that opens or closes the panel. |
CollapsiblePanel | Content shown by the trigger. |
API & Props
| Prop / part | Purpose |
|---|---|
open / defaultOpen | Control or initialize visibility. |
onOpenChange | Receives the requested open state and SDK details. Use details.cancel() to decline the change. |
onOpenChangeComplete | Runs after the opening or closing transition completes. |
disabled | Prevents user activation. The trigger remains focusable and announces its disabled state. |
controller | Use createCollapsibleController() to open, close, toggle, or focus from app code. |
Collapsible.variant / size | Root surface plain or card. Sizes are xs, sm, md (default), and lg. |
CollapsibleTrigger.size | Overrides the size inherited from the root. |
CollapsibleTrigger.icon / iconOnly | Replace or hide the default chevron, or use a compact icon-only trigger. |
CollapsiblePanel.keepMounted | Preserve the content and its local state while closed. |
CollapsiblePanel.hiddenUntilFound | Allow supported browsers to find and reveal closed content. |
CollapsiblePanel.separator | Add the shared Separator divider above the panel content. Defaults to false. |
render / className / ref | SDK render override, caller styles, and access to the rendered element. |
Use defaultOpen for local disclosure state, or pass open and update it from onOpenChange. Choose keepMounted when a panel contains a draft or form field that should retain its value while hidden. Form submission and validation remain owned by the form.
Theming, Layout & Responsiveness
Plain disclosures inherit the surrounding surface. The card variant uses card, border, and foreground theme tokens. Parts expose SDK open, closed, and disabled state attributes.
The full trigger fills the available width and can wrap. The icon-only trigger uses the same shared size scale in a square.
Motion & Accessibility
The panel animates from its measured height using --collapsible-panel-height. The chevron follows the trigger’s open state. Reduced motion disables the transitions.
Use visible text or supply an accessible name for icon-only triggers. UI SDK associates the trigger with the panel and exposes its expanded state. Enter and Space toggle the focused trigger.
Guidance for Coding Agents
Install this component using the command in the Install tab, then read this Info page before composing it.
Start with the closest Preview → Usage example. Use Collapsible for one independent section. Compose the trigger and panel under the same root, keep the heading outside an icon-only trigger, and give that trigger a descriptive name. Choose controlled state when other parts of the app need to open or close the panel. Use keepMounted when closing should preserve a draft.
Use the API table above for component styling and the UI SDK Collapsible reference for inherited callbacks, render overrides, browser Find behavior, and controller methods.
CLI installation
bunx --bun shadcn@latest add https://ui.devvarena.com/api/r/collapsible'use client';
import {
Collapsible as SDKCollapsible,
CollapsiblePanel as SDKCollapsiblePanel,
CollapsibleTrigger as SDKCollapsibleTrigger,
} from '@ui-sdk/react/collapsible';
import { cva } from 'class-variance-authority';
import { ChevronDown } from 'lucide-react';
import { type ComponentProps, createContext, type ReactNode, useContext } from 'react';
import { Separator } from '@/components/devvarena-ui/separator';
import {
concentricRadius,
controlHeights,
controlMinHeights,
controlTextSizes,
partClass,
type StandardControlSize,
surfacePadding,
surfaceRegionInset,
} from '@/lib/devvarena-ui';
import { cn } from '@/lib/utils';
const SizeContext = createContext<StandardControlSize>('md');
const collapsibleVariants = cva('group/collapsible min-w-0', {
variants: {
variant: {
plain: 'text-foreground',
card: `rounded-lg border border-border bg-card text-card-foreground shadow-sm ${surfacePadding}`,
},
size: {
xs: `${controlTextSizes.xs} [--surface-padding:1.3rem]`,
sm: `${controlTextSizes.sm} [--surface-padding:1.35rem]`,
md: controlTextSizes.md,
lg: `${controlTextSizes.lg} [--surface-padding:1.75rem]`,
} satisfies Record<StandardControlSize, string>,
},
});
const defaultIcon = <ChevronDown />;
type CollapsibleProps = ComponentProps<typeof SDKCollapsible> & {
size?: StandardControlSize;
variant?: 'plain' | 'card';
};
function Collapsible({ size = 'md', variant = 'plain', className, ...props }: CollapsibleProps) {
return (
<SizeContext value={size}>
<SDKCollapsible
{...props}
data-size={size}
data-variant={variant}
className={partClass(collapsibleVariants({ variant, size }), className)}
/>
</SizeContext>
);
}
type CollapsibleTriggerProps = ComponentProps<typeof SDKCollapsibleTrigger> & {
size?: StandardControlSize;
icon?: ReactNode;
iconOnly?: boolean;
};
function CollapsibleTrigger({
size: sizeProp,
icon = defaultIcon,
iconOnly = false,
children,
className,
render,
...props
}: CollapsibleTriggerProps) {
const inheritedSize = useContext(SizeContext);
const size = sizeProp ?? inheritedSize;
return (
<SDKCollapsibleTrigger
{...props}
render={render}
data-size={size}
className={partClass(
`group/collapsible-trigger flex min-w-0 cursor-pointer touch-manipulation items-center gap-2 ${concentricRadius} text-start font-medium transition-colors hover:not-data-disabled:bg-accent hover:not-data-disabled:text-accent-foreground data-disabled:cursor-not-allowed data-disabled:opacity-50 motion-reduce:transition-none`,
iconOnly ? controlHeights[size] : controlMinHeights[size],
iconOnly ? 'aspect-square shrink-0 justify-center' : 'w-full justify-between px-3 py-1',
className,
)}
>
{render && children === undefined && icon === defaultIcon && !iconOnly
? undefined
: (state) => (
<>
{children != null && (
<span className="min-w-0 wrap-anywhere">
{typeof children === 'function' ? children(state) : children}
</span>
)}
{icon != null && (
<span
aria-hidden="true"
className="inline-flex size-4 shrink-0 items-center justify-center text-muted-foreground transition-transform duration-200 group-data-open/collapsible-trigger:rotate-180 motion-reduce:transition-none [&>svg]:size-full"
>
{icon}
</span>
)}
</>
)}
</SDKCollapsibleTrigger>
);
}
type CollapsiblePanelProps = ComponentProps<typeof SDKCollapsiblePanel> & {
separator?: boolean;
};
function CollapsiblePanel({
separator = false,
className,
children,
...props
}: CollapsiblePanelProps) {
return (
<SDKCollapsiblePanel
{...props}
className={partClass(
'h-[var(--collapsible-panel-height,auto)] min-w-0 overflow-hidden transition-[height,opacity] duration-200 ease-out data-starting-style:h-0 data-starting-style:opacity-0 data-ending-style:h-0 data-ending-style:opacity-0 motion-reduce:transition-none',
className,
)}
>
{(state) => (
<>
{separator && <Separator />}
<div className={cn(surfaceRegionInset, 'leading-relaxed text-muted-foreground')}>
{typeof children === 'function' ? children(state) : children}
</div>
</>
)}
</SDKCollapsiblePanel>
);
}
export type {
CollapsibleChangeOptions,
CollapsibleController,
CollapsibleOpenChangeReason,
CollapsibleOptions,
CollapsiblePanelState,
CollapsibleState,
CollapsibleTriggerState,
} from '@ui-sdk/react/collapsible';
export { createCollapsibleController } from '@ui-sdk/react/collapsible';
export {
Collapsible,
CollapsiblePanel,
type CollapsiblePanelProps,
type CollapsibleProps,
CollapsibleTrigger,
type CollapsibleTriggerProps,
};
import { Separator as SDKSeparator } from '@ui-sdk/react/separator';
import type { ComponentProps } from 'react';
import { partClass } from '@/lib/devvarena-ui';
type SeparatorProps = ComponentProps<typeof SDKSeparator> & { variant?: 'fade' | 'solid' };
/** Theme-derived fading rule built on `@ui-sdk/react/separator`. */
function Separator({
orientation = 'horizontal',
decorative = true,
variant = 'fade',
className,
...props
}: SeparatorProps) {
return (
<SDKSeparator
{...props}
orientation={orientation}
decorative={decorative}
data-variant={variant}
className={partClass(
'pointer-events-none shrink-0',
orientation === 'horizontal' ? 'h-px w-full' : 'w-px self-stretch',
variant === 'solid'
? 'bg-[var(--separator-color,var(--border))]'
: orientation === 'horizontal'
? 'bg-[linear-gradient(to_right,transparent,var(--separator-color,var(--border))_18%,var(--separator-color,var(--border))_82%,transparent)]'
: 'bg-[linear-gradient(to_bottom,transparent,var(--separator-color,var(--border))_18%,var(--separator-color,var(--border))_82%,transparent)]',
className,
)}
/>
);
}
export type { SeparatorProps };
export { Separator };
import { cn } from '@/lib/utils';
type ClassValue = string | false | null | undefined;
type PartClassValue<State> = ClassValue | ((state: State) => ClassValue);
/** Composes static class names and UI-SDK state class functions into a single `className` prop. */
export function partClass<State>(...parts: PartClassValue<State>[]) {
if (parts.some((part) => typeof part === 'function')) {
return (state: State) =>
cn(...parts.map((part) => (typeof part === 'function' ? part(state) : part)));
}
return cn(...(parts as ClassValue[]));
}
export const surfacePadding = 'p-1 [--surface-padding:1.5rem] [--concentric-inset:0.25rem]';
export const surfaceRegionInset = 'p-[calc(var(--surface-padding,1rem)-0.5rem)]';
export const surfaceHeaderRadius = 'rounded-t-[max(0px,calc(var(--radius-lg,0.5rem)-0.25rem))]';
export const surfaceFooterRadius = 'rounded-b-[max(0px,calc(var(--radius-lg,0.5rem)-0.25rem))]';
/** Concentric inner radius (`outer - inset`); set `[--concentric-inset:0.125rem]` on `p-0.5` rails (defaults to `0.25rem` for `p-1`). */
export const concentricRadius =
'rounded-[max(0px,calc(var(--radius-lg,0.5rem)-var(--concentric-inset,0.25rem)))]';
export const surfaceBodyPadding =
'py-[max(0px,calc(var(--surface-padding,1rem)-1rem))] has-[>[data-slot=list]:not([data-variant=plain])]:p-0 has-[>[data-slot=list-items-container]]:p-0';
export type StandardControlSize = 'xs' | 'sm' | 'md' | 'lg';
/** Standard control heights (`28px / 32px / 40px / 48px`) for buttons, inputs, selects, tabs, and sliders. */
export const controlHeights = {
xs: 'h-7',
sm: 'h-8',
md: 'h-10',
lg: 'h-12',
} as const satisfies Record<StandardControlSize, string>;
/** Standard minimum control heights (`28px / 32px / 40px / 48px`) for expandable controls and menu items. */
export const controlMinHeights = {
xs: 'min-h-7',
sm: 'min-h-8',
md: 'min-h-10',
lg: 'min-h-12',
} as const satisfies Record<StandardControlSize, string>;
/** Standard control text sizes (`xs / xs / sm / base`) paired with `StandardControlSize`. */
export const controlTextSizes = {
xs: 'text-xs',
sm: 'text-xs',
md: 'text-sm',
lg: 'text-base',
} as const satisfies Record<StandardControlSize, string>;
/** Compact control heights (`1.25rem / 1.3rem / 1.625rem / 1.75rem`), one step smaller than standard, for switches and compact controls. */
export const compactControlHeights = {
xs: 'h-[1.3rem]',
sm: 'h-[1.4rem]',
md: 'h-[1.5rem]',
lg: 'h-[1.6rem]',
} as const satisfies Record<StandardControlSize, string>;
/** Compact minimum control heights (`1rem / 1.25rem / 1.5rem / 1.75rem`) for badges and tooltips. */
export const compactControlMinHeights = {
xs: 'min-h-[1.3rem]',
sm: 'min-h-[1.4rem]',
md: 'min-h-[1.5rem]',
lg: 'min-h-[1.6rem]',
} as const satisfies Record<StandardControlSize, string>;
/** Universal keyboard focus + invalid ring/border styles shared across controls. */
export const focusRing =
'touch-manipulation outline-none focus-visible:border-ring/30 transition-ring focus-visible:ring-3 focus-visible:ring-ring/15 focus-visible:brightness-[0.97] dark:focus-visible:brightness-[1.15] has-[:focus-visible]:border-ring/30 has-[:focus-visible]:ring-3 has-[:focus-visible]:ring-ring/15 has-[:focus-visible]:brightness-[0.97] dark:has-[:focus-visible]:brightness-[1.15] data-active:border-ring/30 data-active:ring-3 data-active:ring-ring/15 data-active:brightness-[0.97] dark:data-active:brightness-[1.15] data-open:border-ring/30 data-open:ring-3 data-open:ring-ring/15 data-open:brightness-[0.97] dark:data-open:brightness-[1.15] data-disabled:cursor-not-allowed data-disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/25 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/25 has-[[data-invalid]]:border-destructive has-[[data-invalid]]:ring-3 has-[[data-invalid]]:ring-destructive/25 group-aria-invalid/control:border-destructive group-aria-invalid/control:ring-3 group-aria-invalid/control:ring-destructive/25 group-data-invalid/control:border-destructive group-data-invalid/control:ring-3 group-data-invalid/control:ring-destructive/25 group-has-[input[aria-invalid=true]]/control:border-destructive group-has-[input[aria-invalid=true]]/control:ring-3 group-has-[input[aria-invalid=true]]/control:ring-destructive/25 hover:brightness-[0.97] dark:hover:brightness-[1.15]';
/** Shared sliding highlight indicator base (pointer-safe, concentric inner radius inside rounded-lg containers, 200ms transition). */
export const slidingHighlight =
'pointer-events-none -z-10 rounded-[max(0px,calc(var(--radius-lg,0.5rem)-var(--concentric-inset,0.25rem)))] bg-primary/10 [clip-path:inset(0px_round_max(0px,calc(var(--radius-lg,0.5rem)-var(--concentric-inset,0.25rem))))] transition-[transform,width,height,opacity] duration-200 ease-out motion-reduce:transition-none';
export const textFieldSurfaces = {
filled: 'border-input bg-card text-card-foreground',
outline: 'border-input bg-transparent text-foreground dark:bg-transparent',
ghost: 'border-transparent bg-muted text-foreground shadow-none',
underline:
'rounded-none border-0 border-b-2 border-input bg-transparent px-0 text-foreground shadow-none focus-visible:border-ring focus-visible:ring-0 aria-invalid:ring-0 data-invalid:ring-0 dark:bg-transparent',
} as const;
export const textFieldUnderlineMotion =
'border-transparent! bg-origin-border bg-bottom bg-no-repeat [--underline-color:var(--primary)] [background-image:linear-gradient(var(--underline-color),var(--underline-color)),linear-gradient(var(--input),var(--input))] [background-size:0%_2px,100%_2px] focus:[background-size:100%_2px,100%_2px] aria-invalid:[--underline-color:var(--destructive)] data-invalid:[--underline-color:var(--destructive)] transition-[border-color,background-color,box-shadow,filter,background-size] duration-200 motion-reduce:transition-none';
/** Generic balanced icon & floating-label layout tokens shared across all text-field sizes. */
export const textFieldLayout = {
icon: 'size-4',
iconStart: 'start-3',
iconEnd: 'end-3',
padStart: 'ps-9',
padEnd: 'pe-9',
underlinePadStart: 'ps-7',
underlinePadEnd: 'pe-7',
floatingStart: 'start-2.5',
floatingWithIcon: 'start-8',
floatingUnderlineIcon: 'start-6',
} as const;
export const textFieldLabelSegmenter = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
/** Clamps per-grapheme wave delay (ms) so total staggering stays within 300ms. */
export function clampWaveDelay(waveDelay: number, count: number): number {
return Math.max(
0,
Math.min(Number.isFinite(waveDelay) ? waveDelay : 24, 80, 300 / Math.max(1, count - 1)),
);
}
/** Shared scale/color transition for floating labels inside a `group/field` wrapper. */
export const textFieldLabelMotion =
'inline-block origin-left backdrop-blur-xs transition-[scale,color] duration-200 ease-out group-has-[input:focus]/field:scale-85 group-has-[input:not(:placeholder-shown)]/field:scale-85 rtl:origin-right motion-reduce:transition-none';
/** Resolves start/end padding for a text field when a leading or trailing icon is present. */
export function getTextFieldIconPadding(iconPosition: 'start' | 'end', underline: boolean): string {
if (iconPosition === 'start') {
return underline ? textFieldLayout.underlinePadStart : textFieldLayout.padStart;
}
return underline ? textFieldLayout.underlinePadEnd : textFieldLayout.padEnd;
}
/** Shared per-grapheme ripple transition for wave labels inside a `group/field` wrapper. */
export const textFieldWaveMotion =
'inline-block origin-left backdrop-blur-xs transition-[translate,scale,color] duration-200 ease-out group-has-[input:focus]/field:-translate-y-5 group-has-[input:focus]/field:scale-85 group-has-[input:focus]/field:text-primary group-has-[input:not(:placeholder-shown)]/field:-translate-y-5 group-has-[input:not(:placeholder-shown)]/field:scale-85 group-has-[input[aria-invalid=true]]/field:text-destructive rtl:origin-right motion-reduce:transition-none';