UNPKG

4.39 kBJavaScriptView Raw
1import _get from 'lodash/get';
2import { e as _defineProperty, d as _inheritsLoose, f as _assertThisInitialized } from './chunk-1f79df9b.js';
3import { createRef, createElement, Component } from 'react';
4import createFocusTrap from 'focus-trap';
5
6var convertToMs = function convertToMs(time) {
7 if (/([0-9]*[.])?[0-9]+ms/.test(time)) {
8 return parseFloat(time.replace('ms', ''));
9 }
10
11 if (/([0-9]*[.])?[0-9]+s/.test(time)) {
12 return parseFloat(time.replace('s', '')) * 1000;
13 }
14
15 return 0;
16};
17
18var noop = function noop() {};
19
20var TrapFocus =
21/*#__PURE__*/
22function (_React$Component) {
23 _inheritsLoose(TrapFocus, _React$Component);
24
25 function TrapFocus() {
26 var _this;
27
28 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
29 args[_key] = arguments[_key];
30 }
31
32 _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
33
34 _defineProperty(_assertThisInitialized(_this), "state", {
35 originalAriaHiddenValues: []
36 });
37
38 _defineProperty(_assertThisInitialized(_this), "fallbackFocus", createRef());
39
40 _defineProperty(_assertThisInitialized(_this), "initialFocus", createRef());
41
42 _defineProperty(_assertThisInitialized(_this), "wrapper", createRef());
43
44 _defineProperty(_assertThisInitialized(_this), "trap", {
45 activate: noop,
46 deactivate: noop
47 });
48
49 _defineProperty(_assertThisInitialized(_this), "componentDidMount", function () {
50 var _this$props = _this.props,
51 delayToActivate = _this$props.delayToActivate,
52 isActive = _this$props.isActive,
53 usesPortal = _this$props.usesPortal; // @ts-ignore
54
55 _this.trap = createFocusTrap(_this.wrapper.current, {
56 initialFocus: _this.initialFocus.current,
57 fallbackFocus: _this.fallbackFocus.current,
58 escapeDeactivates: false,
59 clickOutsideDeactivates: false
60 });
61
62 if (isActive) {
63 if (delayToActivate) {
64 setTimeout(_this.trap.activate, convertToMs(delayToActivate));
65 } else {
66 _this.trap.activate();
67 }
68 }
69
70 if (usesPortal) {
71 Array.from(_get(document, 'body.children', [])).forEach(function (child) {
72 if (child.contains(_this.wrapper.current)) return;
73
74 _this.setState(function (prevState) {
75 return {
76 originalAriaHiddenValues: [].concat(prevState.originalAriaHiddenValues || [], [child.getAttribute('aria-hidden')])
77 };
78 });
79 });
80 }
81 });
82
83 _defineProperty(_assertThisInitialized(_this), "componentDidUpdate", function (prevProps) {
84 var _this$props2 = _this.props,
85 delayToActivate = _this$props2.delayToActivate,
86 isActive = _this$props2.isActive,
87 usesPortal = _this$props2.usesPortal;
88 var originalAriaHiddenValues = _this.state.originalAriaHiddenValues;
89
90 if (isActive !== prevProps.isActive) {
91 if (isActive) {
92 if (delayToActivate) {
93 setTimeout(_this.trap.activate, convertToMs(delayToActivate));
94 } else {
95 _this.trap.activate();
96 }
97 } else {
98 _this.trap.deactivate();
99 }
100
101 if (usesPortal) {
102 Array.from(_get(document, 'body.children', [])).forEach(function (child, i) {
103 if (child.contains(_this.wrapper.current)) {
104 return;
105 }
106
107 if (isActive) {
108 child.setAttribute('aria-hidden', 'true');
109 return;
110 }
111
112 if (originalAriaHiddenValues[i]) {
113 child.setAttribute('aria-hidden', originalAriaHiddenValues[i]);
114 return;
115 }
116
117 child.removeAttribute('aria-hidden');
118 });
119 }
120 }
121 });
122
123 _defineProperty(_assertThisInitialized(_this), "componentWillUnmount", function () {
124 _this.trap.deactivate();
125 });
126
127 _defineProperty(_assertThisInitialized(_this), "render", function () {
128 var children = _this.props.children;
129 return createElement("div", {
130 ref: _this.wrapper
131 }, children({
132 fallbackFocusRef: _this.fallbackFocus,
133 initialFocusRef: _this.initialFocus
134 }));
135 });
136
137 return _this;
138 }
139
140 return TrapFocus;
141}(Component);
142
143_defineProperty(TrapFocus, "defaultProps", {
144 isActive: false,
145 usesPortal: false
146});
147
148export { TrapFocus as a };