Skip to content
Slotted

Collapsible

One region whose content shows or hides, behind a header the reader operates.

Northwind Traders, 402 Maple Street, Seattle, WA 98101. The dock entrance is on the east side.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { Collapsible, CollapsibleActions, CollapsibleDefaultsProvider,  CollapsibleHeader, CollapsibleMore, CollapsiblePanel,  CollapsibleTrigger } from '@slotted/react';

Header and panel

A collapsible is a header and the panel it controls. The trigger is a button inside the header, so Enter and Space toggle the panel and focus stays on the trigger. Every trigger, every control in the actions slot and every control inside an open panel is in the page tab sequence: nothing writes tabindex. A closed panel is out of the tab sequence and out of the accessibility tree. The trigger carries aria-expanded and aria-controls, and the panel carries aria-labelledby.

The panel opens without being measured: its height transitions between 0fr and 1fr, so the content sets it at every frame. In React, defaultOpen sets the starting state. In Angular, the open input does, and it is also the two-way binding described under Controlled.

Variants

variant sets the chrome. plain draws none, ruled draws a hairline above the header so a stack of collapsibles reads as one list, and framed draws a card. The three share one geometry, so changing the variant moves nothing.

Net 30 from the invoice date.
FOB origin. Freight is billed at cost.
Thirty calendar days, unopened pallets only.

Sizes

size sets the header at one step: sm for dense panels, md for most pages, lg for sparse ones. The title follows the type scale, and the height, padding, gap and indicator follow the control scales, so a dense page is tighter rather than smaller.

Next arrival: pallet PL-4410 at 14:20.
Next pickup: UPS Ground at 16:30.
Zone C closes at 17:00.

Indicator and placement

indicator picks the mark: a chevron that turns between down and up, a plus-minus that swaps one bar for the pair, or none for a header whose state aria-expanded and the panel already tell. togglePlacement="start" puts the mark before the title and indents the panel content to line up with the title. The default, end, puts the mark after the words.

Three active contracts renew on January 1.
Commercial invoice and packing list.
Seven invoices are due this week.
Twelve orders await approval.
Door 4 is reserved from 08:00 to 10:00.

Headings and landmarks

level wraps the trigger in a heading from 1 to 6, so a screen reader’s heading list holds the collapsible at its depth. The default, none, writes no heading semantics, for a collapsible that belongs to no outline. In React, level renders the element. In Angular, the header is the element the template writes: a native h1 to h6 is left as it is, and any other element receives role="heading" and the resolved aria-level. A collapsible nests inside another one’s panel without the outer one opening it.

region makes the panel a region landmark, named by its trigger. It is off by default because a landmark per panel fills the landmark list: use it for the two or three collapsibles a reader navigates to directly.

Thirty calendar days from delivery.

Sixty days. The buyer pays return freight.

Title, summary and description

description is a second line under the title. summary stands at the end of the title line, before the indicator, on one line cut with an ellipsis. Both sit inside the trigger, so a screen reader announces them with the title. truncate clips a title to one line, with the whole string still in the DOM. Without it, a long title grows the row instead of pushing the indicator off the line.

PO-2201 to PO-2214, all due before March 15.
UPS 16:30, FedEx 17:00, USPS 18:00, DHL 18:30.
Sixty days. The buyer pays return freight.

Actions beside the header

CollapsibleActions in React and slCollapsibleActions in Angular hold controls that stay visible whether the panel is open or closed. The slot is a sibling of the header, not a child, because the pattern requires the trigger to be the only element inside the heading and a control cannot sit inside a button. Put a toolbar in it so the actions take one tab stop. Below 22rem the slot moves to a row of its own.

Free shipping over $250.00 on orders to the contiguous states.

When the content exists

mount decides when the panel’s content is built. The panel element always exists, so aria-controls never names an element that is gone.

  • active builds the content on opening and drops it on closing, so closing is instant and the content loses its state.
  • lazy builds it on the first opening and keeps it.
  • eager, the default, builds it from the start, which is what lets the panel transition closed.
  • until-found builds it from the start and writes hidden="until-found" while closed, so the browser’s find-in-page opens the panel when the text matches.

Type in each note, close the panel and open it again: the first note is cleared and the other two are kept. To try until-found, search the page for “Anna Rivers” with the browser’s find command. In Angular, active and lazy need the content inside an ng-template marked slCollapsibleContent, because projected content is built whether or not it is inserted.

Delivered on March 2 by Anna Rivers to the Reno warehouse.

Peek

