@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.
- package/dist/cjs/FocusScope.cjs +284 -167
- package/dist/cjs/FocusScope.native.js +28 -26
- package/dist/cjs/FocusScope.native.js.map +1 -1
- package/dist/cjs/FocusScopeController.cjs +52 -50
- package/dist/cjs/FocusScopeController.native.js +49 -47
- package/dist/cjs/FocusScopeController.native.js.map +1 -1
- package/dist/cjs/index.cjs +7 -5
- package/dist/cjs/index.native.js +7 -5
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/types.cjs +7 -5
- package/dist/cjs/types.native.js +7 -5
- package/dist/cjs/types.native.js.map +1 -1
- package/dist/esm/FocusScope.mjs +253 -138
- package/dist/esm/FocusScope.mjs.map +1 -1
- package/dist/esm/FocusScopeController.mjs +24 -24
- package/dist/esm/FocusScopeController.mjs.map +1 -1
- package/dist/esm/FocusScopeController.native.js +21 -21
- package/dist/esm/FocusScopeController.native.js.map +1 -1
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -6
- package/dist/jsx/FocusScope.mjs +253 -138
- package/dist/jsx/FocusScope.mjs.map +1 -1
- package/dist/jsx/FocusScope.native.js +28 -26
- package/dist/jsx/FocusScope.native.js.map +1 -1
- package/dist/jsx/FocusScopeController.mjs +24 -24
- package/dist/jsx/FocusScopeController.mjs.map +1 -1
- package/dist/jsx/FocusScopeController.native.js +49 -47
- package/dist/jsx/FocusScopeController.native.js.map +1 -1
- package/dist/jsx/index.js +2 -2
- package/dist/jsx/index.js.map +1 -6
- package/dist/jsx/index.native.js +7 -5
- package/dist/jsx/types.native.js +7 -5
- package/package.json +11 -14
- package/src/FocusScope.tsx +33 -6
- package/types/FocusScope.d.ts.map +1 -1
- package/dist/cjs/FocusScope.js +0 -245
- package/dist/cjs/FocusScope.js.map +0 -6
- package/dist/cjs/FocusScopeController.js +0 -69
- package/dist/cjs/FocusScopeController.js.map +0 -6
- package/dist/cjs/index.js +0 -16
- package/dist/cjs/index.js.map +0 -6
- package/dist/cjs/types.js +0 -14
- package/dist/cjs/types.js.map +0 -6
- package/dist/esm/FocusScope.js +0 -227
- package/dist/esm/FocusScope.js.map +0 -6
- package/dist/esm/FocusScopeController.js +0 -48
- package/dist/esm/FocusScopeController.js.map +0 -6
- package/dist/esm/types.js +0 -1
- package/dist/esm/types.js.map +0 -6
- package/dist/jsx/FocusScope.js +0 -227
- package/dist/jsx/FocusScope.js.map +0 -6
- package/dist/jsx/FocusScopeController.js +0 -48
- package/dist/jsx/FocusScopeController.js.map +0 -6
- package/dist/jsx/types.js +0 -1
- package/dist/jsx/types.js.map +0 -6
package/dist/cjs/FocusScope.cjs
CHANGED
|
@@ -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
|
-
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
7
|
var __export = (target, all) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
19
|
-
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
20
22
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount"
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
|
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)
|
|
88
|
-
|
|
89
|
-
|
|
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()
|
|
108
|
+
controller.abort();
|
|
109
|
+
if (focusScope.paused) return;
|
|
110
|
+
if (!container.contains(event.relatedTarget)) {
|
|
111
|
+
scheduleRefocus();
|
|
112
|
+
}
|
|
93
113
|
}
|
|
94
114
|
function handleBlur() {}
|
|
95
|
-
|
|
96
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (!
|
|
140
|
-
|
|
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
|
-
|
|
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
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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:
|
|
156
|
-
})
|
|
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()
|
|
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)
|
|
163
|
-
|
|
164
|
-
|
|
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
|
|
169
|
-
|
|
170
|
-
|
|
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
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
|
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"
|
|
282
|
+
if (event.key === "Tab") {
|
|
283
|
+
handleKeyDown(event);
|
|
284
|
+
}
|
|
184
285
|
};
|
|
185
|
-
|
|
186
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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 =
|
|
302
|
+
select = false
|
|
201
303
|
} = {}) {
|
|
202
304
|
const previouslyFocusedElement = document.activeElement;
|
|
203
|
-
for (const candidate of candidates)
|
|
204
|
-
|
|
205
|
-
|
|
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
|
-
|
|
210
|
-
|
|
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
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
}
|
|
221
|
-
|
|
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)
|
|
226
|
-
|
|
227
|
-
|
|
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
|
|
233
|
-
|
|
234
|
-
if (upTo !== void 0 && node === upTo) return
|
|
235
|
-
if (getComputedStyle(node).display === "none") return
|
|
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
|
|
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 =
|
|
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:
|
|
251
|
-
})
|
|
252
|
-
|
|
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
|
|
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)
|
|
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
|
-
|
|
271
|
-
|
|
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
|
-
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
9
|
var __export = (target, all) => {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
21
|
-
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
22
24
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
41
|
-
|
|
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":"
|
|
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":[]}
|