Skip to content

Collapsible ​

A panel that a button opens and closes.

vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Collapsible } from '@vase-ui/vue/collapsible';
import styles from './index.module.css';

const CaretRightIcon: FunctionalComponent = () =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'currentColor',
      style: { display: 'block' },
    },
    [h('path', { d: 'M6 12V4l4.5 4z' })],
  );

// The panel animates its height from 0 to the height of its content, and back.
const enterFromClass = styles.PanelClosed;
const enterToClass = styles.PanelOpen;
const leaveFromClass = styles.PanelOpen;
const leaveToClass = styles.PanelClosed;
const panelTransition = { enterFromClass, enterToClass, leaveFromClass, leaveToClass };
</script>

<template>
  <Collapsible.Root :class="styles.Collapsible">
    <Collapsible.Trigger :class="styles.Trigger">
      Recovery keys
      <CaretRightIcon :class="styles.Icon" />
    </Collapsible.Trigger>
    <Transition v-bind="panelTransition">
      <Collapsible.Panel :class="styles.Panel">
        <div :class="styles.Content">
          <div>alien-bean-pasta</div>
          <div>wild-irish-burrito</div>
          <div>horse-battery-staple</div>
        </div>
      </Collapsible.Panel>
    </Transition>
  </Collapsible.Root>
</template>
css
.Collapsible {
  display: flex;
  width: 12rem;
  min-height: 9rem;
  flex-direction: column;
  justify-content: center;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Icon {
  transition: transform 100ms ease-out;
}

.Trigger {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  height: 2rem;
  padding: 0 0.5rem 0 0.75rem;
  margin: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  color: oklch(14.5% 0 0deg);
  font-family: inherit;
  font-size: 0.875rem;
  font-weight: 400;
  line-height: 1;
  white-space: nowrap;
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:focus-visible {
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }

  &[aria-expanded='true'] .Icon {
    transform: rotate(90deg);
  }

  &:disabled {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }
}

.Panel {
  display: flex;
  flex-direction: column;
  justify-content: end;
  overflow: hidden;
  font-size: 0.875rem;
  line-height: 1.25rem;
  transition: height 150ms ease-out;
}

.PanelOpen {
  height: var(--collapsible-panel-height);
}

.PanelClosed {
  height: 0;
}

.Content {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.5rem 0.875rem;
}
vue
<script setup lang="ts">
import { h } from 'vue';
import type { FunctionalComponent } from 'vue';
import { Collapsible } from '@vase-ui/vue/collapsible';

const CaretRightIcon: FunctionalComponent = () =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'currentColor',
      style: { display: 'block' },
    },
    [h('path', { d: 'M6 12V4l4.5 4z' })],
  );

// The panel animates its height from 0 to the height of its content, and back.
const enterFromClass = 'h-0';
const enterToClass = 'h-[var(--collapsible-panel-height)]';
const leaveFromClass = 'h-[var(--collapsible-panel-height)]';
const leaveToClass = 'h-0';
const panelTransition = { enterFromClass, enterToClass, leaveFromClass, leaveToClass };
</script>

<template>
  <Collapsible.Root
    class="flex min-h-36 w-48 flex-col justify-center text-neutral-950 dark:text-white"
  >
    <Collapsible.Trigger
      class="group flex h-8 items-center justify-between gap-2 rounded-none border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 pl-3 pr-2 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-disabled:bg-neutral-100 dark:hover:not-disabled:bg-neutral-800 active:not-disabled:bg-neutral-200 dark:active:not-disabled:bg-neutral-700 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white disabled:border-neutral-500 disabled:text-neutral-500 dark:disabled:border-neutral-400 dark:disabled:text-neutral-400"
    >
      Recovery keys
      <CaretRightIcon
        class="transition-transform duration-100 ease-[ease-out] group-aria-expanded:rotate-90"
      />
    </Collapsible.Trigger>
    <Transition v-bind="panelTransition">
      <Collapsible.Panel
        class="flex flex-col justify-end overflow-hidden text-sm transition-[height] duration-150 ease-[ease-out]"
      >
        <div class="flex flex-col gap-2 px-3.5 py-2">
          <div>alien-bean-pasta</div>
          <div>wild-irish-burrito</div>
          <div>horse-battery-staple</div>
        </div>
      </Collapsible.Panel>
    </Transition>
  </Collapsible.Root>
