Collapsible
An interactive component which expands/collapses a panel.
@peduarte starred 3 repos
@radix-vue/radix-vue
Features
- Full keyboard navigation.
- Can be controlled or uncontrolled.
Installation
Install the component from your command line.
bash
npm install radix-vue
Anatomy
Import the components and piece the parts together.
vue
<script setup>
import { CollapsibleContent, CollapsibleRoot, CollapsibleTrigger } from 'radix-vue'
</script>
<template>
<CollapsibleRoot>
<CollapsibleTrigger />
<CollapsibleContent />
</CollapsibleRoot>
</template>
API Reference
Root
Contains all the parts of a collapsible
Prop | Type | Default |
---|---|---|
as | string | Component | div |
asChild | boolean | false |
defaultOpen | boolean | |
open | boolean | |
disabled | boolean |
Emit | Type |
---|---|
@update:open | (value: boolean) => void |
Data Attribute | Value |
---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
Trigger
The button that toggles the collapsible
Prop | Type | Default |
---|---|---|
as | string | Component | button |
asChild | boolean | false |
Data Attribute | Value |
---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
Content
The component that contains the collapsible content.
Prop | Type | Default |
---|---|---|
as | string | Component | div |
asChild | boolean | false |
forceMount | boolean |
Data Attribute | Value |
---|---|
[data-state] | "open" | "closed" |
[data-disabled] | Present when disabled |
CSS Variable | Description |
---|---|
--radix-collapsible-content-width | The width of the content when it opens/closes |
--radix-collapsible-content-height | The height of the content when it opens/closes |
Examples
Animating content size
Use the --radix-collapsible-content-width
and/or --radix-collapsible-content-height
CSS variables to animate the size of the content when it opens/closes. Here's a demo:
vue
// index.vue
<script setup>
import { CollapsibleContent, CollapsibleRoot, CollapsibleTrigger } from 'radix-vue'
import './styles.css'
</script>
<template>
<CollapsibleRoot>
<CollapsibleTrigger>…</CollapsibleTrigger>
<CollapsibleContent class="CollapsibleContent">
…
</CollapsibleContent>
</CollapsibleRoot>
</template>
css
/* 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(--radix-collapsible-content-height);
}
}
@keyframes slideUp {
from {
height: var(--radix-collapsible-content-height);
}
to {
height: 0;
}
}
Accessibility
Adheres to the Disclosure WAI-ARIA design pattern.
Keyboard Interactions
Key | Description |
---|---|
Space | Opens/closes the collapsible |
Enter | Opens/closes the collapsible |