Skip to contents

Collapsible

A collapsible panel controlled by a button.

Anatomy

Import the component and assemble its parts:

Examples

Hidden until found

The hiddenUntilFound prop hides the closed panel with hidden="until-found" so the browser can search its contents with find-in-page—Ctrl+F (Cmd+F on macOS)—and reveal the panel when a match is found. The closed panel always remains mounted in the DOM, which also makes its contents indexable by search engines.

Older browsers that don't support hidden="until-found" keep the panel hidden until its trigger opens it, and find-in-page skips over the contents.

See the Accordion example for an interactive demo.

API reference

Root

Groups all parts of the collapsible. Renders a <div> element.

open boolean false
Name
Description

Whether the panel is open. Use bind:open to share it with the parent. A value passed without bind: holds until the parent changes it.

Type
boolean
Default
false
Binding
bind:open
disabled boolean | null false
Description

Whether the collapsible should ignore user interaction.

Type
boolean | null
Default
false
onOpenChange function —
Description

Called before the open state changes. Call eventDetails.cancel() to veto it.

Type
(open: boolean, eventDetails: CollapsibleRootChangeEventDetails) => void
render Snippet<[props: HTMLAttributes<HTMLDivElement>, state: CollapsibleRootState, children: Snippet | undefined]> —
Name
Description

Replace the default <div>. Spread props onto the host and render children.

Type
Snippet<[props: HTMLAttributes<HTMLDivElement>, state: CollapsibleRootState, children: Snippet | undefined]>
open boolean
Name
open
Description

Whether the panel is currently open.

Type
boolean
disabled boolean
Name
disabled
Description

Whether the collapsible should ignore user interaction.

Type
boolean
transitionStatus TransitionStatus
Name
transitionStatus
Description

Shared open/close motion phase.

Type
TransitionStatus
data-open
Present when the collapsible is open.
data-closed
Present when the collapsible is closed.
data-disabled
Present when the collapsible is disabled.
data-starting-style
Present when the collapsible begins animating in.
data-ending-style
Present when the collapsible is animating out.

Trigger

A button that opens and closes the collapsible panel. Renders a <button> element.

disabled boolean | null —
Description

Whether the trigger should ignore user interaction. Defaults to the root's disabled value.

Type
boolean | null
nativeButton boolean true
Description

Whether the host is a native <button>. Set false when render supplies a non-button element.

Type
boolean
Default
true
onclick MouseEventHandler<HTMLElement> | null —
Name
Description

Runs before the trigger's handler. Call event.preventDefault() to skip it.

Type
MouseEventHandler<HTMLElement> | null
render Snippet<[props: CollapsibleTriggerHostProps, state: CollapsibleTriggerState, children: Snippet | undefined]> —
Name
Description

Replace the default <button>. Spread props onto the host and render children.

Type
Snippet<[props: CollapsibleTriggerHostProps, state: CollapsibleTriggerState, children: Snippet | undefined]>
open boolean
Name
open
Description

Whether the panel is currently open.

Type
boolean
disabled boolean
Name
disabled
Description

Whether the collapsible should ignore user interaction.

Type
boolean
transitionStatus TransitionStatus
Name
transitionStatus
Description

Shared open/close motion phase.

Type
TransitionStatus
data-panel-open
Present when the collapsible panel is open.
data-disabled
Present when the collapsible is disabled.

Panel

A panel with the collapsible contents. Renders a <div> element.

hiddenUntilFound boolean false
Description

Allows the browser's page search to find and expand the panel. Overrides keepMounted and uses hidden="until-found".

Type
boolean
Default
false
keepMounted boolean false
Description

Whether to keep the panel in the DOM while it is closed. Ignored when hiddenUntilFound is set.

Type
boolean
Default
false
render Snippet<[props: HTMLAttributes<HTMLDivElement>, state: CollapsiblePanelState, children: Snippet | undefined]> —
Name
Description

Replace the default <div>. Spread props onto the host and render children.

Type
Snippet<[props: HTMLAttributes<HTMLDivElement>, state: CollapsiblePanelState, children: Snippet | undefined]>
transitionStatus TransitionStatus
Name
transitionStatus
Description

Panel motion phase. Can settle to idle before the root does.

Type
TransitionStatus
open boolean
Name
open
Description

Whether the panel is currently open.

Type
boolean
disabled boolean
Name
disabled
Description

Whether the collapsible should ignore user interaction.

Type
boolean
data-open
Present when the collapsible panel is open.
data-closed
Present when the collapsible panel is closed.
data-starting-style
Present when the panel begins animating in.
data-ending-style
Present when the panel is animating out.
--collapsible-panel-height
The collapsible panel's height.
--collapsible-panel-width
The collapsible panel's width.