@tamagui/focus-scope 2.0.0-rc.4 → 2.0.0-rc.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/cjs/FocusScope.cjs +284 -167
  2. package/dist/cjs/FocusScope.native.js +28 -26
  3. package/dist/cjs/FocusScope.native.js.map +1 -1
  4. package/dist/cjs/FocusScopeController.cjs +52 -50
  5. package/dist/cjs/FocusScopeController.native.js +49 -47
  6. package/dist/cjs/FocusScopeController.native.js.map +1 -1
  7. package/dist/cjs/index.cjs +7 -5
  8. package/dist/cjs/index.native.js +7 -5
  9. package/dist/cjs/index.native.js.map +1 -1
  10. package/dist/cjs/types.cjs +7 -5
  11. package/dist/cjs/types.native.js +7 -5
  12. package/dist/cjs/types.native.js.map +1 -1
  13. package/dist/esm/FocusScope.mjs +253 -138
  14. package/dist/esm/FocusScope.mjs.map +1 -1
  15. package/dist/esm/FocusScopeController.mjs +24 -24
  16. package/dist/esm/FocusScopeController.mjs.map +1 -1
  17. package/dist/esm/FocusScopeController.native.js +21 -21
  18. package/dist/esm/FocusScopeController.native.js.map +1 -1
  19. package/dist/esm/index.js +2 -2
  20. package/dist/esm/index.js.map +1 -6
  21. package/dist/jsx/FocusScope.mjs +253 -138
  22. package/dist/jsx/FocusScope.mjs.map +1 -1
  23. package/dist/jsx/FocusScope.native.js +28 -26
  24. package/dist/jsx/FocusScope.native.js.map +1 -1
  25. package/dist/jsx/FocusScopeController.mjs +24 -24
  26. package/dist/jsx/FocusScopeController.mjs.map +1 -1
  27. package/dist/jsx/FocusScopeController.native.js +49 -47
  28. package/dist/jsx/FocusScopeController.native.js.map +1 -1
  29. package/dist/jsx/index.js +2 -2
  30. package/dist/jsx/index.js.map +1 -6
  31. package/dist/jsx/index.native.js +7 -5
  32. package/dist/jsx/types.native.js +7 -5
  33. package/package.json +11 -14
  34. package/src/FocusScope.tsx +33 -6
  35. package/types/FocusScope.d.ts.map +1 -1
  36. package/dist/cjs/FocusScope.js +0 -245
  37. package/dist/cjs/FocusScope.js.map +0 -6
  38. package/dist/cjs/FocusScopeController.js +0 -69
  39. package/dist/cjs/FocusScopeController.js.map +0 -6
  40. package/dist/cjs/index.js +0 -16
  41. package/dist/cjs/index.js.map +0 -6
  42. package/dist/cjs/types.js +0 -14
  43. package/dist/cjs/types.js.map +0 -6
  44. package/dist/esm/FocusScope.js +0 -227
  45. package/dist/esm/FocusScope.js.map +0 -6
  46. package/dist/esm/FocusScopeController.js +0 -48
  47. package/dist/esm/FocusScopeController.js.map +0 -6
  48. package/dist/esm/types.js +0 -1
  49. package/dist/esm/types.js.map +0 -6
  50. package/dist/jsx/FocusScope.js +0 -227
  51. package/dist/jsx/FocusScope.js.map +0 -6
  52. package/dist/jsx/FocusScopeController.js +0 -48
  53. package/dist/jsx/FocusScopeController.js.map +0 -6
  54. package/dist/jsx/types.js +0 -1
  55. package/dist/jsx/types.js.map +0 -6
@@ -2,75 +2,80 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: !0
11
- });
12
- },
13
- __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
16
  get: () => from[key],
16
17
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
18
  });
18
- return to;
19
- };
19
+ }
20
+ return to;
21
+ };
20
22
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: true
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
32
34
  var FocusScope_exports = {};
33
35
  __export(FocusScope_exports, {
34
36
  FocusScope: () => FocusScope,
35
37
  useFocusScope: () => useFocusScope
36
38
  });
37
39
  module.exports = __toCommonJS(FocusScope_exports);
