Search docs

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>
  );
});

Installation#

Install the individual package, or use @leve-ui/core to get every component at once:

pnpm add @leve-ui/collapsible

Anatomy#

import { Collapsible } from '@leve-ui/collapsible'; // or '@leve-ui/core'

<Collapsible.Root>
  <Collapsible.Trigger />
  <Collapsible.Content />
</Collapsible.Root>
PartElementDescription
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.

A headless, accessible UI component library built for Qwik.

Disabled#

disabled on the Root disables the trigger, so the content cannot be opened or closed.

Controlled#

bind:open controls the open state with a signal, so a button outside the collapsible can open and close it.

API Reference#

Root#

PropDefaultType
ids–{ trigger?: string; content?: string }

Overrides the element IDs of the parts.

defaultOpenfalseboolean

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.

disabledfalseboolean

Disables the trigger.

Also accepts all <div> attributes.

Data attributeValue
data-leve-collapsibleAlways present
data-state"open" | "closed"
data-disabledPresent 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 attributeValue
data-leve-collapsible-triggerAlways present
data-state"open" | "closed"
data-disabledPresent 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 attributeValue
data-leve-collapsible-contentAlways present
data-state"open" | "closed"
data-disabledPresent 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.Trigger is a <button type="button"> with aria-expanded and aria-controls pointing to the content. IDs are generated with useId(), or set with ids on 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#

KeyDescription
Enter / SpaceOpens or closes the content.