@tamagui/focus-scope 1.0.1-beta.100
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.js +272 -0
- package/dist/cjs/FocusScope.js.map +7 -0
- package/dist/cjs/FocusScope.native.js +31 -0
- package/dist/cjs/FocusScope.native.js.map +7 -0
- package/dist/cjs/FocusScopeProps.js +16 -0
- package/dist/cjs/FocusScopeProps.js.map +7 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/esm/FocusScope.js +248 -0
- package/dist/esm/FocusScope.js.map +7 -0
- package/dist/esm/FocusScope.native.js +8 -0
- package/dist/esm/FocusScope.native.js.map +7 -0
- package/dist/esm/FocusScopeProps.js +1 -0
- package/dist/esm/FocusScopeProps.js.map +7 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/jsx/FocusScope.js +213 -0
- package/dist/jsx/FocusScope.js.map +7 -0
- package/dist/jsx/FocusScope.native.js +8 -0
- package/dist/jsx/FocusScope.native.js.map +7 -0
- package/dist/jsx/FocusScopeProps.js +1 -0
- package/dist/jsx/FocusScopeProps.js.map +7 -0
- package/dist/jsx/index.js +2 -0
- package/dist/jsx/index.js.map +7 -0
- package/package.json +37 -0
- package/src/FocusScope.native.tsx +7 -0
- package/src/FocusScope.tsx +294 -0
- package/src/FocusScopeProps.tsx +31 -0
- package/src/index.tsx +1 -0
- package/types/FocusScope.d.ts +6 -0
- package/types/FocusScope.d.ts.map +1 -0
- package/types/FocusScope.native.d.ts +4 -0
- package/types/FocusScopeProps.d.ts +9 -0
- package/types/index.d.ts +2 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
9
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
10
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
11
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
+
var __spreadValues = (a, b) => {
|
|
13
|
+
for (var prop in b || (b = {}))
|
|
14
|
+
if (__hasOwnProp.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
if (__getOwnPropSymbols)
|
|
17
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
18
|
+
if (__propIsEnum.call(b, prop))
|
|
19
|
+
__defNormalProp(a, prop, b[prop]);
|
|
20
|
+
}
|
|
21
|
+
return a;
|
|
22
|
+
};
|
|
23
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
24
|
+
var __objRest = (source, exclude) => {
|
|
25
|
+
var target = {};
|
|
26
|
+
for (var prop in source)
|
|
27
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
if (source != null && __getOwnPropSymbols)
|
|
30
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
31
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
32
|
+
target[prop] = source[prop];
|
|
33
|
+
}
|
|
34
|
+
return target;
|
|
35
|
+
};
|
|
36
|
+
var __export = (target, all) => {
|
|
37
|
+
for (var name in all)
|
|
38
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
39
|
+
};
|
|
40
|
+
var __copyProps = (to, from, except, desc) => {
|
|
41
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
42
|
+
for (let key of __getOwnPropNames(from))
|
|
43
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
44
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
45
|
+
}
|
|
46
|
+
return to;
|
|
47
|
+
};
|
|
48
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
49
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
50
|
+
var FocusScope_exports = {};
|
|
51
|
+
__export(FocusScope_exports, {
|
|
52
|
+
FocusScope: () => FocusScope
|
|
53
|
+
});
|
|
54
|
+
module.exports = __toCommonJS(FocusScope_exports);
|
|
55
|
+
var import_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
|
|
56
|
+
var import_compose_refs = require("@tamagui/compose-refs");
|
|
57
|
+
var React = __toESM(require("react"));
|
|
58
|
+
const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
|
|
59
|
+
const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
|
|
60
|
+
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
61
|
+
const FOCUS_SCOPE_NAME = "FocusScope";
|
|
62
|
+
const FocusScope = React.forwardRef((props, forwardedRef) => {
|
|
63
|
+
const _a = props, {
|
|
64
|
+
loop = false,
|
|
65
|
+
trapped = false,
|
|
66
|
+
onMountAutoFocus: onMountAutoFocusProp,
|
|
67
|
+
onUnmountAutoFocus: onUnmountAutoFocusProp
|
|
68
|
+
} = _a, scopeProps = __objRest(_a, [
|
|
69
|
+
"loop",
|
|
70
|
+
"trapped",
|
|
71
|
+
"onMountAutoFocus",
|
|
72
|
+
"onUnmountAutoFocus"
|
|
73
|
+
]);
|
|
74
|
+
const [container, setContainer] = React.useState(null);
|
|
75
|
+
const onMountAutoFocus = (0, import_react_use_callback_ref.useCallbackRef)(onMountAutoFocusProp);
|
|
76
|
+
const onUnmountAutoFocus = (0, import_react_use_callback_ref.useCallbackRef)(onUnmountAutoFocusProp);
|
|
77
|
+
const lastFocusedElementRef = React.useRef(null);
|
|
78
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, (node) => setContainer(node));
|
|
79
|
+
const focusScope = React.useRef({
|
|
80
|
+
paused: false,
|
|
81
|
+
pause() {
|
|
82
|
+
this.paused = true;
|
|
83
|
+
},
|
|
84
|
+
resume() {
|
|
85
|
+
this.paused = false;
|
|
86
|
+
}
|
|
87
|
+
}).current;
|
|
88
|
+
React.useEffect(() => {
|
|
89
|
+
if (trapped) {
|
|
90
|
+
let handleFocusIn = function(event) {
|
|
91
|
+
if (focusScope.paused || !container)
|
|
92
|
+
return;
|
|
93
|
+
const target = event.target;
|
|
94
|
+
if (container.contains(target)) {
|
|
95
|
+
lastFocusedElementRef.current = target;
|
|
96
|
+
} else {
|
|
97
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
98
|
+
}
|
|
99
|
+
}, handleFocusOut = function(event) {
|
|
100
|
+
if (focusScope.paused || !container)
|
|
101
|
+
return;
|
|
102
|
+
if (!container.contains(event.relatedTarget)) {
|
|
103
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
document.addEventListener("focusin", handleFocusIn);
|
|
107
|
+
document.addEventListener("focusout", handleFocusOut);
|
|
108
|
+
return () => {
|
|
109
|
+
document.removeEventListener("focusin", handleFocusIn);
|
|
110
|
+
document.removeEventListener("focusout", handleFocusOut);
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}, [trapped, container, focusScope.paused]);
|
|
114
|
+
React.useEffect(() => {
|
|
115
|
+
if (container) {
|
|
116
|
+
focusScopesStack.add(focusScope);
|
|
117
|
+
const previouslyFocusedElement = document.activeElement;
|
|
118
|
+
const hasFocusedCandidate = container.contains(previouslyFocusedElement);
|
|
119
|
+
if (!hasFocusedCandidate) {
|
|
120
|
+
const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
|
|
121
|
+
container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
122
|
+
container.dispatchEvent(mountEvent);
|
|
123
|
+
if (!mountEvent.defaultPrevented) {
|
|
124
|
+
focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
|
|
125
|
+
if (document.activeElement === previouslyFocusedElement) {
|
|
126
|
+
focus(container);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return () => {
|
|
131
|
+
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
132
|
+
setTimeout(() => {
|
|
133
|
+
const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
|
|
134
|
+
container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
135
|
+
container.dispatchEvent(unmountEvent);
|
|
136
|
+
if (!unmountEvent.defaultPrevented) {
|
|
137
|
+
focus(previouslyFocusedElement ?? document.body, { select: true });
|
|
138
|
+
}
|
|
139
|
+
container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
140
|
+
focusScopesStack.remove(focusScope);
|
|
141
|
+
}, 0);
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
}, [container, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
|
|
145
|
+
const handleKeyDown = React.useCallback((event) => {
|
|
146
|
+
if (!loop && !trapped)
|
|
147
|
+
return;
|
|
148
|
+
if (focusScope.paused)
|
|
149
|
+
return;
|
|
150
|
+
const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
|
|
151
|
+
const focusedElement = document.activeElement;
|
|
152
|
+
if (isTabKey && focusedElement) {
|
|
153
|
+
const container2 = event.currentTarget;
|
|
154
|
+
const [first, last] = getTabbableEdges(container2);
|
|
155
|
+
const hasTabbableElementsInside = first && last;
|
|
156
|
+
if (!hasTabbableElementsInside) {
|
|
157
|
+
if (focusedElement === container2)
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
} else {
|
|
160
|
+
if (!event.shiftKey && focusedElement === last) {
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
if (loop)
|
|
163
|
+
focus(first, { select: true });
|
|
164
|
+
} else if (event.shiftKey && focusedElement === first) {
|
|
165
|
+
event.preventDefault();
|
|
166
|
+
if (loop)
|
|
167
|
+
focus(last, { select: true });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}, [loop, trapped, focusScope.paused]);
|
|
172
|
+
const child = React.Children.only(props.children);
|
|
173
|
+
return React.cloneElement(child, __spreadProps(__spreadValues({
|
|
174
|
+
tabIndex: -1
|
|
175
|
+
}, scopeProps), {
|
|
176
|
+
ref: composedRefs,
|
|
177
|
+
onKeyDown: handleKeyDown
|
|
178
|
+
}));
|
|
179
|
+
});
|
|
180
|
+
FocusScope.displayName = FOCUS_SCOPE_NAME;
|
|
181
|
+
function focusFirst(candidates, { select = false } = {}) {
|
|
182
|
+
const previouslyFocusedElement = document.activeElement;
|
|
183
|
+
for (const candidate of candidates) {
|
|
184
|
+
focus(candidate, { select });
|
|
185
|
+
if (document.activeElement !== previouslyFocusedElement)
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
function getTabbableEdges(container) {
|
|
190
|
+
const candidates = getTabbableCandidates(container);
|
|
191
|
+
const first = findVisible(candidates, container);
|
|
192
|
+
const last = findVisible(candidates.reverse(), container);
|
|
193
|
+
return [first, last];
|
|
194
|
+
}
|
|
195
|
+
function getTabbableCandidates(container) {
|
|
196
|
+
const nodes = [];
|
|
197
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
|
|
198
|
+
acceptNode: (node) => {
|
|
199
|
+
const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
|
|
200
|
+
if (node.disabled || node.hidden || isHiddenInput)
|
|
201
|
+
return NodeFilter.FILTER_SKIP;
|
|
202
|
+
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
while (walker.nextNode())
|
|
206
|
+
nodes.push(walker.currentNode);
|
|
207
|
+
return nodes;
|
|
208
|
+
}
|
|
209
|
+
function findVisible(elements, container) {
|
|
210
|
+
for (const element of elements) {
|
|
211
|
+
if (!isHidden(element, { upTo: container }))
|
|
212
|
+
return element;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
function isHidden(node, { upTo }) {
|
|
216
|
+
if (getComputedStyle(node).visibility === "hidden")
|
|
217
|
+
return true;
|
|
218
|
+
while (node) {
|
|
219
|
+
if (upTo !== void 0 && node === upTo)
|
|
220
|
+
return false;
|
|
221
|
+
if (getComputedStyle(node).display === "none")
|
|
222
|
+
return true;
|
|
223
|
+
node = node.parentElement;
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
function isSelectableInput(element) {
|
|
228
|
+
return element instanceof HTMLInputElement && "select" in element;
|
|
229
|
+
}
|
|
230
|
+
function focus(element, { select = false } = {}) {
|
|
231
|
+
if (element && element.focus) {
|
|
232
|
+
const previouslyFocusedElement = document.activeElement;
|
|
233
|
+
element.focus({ preventScroll: true });
|
|
234
|
+
if (element !== previouslyFocusedElement && isSelectableInput(element) && select)
|
|
235
|
+
element.select();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
const focusScopesStack = createFocusScopesStack();
|
|
239
|
+
function createFocusScopesStack() {
|
|
240
|
+
let stack = [];
|
|
241
|
+
return {
|
|
242
|
+
add(focusScope) {
|
|
243
|
+
const activeFocusScope = stack[0];
|
|
244
|
+
if (focusScope !== activeFocusScope) {
|
|
245
|
+
activeFocusScope == null ? void 0 : activeFocusScope.pause();
|
|
246
|
+
}
|
|
247
|
+
stack = arrayRemove(stack, focusScope);
|
|
248
|
+
stack.unshift(focusScope);
|
|
249
|
+
},
|
|
250
|
+
remove(focusScope) {
|
|
251
|
+
var _a;
|
|
252
|
+
stack = arrayRemove(stack, focusScope);
|
|
253
|
+
(_a = stack[0]) == null ? void 0 : _a.resume();
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function arrayRemove(array, item) {
|
|
258
|
+
const updatedArray = [...array];
|
|
259
|
+
const index = updatedArray.indexOf(item);
|
|
260
|
+
if (index !== -1) {
|
|
261
|
+
updatedArray.splice(index, 1);
|
|
262
|
+
}
|
|
263
|
+
return updatedArray;
|
|
264
|
+
}
|
|
265
|
+
function removeLinks(items) {
|
|
266
|
+
return items.filter((item) => item.tagName !== "A");
|
|
267
|
+
}
|
|
268
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
269
|
+
0 && (module.exports = {
|
|
270
|
+
FocusScope
|
|
271
|
+
});
|
|
272
|
+
//# sourceMappingURL=FocusScope.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/FocusScope.tsx"],
|
|
4
|
+
"sourcesContent": ["import { useCallbackRef } from '@radix-ui/react-use-callback-ref'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport * as React from 'react'\n\nimport { FocusScopeProps } from './FocusScopeProps'\n\nconst AUTOFOCUS_ON_MOUNT = 'focusScope.autoFocusOnMount'\nconst AUTOFOCUS_ON_UNMOUNT = 'focusScope.autoFocusOnUnmount'\nconst EVENT_OPTIONS = { bubbles: false, cancelable: true }\n\ntype FocusableTarget = HTMLElement | { focus(): void }\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope\n * -----------------------------------------------------------------------------------------------*/\n\nconst FOCUS_SCOPE_NAME = 'FocusScope'\n\ntype FocusScopeElement = HTMLDivElement\n\nconst FocusScope = React.forwardRef<FocusScopeElement, FocusScopeProps>((props, forwardedRef) => {\n const {\n loop = false,\n trapped = false,\n onMountAutoFocus: onMountAutoFocusProp,\n onUnmountAutoFocus: onUnmountAutoFocusProp,\n ...scopeProps\n } = props\n const [container, setContainer] = React.useState<HTMLElement | null>(null)\n const onMountAutoFocus = useCallbackRef(onMountAutoFocusProp)\n const onUnmountAutoFocus = useCallbackRef(onUnmountAutoFocusProp)\n const lastFocusedElementRef = React.useRef<HTMLElement | null>(null)\n const composedRefs = useComposedRefs(forwardedRef, (node) => setContainer(node))\n\n const focusScope = React.useRef({\n paused: false,\n pause() {\n this.paused = true\n },\n resume() {\n this.paused = false\n },\n }).current\n\n // Takes care of trapping focus if focus is moved outside programmatically for example\n React.useEffect(() => {\n if (trapped) {\n function handleFocusIn(event: FocusEvent) {\n if (focusScope.paused || !container) return\n const target = event.target as HTMLElement | null\n if (container.contains(target)) {\n lastFocusedElementRef.current = target\n } else {\n focus(lastFocusedElementRef.current, { select: true })\n }\n }\n\n function handleFocusOut(event: FocusEvent) {\n if (focusScope.paused || !container) return\n if (!container.contains(event.relatedTarget as HTMLElement | null)) {\n focus(lastFocusedElementRef.current, { select: true })\n }\n }\n\n document.addEventListener('focusin', handleFocusIn)\n document.addEventListener('focusout', handleFocusOut)\n return () => {\n document.removeEventListener('focusin', handleFocusIn)\n document.removeEventListener('focusout', handleFocusOut)\n }\n }\n }, [trapped, container, focusScope.paused])\n\n React.useEffect(() => {\n if (container) {\n focusScopesStack.add(focusScope)\n const previouslyFocusedElement = document.activeElement as HTMLElement | null\n const hasFocusedCandidate = container.contains(previouslyFocusedElement)\n\n if (!hasFocusedCandidate) {\n const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS)\n container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus)\n container.dispatchEvent(mountEvent)\n if (!mountEvent.defaultPrevented) {\n focusFirst(removeLinks(getTabbableCandidates(container)), { select: true })\n if (document.activeElement === previouslyFocusedElement) {\n focus(container)\n }\n }\n }\n\n return () => {\n container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus)\n\n // We hit a react bug (fixed in v17) with focusing in unmount.\n // We need to delay the focus a little to get around it for now.\n // See: https://github.com/facebook/react/issues/17894\n setTimeout(() => {\n const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS)\n container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus)\n container.dispatchEvent(unmountEvent)\n if (!unmountEvent.defaultPrevented) {\n focus(previouslyFocusedElement ?? document.body, { select: true })\n }\n // we need to remove the listener after we `dispatchEvent`\n container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus)\n\n focusScopesStack.remove(focusScope)\n }, 0)\n }\n }\n }, [container, onMountAutoFocus, onUnmountAutoFocus, focusScope])\n\n // Takes care of looping focus (when tabbing whilst at the edges)\n const handleKeyDown = React.useCallback(\n (event: React.KeyboardEvent) => {\n if (!loop && !trapped) return\n if (focusScope.paused) return\n\n const isTabKey = event.key === 'Tab' && !event.altKey && !event.ctrlKey && !event.metaKey\n const focusedElement = document.activeElement as HTMLElement | null\n\n if (isTabKey && focusedElement) {\n const container = event.currentTarget as HTMLElement\n const [first, last] = getTabbableEdges(container)\n const hasTabbableElementsInside = first && last\n\n // we can only wrap focus if we have tabbable edges\n if (!hasTabbableElementsInside) {\n if (focusedElement === container) event.preventDefault()\n } else {\n if (!event.shiftKey && focusedElement === last) {\n event.preventDefault()\n if (loop) focus(first, { select: true })\n } else if (event.shiftKey && focusedElement === first) {\n event.preventDefault()\n if (loop) focus(last, { select: true })\n }\n }\n }\n },\n [loop, trapped, focusScope.paused]\n )\n\n const child = React.Children.only(props.children)\n\n return React.cloneElement(child as any, {\n tabIndex: -1,\n ...scopeProps,\n ref: composedRefs,\n onKeyDown: handleKeyDown,\n })\n})\n\nFocusScope.displayName = FOCUS_SCOPE_NAME\n\n/* -------------------------------------------------------------------------------------------------\n * Utils\n * -----------------------------------------------------------------------------------------------*/\n\n/**\n * Attempts focusing the first element in a list of candidates.\n * Stops when focus has actually moved.\n */\nfunction focusFirst(candidates: HTMLElement[], { select = false } = {}) {\n const previouslyFocusedElement = document.activeElement\n for (const candidate of candidates) {\n focus(candidate, { select })\n if (document.activeElement !== previouslyFocusedElement) return\n }\n}\n\n/**\n * Returns the first and last tabbable elements inside a container.\n */\nfunction getTabbableEdges(container: HTMLElement) {\n const candidates = getTabbableCandidates(container)\n const first = findVisible(candidates, container)\n const last = findVisible(candidates.reverse(), container)\n return [first, last] as const\n}\n\n/**\n * Returns a list of potential tabbable candidates.\n *\n * NOTE: This is only a close approximation. For example it doesn't take into account cases like when\n * elements are not visible. This cannot be worked out easily by just reading a property, but rather\n * necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.\n *\n * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker\n * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1\n */\nfunction getTabbableCandidates(container: HTMLElement) {\n const nodes: HTMLElement[] = []\n const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (node: any) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden'\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP\n // `.tabIndex` is not the same as the `tabindex` attribute. It works on the\n // runtime's understanding of tabbability, so this automatically accounts\n // for any kind of element that could be tabbed to.\n return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP\n },\n })\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement)\n // we do not take into account the order of nodes with positive `tabIndex` as it\n // hinders accessibility to have tab order different from visual order.\n return nodes\n}\n\n/**\n * Returns the first visible element in a list.\n * NOTE: Only checks visibility up to the `container`.\n */\nfunction findVisible(elements: HTMLElement[], container: HTMLElement) {\n for (const element of elements) {\n // we stop checking if it's hidden at the `container` level (excluding)\n if (!isHidden(element, { upTo: container })) return element\n }\n}\n\nfunction isHidden(node: HTMLElement, { upTo }: { upTo?: HTMLElement }) {\n if (getComputedStyle(node).visibility === 'hidden') return true\n while (node) {\n // we stop at `upTo` (excluding it)\n if (upTo !== undefined && node === upTo) return false\n if (getComputedStyle(node).display === 'none') return true\n node = node.parentElement as HTMLElement\n }\n return false\n}\n\nfunction isSelectableInput(element: any): element is FocusableTarget & { select: () => void } {\n return element instanceof HTMLInputElement && 'select' in element\n}\n\nfunction focus(element?: FocusableTarget | null, { select = false } = {}) {\n // only focus if that element is focusable\n if (element && element.focus) {\n const previouslyFocusedElement = document.activeElement\n // NOTE: we prevent scrolling on focus, to minimize jarring transitions for users\n element.focus({ preventScroll: true })\n // only select if its not the same element, it supports selection and we need to select\n if (element !== previouslyFocusedElement && isSelectableInput(element) && select)\n element.select()\n }\n}\n\n/* -------------------------------------------------------------------------------------------------\n * FocusScope stack\n * -----------------------------------------------------------------------------------------------*/\n\ntype FocusScopeAPI = { paused: boolean; pause(): void; resume(): void }\nconst focusScopesStack = createFocusScopesStack()\n\nfunction createFocusScopesStack() {\n /** A stack of focus scopes, with the active one at the top */\n let stack: FocusScopeAPI[] = []\n\n return {\n add(focusScope: FocusScopeAPI) {\n // pause the currently active focus scope (at the top of the stack)\n const activeFocusScope = stack[0]\n if (focusScope !== activeFocusScope) {\n activeFocusScope?.pause()\n }\n // remove in case it already exists (because we'll re-add it at the top of the stack)\n stack = arrayRemove(stack, focusScope)\n stack.unshift(focusScope)\n },\n\n remove(focusScope: FocusScopeAPI) {\n stack = arrayRemove(stack, focusScope)\n stack[0]?.resume()\n },\n }\n}\n\nfunction arrayRemove<T>(array: T[], item: T) {\n const updatedArray = [...array]\n const index = updatedArray.indexOf(item)\n if (index !== -1) {\n updatedArray.splice(index, 1)\n }\n return updatedArray\n}\n\nfunction removeLinks(items: HTMLElement[]) {\n return items.filter((item) => item.tagName !== 'A')\n}\n\nexport { FocusScope }\n\n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AOA,MAAM,mBAAmB,uBAAuB;AAEhD,kCAAkC;AAEhC,MAAI,QAAyB,CAAC;AAE9B,SAAO;AAAA,IACL,IAAI,YAA2B;AAE7B,YAAM,mBAAmB,MAAM;AAC/B,UAAI,eAAe,kBAAkB;AACnC,6DAAkB;AAAA,MACpB;AAEA,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,QAAQ,UAAU;AAAA,IAC1B;AAAA,IAEA,OAAO,YAA2B;AA/QtC;AAgRM,cAAQ,YAAY,OAAO,UAAU;AACrC,kBAAM,OAAN,mBAAU;AAAA,IACZ;AAAA,EACF;AACF;AAEA,qBAAwB,OAAY,MAAS;AAC3C,QAAM,eAAe,CAAC,GAAG,KAAK;AAC9B,QAAM,QAAQ,aAAa,QAAQ,IAAI;AACvC,MAAI,UAAU,IAAI;AAChB,iBAAa,OAAO,OAAO,CAAC;AAAA,EAC9B;AACA,SAAO;AACT;AAEA,qBAAqB,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var FocusScope_native_exports = {};
|
|
19
|
+
__export(FocusScope_native_exports, {
|
|
20
|
+
FocusScope: () => FocusScope
|
|
21
|
+
});
|
|
22
|
+
module.exports = __toCommonJS(FocusScope_native_exports);
|
|
23
|
+
var import_react = require("react");
|
|
24
|
+
const FocusScope = (0, import_react.forwardRef)((props, _ref) => {
|
|
25
|
+
return props.children;
|
|
26
|
+
});
|
|
27
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
28
|
+
0 && (module.exports = {
|
|
29
|
+
FocusScope
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=FocusScope.native.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/FocusScope.native.tsx"],
|
|
4
|
+
"sourcesContent": ["import { forwardRef } from 'react'\n\nimport { FocusScopeProps } from './FocusScopeProps'\n\nexport const FocusScope = forwardRef((props: FocusScopeProps, _ref) => {\n return props.children as any\n})\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA2B;AAIpB,MAAM,aAAa,6BAAW,CAAC,OAAwB,SAAS;AACrE,SAAO,MAAM;AACf,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var FocusScopeProps_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(FocusScopeProps_exports);
|
|
16
|
+
//# sourceMappingURL=FocusScopeProps.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/FocusScopeProps.tsx"],
|
|
4
|
+
"sourcesContent": ["import React from 'react'\n\nexport interface FocusScopeProps {\n /**\n * When `true`, tabbing from last item will focus first tabbable\n * and shift+tab from first item will focus last tababble.\n * @defaultValue false\n */\n loop?: boolean\n\n /**\n * When `true`, focus cannot escape the focus scope via keyboard,\n * pointer, or a programmatic focus.\n * @defaultValue false\n */\n trapped?: boolean\n\n /**\n * Event handler called when auto-focusing on mount.\n * Can be prevented.\n */\n onMountAutoFocus?: (event: Event) => void\n\n /**\n * Event handler called when auto-focusing on unmount.\n * Can be prevented.\n */\n onUnmountAutoFocus?: (event: Event) => void\n\n children?: React.ReactNode\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;AAAA;AAAA;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var src_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(src_exports);
|
|
17
|
+
__reExport(src_exports, require("./FocusScope"), module.exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
import { useCallbackRef } from "@radix-ui/react-use-callback-ref";
|
|
33
|
+
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
34
|
+
import * as React from "react";
|
|
35
|
+
const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
|
|
36
|
+
const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
|
|
37
|
+
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
38
|
+
const FOCUS_SCOPE_NAME = "FocusScope";
|
|
39
|
+
const FocusScope = React.forwardRef((props, forwardedRef) => {
|
|
40
|
+
const _a = props, {
|
|
41
|
+
loop = false,
|
|
42
|
+
trapped = false,
|
|
43
|
+
onMountAutoFocus: onMountAutoFocusProp,
|
|
44
|
+
onUnmountAutoFocus: onUnmountAutoFocusProp
|
|
45
|
+
} = _a, scopeProps = __objRest(_a, [
|
|
46
|
+
"loop",
|
|
47
|
+
"trapped",
|
|
48
|
+
"onMountAutoFocus",
|
|
49
|
+
"onUnmountAutoFocus"
|
|
50
|
+
]);
|
|
51
|
+
const [container, setContainer] = React.useState(null);
|
|
52
|
+
const onMountAutoFocus = useCallbackRef(onMountAutoFocusProp);
|
|
53
|
+
const onUnmountAutoFocus = useCallbackRef(onUnmountAutoFocusProp);
|
|
54
|
+
const lastFocusedElementRef = React.useRef(null);
|
|
55
|
+
const composedRefs = useComposedRefs(forwardedRef, (node) => setContainer(node));
|
|
56
|
+
const focusScope = React.useRef({
|
|
57
|
+
paused: false,
|
|
58
|
+
pause() {
|
|
59
|
+
this.paused = true;
|
|
60
|
+
},
|
|
61
|
+
resume() {
|
|
62
|
+
this.paused = false;
|
|
63
|
+
}
|
|
64
|
+
}).current;
|
|
65
|
+
React.useEffect(() => {
|
|
66
|
+
if (trapped) {
|
|
67
|
+
let handleFocusIn = function(event) {
|
|
68
|
+
if (focusScope.paused || !container)
|
|
69
|
+
return;
|
|
70
|
+
const target = event.target;
|
|
71
|
+
if (container.contains(target)) {
|
|
72
|
+
lastFocusedElementRef.current = target;
|
|
73
|
+
} else {
|
|
74
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
75
|
+
}
|
|
76
|
+
}, handleFocusOut = function(event) {
|
|
77
|
+
if (focusScope.paused || !container)
|
|
78
|
+
return;
|
|
79
|
+
if (!container.contains(event.relatedTarget)) {
|
|
80
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
document.addEventListener("focusin", handleFocusIn);
|
|
84
|
+
document.addEventListener("focusout", handleFocusOut);
|
|
85
|
+
return () => {
|
|
86
|
+
document.removeEventListener("focusin", handleFocusIn);
|
|
87
|
+
document.removeEventListener("focusout", handleFocusOut);
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}, [trapped, container, focusScope.paused]);
|
|
91
|
+
React.useEffect(() => {
|
|
92
|
+
if (container) {
|
|
93
|
+
focusScopesStack.add(focusScope);
|
|
94
|
+
const previouslyFocusedElement = document.activeElement;
|
|
95
|
+
const hasFocusedCandidate = container.contains(previouslyFocusedElement);
|
|
96
|
+
if (!hasFocusedCandidate) {
|
|
97
|
+
const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
|
|
98
|
+
container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
99
|
+
container.dispatchEvent(mountEvent);
|
|
100
|
+
if (!mountEvent.defaultPrevented) {
|
|
101
|
+
focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
|
|
102
|
+
if (document.activeElement === previouslyFocusedElement) {
|
|
103
|
+
focus(container);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return () => {
|
|
108
|
+
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
109
|
+
setTimeout(() => {
|
|
110
|
+
const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
|
|
111
|
+
container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
112
|
+
container.dispatchEvent(unmountEvent);
|
|
113
|
+
if (!unmountEvent.defaultPrevented) {
|
|
114
|
+
focus(previouslyFocusedElement ?? document.body, { select: true });
|
|
115
|
+
}
|
|
116
|
+
container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
117
|
+
focusScopesStack.remove(focusScope);
|
|
118
|
+
}, 0);
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}, [container, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
|
|
122
|
+
const handleKeyDown = React.useCallback((event) => {
|
|
123
|
+
if (!loop && !trapped)
|
|
124
|
+
return;
|
|
125
|
+
if (focusScope.paused)
|
|
126
|
+
return;
|
|
127
|
+
const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
|
|
128
|
+
const focusedElement = document.activeElement;
|
|
129
|
+
if (isTabKey && focusedElement) {
|
|
130
|
+
const container2 = event.currentTarget;
|
|
131
|
+
const [first, last] = getTabbableEdges(container2);
|
|
132
|
+
const hasTabbableElementsInside = first && last;
|
|
133
|
+
if (!hasTabbableElementsInside) {
|
|
134
|
+
if (focusedElement === container2)
|
|
135
|
+
event.preventDefault();
|
|
136
|
+
} else {
|
|
137
|
+
if (!event.shiftKey && focusedElement === last) {
|
|
138
|
+
event.preventDefault();
|
|
139
|
+
if (loop)
|
|
140
|
+
focus(first, { select: true });
|
|
141
|
+
} else if (event.shiftKey && focusedElement === first) {
|
|
142
|
+
event.preventDefault();
|
|
143
|
+
if (loop)
|
|
144
|
+
focus(last, { select: true });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}, [loop, trapped, focusScope.paused]);
|
|
149
|
+
const child = React.Children.only(props.children);
|
|
150
|
+
return React.cloneElement(child, __spreadProps(__spreadValues({
|
|
151
|
+
tabIndex: -1
|
|
152
|
+
}, scopeProps), {
|
|
153
|
+
ref: composedRefs,
|
|
154
|
+
onKeyDown: handleKeyDown
|
|
155
|
+
}));
|
|
156
|
+
});
|
|
157
|
+
FocusScope.displayName = FOCUS_SCOPE_NAME;
|
|
158
|
+
function focusFirst(candidates, { select = false } = {}) {
|
|
159
|
+
const previouslyFocusedElement = document.activeElement;
|
|
160
|
+
for (const candidate of candidates) {
|
|
161
|
+
focus(candidate, { select });
|
|
162
|
+
if (document.activeElement !== previouslyFocusedElement)
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
function getTabbableEdges(container) {
|
|
167
|
+
const candidates = getTabbableCandidates(container);
|
|
168
|
+
const first = findVisible(candidates, container);
|
|
169
|
+
const last = findVisible(candidates.reverse(), container);
|
|
170
|
+
return [first, last];
|
|
171
|
+
}
|
|
172
|
+
function getTabbableCandidates(container) {
|
|
173
|
+
const nodes = [];
|
|
174
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
|
|
175
|
+
acceptNode: (node) => {
|
|
176
|
+
const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
|
|
177
|
+
if (node.disabled || node.hidden || isHiddenInput)
|
|
178
|
+
return NodeFilter.FILTER_SKIP;
|
|
179
|
+
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
while (walker.nextNode())
|
|
183
|
+
nodes.push(walker.currentNode);
|
|
184
|
+
return nodes;
|
|
185
|
+
}
|
|
186
|
+
function findVisible(elements, container) {
|
|
187
|
+
for (const element of elements) {
|
|
188
|
+
if (!isHidden(element, { upTo: container }))
|
|
189
|
+
return element;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function isHidden(node, { upTo }) {
|
|
193
|
+
if (getComputedStyle(node).visibility === "hidden")
|
|
194
|
+
return true;
|
|
195
|
+
while (node) {
|
|
196
|
+
if (upTo !== void 0 && node === upTo)
|
|
197
|
+
return false;
|
|
198
|
+
if (getComputedStyle(node).display === "none")
|
|
199
|
+
return true;
|
|
200
|
+
node = node.parentElement;
|
|
201
|
+
}
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
function isSelectableInput(element) {
|
|
205
|
+
return element instanceof HTMLInputElement && "select" in element;
|
|
206
|
+
}
|
|
207
|
+
function focus(element, { select = false } = {}) {
|
|
208
|
+
if (element && element.focus) {
|
|
209
|
+
const previouslyFocusedElement = document.activeElement;
|
|
210
|
+
element.focus({ preventScroll: true });
|
|
211
|
+
if (element !== previouslyFocusedElement && isSelectableInput(element) && select)
|
|
212
|
+
element.select();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const focusScopesStack = createFocusScopesStack();
|
|
216
|
+
function createFocusScopesStack() {
|
|
217
|
+
let stack = [];
|
|
218
|
+
return {
|
|
219
|
+
add(focusScope) {
|
|
220
|
+
const activeFocusScope = stack[0];
|
|
221
|
+
if (focusScope !== activeFocusScope) {
|
|
222
|
+
activeFocusScope == null ? void 0 : activeFocusScope.pause();
|
|
223
|
+
}
|
|
224
|
+
stack = arrayRemove(stack, focusScope);
|
|
225
|
+
stack.unshift(focusScope);
|
|
226
|
+
},
|
|
227
|
+
remove(focusScope) {
|
|
228
|
+
var _a;
|
|
229
|
+
stack = arrayRemove(stack, focusScope);
|
|
230
|
+
(_a = stack[0]) == null ? void 0 : _a.resume();
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function arrayRemove(array, item) {
|
|
235
|
+
const updatedArray = [...array];
|
|
236
|
+
const index = updatedArray.indexOf(item);
|
|
237
|
+
if (index !== -1) {
|
|
238
|
+
updatedArray.splice(index, 1);
|
|
239
|
+
}
|
|
240
|
+
return updatedArray;
|
|
241
|
+
}
|
|
242
|
+
function removeLinks(items) {
|
|
243
|
+
return items.filter((item) => item.tagName !== "A");
|
|
244
|
+
}
|
|
245
|
+
export {
|
|
246
|
+
FocusScope
|
|
247
|
+
};
|
|
248
|
+
//# sourceMappingURL=FocusScope.js.map
|