@tamagui/focus-scope 1.116.1 → 1.116.2

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.
@@ -0,0 +1,172 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: !0 });
9
+ }, __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from == "object" || typeof from == "function")
11
+ for (let key of __getOwnPropNames(from))
12
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
13
+ return to;
14
+ };
15
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
16
+ // If the importer is in node compatibility mode or this is not an ESM
17
+ // file that has been converted to a CommonJS file using a Babel-
18
+ // compatible transform (i.e. "__esModule" has not been set), then set
19
+ // "default" to the CommonJS "module.exports" for node compatibility.
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target,
21
+ mod
22
+ )), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
23
+ var FocusScope_exports = {};
24
+ __export(FocusScope_exports, {
25
+ FocusScope: () => FocusScope,
26
+ useFocusScope: () => useFocusScope
27
+ });
28
+ module.exports = __toCommonJS(FocusScope_exports);
29
+ var import_compose_refs = require("@tamagui/compose-refs"), import_use_event = require("@tamagui/use-event"), import_start_transition = require("@tamagui/start-transition"), React = __toESM(require("react")), import_jsx_runtime = require("react/jsx-runtime");
30
+ const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount", AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount", EVENT_OPTIONS = { bubbles: !1, cancelable: !0 }, FocusScope = React.forwardRef(
31
+ function(props, forwardedRef) {
32
+ const childProps = useFocusScope(props, forwardedRef);
33
+ return typeof props.children == "function" ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: props.children(childProps) }) : React.cloneElement(React.Children.only(props.children), childProps);
34
+ }
35
+ );
36
+ function useFocusScope(props, forwardedRef) {
37
+ const {
38
+ loop = !1,
39
+ enabled = !0,
40
+ trapped = !1,
41
+ onMountAutoFocus: onMountAutoFocusProp,
42
+ onUnmountAutoFocus: onUnmountAutoFocusProp,
43
+ forceUnmount,
44
+ children,
45
+ ...scopeProps
46
+ } = props, [container, setContainer] = React.useState(null), onMountAutoFocus = (0, import_use_event.useEvent)(onMountAutoFocusProp), onUnmountAutoFocus = (0, import_use_event.useEvent)(onUnmountAutoFocusProp), lastFocusedElementRef = React.useRef(null), composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, (node) => {
47
+ (0, import_start_transition.startTransition)(() => {
48
+ setContainer(node);
49
+ });
50
+ }), focusScope = React.useRef({
51
+ paused: !1,
52
+ pause() {
53
+ this.paused = !0;
54
+ },
55
+ resume() {
56
+ this.paused = !1;
57
+ }
58
+ }).current;
59
+ React.useEffect(() => {
60
+ if (!enabled || !trapped) return;
61
+ const controller = new AbortController();
62
+ function handleFocusIn(event) {
63
+ if (focusScope.paused || !container) return;
64
+ const target = event.target;
65
+ container.contains(target) ? (target?.addEventListener("blur", handleBlur, { signal: controller.signal }), lastFocusedElementRef.current = target) : focus(lastFocusedElementRef.current, { select: !0 });
66
+ }
67
+ function handleFocusOut(event) {
68
+ controller.abort(), !(focusScope.paused || !container) && (container.contains(event.relatedTarget) || focus(lastFocusedElementRef.current, { select: !0 }));
69
+ }
70
+ function handleBlur() {
71
+ lastFocusedElementRef.current = container;
72
+ }
73
+ return document.addEventListener("focusin", handleFocusIn), document.addEventListener("focusout", handleFocusOut), () => {
74
+ controller.abort(), document.removeEventListener("focusin", handleFocusIn), document.removeEventListener("focusout", handleFocusOut);
75
+ };
76
+ }, [trapped, forceUnmount, container, focusScope.paused]), React.useEffect(() => {
77
+ if (!enabled || !container || forceUnmount) return;
78
+ focusScopesStack.add(focusScope);
79
+ const previouslyFocusedElement = document.activeElement;
80
+ if (!container.contains(previouslyFocusedElement)) {
81
+ const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
82
+ if (container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus), container.dispatchEvent(mountEvent), !mountEvent.defaultPrevented) {
83
+ const candidates = removeLinks(getTabbableCandidates(container));
84
+ focusFirst(candidates, { select: !0 }), document.activeElement === previouslyFocusedElement && focus(container);
85
+ }
86
+ }
87
+ return () => {
88
+ container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
89
+ const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
90
+ container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus), container.dispatchEvent(unmountEvent), unmountEvent.defaultPrevented || focus(previouslyFocusedElement ?? document.body, { select: !0 }), container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus), focusScopesStack.remove(focusScope);
91
+ };
92
+ }, [enabled, container, forceUnmount, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
93
+ const handleKeyDown = React.useCallback(
94
+ (event) => {
95
+ if (!trapped || !loop || focusScope.paused) return;
96
+ const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey, focusedElement = document.activeElement;
97
+ if (isTabKey && focusedElement) {
98
+ const container2 = event.currentTarget, [first, last] = getTabbableEdges(container2);
99
+ first && last ? !event.shiftKey && focusedElement === last ? (event.preventDefault(), loop && focus(first, { select: !0 })) : event.shiftKey && focusedElement === first && (event.preventDefault(), loop && focus(last, { select: !0 })) : focusedElement === container2 && event.preventDefault();
100
+ }
101
+ },
102
+ [loop, trapped, focusScope.paused]
103
+ );
104
+ return {
105
+ tabIndex: -1,
106
+ ...scopeProps,
107
+ ref: composedRefs,
108
+ onKeyDown: handleKeyDown
109
+ };
110
+ }
111
+ function focusFirst(candidates, { select = !1 } = {}) {
112
+ const previouslyFocusedElement = document.activeElement;
113
+ for (const candidate of candidates)
114
+ if (focus(candidate, { select }), document.activeElement !== previouslyFocusedElement) return;
115
+ }
116
+ function getTabbableEdges(container) {
117
+ const candidates = getTabbableCandidates(container), first = findVisible(candidates, container), last = findVisible(candidates.reverse(), container);
118
+ return [first, last];
119
+ }
120
+ function getTabbableCandidates(container) {
121
+ const nodes = [], walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
122
+ acceptNode: (node) => {
123
+ const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
124
+ return node.disabled || node.hidden || isHiddenInput ? NodeFilter.FILTER_SKIP : node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
125
+ }
126
+ });
127
+ for (; walker.nextNode(); ) nodes.push(walker.currentNode);
128
+ return nodes;
129
+ }
130
+ function findVisible(elements, container) {
131
+ for (const element of elements)
132
+ if (!isHidden(element, { upTo: container })) return element;
133
+ }
134
+ function isHidden(node, { upTo }) {
135
+ if (getComputedStyle(node).visibility === "hidden") return !0;
136
+ for (; node; ) {
137
+ if (upTo !== void 0 && node === upTo) return !1;
138
+ if (getComputedStyle(node).display === "none") return !0;
139
+ node = node.parentElement;
140
+ }
141
+ return !1;
142
+ }
143
+ function isSelectableInput(element) {
144
+ return element instanceof HTMLInputElement && "select" in element;
145
+ }
146
+ function focus(element, { select = !1 } = {}) {
147
+ if (element?.focus) {
148
+ const previouslyFocusedElement = document.activeElement;
149
+ element.focus({ preventScroll: !0 }), element !== previouslyFocusedElement && isSelectableInput(element) && select && element.select();
150
+ }
151
+ }
152
+ const focusScopesStack = createFocusScopesStack();
153
+ function createFocusScopesStack() {
154
+ let stack = [];
155
+ return {
156
+ add(focusScope) {
157
+ const activeFocusScope = stack[0];
158
+ focusScope !== activeFocusScope && activeFocusScope?.pause(), stack = arrayRemove(stack, focusScope), stack.unshift(focusScope);
159
+ },
160
+ remove(focusScope) {
161
+ stack = arrayRemove(stack, focusScope), stack[0]?.resume();
162
+ }
163
+ };
164
+ }
165
+ function arrayRemove(array, item) {
166
+ const updatedArray = [...array], index = updatedArray.indexOf(item);
167
+ return index !== -1 && updatedArray.splice(index, 1), updatedArray;
168
+ }
169
+ function removeLinks(items) {
170
+ return items.filter((item) => item.tagName !== "A");
171
+ }
172
+ //# sourceMappingURL=FocusScope.js.map
@@ -0,0 +1,14 @@
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
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
9
+ return to;
10
+ };
11
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
12
+ var FocusScopeProps_exports = {};
13
+ module.exports = __toCommonJS(FocusScopeProps_exports);
14
+ //# sourceMappingURL=FocusScopeProps.js.map
@@ -0,0 +1,15 @@
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
+ !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
9
+ return to;
10
+ }, __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
11
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), mod);
12
+ var src_exports = {};
13
+ module.exports = __toCommonJS(src_exports);
14
+ __reExport(src_exports, require("./FocusScope"), module.exports);
15
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/focus-scope",
3
- "version": "1.116.1",
3
+ "version": "1.116.2",
4
4
  "sideEffects": true,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -32,12 +32,12 @@
