Collapsible

A panel that can be toggled open or closed.

Import

import { Collapsible } from '@matix/components/collapsible';

Usage

3 items tagged
@matix/components

Default Open

Set defaultOpen to render the panel expanded on mount.

<Collapsible.Root defaultOpen>
  <Collapsible.Trigger>Collapse details</Collapsible.Trigger>
  <Collapsible.Panel>This content is visible by default.</Collapsible.Panel>
</Collapsible.Root>

Trigger as a Button

Pass render to draw the trigger as another element. The trigger then drops its own text-button look, so the Button keeps its variant and size.

<Collapsible.Root>
  <Collapsible.Trigger render={<Button variant="outline" size="sm" />}>
    Toggle details
  </Collapsible.Trigger>
  <Collapsible.Panel>Here is some content that can be shown or hidden.</Collapsible.Panel>
</Collapsible.Root>

Controlled

Use open and onOpenChange for controlled state.

import { useState } from 'react';

function ControlledCollapsible() {
  const [open, setOpen] = useState(false);
  return (
    <div>
      <Collapsible.Root open={open} onOpenChange={setOpen}>
        <Collapsible.Trigger>{open ? 'Hide' : 'Show'} content</Collapsible.Trigger>
        <Collapsible.Panel>Controlled collapsible content.</Collapsible.Panel>
      </Collapsible.Root>
      <span>Open: {String(open)}</span>
    </div>
  );
}

Animation

Panel expand/collapse animations are enabled automatically when MatixProvider has animations turned on (the default). Animations respect prefers-reduced-motion.

Custom Styles

Use the sx prop on any sub-component to override styles.

import * as stylex from '@stylexjs/stylex';

const custom = stylex.create({
  trigger: { fontWeight: 600, color: '#0f172a' },
  panel: { padding: '12px 0', color: '#64748b', fontSize: 14 },
});

<Collapsible.Root>
  <Collapsible.Trigger sx={custom.trigger}>Styled trigger</Collapsible.Trigger>
  <Collapsible.Panel sx={custom.panel}>Styled panel content with muted text.</Collapsible.Panel>
</Collapsible.Root>;

Server Components

Collapsible is a client component. A Server Component cannot read a property of a client component, so Collapsible.Root is undefined there. In a Server Component, import the named parts. Each Collapsible.<Part> is also exported as Collapsible<Part>.

import {
  CollapsibleRoot,
  CollapsibleTrigger,
  CollapsiblePanel,
} from '@matix/components/collapsible';

In a client component, both forms work.

API Reference

ComponentPropTypeDefaultDescription
Collapsible.RootdefaultOpenbooleanfalseWhether the panel is open initially (uncontrolled)
Collapsible.Rootopenboolean—Open state (controlled)
Collapsible.RootonOpenChange(open: boolean) => void—Called when the open state changes
Collapsible.RootdisabledbooleanfalseDisables the collapsible
Collapsible.RootsxStyleXStyles—StyleX style override
Collapsible.TriggersxStyleXStyles—StyleX style override
Collapsible.PanelsxStyleXStyles—StyleX style override

All other props on each sub-component are forwarded to the underlying Base UI element.

Accessibility

  • Built on Base UI Collapsible — implements WAI-ARIA disclosure pattern.
  • The trigger has aria-expanded reflecting the current state and aria-controls pointing to the panel.
  • Disabled state sets aria-disabled and blocks interaction.
  • Focus ring is visible on keyboard navigation only (:focus-visible).

Keyboard

KeyAction
EnterToggles the panel open or closed
SpaceToggles the panel open or closed