38
- var import_compose_refs = require("@tamagui/compose-refs"),
39
- import_constants = require("@tamagui/constants"),
40
- import_use_async = require("@tamagui/use-async"),
41
- import_use_event = require("@tamagui/use-event"),
42
- React = __toESM(require("react"), 1),
43
- import_FocusScopeController = require("./FocusScopeController.cjs"),
44
- import_jsx_runtime = require("react/jsx-runtime");
45
- const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount",
46
- AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount",
47
- EVENT_OPTIONS = {
48
- bubbles: !1,
49
- cancelable: !0
50
- },
51
- FocusScope = React.forwardRef(function ({
52
- __scopeFocusScope,
53
- ...props
54
- }, forwardedRef) {
55
- const context = (0, import_FocusScopeController.useFocusScopeControllerContext)("FocusScope", __scopeFocusScope, {
56
- warn: !1,
57
- fallback: {}
58
- }),
59
- mergedProps = {
60
- ...props,
61
- enabled: context.enabled ?? props.enabled,
62
- loop: context.loop ?? props.loop,
63
- trapped: context.trapped ?? props.trapped,
64
- onMountAutoFocus: context.onMountAutoFocus ?? props.onMountAutoFocus,
65
- onUnmountAutoFocus: context.onUnmountAutoFocus ?? props.onUnmountAutoFocus,
66
- forceUnmount: context.forceUnmount ?? props.forceUnmount,
67
- focusOnIdle: context.focusOnIdle ?? props.focusOnIdle
68
- },
69
- childProps = useFocusScope(mergedProps, forwardedRef);
70
- return typeof mergedProps.children == "function" ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
71
- children: mergedProps.children(childProps)
72
- }) : React.cloneElement(React.Children.only(mergedProps.children), childProps);
40
+ var import_compose_refs = require("@tamagui/compose-refs");
41
+ var import_constants = require("@tamagui/constants");
42
+ var import_use_async = require("@tamagui/use-async");
43
+ var import_use_event = require("@tamagui/use-event");
44
+ var React = __toESM(require("react"), 1);
45
+ var import_FocusScopeController = require("./FocusScopeController.cjs");
46
+ var import_jsx_runtime = require("react/jsx-runtime");
47
+ const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
48
+ const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
49
+ const EVENT_OPTIONS = {
50
+ bubbles: false,
51
+ cancelable: true
52
+ };
53
+ const FocusScope = React.forwardRef(function FocusScope2({
54
+ __scopeFocusScope,
55
+ ...props
56
+ }, forwardedRef) {
57
+ const context = (0, import_FocusScopeController.useFocusScopeControllerContext)("FocusScope", __scopeFocusScope, {
58
+ warn: false,
59
+ fallback: {}
73
60
  });
61
+ const mergedProps = {
62
+ ...props,
63
+ enabled: context.enabled ?? props.enabled,
64
+ loop: context.loop ?? props.loop,
65
+ trapped: context.trapped ?? props.trapped,
66
+ onMountAutoFocus: context.onMountAutoFocus ?? props.onMountAutoFocus,
67
+ onUnmountAutoFocus: context.onUnmountAutoFocus ?? props.onUnmountAutoFocus,
68
+ forceUnmount: context.forceUnmount ?? props.forceUnmount,
69
+ focusOnIdle: context.focusOnIdle ?? props.focusOnIdle
70
+ };
71
+ const childProps = useFocusScope(mergedProps, forwardedRef);
72
+ if (typeof mergedProps.children === "function") {
73
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
74
+ children: mergedProps.children(childProps)
75
+ });
76
+ }
77
+ return React.cloneElement(React.Children.only(mergedProps.children), childProps);
78
+ });
74
79
  function setupFocusTrap(container, lastFocusedElementRef, focusScope) {
75
80
  const controller = new AbortController();
76
81
  let rafId = null;
@@ -78,118 +83,215 @@ function setupFocusTrap(container, lastFocusedElementRef, focusScope) {
78
83
  if (rafId) return;
79
84
  const elementToFocus = lastFocusedElementRef.current;
80
85
  rafId = requestAnimationFrame(() => {
81
- rafId = null, !focusScope.paused && (focusScope.stopped || container.isConnected && (container.contains(document.activeElement) || focus(elementToFocus)));
86
+ rafId = null;
87
+ if (focusScope.paused) return;
88
+ if (focusScope.stopped) return;
89
+ if (!container.isConnected) return;
90
+ if (!container.contains(document.activeElement)) {
91
+ focus(elementToFocus);
92
+ }
82
93
  });
83
94
  }
84
95
  function handleFocusIn(event) {
85
96
  if (focusScope.paused) return;
86
97
  const target = event.target;
87
- container.contains(target) ? (target?.addEventListener("blur", handleBlur, {
88
- signal: controller.signal
89
- }), lastFocusedElementRef.current = target) : scheduleRefocus();
98
+ if (container.contains(target)) {
99
+ target?.addEventListener("blur", handleBlur, {
100
+ signal: controller.signal
101
+ });
102
+ lastFocusedElementRef.current = target;
103
+ } else {
104
+ scheduleRefocus();
105
+ }
90
106
  }
91
107
  function handleFocusOut(event) {
92
- controller.abort(), !focusScope.paused && (container.contains(event.relatedTarget) || scheduleRefocus());
108
+ controller.abort();
109
+ if (focusScope.paused) return;
110
+ if (!container.contains(event.relatedTarget)) {
111
+ scheduleRefocus();
112
+ }
93
113
  }
94
114
  function handleBlur() {}
95
- return document.addEventListener("focusin", handleFocusIn), document.addEventListener("focusout", handleFocusOut), () => {
96
- rafId && (cancelAnimationFrame(rafId), rafId = null), controller.abort(), document.removeEventListener("focusin", handleFocusIn), document.removeEventListener("focusout", handleFocusOut);
115
+ document.addEventListener("focusin", handleFocusIn);
116
+ document.addEventListener("focusout", handleFocusOut);
117
+ return () => {
118
+ if (rafId) {
119
+ cancelAnimationFrame(rafId);
120
+ rafId = null;
121
+ }
122
+ controller.abort();
123
+ document.removeEventListener("focusin", handleFocusIn);
124
+ document.removeEventListener("focusout", handleFocusOut);
97
125
  };
98
126
  }
99
127
  function useFocusScope(props, forwardedRef) {
100
128
  const {
101
- loop = !1,
102
- enabled = !0,
103
- trapped = !1,
104
- onMountAutoFocus: onMountAutoFocusProp,
105
- onUnmountAutoFocus: onUnmountAutoFocusProp,
106
- forceUnmount,
107
- focusOnIdle = !0,
108
- children,
109
- ...scopeProps
110
- } = props,
111
- [container, setContainer] = React.useState(null),
112
- containerRef = React.useRef(null),
113
- onMountAutoFocus = (0, import_use_event.useEvent)(onMountAutoFocusProp),
114
- onUnmountAutoFocus = (0, import_use_event.useEvent)(onUnmountAutoFocusProp),
115
- lastFocusedElementRef = React.useRef(null),
116
- focusScopeRef = React.useRef({
117
- paused: !1,
118
- stopped: !1,
119
- // set to true when cleanup starts, signals trap to stop
120
- pause() {
121
- this.paused = !0;
122
- },
123
- resume() {
124
- this.paused = !1;
125
- },
126
- stop() {
127
- this.stopped = !0;
129
+ loop = false,
130
+ enabled = true,
131
+ trapped = false,
132
+ onMountAutoFocus: onMountAutoFocusProp,
133
+ onUnmountAutoFocus: onUnmountAutoFocusProp,
134
+ forceUnmount,
135
+ focusOnIdle = true,
136
+ children,
137
+ ...scopeProps
138
+ } = props;
139
+ const [container, setContainer] = React.useState(null);
140
+ const containerRef = React.useRef(null);
141
+ const onMountAutoFocus = (0, import_use_event.useEvent)(onMountAutoFocusProp);
142
+ const onUnmountAutoFocus = (0, import_use_event.useEvent)(onUnmountAutoFocusProp);
143
+ const lastFocusedElementRef = React.useRef(null);
144
+ const focusScopeRef = React.useRef({
145
+ paused: false,
146
+ stopped: false,
147
+ // set to true when cleanup starts, signals trap to stop
148
+ pause() {
149
+ this.paused = true;
150
+ },
151
+ resume() {
152
+ this.paused = false;
153
+ },
154
+ stop() {
155
+ this.stopped = true;
156
+ }
157
+ });
158
+ const focusScope = focusScopeRef.current;
159
+ const trapCleanupRef = React.useRef(null);
160
+ const setContainerRef = React.useCallback(node => {
161
+ if (trapCleanupRef.current) {
162
+ trapCleanupRef.current();
163
+ trapCleanupRef.current = null;
164
+ }
165
+ containerRef.current = node;
166
+ if (import_constants.isWeb && node && enabled && trapped) {
167
+ trapCleanupRef.current = setupFocusTrap(node, lastFocusedElementRef, focusScopeRef.current);
168
+ }
169
+ setContainer(node);
170
+ }, [enabled, trapped]);
171
+ const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, setContainerRef);
172
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
173
+ if (!trapped) {
174
+ focusScope.stopped = true;
175
+ if (trapCleanupRef.current) {
176
+ trapCleanupRef.current();
177
+ trapCleanupRef.current = null;
128
178
  }
129
- }),
130
- focusScope = focusScopeRef.current,
131
- trapCleanupRef = React.useRef(null),
132
- setContainerRef = React.useCallback(node => {
133
- trapCleanupRef.current && (trapCleanupRef.current(), trapCleanupRef.current = null), containerRef.current = node, import_constants.isWeb && node && enabled && trapped && (trapCleanupRef.current = setupFocusTrap(node, lastFocusedElementRef, focusScopeRef.current)), setContainer(node);
134
- }, [enabled, trapped]),
135
- composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, setContainerRef);
136
- (0, import_constants.useIsomorphicLayoutEffect)(() => () => {
137
- trapCleanupRef.current && (trapCleanupRef.current(), trapCleanupRef.current = null);
138
- }, []), (0, import_use_async.useAsyncEffect)(async signal => {
139
- if (!enabled || !container || forceUnmount) return;
140
- focusScope.stopped = !1, focusScopesStack.add(focusScope);
179
+ }
180
+ return () => {
181
+ if (trapCleanupRef.current) {
182
+ trapCleanupRef.current();
183
+ trapCleanupRef.current = null;
184
+ }
185
+ };
186
+ }, [trapped, focusScope]);
187
+ (0, import_use_async.useAsyncEffect)(async signal => {
188
+ if (!enabled) return;
189
+ if (!container) return;
190
+ if (forceUnmount) return;
191
+ if (trapped) {
192
+ focusScope.stopped = false;
193
+ }
194
+ focusScopesStack.add(focusScope);
141
195
  const previouslyFocusedElement = document.activeElement;
142
- if (!container.contains(previouslyFocusedElement)) {
196
+ const hasFocusedCandidate = container.contains(previouslyFocusedElement) && previouslyFocusedElement !== null && !isHidden(previouslyFocusedElement, {
197
+ upTo: container
198
+ });
199
+ if (!hasFocusedCandidate) {
143
200
  const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
144
- if (container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus), container.dispatchEvent(mountEvent), !mountEvent.defaultPrevented) {
145
- focusOnIdle && (await (0, import_use_async.idle)(signal, typeof focusOnIdle == "object" ? focusOnIdle : {
146
- // we can't wait too long or else user can take an action and then we focus
147
- max: 200,
148
- min: typeof focusOnIdle == "number" ? focusOnIdle : 16
201
+ container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
202
+ container.dispatchEvent(mountEvent);
203
+ if (!mountEvent.defaultPrevented) {
204
+ if (focusOnIdle) {
205
+ await (0, import_use_async.idle)(signal, typeof focusOnIdle == "object" ? focusOnIdle : {
206
+ // we can't wait too long or else user can take an action and then we focus
207
+ max: 200,
208
+ min: typeof focusOnIdle == "number" ? focusOnIdle : 16
209
+ });
210
+ }
211
+ const allCandidates = getTabbableCandidates(container);
212
+ const linkedRemoved = removeLinks(allCandidates);
213
+ const visibleCandidates = linkedRemoved.filter(candidate => !isHidden(candidate, {
214
+ upTo: container
149
215
  }));
150
- const allCandidates = getTabbableCandidates(container),
151
- visibleCandidates = removeLinks(allCandidates).filter(candidate => !isHidden(candidate, {
152
- upTo: container
153
- }));
154
216
  focusFirst(visibleCandidates, {
155
- select: !0
156
- }), visibleCandidates.length > 0 ? lastFocusedElementRef.current = visibleCandidates[0] : lastFocusedElementRef.current = container, document.activeElement === previouslyFocusedElement && visibleCandidates.length === 0 && focus(container);
217
+ select: true
218
+ });
219
+ if (visibleCandidates.length > 0) {
220
+ lastFocusedElementRef.current = visibleCandidates[0];
221
+ } else {
222
+ lastFocusedElementRef.current = container;
223
+ }
224
+ if (document.activeElement === previouslyFocusedElement && visibleCandidates.length === 0) {
225
+ focus(container);
226
+ }
157
227
  }
158
228
  }
159
229
  return () => {
160
- focusScope.stop(), container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
230
+ focusScope.stop();
231
+ container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
161
232
  const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
162
- container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus), container.dispatchEvent(unmountEvent), unmountEvent.defaultPrevented || focus(previouslyFocusedElement ?? document.body, {
163
- select: !0
164
- }), container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus), focusScopesStack.remove(focusScope);
233
+ container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
234
+ container.dispatchEvent(unmountEvent);
235
+ if (!unmountEvent.defaultPrevented) {
236
+ const currentFocus = document.activeElement;
237
+ const focusHasMovedOutside = currentFocus && currentFocus !== document.body && currentFocus !== container && !container.contains(currentFocus);
238
+ if (!focusHasMovedOutside) {
239
+ focus(previouslyFocusedElement ?? document.body, {
240
+ select: true
241
+ });
242
+ }
243
+ }
244
+ container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
245
+ focusScopesStack.remove(focusScope);
165
246
  };
166
247
  }, [enabled, container, forceUnmount, onMountAutoFocus, onUnmountAutoFocus, focusScope, focusOnIdle]);