</template>

Anatomy ​

Import the component and assemble its parts:

vue
<script setup lang="ts">
import { Collapsible } from '@vase-ui/vue/collapsible';
</script>

<template>
  <Collapsible.Root>
    <Collapsible.Trigger />
    <Collapsible.Panel />
  </Collapsible.Root>
</template>

Examples ​

Hidden until found ​

The hiddenUntilFound prop hides the closed panel with hidden="until-found". The find-in-page search of the browser (Ctrl+F, or Cmd+F on macOS) can then search its contents and open the panel when it finds a match. The closed panel always stays in the DOM, so search engines can also index its contents.

vue
<script setup lang="ts">
import { Collapsible } from '@vase-ui/vue/collapsible';
</script>

<template>
  <Collapsible.Root>
    <Collapsible.Trigger>Shipping details</Collapsible.Trigger>
    <Collapsible.Panel hidden-until-found>
      Standard shipping takes 3–5 business days.
    </Collapsible.Panel>
  </Collapsible.Root>
</template>
  • A browser that does not support hidden="until-found" keeps the panel hidden until its trigger opens it, and find-in-page skips the contents.
  • The box of a closed panel keeps its padding and its border. Put them on a child element.
  • Give such a panel no display transition and no size transition. A size transition at the open moves the found text out of the view, and the browser hides the content at once at a close.

See the Accordion example for an interactive demo.

Animate the panel ​

Put the panel in a <Transition>. The panel enters when it opens and leaves when it closes. Before each open and each close, the panel writes the size of its content in the CSS variables --collapsible-panel-height and --collapsible-panel-width. Use the height in the classes of the transition:

vue
<script setup lang="ts">
import { Collapsible } from '@vase-ui/vue/collapsible';
</script>

<template>
  <Collapsible.Root>
    <Collapsible.Trigger>Recovery keys</Collapsible.Trigger>
    <Transition name="panel">
      <Collapsible.Panel class="Panel">alien-bean-pasta</Collapsible.Panel>
    </Transition>
  </Collapsible.Root>
</template>

<style>
.Panel {
  overflow: hidden;
  transition: height 150ms ease-out;
}
.panel-enter-from,
.panel-leave-to {
  height: 0;
}
.panel-enter-to,
.panel-leave-from {
  height: var(--collapsible-panel-height);
}
</style>

At rest, the panel has the height of its content, so a change of the content while it is open needs no code. The panel reads the variables at its closed size. In a flex column, an item that can shrink then gives its shrunk size, so put a flex layout on a child of the panel. When the user opens the panel again during its leave, the new panel enters after the leave ends.

API reference ​

Collapsible.Root ​

Groups all parts of the collapsible. Renders a <div> element.

Props

PropTypeDefaultDescription
defaultOpenboolean | undefinedfalseThe panel is open at the mount of an uncontrolled root.
disabledboolean | undefinedfalseThe user cannot open or close the panel with the trigger.
openboolean | undefinedThe panel is open. v-model:open binds it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: CollapsibleRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: CollapsibleRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Events

EventArgumentsDescription
openChange[open: boolean, details: CollapsibleRootChangeEventDetails]The user opens or closes the panel: trigger-press for a press of the trigger, none for a reveal by find-in-page. details.cancel() keeps the panel as it is.
update:open[open: boolean]Comes after openChange, unless a listener cancels the change. v-model:open uses it.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-open]The panel is open.
[data-disabled]The collapsible is disabled.

