An interactive component which expands/collapses a panel.
Full keyboard navigation.
Can be controlled or uncontrolled.
Install the component from your command line.
npm install @radix-ui/react-collapsible
Import the components and piece the parts together.
import * as Collapsible from '@radix-ui/react-collapsible';
export default () => (
<Collapsible.Root>
<Collapsible.Trigger />
<Collapsible.Content />
</Collapsible.Root>
);
Contains all the parts of a collapsible.
The button that toggles the collapsible.
The component that contains the collapsible content.
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:
// index.jsx
import * as Collapsible from '@radix-ui/react-collapsible';
import './styles.css';
export default () => (
<Collapsible.Root>
<Collapsible.Trigger>…</Collapsible.Trigger>
<Collapsible.Content className="CollapsibleContent">
…
</Collapsible.Content>
</Collapsible.Root>
);
/* 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;
}
}
Adheres to the Disclosure WAI-ARIA design pattern.