Import
import { Collapse } from '@contentful/f36-components';// orimport { Collapse } from '@contentful/f36-collapse';
Examples
The collapse component is a basic component to show and hide content programmatically.
Basic usage
function CollapseExample() {const [isExpanded, setIsExpanded] = React.useState(true);return (<Stack flexDirection="column"><Button onClick={() => setIsExpanded(!isExpanded)}>Toggle</Button><Collapse isExpanded={isExpanded}><Text>Customers on the Team tier can pay with a credit card (AmericanExpress, MasterCard or Visa). Enterprise customers have the choice ofpaying with a credit card or wire transfer.</Text></Collapse></Stack>);}
Custom transition duration
Use transitionDuration to control how long the expand and collapse animation takes.
function CollapseTransitionDurationExample() {const [isExpanded, setIsExpanded] = React.useState(false);return (<Stack flexDirection="column"><Button onClick={() => setIsExpanded(!isExpanded)}>Toggle</Button><CollapseisExpanded={isExpanded}transitionDuration="transitionDurationDefault"><Text>Customers on the Team tier can pay with a credit card (AmericanExpress, MasterCard or Visa). Enterprise customers have the choice ofpaying with a credit card or wire transfer.</Text></Collapse></Stack>);}
Props (API reference)
Open in StorybookName | Type | Default |
|---|---|---|
| children | ReactNode Child nodes to be rendered in the component | |
| className | string string for additional classNames | |
| isExpanded | false true A boolean that tells if the accordion should be expanded or collapsed | false |
| testId | string A [data-test-id] attribute used for testing purposes | cf-collapse |
| transitionDuration | "transitionDurationShort" "transitionDurationDefault" "transitionDurationLong" Control the expansion/collapsing transition duration. Pass one of the transition duration token values. | transitionDurationDefault |
Content guidelines
- This component offers a controllable way to hide or show content programmatically
- Anything can be passed as the content of the collapse
Accessibility
- Trigger for expanding and closing should be an accessible button. If content is hidden, it gets set to "aria-hidden"="true".