167
248
  const handleKeyDown = React.useCallback(event => {
168
- if (!trapped || !loop || focusScope.paused || !enabled || !container) return;
169
- const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey,
170
- focusedElement = document.activeElement;
249
+ if (!trapped) return;
250
+ if (!loop) return;
251
+ if (focusScope.paused) return;
252
+ if (!enabled) return;
253
+ if (!container) return;
254
+ const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
255
+ const focusedElement = document.activeElement;
171
256
  if (isTabKey && focusedElement) {
172
257
  const [first, last] = getTabbableEdges(container);
173
- first && last ? !event.shiftKey && focusedElement === last ? (event.preventDefault(), loop && focus(first, {
174
- select: !0
175
- })) : event.shiftKey && focusedElement === first && (event.preventDefault(), loop && focus(last, {
176
- select: !0
177
- })) : focusedElement === container && event.preventDefault();
258
+ const hasTabbableElementsInside = first && last;
259
+ if (!hasTabbableElementsInside) {
260
+ if (focusedElement === container) event.preventDefault();
261
+ } else {
262
+ if (!event.shiftKey && focusedElement === last) {
263
+ event.preventDefault();
264
+ if (loop) focus(first, {
265
+ select: true
266
+ });
267
+ } else if (event.shiftKey && focusedElement === first) {
268
+ event.preventDefault();
269
+ if (loop) focus(last, {
270
+ select: true
271
+ });
272
+ }
273
+ }
178
274
  }
179
275
  }, [loop, trapped, focusScope.paused, enabled, container]);
