Collapsible
Features
- Full keyboard navigation.
- Can be controlled or uncontrolled.
Installation
Install the component from your command line.
$ npm add reka-uiAnatomy
Import the components and piece the parts together.
<script setup>
import { CollapsibleContent, CollapsibleRoot, CollapsibleTrigger } from 'reka-ui'
</script>
<template>
<CollapsibleRoot>
<CollapsibleTrigger />
<CollapsibleContent />
</CollapsibleRoot>
</template>API Reference
Root
Contains all the parts of a collapsible
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
Trigger
The button that toggles the collapsible
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
Content
The component that contains the collapsible content.
| Data Attribute | Value |
|---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
| CSS Variable | Description |
|---|---|
--reka-collapsible-content-width | The width of the content when it opens/closes |
--reka-collapsible-content-height | The height of the content when it opens/closes |
Examples
Animating content size
Use the --reka-collapsible-content-width and/or --reka-collapsible-content-height CSS variables to animate the size of the content when it opens/closes. Here's a demo:
// index.vue
<script setup>
import { CollapsibleContent, CollapsibleRoot, CollapsibleTrigger } from 'reka-ui'
import './styles.css'
</script>
<template>
<CollapsibleRoot>
<CollapsibleTrigger>…</CollapsibleTrigger>
<CollapsibleContent class="CollapsibleContent">
…
</CollapsibleContent>
</CollapsibleRoot>
</template>/* styles.css */
.CollapsibleContent {
overflow: hidden;
}
.CollapsibleContent[data-state="open"] {
animation: slideDown 300ms ease-out;
}
.CollapsibleContent[data-state="closed"] {
animation: slideUp 300ms ease-out;
}
@keyframes slideDown {
from {
height: 0;
}
to {
height: var(--reka-collapsible-content-height);
}
}
@keyframes slideUp {
from {
height: var(--reka-collapsible-content-height);
}
to {
height: 0;
}
}Render content even when collapsed
By default hidden content will be removed, use :unmountOnHide="false" to keep the content always available.
This will also allow browser to search the hidden text, and open the collapsible.
<script setup>
import { CollapsibleContent, CollapsibleRoot, CollapsibleTrigger } from 'reka-ui'
</script>
<template>
<CollapsibleRoot :unmount-on-hide="false">
…
</CollapsibleRoot>
</template>Accessibility
Adheres to the Disclosure WAI-ARIA design pattern.
Keyboard Interactions
| Key | Description |
|---|---|
Space | Opens/closes the collapsible |
Enter | Opens/closes the collapsible |
Headless composable
useCollapsible exposes reactive open state and root, trigger, and content part surfaces for custom rendering. Bind each surface's attrs.value to its element.
<script setup lang="ts">
import { useCollapsible } from 'reka-ui'
const disclosure = useCollapsible({ baseId: 'details', unmountOnHide: false })
</script>
<template>
<div v-bind="disclosure.root.attrs.value">
<button type="button" v-bind="disclosure.trigger.attrs.value">
Details
</button>
<div v-bind="disclosure.content.attrs.value">
Additional information
</div>
</div>
</template>Pass open as a writable ref for ref-owned state, or a getter with onUpdate for controlled state. onBeforeUpdate(value, details) can call details.cancel() to prevent a change. Changes include trigger-press, content-found, or imperative-action reasons and the originating event when available.
The composable is experimental and callable outside component setup. Supply a stable, unique baseId when rendering on the server. Custom renderers own mounting, presence, measurements, animations, and browser beforematch handling; call disclosure.onContentFound(event) to request opening discovered content. Use the components when you need those rendering behaviors included.
