Explore the index
Browse components
SystemsstableReactResizeObserverCSS

Spring Disclosure

Content that stays connected to its container.

Open behaviour

Open Care and repair to use the nested postcode form. Switch to Multiple to keep several panels open.

A real browser viewport. Scroll within the preview when the sample needs more space. Configuration stays in sync between sizes.

01What it does

A disclosure system that interpolates between measured content heights while the icon, rule, copy and spacing settle together. Supports single and multiple open panels, live content resizing and nested controls.

In this previewA product information panel for a fictional lamp. Panels include prose, a box list with an add-on, a specifications table, a comparison table and a working repair-partner form. The shipping panel can be disabled by the preview controls.

Design decisions

Heights are measured from the content element and observed while open. Nothing relies on height: auto mid-transition, so a panel that grows still fits without a snap.

Only the height, icon, opacity and small copy offsets animate. There is no overshoot: the content should feel attached, not elastic for its own sake.

The summary line remains visible while open at reduced emphasis, so the reader keeps the panel's subject even when the content is tall.

02Install

Registry install

registry:component

Run this in a React project with components.json. The component and its stylesheet land in components/lab/spring-disclosure/.

npx shadcn@latest add https://lab.tanishk.me/r/spring-disclosure.json

Files included

2
  • components/lab/spring-disclosure/spring-disclosure.tsxcomponent
  • components/lab/spring-disclosure/spring-disclosure.cssstyles

Manual install: copy each file into the same folder and preserve its relative imports. Open the source

03Usage

spring-disclosure.usage.tsx
import { SpringDisclosure } from "@/components/lab/spring-disclosure/spring-disclosure";

export function ProductInfo() {
return (
<SpringDisclosure
mode="multiple"
defaultOpen={["overview"]}
items={[
{ id: "overview", title: "Overview", content: <p>A quiet task light.</p> },
{ id: "specs", title: "Specifications", content: <SpecTable /> },
{ id: "care", title: "Care and repair", content: <CareForm /> },
]}
/>
);
}

04Props

Props accepted by Spring Disclosure
PropTypeDefaultNotes
items{ id: string; title: string; content: ReactNode; summary?: string; disabled?: boolean }[]—Panels in display order. Content can contain interactive controls.
mode"single" | "multiple""single"Single closes the previous panel; multiple keeps a set open.
valuestring[]—Controlled open ids.
defaultOpenstring[]—Initial open ids; defaults to the first panel in uncontrolled use.
onValueChange(ids: string[]) => void—Called with the next open set.
classNamestring—Forwarded to the root for local layout.

05Accessibility

  • Each trigger is a native button inside a heading, with aria-expanded and aria-controls pointing at its region.
  • Closed panels are inert and aria-hidden, so nested links and inputs never become hidden tab stops.
  • If a panel closes while focus is inside it, focus returns to that panel's trigger.
  • The compare table and specification table keep real table semantics, and disabled rows are true disabled buttons.
  • The height transition is disabled under reduced motion while all content remains reachable.

06Source

2 files, 340 lines. The combined copy is a reference; save each file separately for use.

components/lab/spring-disclosure/spring-disclosure.tsx— press enter to show or hide the source
tsx
"use client";

import {
useEffect,
useId,
useLayoutEffect,
useRef,
useState,
useSyncExternalStore,
type ReactNode,
} from "react";
import "./spring-disclosure.css";

const noopSubscribe = () => () => {};
function useClientReady() {
return useSyncExternalStore(
noopSubscribe,
() => true,
() => false,
);
}
export type DisclosureItem = {
id: string;
title: string;
content: ReactNode;
/** A short line under the title, visible while collapsed. */
summary?: string;
disabled?: boolean;
};

export type SpringDisclosureProps = {
items: DisclosureItem[];
/** single closes the previous panel when another opens. */
mode?: "single" | "multiple";
/** Controlled open ids. */
value?: string[];
defaultOpen?: string[];
onValueChange?: (ids: string[]) => void;
className?: string;
};

function DisclosureRow({
item,
open,
onToggle,
}: {
item: DisclosureItem;
open: boolean;
onToggle: () => void;
}) {
const uid = useId();
const triggerRef = useRef<HTMLButtonElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const [height, setHeight] = useState(0);
const mounted = useClientReady();

useLayoutEffect(() => {
const content = contentRef.current;
if (!content) return;
const measure = () => setHeight(content.scrollHeight);
measure();
if (!open || typeof ResizeObserver !== "function") return;
const observer = new ResizeObserver(measure);
observer.observe(content);
return () => observer.disconnect();
}, [open, item.content]);

useEffect(() => {
if (!open && contentRef.current?.contains(document.activeElement)) {
triggerRef.current?.focus();
}
}, [open]);

return (
<div
className="sd-item"
data-open={open ? "true" : "false"}
data-disabled={item.disabled ? "true" : "false"}
data-ready={mounted ? "true" : "false"}
>
<h3 className="sd-heading">
<button
ref={triggerRef}
type="button"
id={`${uid}-trigger`}
className="sd-trigger"
aria-expanded={open}
aria-controls={`${uid}-panel`}
disabled={item.disabled}
onClick={onToggle}
>
<span className="sd-text">
<span className="sd-title">{item.title}</span>
{item.summary ? (
<span className="sd-summary">{item.summary}</span>
) : null}
</span>
<span className="sd-icon" aria-hidden="true">
<i />
<i />
</span>
</button>
</h3>
<div
id={`${uid}-panel`}
className="sd-panel"
role="region"
aria-labelledby={`${uid}-trigger`}
aria-hidden={!open}
inert={!open}
style={{ height: open ? height : 0 }}
>
<div ref={contentRef} className="sd-content">
{item.content}
</div>
</div>
</div>
);
}