180
276
  React.useEffect(() => {
181
- if (!container || !trapped || !loop || !enabled) return;
277
+ if (!container) return;
278
+ if (!trapped) return;
279
+ if (!loop) return;
280
+ if (!enabled) return;
182
281
  const handleKeyDownCapture = event => {
183
- event.key === "Tab" && handleKeyDown(event);
282
+ if (event.key === "Tab") {
283
+ handleKeyDown(event);
284
+ }
184
285
  };
185
- return container.addEventListener("keydown", handleKeyDownCapture, !0), () => {
186
- container.removeEventListener("keydown", handleKeyDownCapture, !0);
286
+ container.addEventListener("keydown", handleKeyDownCapture, true);
287
+ return () => {
288
+ container.removeEventListener("keydown", handleKeyDownCapture, true);
187
289
  };
188
290
  }, [container, trapped, loop, enabled, handleKeyDown]);
189
- const existingOnKeyDown = scopeProps.onKeyDown,
190
- composedOnKeyDown = React.useCallback(event => {
191
- existingOnKeyDown?.(event);
192
- }, [existingOnKeyDown]);
291
+ const existingOnKeyDown = scopeProps.onKeyDown;
292
+ const composedOnKeyDown = React.useCallback(event => {
293
+ existingOnKeyDown?.(event);
294
+ }, [existingOnKeyDown]);
193
295
  return {
194
296
  ...scopeProps,
195
297
  ref: composedRefs,
@@ -197,59 +299,66 @@ function useFocusScope(props, forwardedRef) {
197
299
  };
198
300
  }
199
301
  function focusFirst(candidates, {
200
- select = !1
302
+ select = false
201
303
  } = {}) {
202
304
  const previouslyFocusedElement = document.activeElement;
203
- for (const candidate of candidates) if (focus(candidate, {
204
- select
205
- }), document.activeElement !== previouslyFocusedElement) return;
305
+ for (const candidate of candidates) {
306
+ focus(candidate, {
307
+ select
308
+ });
309
+ if (document.activeElement !== previouslyFocusedElement) return;
310
+ }
206
311
  }
207
312
  function getTabbableEdges(container) {
208
- const candidates = getTabbableCandidates(container),
209
- first = findVisible(candidates, container),
210
- last = findVisible(candidates.reverse(), container);
313
+ const candidates = getTabbableCandidates(container);
314
+ const first = findVisible(candidates, container);
315
+ const last = findVisible(candidates.reverse(), container);
211
316
  return [first, last];
212
317
  }
213
318
  function getTabbableCandidates(container) {
214
- const nodes = [],
215
- walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
216
- acceptNode: node => {
217
- const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
218
- return node.disabled || node.hidden || isHiddenInput ? NodeFilter.FILTER_SKIP : node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
219
- }
220
- });
221
- for (; walker.nextNode();) nodes.push(walker.currentNode);
319
+ const nodes = [];
320
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
321
+ acceptNode: node => {
322
+ const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
323
+ if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;
324
+ return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
325
+ }
326
+ });
327
+ while (walker.nextNode()) nodes.push(walker.currentNode);
222
328
  return nodes;
