-
Notifications
You must be signed in to change notification settings - Fork 37
/
Accordion.js
244 lines (220 loc) · 7.09 KB
/
Accordion.js
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import React, { useEffect, useState, useRef } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {
uniqueId,
noop,
} from 'lodash';
import { DefaultAccordionHeader } from './headers';
import css from './Accordion.css';
import { HotKeys } from '../HotKeys';
import { withAccordionSet } from './AccordionSetContext';
import omitProps from '../../util/omitProps';
const propTypes = {
accordionSet: PropTypes.object,
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.func,
]).isRequired,
className: PropTypes.string,
closedByDefault: PropTypes.bool,
contentHeight: PropTypes.string,
contentId: PropTypes.string,
contentRef: PropTypes.func,
disabled: PropTypes.bool,
displayWhenClosed: PropTypes.element, // eslint-disable-line react/no-unused-prop-types
displayWhenOpen: PropTypes.element, // eslint-disable-line react/no-unused-prop-types
header: PropTypes.elementType,
headerProps: PropTypes.object,
id: PropTypes.string,
label: PropTypes.oneOfType([ // eslint-disable-line react/no-unused-prop-types
PropTypes.element,
PropTypes.string
]).isRequired,
onClickToggle: PropTypes.func,
onToggle: PropTypes.func, // eslint-disable-line react/no-unused-prop-types
open: PropTypes.bool,
separator: PropTypes.bool,
toggleKeyHandlers: PropTypes.object,
toggleKeyMap: PropTypes.object,
};
function getContentClass(open) {
return classNames(
css['content-region'],
{ [`${css.expanded}`]: open },
);
}
function getRootClasses(separator, disabled, className) {
return classNames(
css.accordion,
{ [css.hasSeparator]: separator },
{ [`${css.disabled}`]: disabled },
className,
);
}
function getWrapClass(open) {
return classNames(
css['content-wrap'],
{ [`${css.expanded}`]: open },
);
}
/* The z-index requirements for accordions:
* Accordions should not overlap any overlays/dropdowns from previous/next accordions.
* Accordions/overlays should not be overlapped if focus left the accordion or went to another pane...
*
*/
// loops through other accordions rendered within the UI to find the highest z-index among them all.
const getHighestStackOrder = () => {
const accordions = Array.from(document.querySelectorAll(`.${css['content-wrap']}`));
const highest = accordions?.reduce((acc, cur) => {
let currentZ = getComputedStyle(cur).getPropertyValue('z-index');
// skip any that might have non-integer z-index settings.
if (currentZ === 'auto' || currentZ === null) return acc;
currentZ = parseInt(currentZ, 10);
// this will prevent duplicated highest z-index, since with matching z-index, the tag
// that's ordered last will overlap.
if (currentZ === acc) currentZ += 1;
return currentZ > acc ? currentZ : acc;
}, 2);
return highest;
}
const Accordion = (props) => {
const {
accordionSet,
children,
className = '',
closedByDefault,
contentHeight,
contentId: contentIdProp,
contentRef,
disabled,
header = DefaultAccordionHeader,
headerProps = { headingLevel: 3 },
id,
label,
onClickToggle = noop,
onToggle: onToggleProp,
open,
separator = true,
toggleKeyHandlers,
toggleKeyMap,
} = props;
const toggle = useRef(null);
const content = useRef(null);
const setContentRef = useRef((ref) => {
content.current = ref;
if (typeof contentRef === 'function') {
contentRef(ref);
}
}).current;
const contentId = useRef(contentIdProp || uniqueId('accordion')).current;
const trackingId = useRef(id || uniqueId('acc')).current;
const labelId = useRef(headerProps?.labelId || `accordion-toggle-button-${trackingId}`).current;
const headerRef = useRef(null);
const getRef = useRef(() => toggle.current).current;
const [isOpen, updateOpen] = useState(open || !closedByDefault);
const [registered, updateRegistered] = useState(!accordionSet);
const [zIndex, updateZIndex] = useState(1);
const [focused, updateFocused] = useState(false);
const uncontrolledToggle = useRef(() => {
updateOpen(current => !current);
}).current;
// Affecting z-index when accordions are focused within.
// We only update the accordion z-index if it does not contain focus _and_ if it's not
// already the highest z-index among other accordions.
const handleFocusIn = () => {
if (!focused) {
updateFocused(true);
updateZIndex((cur) => {
if (content.current.matches(':focus-within')) {
// we assign one greater than the highest z-index value.
const highest = getHighestStackOrder() + 1;
if (cur !== highest) {
return highest;
}
}
return cur;
});
}
}
const handleFocusOut = () => {
updateFocused(false);
}
const onToggle = (toggleArgs) => {
if (typeof open === 'undefined') {
uncontrolledToggle(toggleArgs);
} else {
onToggleProp(toggleArgs);
}
onClickToggle({ ...toggleArgs, open: !isOpen });
};
useEffect(() => {
if (open !== undefined) {
updateOpen(open);
}
}, [open]);
// At registration, accordions are assigned a z-index that, in most cases,
// will render in reverse order.
useEffect(() => { // eslint-disable-line
function registrationCallback(isRegistered) {
updateRegistered(isRegistered);
if (accordionSet) {
const defaultZIndex = accordionSet.getStackOrder(trackingId);
updateZIndex(defaultZIndex);
}
}
if (accordionSet) {
accordionSet.registerAccordion(getRef, trackingId, closedByDefault, registrationCallback);
return () => {
accordionSet.unregisterAccordion(trackingId);
};
}
}, []); // eslint-disable-line react-hooks/exhaustive-deps
const accordionHeaderProps = Object.assign({}, omitProps(props, ['contentHeight', 'headerProps']), {
contentId,
toggleRef: (ref) => { toggle.current = ref; },
open: isOpen,
onToggle,
label,
labelId,
...headerProps
});
const headerElement = React.createElement(header, accordionHeaderProps);
if (!registered) return null;
return (
<section
id={trackingId}
className={getRootClasses(separator, disabled, className)}
data-test-accordion-section
onFocus={handleFocusIn}
onBlur={handleFocusOut}
>
<HotKeys
id={`${trackingId}-hotkeys`}
keyMap={toggleKeyMap}
handlers={toggleKeyHandlers}
attach={headerRef.current}
noWrapper
>
<div ref={headerRef} style={{ width: '100%', display: 'flex' }}>
{headerElement}
</div>
</HotKeys>
<div className={getWrapClass(isOpen)} style={{ zIndex }}>
<div
role="region"
className={getContentClass(isOpen)}
ref={setContentRef}
id={contentId}
aria-labelledby={accordionHeaderProps.labelId}
style={contentHeight ? { height: contentHeight } : null}
data-test-accordion-wrapper
>
{typeof children === 'function' ? children(isOpen) : children}
</div>
</div>
</section>
);
};
Accordion.propTypes = propTypes;
export default withAccordionSet(Accordion);