Collapsible unstable
A panel that a button shows and hides, such as a section of settings or the answer to a question.
Collapsible is headless: it owns the panel's id, the ARIA, the data
attributes and the panel's size while it opens and closes, and the consumer
draws the markup. The consumer owns whether the panel is open, passes it as
open, and stores what the Toggled event reports.
Anatomy
| Part | Element |
|---|---|
root |
any element, around the trigger and the panel |
trigger |
<button> that opens and closes the panel |
panel |
any element, with the content |
Each part takes the consumer's builder and returns it with the part
applied, and the consumer adds the children:
collapsible.panel(h.div)?.children("Content"). The panel returns undefined
once it has closed, unless keepMounted or hiddenUntilFound is set. The
root is optional. A part's result is a builder, so it can also be another
kit call's element.
Data attributes
Each present with an empty value or absent:
rootandpanel:data-open/data-closed,data-disabled,data-starting-style,data-ending-styletrigger:data-panel-open,data-disabled,data-starting-style,data-ending-style
The panel also sets the CSS variables --collapsible-panel-height and
--collapsible-panel-width: the size of its content in px while it opens
and closes, and auto otherwise, so an open panel follows its content.
data-starting-style is present for the first frame after the panel opens,
and data-ending-style while it closes, so a transition can run from and to
a closed style: give the panel height: var(--collapsible-panel-height),
overflow: hidden and a transition on height, and height: 0 under
[data-starting-style] and [data-ending-style].
The size is read from scrollHeight and scrollWidth, which report 0 on
an inline box, so an inline panel such as a <span> needs display: block
to animate with --collapsible-panel-height.
A keyframe animation works too, from or to var(--collapsible-panel-height).
The panel stays in the page until its own transitions and animations finish,
then the collapsible emits Settled. One that loops doesn't count. For an
exit the kit can't see, set holdExit while it runs. A panel that is open on
the first render doesn't animate.
Keyboard
The trigger is a native button, so Enter and Space press it.
Find in page
With hiddenUntilFound, the closed panel stays in the page as
hidden="until-found", so the browser's find-in-page searches it. A match
emits Toggled with the reason "find-in-page", and the panel opens at once,
without its transition, when the consumer passes open: true. If it doesn't,
or the collapsible is disabled, the panel goes back to hidden. A browser
without until-found keeps the panel hidden, and find-in-page skips it. A
panel hidden until found isn't display: none, so a panel styled with its
own display needs .panel[hidden]:not([hidden="until-found"]) { display: none }
to hide when it is closed with keepMounted: true.
Collapsing below a breakpoint
Pass keepMounted: "css" for a panel that collapses below a breakpoint and
stays open above it, such as a section list that toggles behind a button
below 1200px and always shows from 1200px up. The closed panel stays in
the page with data-closed and no hidden attribute, so CSS decides
whether it shows on the first paint without a viewport subscription:
- Hide the closed panel below the breakpoint with
display: noneorvisibility: hidden, so its links leave the tab order;height: 0oropacity: 0alone leaves them focusable. - Hide the trigger wherever the panel always shows (
>= 1200px), so it doesn't reportaria-expanded="false"beside a visible panel. - For a panel with an exit transition, key the hiding class on
collapsible.mountedrather than[data-closed]:data-closedappears two frames beforedata-ending-style, so hiding on[data-closed]hides the panel before its exit transition starts.
Example A section whose open state lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Collapsible } from "effect/ui/kit"import { Effect, Schema } from "effect"
const ShippingMessage = UiSchema.TaggedEnum({ ToggledDetails: { open: Schema.Boolean } })
const Shipping = Html.component({ name: "Shipping", model: Schema.Struct({ details: Schema.Boolean }), message: ShippingMessage}).make({ init: () => ({ details: true }), update: { ToggledDetails: ($, { open }) => $.evolve({ details: open }) }, view: (h, model) => Collapsible(h, { props: { open: model.details }, onEmit: { Toggled: ({ open }) => ShippingMessage.ToggledDetails({ open }), Settled: () => undefined }, view: (collapsible) => collapsible.root(h.div).children( collapsible.trigger(h.button.class("trigger")).children("Shipping details"), collapsible.panel(h.div.class("panel"))?.children("Standard shipping takes 3 to 5 days.") ) })})
// The trigger points at the panel it opened.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Shipping, {}))const panel = /<div class="panel"[^>]* id="([^"]+)"/.exec(html)?.[1]if (panel === undefined || !html.includes(`aria-controls="${panel}"`)) { throw new Error("the trigger doesn't point at the panel")}Components
Collapsible
unstableA headless collapsible. Call it with the consumer's h and a view closure
that draws the trigger with collapsible.trigger and the panel with
collapsible.panel, usually inside collapsible.root.
Details
The parts also return plain values for markup that depends on them: open,
disabled, mounted (the panel shows, through its exit transition) and
transitionStatus.
Each part's attributes win over the ones the consumer set on the builder it
passes in, including the ones a state leaves absent: the data attributes on
every part; on the trigger type, disabled, aria-disabled, which it
removes, aria-expanded and aria-controls, which it sets only while the
panel is open; on the panel hidden, and inert while closing. The panel
keeps a consumer id and gets a generated one otherwise; the trigger's
aria-controls always points at the panel's final id, even when built
before the panel, wrapped with forward, or rendered without root. The
panel's CSS variables, and the animation-name and durations it overrides
to skip an animation, are added after the consumer's inline style. A
handler the consumer adds to a part runs after the collapsible's own.
On the server and on a host that doesn't run mounts, a panel that opens
stays at data-starting-style, and one that closes stays in the page.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const Collapsible: Headless<{ readonly disabled?: boolean; readonly hiddenUntilFound?: boolean; readonly holdExit?: boolean; readonly keepMounted?: boolean | "css"; readonly open: boolean;}, { readonly _tag: "Toggled"; readonly open: boolean; readonly reason: "trigger-press" | "find-in-page";} | { readonly _tag: "Settled"; readonly open: boolean;}, { disabled: boolean; mounted: boolean; open: boolean; panel: <B extends PartTarget<string>>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<string, [id: string], never>, "~R">; root: Part<string, [], never>; transitionStatus: CollapsibleTransitionStatus; trigger: Part<"button", [], never>;}, never, never, never, never, never, never, { readonly ending: boolean; readonly instant: boolean; readonly mounted: boolean; readonly openOnLoad: boolean; readonly size: { readonly height: number; readonly width: number; } | null; readonly started: boolean;}, { readonly _tag: "ClickedTrigger";} | { readonly _tag: "MeasuredPanel"; readonly size: { readonly height: number; readonly width: number; };} | { readonly _tag: "RenderedFirstFrame";} | { readonly _tag: "RenderedSize";} | { readonly _tag: "FinishedTransition"; readonly open: boolean;} | { readonly _tag: "MatchedFind";} | { readonly _tag: "StayedHidden";}, QueryEntries<{}, {}>>Models
What a Collapsible emits.
Signature
type CollapsibleEvents = typeof CollapsibleEvents.TypeThe props of a Collapsible.
Signature
type CollapsibleProps = typeof CollapsibleProps.TypeWhy a collapsible asks to open or close.
Signature
type CollapsibleToggleReason = typeof CollapsibleToggleReason.TypeWhere a panel is in its open or close transition: starting for the first
frame after it opens, ending while it closes, and null otherwise.
Signature
type CollapsibleTransitionStatus = "starting" | "ending" | nullSchemas
CollapsibleEvents
unstableToggled asks for the value in open, with the reason. Store it and pass it
back as the open prop. Settled reports that the transitions and
animations of an open or a close finished.
Signature
declare const CollapsibleEvents: TaggedEnum<{ Settled: TaggedStruct<"Settled", { readonly open: Boolean; }>; Toggled: TaggedStruct<"Toggled", { readonly open: Boolean; readonly reason: Literals<readonly ["trigger-press", "find-in-page"]>; }>;}>CollapsibleProps
unstableControls whether the panel is open and how it stays in the page while closed.
Details
open: whether the panel shows. Required: the consumer owns it.disabled: the trigger gets the nativedisabled, and neither a press nor a find-in-page match emitsToggled.keepMounted:truekeeps the closed panel in the page withhidden."css"keeps it in the page withdata-closedand nohidden, so CSS decides whether it shows.hiddenUntilFound: the closed panel stays in the page ashidden="until-found", so find-in-page can reveal it. It wins overkeepMounted.holdExit: a closed panel stays in its ending state while this istrue, for an exit animation that the consumer runs itself. Set it back tofalseonce that animation ends, and the panel closes and the collapsible emitsSettled.
Signature
declare const CollapsibleProps: Struct<{ readonly disabled: optional<Boolean>; readonly hiddenUntilFound: optional<Boolean>; readonly holdExit: optional<Boolean>; readonly keepMounted: optional<Union<readonly [Boolean, Literal<"css">]>>; readonly open: Boolean;}>CollapsibleToggleReason
unstableWhy a collapsible asks to open or close: a press on its trigger, or a find-in-page match in its hidden panel.
Signature
declare const CollapsibleToggleReason: Literals<readonly ["trigger-press", "find-in-page"]>