223
329
  }
224
330
  function findVisible(elements, container) {
225
- for (const element of elements) if (!isHidden(element, {
226
- upTo: container
227
- })) return element;
331
+ for (const element of elements) {
332
+ if (!isHidden(element, {
333
+ upTo: container
334
+ })) return element;
335
+ }
228
336
  }
229
337
  function isHidden(node, {
230
338
  upTo
231
339
  }) {
232
- if (getComputedStyle(node).visibility === "hidden") return !0;
233
- for (; node;) {
234
- if (upTo !== void 0 && node === upTo) return !1;
235
- if (getComputedStyle(node).display === "none") return !0;
340
+ if (getComputedStyle(node).visibility === "hidden") return true;
341
+ while (node) {
342
+ if (upTo !== void 0 && node === upTo) return false;
343
+ if (getComputedStyle(node).display === "none") return true;
236
344
  node = node.parentElement;
237
345
  }
238
- return !1;
346
+ return false;
239
347
  }
240
348
  function isSelectableInput(element) {
241
349
  return element instanceof HTMLInputElement && "select" in element;
242
350
  }
243
351
  function focus(element, {
244
- select = !1
352
+ select = false
245
353
  } = {}) {
246
354
  if (element?.focus) {
247
355
  const previouslyFocusedElement = document.activeElement;
248
356
  try {
249
357
  element.focus({
250
- preventScroll: !0
251
- }), element !== previouslyFocusedElement && isSelectableInput(element) && select && element.select();
252
- } catch {}
358
+ preventScroll: true
359
+ });
360
+ if (element !== previouslyFocusedElement && isSelectableInput(element) && select) element.select();
361
+ } catch (error) {}
253
362
  }
