Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 3x 3x 3x 3x 3x | /*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// references https://github.com/davidtheclark/focus-trap-react
// @flow
import * as React from 'react';
import createFocusTrap from 'focus-trap';
type Props = {
children: React.Node,
};
class TrapFocus extends React.Component<Props, {}> {
focusTrapElement = (React.createRef(): {current: HTMLDivElement | null});
previouslyFocusedElement: ?HTMLElement = null;
// eslint-disable-next-line flowtype/no-weak-types
focusTrap: ?{deactivate: Function, activate: Function} = null;
componentDidMount() {
Eif (__BROWSER__) {
Eif (typeof document !== 'undefined') {
this.previouslyFocusedElement = document.activeElement;
}
this.focusTrap = createFocusTrap(this.focusTrapElement.current);
// enables proper execution when testing with enzyme similar issue to:
// https://github.com/davidtheclark/focus-trap-react/issues/24
// relevant Quora answer for why setTimeout with 0 ms is useful:
// https://www.quora.com/What-does-setTimeout-with-a-0ms-delay-do-Is-this-some-trick-to-spawn-a-new-thread-but-why/answer/Andreas-Blixt
setTimeout(() => this.focusTrap && this.focusTrap.activate(), 0);
}
}
componentWillUnmount() {
if (__BROWSER__) {
this.focusTrap && this.focusTrap.deactivate();
this.previouslyFocusedElement && this.previouslyFocusedElement.focus();
}
}
render() {
return <div ref={this.focusTrapElement}>{this.props.children}</div>;
}
}
export default TrapFocus;
|