-
Notifications
You must be signed in to change notification settings - Fork 37
/
Layer.js
161 lines (137 loc) · 3.82 KB
/
Layer.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
/*
* used to create a new layer of panes within a <Paneset/>
*/
import React from 'react';
import { createPortal } from 'react-dom';
import PropTypes from 'prop-types';
import componentOrElement from 'prop-types-extra/lib/componentOrElement';
import trapFocus from '../../util/trapFocus';
import css from './Layer.css';
import { withPaneset } from '../Paneset/PanesetContext';
import { withResize } from '../Paneset/ResizeContext';
class Layer extends React.Component {
static propTypes = {
afterClose: PropTypes.func,
afterOpen: PropTypes.func,
autoFocus: PropTypes.bool,
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
container: componentOrElement,
contentLabel: PropTypes.string,
enforceFocus: PropTypes.bool,
inRootSet: PropTypes.bool,
isOpen: PropTypes.bool.isRequired,
paneset: PropTypes.shape({
getContainer: PropTypes.func,
getTopmostContainer: PropTypes.func,
handleClose: PropTypes.func,
registerPane: PropTypes.func,
removePane: PropTypes.func,
}),
resizer: PropTypes.shape({
resume: PropTypes.func.isRequired,
suspend: PropTypes.func.isRequired,
}).isRequired,
}
static defaultProps = {
enforceFocus: true,
autoFocus: true,
afterOpen: () => {},
afterClose: () => {},
}
constructor(props) {
super(props);
this.contentRef = React.createRef();
this.layerContainer = null;
this.maybeEnforceFocus = this.maybeEnforceFocus.bind(this);
this.handleOpen = this.handleOpen.bind(this);
this.handleClose = this.handleClose.bind(this);
}
componentDidMount() {
const {
isOpen,
} = this.props;
if (isOpen) {
if (!this.layerContainer) {
this.forceUpdate();
}
this.handleOpen();
}
}
componentDidUpdate(prevProps) {
const {
isOpen,
} = this.props;
if (!prevProps.isOpen && isOpen) {
this.handleOpen();
}
if (prevProps.isOpen && !isOpen) {
this.handleClose();
}
}
componentWillUnmount() {
document.removeEventListener('focus', this.maybeEnforceFocus, true);
}
handleOpen() {
const { afterOpen, contentLabel, autoFocus, resizer } = this.props;
resizer.suspend();
if (autoFocus && this.contentRef.current) {
if (!this.contentRef.current.contains(document.activeElement)) {
this.contentRef.current.focus();
}
}
document.addEventListener('focus', this.maybeEnforceFocus, true);
afterOpen();
if (!contentLabel) {
console.warn("A11y: please supply a string to the <Layer> prop 'contentLabel' for assistive technology users");
}
}
handleClose() {
const { afterClose, resizer } = this.props;
resizer.resume();
document.removeEventListener('focus', this.maybeEnforceFocus, true);
afterClose();
}
maybeEnforceFocus() {
if (this.props.enforceFocus) {
trapFocus(this.contentRef.current, [...document.querySelectorAll(`.${css.LayerRoot}`)]);
}
}
getLayerContainer = () => {
const { container, inRootSet, paneset } = this.props;
return container || inRootSet ?
paneset.getTopmostContainer() : paneset.getContainer();
}
render() {
const {
isOpen,
children,
contentLabel,
} = this.props;
if (!this.layerContainer) {
this.layerContainer = this.getLayerContainer();
}
if (isOpen && this.layerContainer) {
return createPortal(
<>
<div
className={css.LayerRoot}
role="dialog"
key="container"
tabIndex="-1"
ref={this.contentRef}
aria-label={contentLabel}
>
{children}
</div>
</>,
this.layerContainer
);
} else {
return null;
}
}
}
export default withPaneset(withResize(Layer));