254
363
  }
255
364
  const focusScopesStack = createFocusScopesStack();
@@ -258,17 +367,25 @@ function createFocusScopesStack() {
258
367
  return {
259
368
  add(focusScope) {
260
369
  const activeFocusScope = stack[0];
261
- focusScope !== activeFocusScope && activeFocusScope?.pause(), stack = arrayRemove(stack, focusScope), stack.unshift(focusScope);
370
+ if (focusScope !== activeFocusScope) {
371
+ activeFocusScope?.pause();
372
+ }
373
+ stack = arrayRemove(stack, focusScope);
374
+ stack.unshift(focusScope);
262
375
  },
263
376
  remove(focusScope) {
264
- stack = arrayRemove(stack, focusScope), stack[0]?.resume();
377
+ stack = arrayRemove(stack, focusScope);
378
+ stack[0]?.resume();
265
379
  }
266
380
  };
267
381
  }
268
382
  function arrayRemove(array, item) {
269
- const updatedArray = [...array],
270
- index = updatedArray.indexOf(item);
271
- return index !== -1 && updatedArray.splice(index, 1), updatedArray;
383
+ const updatedArray = [...array];
384
+ const index = updatedArray.indexOf(item);
385
+ if (index !== -1) {
386
+ updatedArray.splice(index, 1);
387
+ }
388
+ return updatedArray;
272
389
  }
