-
Notifications
You must be signed in to change notification settings - Fork 37
/
WrappingElement.js
179 lines (156 loc) · 5.68 KB
/
WrappingElement.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
import {
useEffect,
useRef,
forwardRef,
useImperativeHandle,
useCallback
} from 'react';
import PropTypes from 'prop-types';
import * as focusTrap from 'focus-trap';
import { listen } from '../../util/listen';
import StripesOverlayWrapper from '../../util/StripesOverlayWrapper';
import { OVERLAY_CONTAINER_SELECTOR } from '../../util/consts';
import calloutCSS from '../Callout/Callout.css';
import overlayCSS from '../Popper/Popper.css';
import css from './Modal.css';
// applies/removes the `inert` attribute to elements outside of the `OverlayContainer` element.
// `inert` blocks focus/mouse clicks as well as prevents screen readers from accessing those parts
// of the document.
export function useAssistiveTechTrap() {
const apply = () => {
const mainNav = document.querySelector('header[class^=navRoot]'); // main navigation
const overlaySiblings = Array.from(document.querySelectorAll( // siblings of the overlay container
`${OVERLAY_CONTAINER_SELECTOR} ~ *`
));
const elementList = [mainNav, ...overlaySiblings];
elementList.forEach(el => el?.setAttribute?.('inert', ''));
}
const release = () => {
const inertElements = Array.from(document.querySelectorAll('[inert]'));
inertElements?.forEach(el => el?.removeAttribute?.('inert'));
}
return {
release,
apply
}
}
const WrappingElement = forwardRef(({
children,
enforceFocus,
tagName = 'div',
portalElement,
triggerRef,
modalElementRef,
onClose,
onOpen,
setContentFocused,
restoreFocus,
sendFocusToModal,
...rest
}, ref) => {
// Ref to the outer element provided as props but defaults as a div.
const wrappingElementRef = useRef(null);
// imperitive handle since we want to provide the wrapping element ref to the consuming app as the 'ref' prop.
useImperativeHandle(ref, () => wrappingElementRef.current, [wrappingElementRef]);
// Ref for the `focus-trap` instance.
const focusTrapRef = useRef(null);
// refs to hold handler-removal functions so that any DOM-level listeners can be removed when the modal is hidden
// or dismounts.
const keyDownHandlerRef = useRef(null);
const focusHandlerRef = useRef(null);
// mechanism for applying 'inert' attribute (screen-reader trap);
const ATTrap = useAssistiveTechTrap();
// keydown Handler for detecting 'escape' key presses...
const keyDownHandler = useCallback((e) => {
if (e.key.toLowerCase() === 'escape') {
e.preventDefault();
onClose();
}
}, [onClose]);
// if focus is outside of the modal or acceptable containers, return it to the modal.
const enforceFocusHandler = useCallback(() => {
// collect possible DOM elements for focus when a modal is active (<Modal>s, <Callout>s, <Popper>s (overlays)).
// check if the active element is contained among them to prevent multiple modals from fighting over focus.
const containers = Array.from(document.querySelectorAll(
`.${css.modalRoot}, .${calloutCSS.callout}, .${overlayCSS.overlay}`
));
const focusIsWithin = containers.some((m) => m.contains(document.activeElement));
if (!focusIsWithin) {
sendFocusToModal();
}
}, [sendFocusToModal]);
const enforceFocusTimeout = useCallback(() => {
setTimeout(enforceFocusHandler);
}, [enforceFocusHandler]);
// open/closed effects - sending focus to modal, setting up focus-trapping,
// keyboard/focus listeners...
useEffect(() => {
if (!triggerRef.current) {
triggerRef.current = document.activeElement;
}
const modalContainsFocus = modalElementRef.current?.contains(document.activeElement);
if (!modalContainsFocus) {
sendFocusToModal();
}
if (modalElementRef.current) {
// create the focus trap if we don't already have one.
if (!focusTrapRef.current) {
focusTrapRef.current = focusTrap.createFocusTrap(portalElement, {
returnFocusOnDeactivate: false,
checkCanReturnFocus: null,
fallbackFocus: modalElementRef.current,
// we handle our own deactivation when the modal closes...
clickOutsideDeactivates: false,
});
}
if (enforceFocus) {
focusTrapRef.current.activate();
ATTrap.apply();
}
keyDownHandlerRef.current = listen(modalElementRef.current, 'keydown', keyDownHandler);
focusHandlerRef.current = listen(document, 'focus', enforceFocusTimeout, true);
onOpen();
}
return () => {
if (focusTrapRef.current?.active) {
focusTrapRef.current?.deactivate({
returnFocus: false,
checkCanReturnFocus: null
});
}
focusTrapRef.current = null;
ATTrap.release();
keyDownHandlerRef.current?.();
focusHandlerRef.current?.();
// if lastActive is within the modal itself, focusManagement was probably handled
// outside of the component, so don't do anything if the suspected trigger is inside of the modal.
setContentFocused(false);
if (restoreFocus) triggerRef.current?.focus();
}
}, []);
const WrappingElementComponent = tagName;
return (
<WrappingElementComponent
ref={wrappingElementRef}
{...rest}
>
<StripesOverlayWrapper>
{children}
</StripesOverlayWrapper>
</WrappingElementComponent>
);
});
WrappingElement.propTypes = {
children: PropTypes.node,
enforceFocus: PropTypes.bool,
modalElementRef: PropTypes.object,
onClose: PropTypes.func,
onOpen: PropTypes.func,
portalElement: PropTypes.instanceOf(HTMLElement),
restoreFocus: PropTypes.bool,
sendFocusToModal: PropTypes.func,
setContentFocused: PropTypes.func,
tagName: PropTypes.oneOfType([PropTypes.string, PropTypes.elementType]),
triggerRef: PropTypes.object,
}
export default WrappingElement;