-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathCollapsible.tsx
More file actions
70 lines (63 loc) · 1.78 KB
/
Copy pathCollapsible.tsx
File metadata and controls
70 lines (63 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { ClassNames } from '@emotion/react';
import { IconAngleDown, IconAngleUp, IconSize } from 'hds-react';
import React from 'react';
import { useTheme } from '../../../domain/app/theme/Theme';
import styles from './collapsible.module.scss';
type Props = {
defaultOpen?: boolean;
headingLevel?: number;
title: string;
};
const Collabsible: React.FC<React.PropsWithChildren<Props>> = ({
children,
defaultOpen = false,
headingLevel = 3,
title,
}) => {
const { theme } = useTheme();
const [isOpen, setIsOpen] = React.useState(defaultOpen);
const id = React.useId();
const collapsibleHeadingId = `collapsible-heading-${id}`;
const collapsiblePanelId = `collapsible-panel-${id}`;
const togglePanel = () => {
setIsOpen(!isOpen);
};
return (
<ClassNames>
{({ css, cx }) => (
<div
className={cx(styles.collapsible, css(theme.collapsible), {
[styles.expanded]: isOpen,
})}
>
<div role="heading" aria-level={headingLevel}>
<button
className={styles.button}
id={collapsibleHeadingId}
aria-expanded={isOpen}
aria-controls={collapsiblePanelId}
onClick={togglePanel}
type="button"
>
{isOpen ? (
<IconAngleUp size={IconSize.Medium} />
) : (
<IconAngleDown size={IconSize.Medium} />
)}
<span>{title}</span>
</button>
</div>
<div
role="region"
aria-labelledby={collapsibleHeadingId}
id={collapsiblePanelId}
hidden={!isOpen}
>
{children}
</div>
</div>
)}
</ClassNames>
);
};
export default Collabsible;