Collapse
A section that opens and closes. Stack them and you have an accordion, which is what Kobalte's Accordion renders: a heading with a trigger, then a region.
alpha The class names and states here are stable. The look is not: these styles can change in a minor release, without a major version bump.
| Class | Does what |
|---|---|
collapse | One section, with a rule under it |
collapse-title | The trigger row |
collapse-content | The panel |
Basic
<details> gives you this with no script. Kobalte gives you
keyboard navigation across the whole stack, and marks the open item
data-expanded.
<details class="collapse" open>
<summary class="collapse-title">What is included? <span>−</span></summary>
<div class="collapse-content">Everything in the free plan, plus unlimited projects.</div>
</details>
<details class="collapse">
<summary class="collapse-title">Can I cancel? <span>+</span></summary>
<div class="collapse-content">Any time, from the billing page.</div>
</details>
<details class="collapse" open>
<summary class="collapse-title">What is included? <span>−</span></summary>
<div class="collapse-content">Everything in the free plan, plus unlimited projects.</div>
</details>
<details class="collapse">
<summary class="collapse-title">Can I cancel? <span>+</span></summary>
<div class="collapse-content">Any time, from the billing page.</div>
</details>
import { Accordion } from "@kobalte/core/accordion";
<Accordion defaultValue={["included"]}>
<Accordion.Item value="included">
<Accordion.Header>
<Accordion.Trigger>What is included? <span>−</span></Accordion.Trigger>
</Accordion.Header>
<Accordion.Content>
Everything in the free plan, plus unlimited projects.
</Accordion.Content>
</Accordion.Item>
<Accordion.Item value="cancel">
<Accordion.Header>
<Accordion.Trigger>Can I cancel? <span>+</span></Accordion.Trigger>
</Accordion.Header>
<Accordion.Content>Any time, from the billing page.</Accordion.Content>
</Accordion.Item>
</Accordion>
<div>
<h3><button aria-expanded="true" aria-controls="r1" data-expanded>What is included? <span>−</span></button></h3>
<div role="region" id="r1" data-expanded>Everything in the free plan, plus unlimited projects.</div>
<h3><button aria-expanded="false" aria-controls="r2">Can I cancel? <span>+</span></button></h3>
<div role="region" id="r2" hidden>Any time, from the billing page.</div>
</div>
Kobalte's Trigger has no role of its own, so the binding reaches it
as the button inside the heading that precedes a role="region".
That is the most positional selector in the Kobalte layer.
Composition
Common ways to put this together with other components. The general rules are on Composition.
Questions and answers
A stack of questions under a heading; each answer is prose.
<section>
<div class="prose"><h2>Questions</h2></div>
<details class="collapse" open>
<summary class="collapse-title">How long does it take? <span>−</span></summary>
<div class="collapse-content prose">
<p>Most projects launch in two to six weeks.</p>
<ul><li>One call to scope it</li><li>A demo before launch</li></ul>
</div>
</details>
<details class="collapse">
<summary class="collapse-title">Can my team edit it? <span>+</span></summary>
<div class="collapse-content prose"><p>Yes, from a browser, with no code.</p></div>
</details>
</section>
- Text:
.prosegoes on.collapse-content, not on the stack, so the titles keep their own size and spacing. - Surface: the stack has a rule under each item and no box, so it reads as a list rather than a pile of cards.
Advanced options in a form
Settings most people never need fold away under the ones they do.
<form class="card">
<label class="label"><p>Site name</p><input class="input" /></label>
<details class="collapse" open>
<summary class="collapse-title">Advanced options <span>−</span></summary>
<div class="collapse-content">
<label class="label"><p>Custom domain</p><input class="input" placeholder="www.example.com" /></label>
</div>
</details>
<footer class="card-actions flex" style="justify-content: end">
<button class="btn">Save</button>
</footer>
</form>
- Controls: fields inside the collapse are ordinary labels, so they space themselves the same as the fields above.
- Actions: the save button stays outside the collapse, so it is there whether the section is open or not.