-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathaccordion.tsx
77 lines (73 loc) · 1.87 KB
/
accordion.tsx
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
71
72
73
74
75
76
77
import React, { useState } from 'react';
import { spacing } from '@leafygreen-ui/tokens';
import { css } from '@leafygreen-ui/emotion';
import { uiColors } from '@leafygreen-ui/palette';
import { useId } from '@react-aria/utils';
import { Icon } from './leafygreen';
import { defaultFontSize } from '../compass-font-sizes';
const buttonStyles = css({
fontWeight: 'bold',
fontSize: defaultFontSize,
display: 'flex',
alignItems: 'center',
border: 'none',
background: 'none',
borderRadius: '6px',
boxShadow: 'none',
transition: 'box-shadow 150ms ease-in-out',
'&:hover': {
cursor: 'pointer',
},
'&:focus-visible': {
outline: 'none',
boxShadow: `0 0 0 3px ${uiColors.focus}`,
},
});
const containerStyles = css({
marginTop: spacing[3],
display: 'flex',
alignItems: 'center',
});
const buttonIconStyles = css({
marginRight: spacing[1],
});
interface AccordionProps {
'data-testid'?: string;
text: string;
}
function Accordion(
props: React.PropsWithChildren<AccordionProps>
): React.ReactElement {
const [open, setOpen] = useState(false);
const regionId = useId('region-');
const labelId = useId('label-');
return (
<>
<div className={containerStyles}>
<button
data-testid={props['data-testid']}
className={buttonStyles}
id={labelId}
type="button"
aria-expanded={open ? 'true' : 'false'}
aria-controls={regionId}
onClick={() => {
setOpen((currentOpen) => !currentOpen);
}}
>
<Icon
className={buttonIconStyles}
glyph={open ? 'ChevronDown' : 'ChevronRight'}
/>
{props.text}
</button>
</div>
{open && (
<div role="region" aria-labelledby={labelId} id={regionId}>
{props.children}
</div>
)}
</>
);
}
export default Accordion;