-
Notifications
You must be signed in to change notification settings - Fork 2
/
Accordion.tsx
115 lines (111 loc) · 3.11 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React, { useState, ReactNode } from 'react';
import { css } from '@emotion/core';
import { Heading, Text } from '../content';
import { Icon, ArrowIosDownwardOutline } from '../icon';
import { classNames } from '../utils/classNames';
import theme from '../theme';
export interface AccordionProps {
children: ReactNode;
}
/**
* Accordion component for having collapsible sections
* @see https://www.w3.org/TR/wai-aria-practices-1.1/#accordion
*/
export function Accordion({ children }: AccordionProps) {
return (
<div
className="ac-accordion"
role="region"
css={css`
background-color: ${theme.components.accordion.backgroundColor};
--accordion-animation-duration: ${theme.animation.global.duration}ms;
`}
>
{children}
</div>
);
}
export interface AccordionItemProps {
title: string;
/**
* A unique id for this part of the UI. Necessary for ally
*/
id: string;
defaultIsOpen?: boolean;
children: ReactNode;
}
export function AccordionItem(props: AccordionItemProps) {
const { title, id, defaultIsOpen = true, children } = props;
const [isOpen, setIsOpen] = useState(defaultIsOpen);
const contentId = `${id}-content`,
headerId = `${id}-heading`;
return (
<div
className={classNames('ac-accordion-item', {
'is-open': isOpen,
})}
role="presentation"
css={css`
&.is-open {
.ac-accordion-itemIndicator {
transform: rotate(180deg);
}
}
`}
>
<Heading level={3}>
<button
id={headerId}
css={css`
cursor: pointer;
padding: 16px 16px;
display: block;
width: 100%;
display: flex;
flex-direction: row;
flex: 1 1 auto;
justify-content: space-between;
align-items: center;
appearance: none;
background-color: inherit;
border: 0;
text-align: start;
color: ${theme.colors.text1};
border-bottom: 1px solid ${theme.colors.dividerColor};
/* remove outline - TODO might need to give a visual cue that this area is in focus */
outline: none;
`}
onClick={() => {
setIsOpen(!isOpen);
}}
aria-controls={contentId}
aria-expanded={isOpen}
>
<Text textSize="large">{title}</Text>
<Icon
svg={<ArrowIosDownwardOutline />}
className="ac-accordion-itemIndicator"
css={css`
transition: transform ease var(--accordion-animation-duration);
transform: rotate(0deg);
`}
aria-hidden={true}
/>
</button>
</Heading>
<div
className="ac-accordion-itemContent"
id={contentId}
role="region"
css={css`
border-bottom: 1px solid ${theme.colors.dividerColor};
display: ${isOpen ? 'block' : 'none'};
`}
aria-labelledby={headerId}
aria-hidden={!isOpen}
>
{children}
</div>
</div>
);
}