/**
* SPRING DISCLOSURE
*
* Each panel interpolates between measured content heights; the icon,
* rule, spacing and copy all settle together. Content is observed while
* open, so a list that grows still fits. When a panel closes with focus
* inside it, focus returns to its trigger instead of disappearing into
* the document.
*/
export function SpringDisclosure({
items,
mode = "single",
value,
defaultOpen,
onValueChange,
className,
}: SpringDisclosureProps) {
const [internal, setInternal] = useState<string[]>(
defaultOpen ?? (items[0] ? [items[0].id] : []),
);
const controlled = value !== undefined;
const rawOpen = (controlled ? value : internal).filter((id) =>
items.some((item) => item.id === id && !item.disabled),
);
const open =
mode === "single" && rawOpen.length > 1
? [rawOpen[rawOpen.length - 1]]
: rawOpen;

const toggle = (id: string) => {
const isOpen = open.includes(id);
let next: string[];
if (mode === "single") next = isOpen ? [] : [id];
else next = isOpen ? open.filter((entry) => entry !== id) : [...open, id];
if (controlled) onValueChange?.(next);
else setInternal(next);
};

return (
<div className={`sd-root${className ? ` ${className}` : ""}`} data-mode={mode}>
{items.map((item) => (
<DisclosureRow
key={item.id}
item={item}
open={open.includes(item.id)}
onToggle={() => toggle(item.id)}
/>
))}
</div>
);
}
components/lab/spring-disclosure/spring-disclosure.css— press enter to show or hide the source
css
.sd-root {
--sd-ink: #20261f;
--sd-muted: #6c716a;
--sd-line: rgba(32, 38, 31, 0.14);
--sd-surface: #ffffff;
--sd-soft: #f6f5ef;
--sd-accent: #2d5a43;
--sd-focus: #2d5a43;
--sd-ease: cubic-bezier(0.22, 1, 0.36, 1);
border-top: 1px solid var(--sd-line);
color: var(--sd-ink);
font: inherit;
}

.sd-item {
border-bottom: 1px solid var(--sd-line);
transition: background-color 220ms var(--sd-ease);
}

.sd-item[data-open="true"] {
background: color-mix(in srgb, var(--sd-accent) 3%, var(--sd-surface));
}

.sd-heading {
margin: 0;
font: inherit;
}

.sd-trigger {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
width: 100%;
min-height: 58px;
padding: 17px 4px;
border: 0;
background: transparent;
color: inherit;
font: inherit;
text-align: left;
cursor: pointer;
}

.sd-trigger:focus-visible {
outline: 2px solid var(--sd-focus);
outline-offset: 3px;
border-radius: 5px;
}

.sd-trigger:disabled {
opacity: 0.42;
cursor: not-allowed;
}

.sd-text {
display: grid;
gap: 3px;
min-width: 0;
}

.sd-title {
font-size: 0.9375rem;
font-weight: 550;
letter-spacing: -0.008em;
line-height: 1.35;
}

.sd-summary {
color: var(--sd-muted);
font-size: 0.8125rem;
line-height: 1.45;
transition:
opacity 220ms var(--sd-ease),
transform 220ms var(--sd-ease);
}

.sd-icon {
position: relative;
flex: none;
width: 18px;
height: 18px;
margin-top: 2px;
}

.sd-icon i {
position: absolute;
top: 8px;
left: 2px;
width: 14px;
height: 1.5px;
border-radius: 2px;
background: currentColor;
transition: transform 260ms var(--sd-ease);
}

.sd-icon i:last-child {
transform: rotate(90deg);
}

.sd-item[data-open="true"] .sd-icon i:last-child {
transform: rotate(0deg);
}

.sd-item[data-open="true"] .sd-icon i:first-child {
transform: rotate(180deg);
}

.sd-panel {
overflow: hidden;
transition: height 320ms var(--sd-ease);
}

.sd-item:not([data-ready="true"]) .sd-panel {
transition: none;
}

.sd-content {
padding: 0 4px 20px;
color: var(--sd-ink);
font-size: 0.875rem;
line-height: 1.62;
opacity: 0;
transform: translateY(-4px);
transition:
opacity 240ms var(--sd-ease),
transform 300ms var(--sd-ease);
}

.sd-item[data-open="true"] .sd-content {
opacity: 1;
transform: translateY(0);
transition-delay: 40ms;
}

.sd-content p {
margin: 0 0 12px;
}

.sd-content p:last-child {
margin-bottom: 0;
}

@media (max-width: 520px) {
.sd-trigger {
min-height: 54px;
padding-block: 15px;
}

.sd-summary {
font-size: 0.75rem;
}

.sd-content {
padding-bottom: 18px;
}
}

@media (prefers-reduced-motion: reduce) {
.sd-panel,
.sd-content,
.sd-icon i,
.sd-item,
.sd-summary {
transition-duration: 0.01ms;
}
}