32
32
  }
33
33
  },
34
34
  "dependencies": {
35
- "@tamagui/compose-refs": "1.116.1",
36
- "@tamagui/start-transition": "1.116.1",
37
- "@tamagui/use-event": "1.116.1"
35
+ "@tamagui/compose-refs": "1.116.2",
36
+ "@tamagui/start-transition": "1.116.2",
37
+ "@tamagui/use-event": "1.116.2"
38
38
  },
39
39
  "devDependencies": {
40
- "@tamagui/build": "1.116.1",
40
+ "@tamagui/build": "1.116.2",
41
41
  "react": "^18.2.0 || ^19.0.0"
42
42
  },
43
43
  "publishConfig": {
@@ -1,38 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
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, {
15
- get: () => from[key],
16
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
- });
18
- return to;
19
- };
20
- 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);
32
- var FocusScope_native_exports = {};
33
- __export(FocusScope_native_exports, {
34
- FocusScope: () => FocusScope
35
- });
36
- module.exports = __toCommonJS(FocusScope_native_exports);
37
- var import_react = __toESM(require("react"));
38
- const FocusScope = import_react.default.forwardRef((props, _ref) => props.children);
@@ -1,6 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/FocusScope.native.tsx"],
4
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkB;AAIX,MAAM,aAAa,aAAAA,QAAM,WAAW,CAAC,OAAwB,SAC3D,MAAM,QACd;",
5
- "names": ["React"]
6
- }
File without changes