Accordion unstable
A set of panels, each under a heading with a button that shows and hides it, such as the questions of a FAQ.
Accordion is headless and shares its state with the AccordionItems in
its view closure. Each item is a headless Collapsible that reads which
values are open from the accordion and asks it to change them. The consumer
owns the values of the open items, passes them as value, and stores what
the accordion's Toggled event reports.
Anatomy
| Part | Element |
|---|---|
accordion.root |
<div> around the items |
item.root |
<div> around an item's header and panel |
item.header |
<h1> to <h6> around the trigger |
item.trigger |
<button> that opens and closes the item's panel |
item.panel |
<div> with the item's content |
Each part takes the consumer's builder and returns it with the part
applied, and the consumer adds the children:
item.panel(h.div)?.children("Content"). The panel returns undefined once
it has closed, unless keepMounted or hiddenUntilFound is set. 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, unless it has a value:
accordion.root:data-orientation,data-disableditem.root,item.headeranditem.panel:data-open/data-closed,data-disabled,data-orientation,data-indexitem.trigger:data-panel-open,data-disabled,data-orientation,data-indexitem.root,item.triggeranditem.panel:data-starting-styleanddata-ending-style
data-index is the item's index prop, and absent without one.
data-orientation is the accordion's orientation, vertical by default.
The panel sets the CSS variables --accordion-panel-height and
--accordion-panel-width: the size of its content in px while it opens and
closes, and auto otherwise. They equal Collapsible's
--collapsible-panel-height and --collapsible-panel-width, which the panel
also has. To animate the panel, give it height: var(--accordion-panel-height),
overflow: hidden and a transition on height, and height: 0 under
[data-starting-style] and [data-ending-style].
Keyboard
Each trigger is a native button, so Tab and Shift+Tab move between the triggers, and Enter and Space press them. The arrow keys don't move between triggers, following the ARIA Authoring Practices accordion pattern, which dropped that roving focus.
Find in page
With hiddenUntilFound, a 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 value that opens
its item, which in a single-item accordion closes the others.
Example A FAQ whose open question lives in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Accordion, AccordionItem } from "effect/ui/kit"import { Effect, Schema } from "effect"
const questions = [ { id: "shipping", question: "How long does shipping take?", answer: "3 to 5 days." }, { id: "returns", question: "Can I return an item?", answer: "Within 30 days." }]
const FaqMessage = UiSchema.TaggedEnum({ ToggledQuestions: { open: Schema.Array(Schema.String) } })
const Faq = Html.component({ name: "Faq", model: Schema.Struct({ open: Schema.Array(Schema.String) }), message: FaqMessage}).make({ init: () => ({ open: ["shipping"] }), update: { ToggledQuestions: ($, { open }) => ({ ...$.model, open }) }, view: (h, model) => Accordion(h, { props: { value: model.open }, onEmit: { Toggled: ({ value }) => FaqMessage.ToggledQuestions({ open: value }) }, view: (accordion) => accordion.root(h.div.class("faq")).children( questions.map((entry, index) => AccordionItem(h, { key: entry.id, props: { value: entry.id, index }, view: (item) => item.root(h.div).children( item.header(h.h3).children(item.trigger(h.button).children(entry.question)), item.panel(h.div.class("answer"))?.children(entry.answer) ) }) ) ) })})
// The open answer is a region named by its question's button.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Faq, {}))const trigger = /<button[^>]* id="([^"]+)"/.exec(html)?.[1]if (trigger === undefined || !/<div class="answer"[^>]* role="region"/.test(html) || !html.includes(`aria-labelledby="${trigger}"`)) { throw new Error("the open answer isn't a region named by its trigger")}Components
A headless accordion. Call it with the consumer's h, the props, an
onEmit for Toggled, and a view closure that draws the root with
accordion.root(h.div) and, inside it, one AccordionItem for each item.
Details
The parts also return value, disabled, multiple and orientation,
resolved, for markup that depends on them.
The root numbers the AccordionItems it holds in the order it draws them,
and passes each its position as index unless the item has one. An item
the root can't see, such as one drawn by another component's view, keeps
the index its caller gives it.
The root's data-orientation and data-disabled win over the consumer's,
including when disabled leaves the attribute absent. A part writes them
when it is applied, so an attribute the consumer chains after the part is
the consumer's to answer for.
The accordion shares { value, disabled, keepMounted, hiddenUntilFound, orientation } with the components in its view closure, which is how an
AccordionItem inside reads it, and an item asks it to open or close
with a ToggledItem message.
Signature
declare const Accordion: Headless<{ readonly disabled?: boolean; readonly hiddenUntilFound?: boolean; readonly keepMounted?: boolean; readonly multiple?: boolean; readonly orientation?: "horizontal" | "vertical"; readonly value: readonly Array<string>;}, { readonly _tag: "Toggled"; readonly reason: "trigger-press" | "find-in-page"; readonly value: readonly Array<string>;}, { disabled: boolean; multiple: boolean; orientation: "horizontal" | "vertical"; root: Part<"div", [], never>; value: readonly Array<string>;}, never, { readonly _tag: "ToggledItem"; readonly item: string; readonly open: boolean; readonly reason: "trigger-press" | "find-in-page";}, Shared<"Accordion">, { readonly disabled: boolean; readonly hiddenUntilFound: boolean; readonly keepMounted: boolean; readonly orientation: "horizontal" | "vertical"; readonly value: readonly Array<string>;}, never, never, {}, { readonly _tag: "ToggledItem"; readonly item: string; readonly open: boolean; readonly reason: "trigger-press" | "find-in-page";}, QueryEntries<{}, {}>>AccordionItem
unstableA headless item of an Accordion, built on Collapsible. Call it with the
consumer's h, a key, the props, and a view closure that draws the
root with item.root(h.div) and, inside it, the heading with
item.header(h.h3) around item.trigger(h.button), and the panel with
item.panel(h.div). It only renders inside an Accordion's view
closure.
Details
The parts also return plain values for markup that depends on them:
value, index, open, disabled, orientation, 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 id, type, disabled, aria-disabled,
which it removes, aria-expanded and aria-controls, which it sets only
while the panel is open; on the panel role="region", aria-labelledby
(merging the trigger's id with any the consumer set), hidden, and
inert while closing. The item's root links whichever trigger it holds
regardless of build order, and adds no label when it holds none. The
trigger and the panel keep a consumer id and get a generated one
otherwise. The panel's CSS variables are added after the consumer's inline
style. A handler the consumer adds to a part runs after the item'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.
Signature
declare const AccordionItem: Headless<{ readonly disabled?: boolean; readonly hiddenUntilFound?: boolean; readonly holdExit?: boolean; readonly index?: number; readonly keepMounted?: boolean; readonly value: string;}, { readonly _tag: "Toggled"; readonly open: boolean; readonly reason: "trigger-press" | "find-in-page";} | { readonly _tag: "Settled"; readonly open: boolean;}, { disabled: boolean; header: Part<"h1" | "h2" | "h3" | "h4" | "h5" | "h6", [], never>; index: number | undefined; mounted: boolean; open: boolean; orientation: "horizontal" | "vertical"; panel: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"div", [], never>, "~R">; root: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"div", [], never>, "~R">; transitionStatus: CollapsibleTransitionStatus; trigger: <B extends PartTarget<"button">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"button", [id: string], never>, "~R">; value: string;}, Shared<"Accordion">, never, never, never, never, never, {}, { readonly _tag: "ToggledCollapsible"; readonly open: boolean; readonly reason: "trigger-press" | "find-in-page";} | { readonly _tag: "SettledCollapsible"; readonly open: boolean;}, QueryEntries<{}, {}>>Models
What an Accordion emits.
Signature
type AccordionEvents = typeof AccordionEvents.TypeWhat an AccordionItem emits.
Signature
type AccordionItemEvents = typeof AccordionItemEvents.TypeThe props of an AccordionItem.
Signature
type AccordionItemProps = typeof AccordionItemProps.TypeThe axis an accordion's items are laid out along.
Signature
type AccordionOrientation = typeof AccordionOrientation.TypeThe props of an Accordion.
Signature
type AccordionProps = typeof AccordionProps.TypeSchemas
AccordionEvents
unstableWhat an accordion emits.
Details
Toggled: the accordion's value the user asked for, with the reason. Opening an item gives[value], and closing it gives[]. Withmultiple, the item's value is added at the end or removed. Store it and pass it back as thevalueprop. A disabled accordion or item emits nothing.
Signature
declare const AccordionEvents: TaggedEnum<{ Toggled: TaggedStruct<"Toggled", { readonly reason: Literals<readonly ["trigger-press", "find-in-page"]>; readonly value: $Array<String>; }>;}>AccordionItemEvents
unstableWhat an accordion item emits.
Details
Toggled: the user asked to open or close this item, with the reason. The accordion'sToggledcarries the value to store, so an item'sonEmitis optional.Settled: the transitions and animations of the panel's open or close finished.
Signature
declare const AccordionItemEvents: TaggedEnum<{ Settled: TaggedStruct<"Settled", { readonly open: Boolean; }>; Toggled: TaggedStruct<"Toggled", { readonly open: Boolean; readonly reason: Literals<readonly ["trigger-press", "find-in-page"]>; }>;}>AccordionItemProps
unstableControls one item of an accordion.
Details
value: the item's value, which the accordion'svaluelists while the item is open. Required, and unique within its accordion.index: the item's position among the accordion's items, written asdata-indexon every part of the item. Left out, the accordion's root gives the item its position in the order the root draws its items, so a reordered list renumbers itself. Give it only for an item the root can't see, such as one drawn by another component's view.disabled: disables this item. A disabled accordion disables every item.keepMountedandhiddenUntilFound: as the accordion's, for this item's panel. Left out, the item follows the accordion.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 item emitsSettled.
Signature
declare const AccordionItemProps: Struct<{ readonly disabled: optional<Boolean>; readonly hiddenUntilFound: optional<Boolean>; readonly holdExit: optional<Boolean>; readonly index: optional<Number>; readonly keepMounted: optional<Boolean>; readonly value: String;}>AccordionOrientation
unstableThe axis an accordion's items are laid out along.
Signature
declare const AccordionOrientation: Literals<readonly ["vertical", "horizontal"]>AccordionProps
unstableControls which items are open and how closed panels stay in the page.
Details
value: the values of the open items. Required: the consumer owns it.multiple: lets several items be open at once. Off by default, so opening an item closes the others.disabled: disables every item.keepMounted: closed panels stay in the page, withhidden. An item's ownkeepMountedwins.hiddenUntilFound: closed panels stay in the page ashidden="until-found", so find-in-page can reveal them. It wins overkeepMounted. An item's ownhiddenUntilFoundwins.orientation: written asdata-orientationon every part, for styling."vertical"by default. It doesn't change the keyboard.
Signature
declare const AccordionProps: Struct<{ readonly disabled: optional<Boolean>; readonly hiddenUntilFound: optional<Boolean>; readonly keepMounted: optional<Boolean>; readonly multiple: optional<Boolean>; readonly orientation: optional<Literals<readonly ["vertical", "horizontal"]>>; readonly value: $Array<String>;}>