peek takes a length. A closed panel then keeps that block size of its content in view under a fade, and stays inert. It animates to its full size on opening. CollapsibleMore in React and slCollapsibleMore in Angular draw the control that opens the panel, after the panel, over the fade. The control exists only while the panel is closed and peek is set. A peeking panel always mounts its content, whatever mount says.

08:05 Pallet PL-4401 received at door 2, 24 cartons, no damage.

08:40 Pallet PL-4402 received at door 2, 18 cartons, one crushed corner.

09:15 Pallet PL-4403 received at door 4, 30 cartons, no damage.

10:02 Pallet PL-4404 held for inspection, seal broken on arrival.

10:48 Pallet PL-4405 received at door 4, 12 cartons, no damage.

11:30 Pallet PL-4406 received at door 1, 40 cartons, no damage.

States

disabled writes the native disabled attribute on the trigger, so the header answers neither the pointer nor the keyboard. The panel keeps its current state. loading marks the panel with aria-busy and keeps it at least one line tall. The content that stands in for the missing data is the consumer’s: here, a text skeleton in the shape of the history it will hold.

Contracts with these carriers closed on December 31.

Controlled

Pass open to own the state. In React, onOpenChange receives the new value and a request with the reason (pointer, keyboard, select or programmatic), and the panel moves only when open changes. In Angular, [(open)] keeps a signal in step with the panel, and openChangeRequest emits the same request before the panel moves. The buttons above the panel set the state without a request, so the reason reads the last request from the trigger.

42 lines across 9 orders, starting at aisle B.
Reason of the last request from the trigger: none yet.

Defaults for an application

CollapsibleDefaultsProvider in React and provideCollapsibleDefaults in Angular set size, variant, indicator, togglePlacement, level, mount, peek and truncate once for everything under them. A collapsible that states a value of its own keeps it, as the last one does here.

Six pallets are waiting for putaway.
Temperature held at 36 degrees F.
Fourteen units are waiting for inspection.

Accessibility

KeyEffectFocus
Entertoggles the panelstays on the trigger
Spacetoggles the panelstays on the trigger
Tabmoves to the next focusable element; the trigger, everything in the actions slot and everything inside an open panel are all in the page tab sequencethe next focusable element
Shift+Tabmoves to the previous focusable element, under the same rulethe previous focusable element
  • The pattern is the WAI-ARIA APG’s Disclosure (w3.org/WAI/ARIA/apg/patterns/disclosure/), and inside an Accordion it answers the Accordion pattern as well. Its whole keyboard table is the platform’s: the trigger is a button, so Enter and Space toggle without Collapsible binding a key, and Tab reaches it like any other control — the pattern requires every focusable element in an accordion to be in the page tab sequence, which is why nothing here writes tabindex.
  • The trigger carries aria-expanded and aria-controls; the panel carries aria-labelledby pointing back. Both identifiers come from useId, so a server response and the first client render agree. The panel’s box is never removed — mount drops its content, not the box — because aria-controls naming an element that is not there is a reference a screen reader has to make sense of.
  • A closed panel is out of the tab sequence and out of the accessibility tree twice over: the stylesheet with content-visibility, and the binding with inert, so a browser without the property still cannot put focus inside it. Under mount="until-found" neither applies: hidden="until-found" is what lets the browser’s page search reach the text and open the panel, and beforematch is what tells Collapsible to open.
  • The heading is real. level renders h1 to h6 and the trigger is its only element, which is what the pattern requires; an accordion sets the level once for a whole set. level="none" writes no heading semantics at all, for a disclosure that belongs to no outline.
  • aria-disabled appears on the trigger only when an Accordion refuses to let this panel close. That is the pattern’s own instruction for a set that requires one panel to stay open, and it leaves the control focusable and announced rather than removing it from the reader’s path.
  • The trigger is a button and the panel a div, rather than details and summary. What that costs is written down: a page with JavaScript disabled cannot open a panel. What it buys is the accordion pattern — a summary cannot be wrapped in a heading — a value the consumer can own, a request that carries a reason, and a panel that animates without interpolate-size.
  • The mark is never the only thing that says the state: aria-expanded says it to a reader, and the indicator turns rather than changing color, so the high-contrast scheme and forced colors both keep it.

API

Collapsible

Props

