| 1 |
--- |
| 2 |
id: Expandable section |
| 3 |
section: components |
| 4 |
cssPrefix: pf-v6-c-expandable-section |
| 5 |
propComponents: ['ExpandableSection', 'ExpandableSectionToggle'] |
| 6 |
--- |
| 7 |
|
| 8 |
import { useState } from 'react'; |
| 9 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon'; |
| 10 |
|
| 11 |
## Examples |
| 12 |
|
| 13 |
### Basic |
| 14 |
|
| 15 |
```ts file="ExpandableSectionBasic.tsx" |
| 16 |
|
| 17 |
``` |
| 18 |
|
| 19 |
### Uncontrolled |
| 20 |
|
| 21 |
```ts file="ExpandableSectionUncontrolled.tsx" |
| 22 |
|
| 23 |
``` |
| 24 |
|
| 25 |
### Uncontrolled with dynamic toggle text |
| 26 |
|
| 27 |
```ts file="ExpandableSectionUncontrolledDynamicToggleText.tsx" |
| 28 |
|
| 29 |
``` |
| 30 |
|
| 31 |
### Uncontrolled with dynamic toggle content (function) |
| 32 |
|
| 33 |
Use `toggleContent` as a function to dynamically render different content based on the expanded state without managing state yourself. |
| 34 |
|
| 35 |
```ts file="ExpandableSectionUncontrolledDynamicToggleFunction.tsx" |
| 36 |
|
| 37 |
``` |
| 38 |
|
| 39 |
### Detached |
| 40 |
|
| 41 |
When passing the `isDetached` property into `<ExpandableSection>`, you must also manually pass in the same `toggleId` and `contentId` properties to both `<ExpandableSection>` and `<ExpandableSectionToggle>`. This will link the content to the toggle via ARIA attributes. |
| 42 |
|
| 43 |
By default animations will not be enabled for a detached `<ExpandableSection>`. You must manually pass the `direction` property with an appropriate value based on where the expandable content is rendered. If the expandable content is above the expandable toggle, `direction="up"` must be passed like in this example. |
| 44 |
|
| 45 |
```ts file="ExpandableSectionDetached.tsx" |
| 46 |
|
| 47 |
``` |
| 48 |
|
| 49 |
### Disclosure variation |
| 50 |
|
| 51 |
You can pass in the `displaySize="lg"` property for a disclosure variation styling. |
| 52 |
|
| 53 |
```ts file="ExpandableSectionDisclosure.tsx" |
| 54 |
|
| 55 |
``` |
| 56 |
|
| 57 |
### Indented expandable content |
| 58 |
|
| 59 |
You can indent the expandable content by passing in the `isIndented` property. This will not affect the expandable toggle. |
| 60 |
|
| 61 |
```ts file="ExpandableSectionIndented.tsx" |
| 62 |
|
| 63 |
``` |
| 64 |
|
| 65 |
### With custom toggle content |
| 66 |
|
| 67 |
By using the `toggleContent` prop, you can pass in content other than a simple string such as an icon or a badge. When passing in custom content in this way, you should not pass in any interactive element such as a button. |
| 68 |
|
| 69 |
```ts file="ExpandableSectionCustomToggle.tsx" |
| 70 |
|
| 71 |
``` |
| 72 |
|
| 73 |
### With heading semantics |
| 74 |
|
| 75 |
When the toggle text should function as a heading in the document structure, use the `toggleWrapper` prop to specify a heading element (h1-h6). This ensures proper semantic structure for screen readers and other assistive technologies. The component automatically uses a native button element when heading wrappers are used, allowing the heading styles to display properly. |
| 76 |
|
| 77 |
```ts file="ExpandableSectionWithHeading.tsx" |
| 78 |
|
| 79 |
``` |
| 80 |
|
| 81 |
### Truncate expansion |
| 82 |
|
| 83 |
By passing in `variant="truncate"`, the expandable content will be visible up to a maximum number of lines before being truncated, with the toggle revealing or hiding the truncated content. By default the expandable content will truncate after 3 lines, and this can be customized by also passing in the `truncateMaxLines` prop. |
| 84 |
|
| 85 |
```ts file="ExpandableSectionTruncateExpansion.tsx" |
| 86 |
|
| 87 |
``` |