273
390
  function removeLinks(items) {
274
391
  return items.filter(item => item.tagName !== "A");
@@ -4,40 +4,42 @@ var __create = Object.create;
4
4
  var __defProp = Object.defineProperty;
5
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf,
8
- __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
9
  var __export = (target, all) => {
10
- for (var name in all) __defProp(target, name, {
11
- get: all[name],
12
- enumerable: !0
13
- });
14
- },
15
- __copyProps = (to, from, except, desc) => {
16
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
10
+ for (var name in all) __defProp(target, name, {
11
+ get: all[name],
12
+ enumerable: true
13
+ });
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
18
  get: () => from[key],
18
19
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
20
  });
20
- return to;
21
- };
21
+ }
22
+ return to;
23
+ };
22
24
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
- // If the importer is in node compatibility mode or this is not an ESM
24
- // file that has been converted to a CommonJS file using a Babel-
25
- // compatible transform (i.e. "__esModule" has not been set), then set
26
- // "default" to the CommonJS "module.exports" for node compatibility.
27
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
- value: mod,
29
- enumerable: !0
30
- }) : target, mod)),
31
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
- value: !0
33
- }), mod);
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
30
+ value: mod,
31
+ enumerable: true
32
+ }) : target, mod));
33
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
34
+ value: true
35
+ }), mod);
34
36
  var FocusScope_native_exports = {};
35
37
  __export(FocusScope_native_exports, {
36
38
  FocusScope: () => FocusScope
37
39
  });
38
40
  module.exports = __toCommonJS(FocusScope_native_exports);
39
- var import_react = __toESM(require("react"), 1),
40
- FocusScope = /* @__PURE__ */import_react.default.forwardRef(function (props, _ref) {
41
- return props.children;
42
- });
41
+ var import_react = __toESM(require("react"), 1);
42
+ var FocusScope = /* @__PURE__ */import_react.default.forwardRef(function (props, _ref) {
43
+ return props.children;
44
+ });
43
45
  //# sourceMappingURL=FocusScope.native.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["FocusScope_native_exports","__export","FocusScope","module","exports","__toCommonJS","import_react","__toESM","require","default","forwardRef","props","_ref","children"],"sources":["../../src/FocusScope.native.tsx"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,yBAAA;AAAAC,QAAA,CAAAD,yBAAA;EAAAE,UAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAC,YAAA,CAAAL,yBAAA;AAAA,IAAAM,YAAA,GAAkBC,OAAA,CAAAC,OAAA;EACPN,UAAA,GAA2B,eAAAI,YAAA,CAAAG,OAAA,CAAMC,UAAA,CAAW,UAASC,KAAA,EAAOC,IAAA,EAAM;IACzE,OAAOD,KAAA,CAAME,QAAA;EACjB,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["FocusScope_native_exports","__export","FocusScope","module","exports","__toCommonJS","import_react","__toESM","require","default","forwardRef","props","_ref","children"],"sources":["../../src/FocusScope.native.tsx"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,yBAAA;AAAAC,QAAA,CAAAD,yBAAA;EAAAE,UAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAA,GAAAC,YAAA,CAAAL,yBAAA;AAAA,IAAAM,YAAA,GAAkBC,OAAA,CAAAC,OAAA;AACX,IAAIN,UAAA,GAA2B,eAAAI,YAAA,CAAAG,OAAA,CAAMC,UAAA,CAAW,UAASC,KAAA,EAAOC,IAAA,EAAM;EACzE,OAAOD,KAAA,CAAME,QAAA;AACjB,CAAC","ignoreList":[]}