Collapsible
A button that shows or hides a section of content.
Collapsible follows the Disclosure (Show/Hide) WAI-ARIA design pattern.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Collapsible } from '@leve-ui/collapsible';
import { LuChevronDown } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Collapsible.Root class="collapsible-default-root">
<Collapsible.Trigger class="collapsible-default-trigger">
<span>Can I use this component in my project?</span>
<LuChevronDown class="collapsible-default-chevron" aria-hidden="true" />
</Collapsible.Trigger>
<Collapsible.Content class="collapsible-default-content">
Yes! It is headless, fully accessible, and styled with plain CSS.
</Collapsible.Content>
</Collapsible.Root>
);
});.collapsible-default-root {
display: flex;
flex-direction: column;
gap: 8px;
width: 320px;
font-family: inherit;
}
.collapsible-default-trigger {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
padding: 10px 14px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
text-align: left;
cursor: pointer;
}
.collapsible-default-trigger:hover:not(:disabled) {
background: light-dark(#f1f5f9, #ffffff14);
}
.collapsible-default-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.collapsible-default-trigger:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Rotate the chevron while the collapsible is open */
.collapsible-default-chevron {
flex-shrink: 0;
width: 16px;
height: 16px;
color: light-dark(#475569, #c9ddf0);
transition: transform 200ms ease;
}
.collapsible-default-trigger[data-state="open"] .collapsible-default-chevron {
transform: rotate(180deg);
}
.collapsible-default-content {
padding: 10px 14px;
border-radius: 6px;
background: light-dark(#f1f5f9, #ffffff14);
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}Installation#
Install the individual package, or use @leve-ui/core to get every component at once:
pnpm add @leve-ui/collapsiblenpm install @leve-ui/collapsibleyarn add @leve-ui/collapsiblebun add @leve-ui/collapsiblepnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/coreAnatomy#
import { Collapsible } from '@leve-ui/collapsible'; // or '@leve-ui/core'
<Collapsible.Root>
<Collapsible.Trigger />
<Collapsible.Content />
</Collapsible.Root>
| Part | Element | Description |
|---|---|---|
Collapsible.Root | <div> | Wraps the parts and holds the open state. |
Collapsible.Trigger | <button> | The button that opens and closes the content. |
Collapsible.Content | <div> | The content that is shown or hidden. |
Examples#
Open#
defaultOpen opens the content initially.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Collapsible } from '@leve-ui/collapsible';
import { LuChevronDown } from '@qwikest/icons/lucide';
import styles from './open.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Collapsible.Root defaultOpen class="collapsible-open-root">
<Collapsible.Trigger class="collapsible-open-trigger">
<span>What is leve-ui?</span>
<LuChevronDown class="collapsible-open-chevron" aria-hidden="true" />
</Collapsible.Trigger>
<Collapsible.Content class="collapsible-open-content">
A headless, accessible UI component library built for Qwik.
</Collapsible.Content>
</Collapsible.Root>
);
});.collapsible-open-root {
display: flex;
flex-direction: column;
gap: 8px;
width: 320px;
font-family: inherit;
}
.collapsible-open-trigger {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
padding: 10px 14px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
text-align: left;
cursor: pointer;
}
.collapsible-open-trigger:hover:not(:disabled) {
background: light-dark(#f1f5f9, #ffffff14);
}
.collapsible-open-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.collapsible-open-trigger:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Rotate the chevron while the collapsible is open */
.collapsible-open-chevron {
flex-shrink: 0;
width: 16px;
height: 16px;
color: light-dark(#475569, #c9ddf0);
transition: transform 200ms ease;
}
.collapsible-open-trigger[data-state="open"] .collapsible-open-chevron {
transform: rotate(180deg);
}
.collapsible-open-content {
padding: 10px 14px;
border-radius: 6px;
background: light-dark(#f1f5f9, #ffffff14);
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}Disabled#
disabled on the Root disables the trigger, so the content cannot be opened or closed.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Collapsible } from '@leve-ui/collapsible';
import { LuChevronDown } from '@qwikest/icons/lucide';
import styles from './disabled.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Collapsible.Root disabled class="collapsible-disabled-root">
<Collapsible.Trigger class="collapsible-disabled-trigger">
<span>Disabled section</span>
<LuChevronDown class="collapsible-disabled-chevron" aria-hidden="true" />
</Collapsible.Trigger>
<Collapsible.Content class="collapsible-disabled-content">
This content cannot be toggled because the collapsible is disabled.
</Collapsible.Content>
</Collapsible.Root>
);
});.collapsible-disabled-root {
display: flex;
flex-direction: column;
gap: 8px;
width: 320px;
font-family: inherit;
}
.collapsible-disabled-trigger {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
padding: 10px 14px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
text-align: left;
cursor: pointer;
}
.collapsible-disabled-trigger:hover:not(:disabled) {
background: light-dark(#f1f5f9, #ffffff14);
}
.collapsible-disabled-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.collapsible-disabled-trigger:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Rotate the chevron while the collapsible is open */
.collapsible-disabled-chevron {
flex-shrink: 0;
width: 16px;
height: 16px;
color: light-dark(#475569, #c9ddf0);
transition: transform 200ms ease;
}
.collapsible-disabled-trigger[data-state="open"] .collapsible-disabled-chevron {
transform: rotate(180deg);
}
.collapsible-disabled-content {
padding: 10px 14px;
border-radius: 6px;
background: light-dark(#f1f5f9, #ffffff14);
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}Controlled#
bind:open controls the open state with a signal, so a button outside the collapsible can open and close it.
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Collapsible } from '@leve-ui/collapsible';
import { LuChevronDown } from '@qwikest/icons/lucide';
import styles from './controlled.css?inline';
export default component$(() => {
useStyles$(styles);
const open = useSignal(false);
return (
<div class="collapsible-controlled-wrapper">
<button
type="button"
class="collapsible-controlled-external-button"
onClick$={() => {
open.value = !open.value;
}}
>
{open.value ? 'Close' : 'Open'} from outside
</button>
<Collapsible.Root bind:open={open} class="collapsible-controlled-root">
<Collapsible.Trigger class="collapsible-controlled-trigger">
<span>Controlled collapsible</span>
<LuChevronDown class="collapsible-controlled-chevron" aria-hidden="true" />
</Collapsible.Trigger>
<Collapsible.Content class="collapsible-controlled-content">
This collapsible state is controlled by an external signal.
</Collapsible.Content>
</Collapsible.Root>
</div>
);
});.collapsible-controlled-wrapper {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 12px;
font-family: inherit;
}
.collapsible-controlled-external-button {
height: 32px;
padding: 0 12px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
color: light-dark(#0f172a, #ffffff);
font: inherit;
font-size: 13px;
font-weight: 500;
cursor: pointer;
}
.collapsible-controlled-external-button:hover {
background: light-dark(#f1f5f9, #ffffff14);
}
.collapsible-controlled-external-button:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.collapsible-controlled-root {
display: flex;
flex-direction: column;
gap: 8px;
width: 320px;
}
.collapsible-controlled-trigger {
display: flex;
align-items: center;
justify-content: space-between;
width: 100%;
padding: 10px 14px;
border: 1px solid light-dark(#cbd5e1, #ffffff3d);
border-radius: 6px;
background: light-dark(#ffffff, #1b2045);
font: inherit;
font-size: 14px;
font-weight: 500;
color: light-dark(#0f172a, #ffffff);
text-align: left;
cursor: pointer;
}
.collapsible-controlled-trigger:hover:not(:disabled) {
background: light-dark(#f1f5f9, #ffffff14);
}
.collapsible-controlled-trigger:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.collapsible-controlled-trigger:disabled {
opacity: 0.5;
cursor: not-allowed;
}
/* Rotate the chevron while the collapsible is open */
.collapsible-controlled-chevron {
flex-shrink: 0;
width: 16px;
height: 16px;
color: light-dark(#475569, #c9ddf0);
transition: transform 200ms ease;
}
.collapsible-controlled-trigger[data-state="open"] .collapsible-controlled-chevron {
transform: rotate(180deg);
}
.collapsible-controlled-content {
padding: 10px 14px;
border-radius: 6px;
background: light-dark(#f1f5f9, #ffffff14);
font-size: 13px;
line-height: 1.6;
color: light-dark(#475569, #c9ddf0);
}API Reference#
Root#
| Prop | Default | Type |
|---|---|---|
| ids | – | { trigger?: string; content?: string } Overrides the element IDs of the parts. |
| defaultOpen | false | boolean Whether the content is open initially (uncontrolled). |
| bind:open | – | Signal<boolean> A signal with the open state (controlled). |
| onOpenChange$ | – | QRL<(open: boolean) => void> Called when the user opens or closes the content. |
| disabled | false | boolean Disables the trigger. |
Also accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-collapsible | Always present |
data-state | "open" | "closed" |
data-disabled | Present when disabled |
The open state is uncontrolled with defaultOpen, or controlled with bind:open. onOpenChange$ is called when the user opens or closes the content.
Trigger#
No props of its own. Accepts all <button> attributes.
| Data attribute | Value |
|---|---|
data-leve-collapsible-trigger | Always present |
data-state | "open" | "closed" |
data-disabled | Present when disabled |
When the Root is disabled, the trigger gets the native disabled attribute.
Content#
No props of its own. Accepts all <div> attributes.
| Data attribute | Value |
|---|---|
data-leve-collapsible-content | Always present |
data-state | "open" | "closed" |
data-disabled | Present when disabled |
The content has the hidden attribute while it is closed. It stays in the DOM so that the trigger's aria-controls keeps pointing to it.
Accessibility#
Complies with the Disclosure (Show/Hide) WAI-ARIA design pattern.
Collapsible.Triggeris a<button type="button">witharia-expandedandaria-controlspointing to the content. IDs are generated withuseId(), or set withidson the Root.- The trigger is named by its text. If it only contains an icon, give it an
aria-label. - A disabled trigger is skipped by Tab.
Keyboard Interactions#
| Key | Description |
|---|---|
Enter / Space | Opens or closes the content. |