1 | "use strict";
|
2 |
|
3 | var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
|
4 |
|
5 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
6 |
|
7 | exports.__esModule = true;
|
8 | exports["default"] = void 0;
|
9 |
|
10 | var _matches = _interopRequireDefault(require("dom-helpers/matches"));
|
11 |
|
12 | var _querySelectorAll = _interopRequireDefault(require("dom-helpers/querySelectorAll"));
|
13 |
|
14 | var _addEventListener = _interopRequireDefault(require("dom-helpers/addEventListener"));
|
15 |
|
16 | var _react = _interopRequireWildcard(require("react"));
|
17 |
|
18 | var _propTypes = _interopRequireDefault(require("prop-types"));
|
19 |
|
20 | var _uncontrollable = require("uncontrollable");
|
21 |
|
22 | var _usePrevious = _interopRequireDefault(require("@restart/hooks/usePrevious"));
|
23 |
|
24 | var _useForceUpdate = _interopRequireDefault(require("@restart/hooks/useForceUpdate"));
|
25 |
|
26 | var _useGlobalListener = _interopRequireDefault(require("@restart/hooks/useGlobalListener"));
|
27 |
|
28 | var _useEventCallback = _interopRequireDefault(require("@restart/hooks/useEventCallback"));
|
29 |
|
30 | var _DropdownContext = _interopRequireDefault(require("./DropdownContext"));
|
31 |
|
32 | var _DropdownMenu = _interopRequireDefault(require("./DropdownMenu"));
|
33 |
|
34 | var _DropdownToggle = _interopRequireDefault(require("./DropdownToggle"));
|
35 |
|
36 | var propTypes = {
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | children: _propTypes["default"].node,
|
49 |
|
50 | |
51 |
|
52 |
|
53 | drop: _propTypes["default"].oneOf(['up', 'left', 'right', 'down']),
|
54 |
|
55 | |
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | focusFirstItemOnShow: _propTypes["default"].oneOf([false, true, 'keyboard']),
|
64 |
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 | itemSelector: _propTypes["default"].string,
|
71 |
|
72 | |
73 |
|
74 |
|
75 | alignEnd: _propTypes["default"].bool,
|
76 |
|
77 | |
78 |
|
79 |
|
80 |
|
81 |
|
82 | show: _propTypes["default"].bool,
|
83 |
|
84 | |
85 |
|
86 |
|
87 | defaultShow: _propTypes["default"].bool,
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | onToggle: _propTypes["default"].func
|
103 | };
|
104 |
|
105 | function useRefWithUpdate() {
|
106 | var forceUpdate = (0, _useForceUpdate["default"])();
|
107 | var ref = (0, _react.useRef)(null);
|
108 | var attachRef = (0, _react.useCallback)(function (element) {
|
109 | ref.current = element;
|
110 |
|
111 | forceUpdate();
|
112 | }, [forceUpdate]);
|
113 | return [ref, attachRef];
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | function Dropdown(_ref) {
|
122 | var drop = _ref.drop,
|
123 | alignEnd = _ref.alignEnd,
|
124 | defaultShow = _ref.defaultShow,
|
125 | rawShow = _ref.show,
|
126 | rawOnToggle = _ref.onToggle,
|
127 | _ref$itemSelector = _ref.itemSelector,
|
128 | itemSelector = _ref$itemSelector === void 0 ? '* > *' : _ref$itemSelector,
|
129 | focusFirstItemOnShow = _ref.focusFirstItemOnShow,
|
130 | children = _ref.children;
|
131 |
|
132 | var _useUncontrolledProp = (0, _uncontrollable.useUncontrolledProp)(rawShow, defaultShow, rawOnToggle),
|
133 | show = _useUncontrolledProp[0],
|
134 | onToggle = _useUncontrolledProp[1];
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | var _useRefWithUpdate = useRefWithUpdate(),
|
140 | menuRef = _useRefWithUpdate[0],
|
141 | setMenu = _useRefWithUpdate[1];
|
142 |
|
143 | var menuElement = menuRef.current;
|
144 |
|
145 | var _useRefWithUpdate2 = useRefWithUpdate(),
|
146 | toggleRef = _useRefWithUpdate2[0],
|
147 | setToggle = _useRefWithUpdate2[1];
|
148 |
|
149 | var toggleElement = toggleRef.current;
|
150 | var lastShow = (0, _usePrevious["default"])(show);
|
151 | var lastSourceEvent = (0, _react.useRef)(null);
|
152 | var focusInDropdown = (0, _react.useRef)(false);
|
153 | var toggle = (0, _react.useCallback)(function (nextShow, event) {
|
154 | onToggle(nextShow, event);
|
155 | }, [onToggle]);
|
156 | var context = (0, _react.useMemo)(function () {
|
157 | return {
|
158 | toggle: toggle,
|
159 | drop: drop,
|
160 | show: show,
|
161 | alignEnd: alignEnd,
|
162 | menuElement: menuElement,
|
163 | toggleElement: toggleElement,
|
164 | setMenu: setMenu,
|
165 | setToggle: setToggle
|
166 | };
|
167 | }, [toggle, drop, show, alignEnd, menuElement, toggleElement, setMenu, setToggle]);
|
168 |
|
169 | if (menuElement && lastShow && !show) {
|
170 | focusInDropdown.current = menuElement.contains(document.activeElement);
|
171 | }
|
172 |
|
173 | var focusToggle = (0, _useEventCallback["default"])(function () {
|
174 | if (toggleElement && toggleElement.focus) {
|
175 | toggleElement.focus();
|
176 | }
|
177 | });
|
178 | var maybeFocusFirst = (0, _useEventCallback["default"])(function () {
|
179 | var type = lastSourceEvent.current;
|
180 | var focusType = focusFirstItemOnShow;
|
181 |
|
182 | if (focusType == null) {
|
183 | focusType = menuRef.current && (0, _matches["default"])(menuRef.current, '[role=menu]') ? 'keyboard' : false;
|
184 | }
|
185 |
|
186 | if (focusType === false || focusType === 'keyboard' && !/^key.+$/.test(type)) {
|
187 | return;
|
188 | }
|
189 |
|
190 | var first = (0, _querySelectorAll["default"])(menuRef.current, itemSelector)[0];
|
191 | if (first && first.focus) first.focus();
|
192 | });
|
193 | (0, _react.useEffect)(function () {
|
194 | if (show) maybeFocusFirst();else if (focusInDropdown.current) {
|
195 | focusInDropdown.current = false;
|
196 | focusToggle();
|
197 | }
|
198 | }, [show, focusInDropdown, focusToggle, maybeFocusFirst]);
|
199 | (0, _react.useEffect)(function () {
|
200 | lastSourceEvent.current = null;
|
201 | });
|
202 |
|
203 | var getNextFocusedChild = function getNextFocusedChild(current, offset) {
|
204 | if (!menuRef.current) return null;
|
205 | var items = (0, _querySelectorAll["default"])(menuRef.current, itemSelector);
|
206 | var index = items.indexOf(current) + offset;
|
207 | index = Math.max(0, Math.min(index, items.length));
|
208 | return items[index];
|
209 | };
|
210 |
|
211 | (0, _useGlobalListener["default"])('keydown', function (event) {
|
212 | var _menuRef$current, _toggleRef$current;
|
213 |
|
214 | var key = event.key;
|
215 | var target = event.target;
|
216 | var fromMenu = (_menuRef$current = menuRef.current) == null ? void 0 : _menuRef$current.contains(target);
|
217 | var fromToggle = (_toggleRef$current = toggleRef.current) == null ? void 0 : _toggleRef$current.contains(target);
|
218 |
|
219 |
|
220 | var isInput = /input|textarea/i.test(target.tagName);
|
221 |
|
222 | if (isInput && (key === ' ' || key !== 'Escape' && fromMenu)) {
|
223 | return;
|
224 | }
|
225 |
|
226 | if (!fromMenu && !fromToggle) {
|
227 | return;
|
228 | }
|
229 |
|
230 | if (!menuRef.current && key === 'Tab') {
|
231 | return;
|
232 | }
|
233 |
|
234 | lastSourceEvent.current = event.type;
|
235 |
|
236 | switch (key) {
|
237 | case 'ArrowUp':
|
238 | {
|
239 | var next = getNextFocusedChild(target, -1);
|
240 | if (next && next.focus) next.focus();
|
241 | event.preventDefault();
|
242 | return;
|
243 | }
|
244 |
|
245 | case 'ArrowDown':
|
246 | event.preventDefault();
|
247 |
|
248 | if (!show) {
|
249 | onToggle(true, event);
|
250 | } else {
|
251 | var _next = getNextFocusedChild(target, 1);
|
252 |
|
253 | if (_next && _next.focus) _next.focus();
|
254 | }
|
255 |
|
256 | return;
|
257 |
|
258 | case 'Tab':
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | (0, _addEventListener["default"])(document, 'keyup', function (e) {
|
264 | var _menuRef$current2;
|
265 |
|
266 | if (e.key === 'Tab' && !e.target || !((_menuRef$current2 = menuRef.current) != null && _menuRef$current2.contains(e.target))) {
|
267 | onToggle(false, event);
|
268 | }
|
269 | }, {
|
270 | once: true
|
271 | });
|
272 | break;
|
273 |
|
274 | case 'Escape':
|
275 | event.preventDefault();
|
276 | event.stopPropagation();
|
277 | onToggle(false, event);
|
278 | break;
|
279 |
|
280 | default:
|
281 | }
|
282 | });
|
283 | return _react["default"].createElement(_DropdownContext["default"].Provider, {
|
284 | value: context
|
285 | }, children);
|
286 | }
|
287 |
|
288 | Dropdown.displayName = 'ReactOverlaysDropdown';
|
289 | Dropdown.propTypes = propTypes;
|
290 | Dropdown.Menu = _DropdownMenu["default"];
|
291 | Dropdown.Toggle = _DropdownToggle["default"];
|
292 | var _default = Dropdown;
|
293 | exports["default"] = _default;
|
294 | module.exports = exports.default; |
\ | No newline at end of file |