| 1 |
import { useState, MouseEvent } from 'react'; |
| 2 |
import { ExpandableSection, ExpandableSectionToggle, Stack, StackItem } from '@patternfly/react-core'; |
| 3 |
import RhUiCheckCircleFillIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-check-circle-fill-icon'; |
| 4 |
|
| 5 |
export const ExpandableSectionWithHeading = () => { |
| 6 |
const [isExpanded1, setIsExpanded1] = useState(false); |
| 7 |
const [isExpanded2, setIsExpanded2] = useState(false); |
| 8 |
const [isExpandedDetached, setIsExpandedDetached] = useState(false); |
| 9 |
|
| 10 |
const onToggle1 = (_event: MouseEvent, isExpanded: boolean) => { |
| 11 |
setIsExpanded1(isExpanded); |
| 12 |
}; |
| 13 |
|
| 14 |
const onToggle2 = (_event: MouseEvent, isExpanded: boolean) => { |
| 15 |
setIsExpanded2(isExpanded); |
| 16 |
}; |
| 17 |
|
| 18 |
const onToggleDetached = (isExpanded: boolean) => { |
| 19 |
setIsExpandedDetached(isExpanded); |
| 20 |
}; |
| 21 |
|
| 22 |
return ( |
| 23 |
<Stack hasGutter> |
| 24 |
<StackItem> |
| 25 |
<h4>Document with Expandable Sections</h4> |
| 26 |
<p>This demonstrates how to use expandable sections with proper heading semantics.</p> |
| 27 |
|
| 28 |
{/* Using toggleWrapper prop for proper heading semantics */} |
| 29 |
<ExpandableSection |
| 30 |
toggleWrapper="h5" |
| 31 |
toggleText="Toggle as a heading" |
| 32 |
onToggle={onToggle1} |
| 33 |
isExpanded={isExpanded1} |
| 34 |
> |
| 35 |
<p> |
| 36 |
This content is visible only when the component is expanded. The toggle text above functions as a proper |
| 37 |
heading in the document structure, which is important for screen readers and other assistive technologies. |
| 38 |
</p> |
| 39 |
<p> |
| 40 |
When using the <code>toggleWrapper</code> prop with heading elements (h1-h6), the button is rendered inside |
| 41 |
the heading element, maintaining proper semantic structure. |
| 42 |
</p> |
| 43 |
</ExpandableSection> |
| 44 |
</StackItem> |
| 45 |
|
| 46 |
<StackItem> |
| 47 |
<h4>Detached Variant with Heading</h4> |
| 48 |
<p>You can also use the detached variant with heading semantics:</p> |
| 49 |
|
| 50 |
<ExpandableSectionToggle |
| 51 |
toggleWrapper="h5" |
| 52 |
toggleId="detached-heading-toggle" |
| 53 |
contentId="detached-heading-content" |
| 54 |
isExpanded={isExpandedDetached} |
| 55 |
onToggle={onToggleDetached} |
| 56 |
> |
| 57 |
Detached Toggle with Heading |
| 58 |
</ExpandableSectionToggle> |
| 59 |
|
| 60 |
<ExpandableSection |
| 61 |
isDetached |
| 62 |
toggleId="detached-heading-toggle" |
| 63 |
contentId="detached-heading-content" |
| 64 |
isExpanded={isExpandedDetached} |
| 65 |
> |
| 66 |
<p>This is detached content that can be positioned anywhere in the DOM.</p> |
| 67 |
</ExpandableSection> |
| 68 |
</StackItem> |
| 69 |
|
| 70 |
<StackItem> |
| 71 |
<h4>Custom Content with Heading</h4> |
| 72 |
<p>You can also use custom content within heading wrappers:</p> |
| 73 |
|
| 74 |
<ExpandableSection |
| 75 |
toggleWrapper="h5" |
| 76 |
toggleContent={ |
| 77 |
<span> |
| 78 |
<RhUiCheckCircleFillIcon /> Custom Heading Content with Icon |
| 79 |
</span> |
| 80 |
} |
| 81 |
onToggle={onToggle2} |
| 82 |
isExpanded={isExpanded2} |
| 83 |
> |
| 84 |
<p>This expandable section uses custom content with an icon inside a heading wrapper.</p> |
| 85 |
</ExpandableSection> |
| 86 |
</StackItem> |
| 87 |
</Stack> |
| 88 |
); |
| 89 |
}; |