openboolean
Present means the consumer owns the state, for the life of the component. Inside an Accordion the set owns it instead, and an explicit open still wins over both.
defaultOpenboolean
The value an uncontrolled disclosure starts from, read once. A later change to it does not silently become new state.
size'sm' | 'md' | 'lg'Default md
On Collapsible, Accordion, CollapsibleDefaultsProviderThe step the header sits at. The title comes from the type scale and everything geometric from the control scales, so a density changes the rhythm rather than the reading size.
variant'plain' | 'ruled' | 'framed'Default plain
On Collapsible, CollapsibleDefaultsProviderplain paints no chrome, ruled draws a hairline above the header so a stack reads as one list, and framed is its own card. An Accordion hands its items the one its own variant maps onto.
indicator'chevron' | 'plus-minus' | 'none'Default chevron
On Collapsible, Accordion, CollapsibleDefaultsProviderThe mark, drawn from a glyph token. The chevron turns between down and up rather than pointing along the inline axis, so nothing is mirrored for a right-to-left document.
togglePlacement'end' | 'start'Default end
On Collapsible, Accordion, CollapsibleDefaultsProviderWhich side of the words the mark stands on. With it at the start the panel’s content is indented to line up with the title rather than with the mark.
level'none' | '1' | '2' | '3' | '4' | '5' | '6'Default none
On Collapsible, CollapsibleDefaultsProviderThe heading element the trigger is wrapped in. none renders a div and writes no heading semantics; an Accordion sets it for a whole set through headingLevel.
mount'active' | 'lazy' | 'eager' | 'until-found'Default eager
On Collapsible, Accordion, CollapsibleDefaultsProviderWhen the panel’s content exists. The box always does, so aria-controls never names an element that is gone. until-found writes hidden="until-found" and answers beforematch by opening.
peekstring
On Collapsible, CollapsibleDefaultsProviderA length (9rem): a closed panel keeps that block size of its content in view under a fade, stays inert, and animates to its full size with the same durations; forced colors drop the fade and keep the clip. The panel carries data-state="peek", and mount is forced to eager.
truncatebooleanDefault false
On Collapsible, Accordion, CollapsibleDefaultsProviderClips the title to one line instead of letting the row grow. The whole string stays in the DOM, which holds for every component.
regionbooleanDefault false
Makes the panel a region landmark. Off by default, because the pattern’s own example warns that a landmark per panel fills the landmark list.
disabledbooleanDefault false
Reaches the trigger as the native attribute, which is also what stops the press. Unstated rather than false, so the disclosure defers to the Accordion around it.
loadingbooleanDefault false
The panel keeps a line’s height and carries aria-busy; what stands in for the content is Skeleton, composed by the consumer.
valuestringDefault from useId
What this disclosure is called inside an Accordion. It is identity rather than state, and it reaches the DOM as data-value.
summaryReactNode
On CollapsibleTriggerWhat the disclosure holds, at the end of the title line and before the mark, cut with an ellipsis; inside the trigger and therefore inside its accessible name.
descriptionReactNode
On CollapsibleTriggerA second line under the title, inside the trigger and therefore inside its accessible name.

Events

NamePayloadWhen
onOpenChange(open: boolean, request: ChangeRequest<boolean>)Before the panel moves. The request carries the new value, the value it replaces, the reason (pointer, keyboard, select or programmatic), the originating event, and cancel(), which refuses the move before anything commits. Inside an Accordion it still fires, so a consumer may watch one panel without unpacking the set’s array.
onClick, onFocus, onKeyDown, on the triggerthe element’s own eventsWhenever the button fires them. Collapsible composes with a consumer’s handler rather than replacing it: the consumer’s runs first, and preventDefault() in onClick refuses the toggle as well.

Composition

render(props) => ReactNode
On Collapsible, CollapsibleHeader, CollapsibleTrigger, CollapsibleActions, CollapsiblePanelSubstitutes any of the five roots and hands it every attribute — a disclosure can be a section, a panel an article. The heading may become another element, but the trigger has to stay its only child.
refRef<HTMLElement>
On every partReaches the element itself, so focus() and click() are the platform’s and no imperative handle is invented for what it already ships.
useCollapsible()() => CollapsibleContextValue
On inside CollapsibleWhat the parts read: the open state, the identifiers, the mount strategy, whether the group holds this one open, and toggle(next, reason). It throws outside Collapsible rather than rendering a control that does nothing.
CollapsibleDefaultsProvider{ size, variant, indicator, togglePlacement, level, mount, peek, truncate }
On a subtreeThe decisions an application takes once: size, variant, indicator, togglePlacement, level, mount, peek and truncate. An Accordion sits between it and the item, and a disclosure that states any of them itself still wins.

Parts