Collapsible.Trigger ​

A button that opens and closes the collapsible panel. Renders a <button> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe user cannot press the trigger. A disabled root also disables it.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: CollapsibleTriggerState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: CollapsibleTriggerState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[aria-expanded="true"]The panel is open.
:disabledThe trigger is a native button, and the trigger, the collapsible or a fieldset around it is disabled.
[aria-disabled="true"]The trigger is another element, and the trigger or the collapsible is disabled.

Collapsible.Panel ​

A panel with the collapsible contents. Renders a <div> element.

Props

PropTypeDefaultDescription
hiddenUntilFoundboolean | undefinedfalseA closed panel stays in the DOM with hidden="until-found", so that find-in-page of the browser can find its text and open it.
idstring | undefinedThe trigger names the panel by this id in aria-controls.
keepMountedboolean | undefinedfalseA closed panel stays in the DOM, with hidden and inert, so its content keeps its state, and the server HTML has it. <Transition> does not animate such a panel: animate it with CSS on [hidden].
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: CollapsibleRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: CollapsibleRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[hidden]The panel is closed, and keepMounted or hiddenUntilFound keeps it in the DOM.
[data-disabled]The collapsible is disabled.

CSS variables

VariableDescription
--collapsible-panel-heightThe height of the content of the panel, in pixels. The panel writes it before each open and each close.
--collapsible-panel-widthThe width of the content of the panel, in pixels. The panel writes it before each open and each close.

Differences from Base UI ​

Vase UI ports Collapsible from Base UI 1.9.0.

API ​

Base UIVase UI
Collapsible.Root<Transition> around the panel animates it, so the state has no transitionStatus, and the root has no data-starting-style and no data-ending-style. CSS selects a closed root with :not([data-open]), so the root has no data-closed.
Collapsible.TriggerThe trigger reads the element that it renders, so it has no nativeButton. <Transition> around the panel animates it, so the state has no transitionStatus. CSS selects [aria-expanded="true"], and :disabled on a native button or [aria-disabled="true"] on another element, so the trigger has no data-panel-open and no data-disabled.
Collapsible.Panel<Transition> around the panel animates it, so the state has no transitionStatus, and the panel has no data-starting-style and no data-ending-style. CSS selects a closed panel that stays in the DOM with [hidden], so the panel has no data-open and no data-closed.

Behavior ​

Vase UIEvidence
The server HTML of a closed panel with hiddenUntilFound has hidden="until-found".
An open Collapsible.Trigger or Accordion.Trigger names in aria-controls only a panel that is mounted now, and an Accordion.Panel names in aria-labelledby only a trigger that is mounted now. With no such part, the attribute is absent. In the server HTML, a part names only the part that comes before it, and the hydration gives the other name. Base UI names a generated id until the first panel or trigger mounts. No element has that id in the server HTML of a part with an id prop, and in a root or an item that has no such part.
The panels of Collapsible and Accordion put back the value and the priority of each inline alignment style after a read of their size. Base UI saves only the alignment values and puts them back with no priority.
When JavaScript gives null to defaultOpen, Collapsible.Root starts closed, and its trigger has aria-expanded="false". Base UI keeps the null in its state and leaves out aria-expanded.
hiddenUntilFound keeps a closed panel in the DOM with hidden="until-found", because find-in-page needs the content. keepMounted of false changes nothing then, and the part gives no warning. Base UI gives a warning in a development build.
A button part knows the element of a component in as or of the element slot only at the mount, and it needs no prop that says what the element is. Until the mount, the element has the attributes that suit a <button> and each other element: type="button", role="button", a tab stop, and the disabled state of both. So in the server HTML, Tab reaches a <div> before a lazy hydration, and a <button> does not submit its form. At the mount, the part keeps the attributes of its element. Base UI needs nativeButton set to false for an element that is not a <button>.

The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.