Debian Codegraph @

node/@patternfly/react-core/src/components/ExpandableSection/ExpandableSectionToggle.tsx

raw
1 import styles from '@patternfly/react-styles/css/components/ExpandableSection/expandable-section';
2 import { css } from '@patternfly/react-styles';
3 import RhMicronsCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-microns-caret-down-icon';
4 import { Button } from '../Button';
5
6 /** Acts as the toggle sub-component when the main expandable section component has the isDetached
7 * property passed in. Allows for more custom control over the expandable section's toggle.
8 */
9
10 export interface ExpandableSectionToggleProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onToggle'> {
11 /** Content rendered inside the expandable toggle. */
12 children?: React.ReactNode;
13 /** Additional classes added to the expandable toggle. */
14 className?: string;
15 /** Id of the toggle's respective expandable section content. The value passed into this
16 * property should match the contentId property of the main expandable section component.
17 */
18 contentId?: string;
19 /** Id of the toggle. The value passed into this property should match the aria-labelledby
20 * property of the main expandable section component.
21 */
22 toggleId?: string;
23 /** Direction the toggle arrow should point when the expandable section is expanded. */
24 direction?: 'up' | 'down';
25 /** Flag to determine toggle styling when the expandable content is truncated. */
26 hasTruncatedContent?: boolean;
27 /** Flag indicating if the expandable section is expanded. */
28 isExpanded?: boolean;
29 /** Callback function to toggle the expandable content. */
30 onToggle?: (isExpanded: boolean) => void;
31 /** Flag indicating that the expandable section and expandable toggle are detached from one another. */
32 isDetached?: boolean;
33 /** Accessible name via human readable string for the expandable section toggle. */
34 toggleAriaLabel?: string;
35 /** Accessible name via space delimtted list of IDs for the expandable section toggle. */
36 toggleAriaLabelledBy?: string;
37 /** The HTML element to use for the toggle wrapper. Can be 'div' (default) or any heading level.
38 * When using heading elements, the button will be rendered inside the heading for proper semantics.
39 * This is useful when the toggle text should function as a heading in the document structure.
40 */
41 toggleWrapper?: 'div' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
42 }
43
44 export const ExpandableSectionToggle: React.FunctionComponent<ExpandableSectionToggleProps> = ({
45 children,
46 className = '',
47 isExpanded = false,
48 onToggle,
49 contentId,
50 toggleId,
51 direction = 'down',
52 hasTruncatedContent = false,
53 isDetached,
54 toggleAriaLabel,
55 toggleAriaLabelledBy,
56 toggleWrapper = 'div',
57 ...props
58 }: ExpandableSectionToggleProps) => {
59 const ToggleWrapper = toggleWrapper as any;
60
61 return (
62 <div
63 className={css(
64 styles.expandableSection,
65 isExpanded && styles.modifiers.expanded,
66 hasTruncatedContent && styles.modifiers.truncate,
67 isDetached && 'pf-m-detached',
68 className
69 )}
70 {...props}
71 >
72 <ToggleWrapper className={`${styles.expandableSection}__toggle`}>
73 <Button
74 variant="link"
75 {...(hasTruncatedContent && { isInline: true })}
76 aria-expanded={isExpanded}
77 aria-controls={contentId}
78 onClick={() => onToggle(!isExpanded)}
79 id={toggleId}
80 {...(!hasTruncatedContent && {
81 icon: (
82 <span
83 className={css(
84 styles.expandableSectionToggleIcon,
85 isExpanded && direction === 'up' && styles.modifiers.expandTop // TODO: next breaking change move this class to the outer styles.expandableSection wrapper
86 )}
87 >
88 <RhMicronsCaretDownIcon />
89 </span>
90 )
91 })}
92 aria-label={toggleAriaLabel}
93 aria-labelledby={toggleAriaLabelledBy}
94 >
95 {children}
96 </Button>
97 </ToggleWrapper>
98 </div>
99 );
100 };
101
102 ExpandableSectionToggle.displayName = 'ExpandableSectionToggle';