PartElementRequiredPurpose
rootdiv, or the element render returnsYesThe root. A two-column, two-row grid carrying data-ui="collapsible", data-size, data-variant, data-indicator, data-toggle-placement, data-mount, data-truncate, data-open, data-disabled, data-loading, data-peek and data-value. It is also its own container, so the header reflows on its own inline size rather than the viewport’s.
headerh1 to h6, or a div at level="none"YesThe heading. level decides the element, and the accordion sets it for a whole set at once. Its only child is the trigger: the pattern requires the button to be the only element inside the heading.
triggerbuttonYesThe control. Holds aria-expanded, aria-controls, the identifier, and every native prop and ref the consumer wrote. Enter and Space are the platform’s, so Collapsible binds no key of its own.
indicatorspanNoThe mark, drawn as a mask from a glyph token. A chevron turns between down and up; plus-minus swaps one bar for the pair. indicator="none" draws nothing at all.
labelspanYesThe title. A box of its own, so text-overflow has something to clip under truncate and so a title in another script reads its own way without reordering the row around it.
summaryspanNoWhat the disclosure holds, at the end of the title line and before the mark: one line, cut with an ellipsis, in the description ink. It is inside the trigger, so it is part of the accessible name.
descriptionspanNoA second line under the title. It is inside the trigger, so it is part of the accessible name — “Delivery, 3 of 12 items” is what a reader hears.
actionsdiv, or the element render returnsNoThe always-visible controls beside the header, on its own row and outside the button. What belongs in it is a Toolbar. Below 22rem it takes a row of its own.
paneldiv, optionally role="region"YesThe box the trigger controls: aria-labelledby its trigger, inert while closed, and hidden="until-found" under that mount. The box always exists, so aria-controls never names an element that is gone.
contentdivYesWhat the panel’s grid track clips. Its content-visibility is what takes closed content out of the tab sequence, out of the accessibility tree and out of the browser’s page search. mount decides whether it is built at all.
morebuttonNoThe control that opens a peeking panel, placed after the panel and drawn over its fade. It exists only while the panel is closed and peek is set. It opens the panel and names it through aria-controls, as the trigger does. render replaces it, with a Button for example.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every collapsible inside it.

Collapsible tokens

TokenDefault
--slotted-collapsible-active-backgroundvar(--slotted-tone-secondary-subtle-active)
--slotted-collapsible-backgroundtransparent
--slotted-collapsible-border-colorvar(--slotted-collapsible-rule-color, var(--slotted-border-subtle, ButtonBorder))
--slotted-collapsible-colorvar(--slotted-text, CanvasText)
--slotted-collapsible-description-colorvar(--slotted-text-muted, GrayText)
--slotted-collapsible-hover-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-collapsible-hover-colorvar(--slotted-text, CanvasText)
--slotted-collapsible-indicator-colorcurrentColor
--slotted-collapsible-more-overlap1.5rem
--slotted-collapsible-padding-inlinevar(--_padding-inline)
--slotted-collapsible-panel-colorvar(--slotted-text, CanvasText)
--slotted-collapsible-panel-padding-block-startvar(--_padding-block)
--slotted-collapsible-peek-fade3rem
--slotted-collapsible-radiusvar(--slotted-control-radius, 4px)
--slotted-collapsible-rule-colorvar(--slotted-border-subtle, ButtonBorder)
--slotted-collapsible-shadownone
--slotted-collapsible-surfacevar(--slotted-surface-raised, Canvas)

Theme tokens it reads

TokenDefault
--slotted-border-subtleSet per scheme by the theme
--slotted-border-width-thin1px
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-gap-lgcomfortable 8px · compact 6px · dense 6px
--slotted-control-gap-mdcomfortable 8px · compact 6px · dense 4px
--slotted-control-gap-smcomfortable 6px · compact 4px · dense 4px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-icon-size-lgcomfortable 18px · compact 16px · dense 14px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-icon-size-smcomfortable 14px · compact 13px · dense 12px
--slotted-control-letter-spacing0
--slotted-control-padding-inline-lgcomfortable 16px · compact 12px · dense 10px
--slotted-control-padding-inline-mdcomfortable 13px · compact 10px · dense 8px
--slotted-control-padding-inline-smcomfortable 10px · compact 8px · dense 6px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-medium200ms
--slotted-duration-short120ms
--slotted-easing-standardcubic-bezier(0.2, 0, 0, 1)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-size-lg1rem
--slotted-font-size-md0.875rem
--slotted-font-size-sm0.8125rem
--slotted-font-size-xs0.75rem
--slotted-font-weight-medium500
--slotted-font-weight-regular400
--slotted-line-height-md1.5
--slotted-radius-md6px
--slotted-space-30.375rem
--slotted-space-40.5rem
--slotted-space-50.75rem
--slotted-surface-raisedSet per scheme by the theme
--slotted-textSet per scheme by the theme
--slotted-text-mutedSet per scheme by the theme
--slotted-tone-secondary-subtle-activeSet per scheme by the theme
--slotted-tone-secondary-subtle-hoverSet per scheme by the theme