@tamagui/focus-scope 1.46.2 → 1.47.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/focus-scope",
3
- "version": "1.46.2",
3
+ "version": "1.47.1",
4
4
  "sideEffects": true,
5
5
  "source": "src/index.ts",
6
6
  "types": "./types/index.d.ts",
@@ -21,14 +21,14 @@
21
21
  "clean:build": "tamagui-build clean:build"
22
22
  },
23
23
  "dependencies": {
24
- "@tamagui/compose-refs": "1.46.2",
25
- "@tamagui/use-event": "1.46.2"
24
+ "@tamagui/compose-refs": "1.47.1",
25
+ "@tamagui/use-event": "1.47.1"
26
26
  },
27
27
  "peerDependencies": {
28
28
  "react": "*"
29
29
  },
30
30
  "devDependencies": {
31
- "@tamagui/build": "1.46.2",
31
+ "@tamagui/build": "1.47.1",
32
32
  "react": "^18.2.0"
33
33
  },
34
34
  "publishConfig": {
@@ -1,229 +0,0 @@
1
- import { useComposedRefs } from "@tamagui/compose-refs";
2
- import { useEvent } from "@tamagui/use-event";
3
- import * as React from "react";
4
- const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
5
- const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
6
- const EVENT_OPTIONS = { bubbles: false, cancelable: true };
7
- const FocusScope = React.forwardRef(
8
- function FocusScope2(props, forwardedRef) {
9
- const childProps = useFocusScope(props, forwardedRef);
10
- if (typeof props.children === "function") {
11
- return <>{props.children(childProps)}</>;
12
- }
13
- return React.cloneElement(React.Children.only(props.children), childProps);
14
- }
15
- );
16
- function useFocusScope(props, forwardedRef) {
17
- const {
18
- loop = false,
19
- trapped = false,
20
- onMountAutoFocus: onMountAutoFocusProp,
21
- onUnmountAutoFocus: onUnmountAutoFocusProp,
22
- forceUnmount,
23
- children,
24
- ...scopeProps
25
- } = props;
26
- const [container, setContainer] = React.useState(null);
27
- const onMountAutoFocus = useEvent(onMountAutoFocusProp);
28
- const onUnmountAutoFocus = useEvent(onUnmountAutoFocusProp);
29
- const lastFocusedElementRef = React.useRef(null);
30
- const composedRefs = useComposedRefs(forwardedRef, (node) => setContainer(node));
31
- const focusScope = React.useRef({
32
- paused: false,
33
- pause() {
34
- this.paused = true;
35
- },
36
- resume() {
37
- this.paused = false;
38
- }
39
- }).current;
40
- React.useEffect(() => {
41
- if (!trapped)
42
- return;
43
- function handleFocusIn(event) {
44
- if (focusScope.paused || !container)
45
- return;
46
- const target = event.target;
47
- if (container.contains(target)) {
48
- lastFocusedElementRef.current = target;
49
- } else {
50
- focus(lastFocusedElementRef.current, { select: true });
51
- }
52
- }
53
- function handleFocusOut(event) {
54
- if (focusScope.paused || !container)
55
- return;
56
- if (!container.contains(event.relatedTarget)) {
57
- focus(lastFocusedElementRef.current, { select: true });
58
- }
59
- }
60
- document.addEventListener("focusin", handleFocusIn);
61
- document.addEventListener("focusout", handleFocusOut);
62
- return () => {
63
- document.removeEventListener("focusin", handleFocusIn);
64
- document.removeEventListener("focusout", handleFocusOut);
65
- };
66
- }, [trapped, forceUnmount, container, focusScope.paused]);
67
- React.useEffect(() => {
68
- if (!trapped)
69
- return;
70
- if (!container)
71
- return;
72
- if (forceUnmount)
73
- return;
74
- focusScopesStack.add(focusScope);
75
- const previouslyFocusedElement = document.activeElement;
76
- const hasFocusedCandidate = container.contains(previouslyFocusedElement);
77
- if (!hasFocusedCandidate) {
78
- const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
79
- container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
80
- container.dispatchEvent(mountEvent);
81
- if (!mountEvent.defaultPrevented) {
82
- focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
83
- if (document.activeElement === previouslyFocusedElement) {
84
- focus(container);
85
- }
86
- }
87
- }
88
- return () => {
89
- container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
90
- const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
91
- container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
92
- container.dispatchEvent(unmountEvent);
93
- if (!unmountEvent.defaultPrevented) {
94
- focus(previouslyFocusedElement ?? document.body, { select: true });
95
- }
96
- container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
97
- focusScopesStack.remove(focusScope);
98
- };
99
- }, [container, forceUnmount, onMountAutoFocus, onUnmountAutoFocus, focusScope]);
100
- const handleKeyDown = React.useCallback(
101
- (event) => {
102
- if (!trapped)
103
- return;
104
- if (!loop)
105
- return;
106
- if (focusScope.paused)
107
- return;
108
- const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
109
- const focusedElement = document.activeElement;
110
- if (isTabKey && focusedElement) {
111
- const container2 = event.currentTarget;
112
- const [first, last] = getTabbableEdges(container2);
113
- const hasTabbableElementsInside = first && last;
114
- if (!hasTabbableElementsInside) {
115
- if (focusedElement === container2)
116
- event.preventDefault();
117
- } else {
118
- if (!event.shiftKey && focusedElement === last) {
119
- event.preventDefault();
120
- if (loop)
121
- focus(first, { select: true });
122
- } else if (event.shiftKey && focusedElement === first) {
123
- event.preventDefault();
124
- if (loop)
125
- focus(last, { select: true });
126
- }
127
- }
128
- }
129
- },
130
- [loop, trapped, focusScope.paused]
131
- );
132
- return {
133
- tabIndex: -1,
134
- ...scopeProps,
135
- ref: composedRefs,
136
- onKeyDown: handleKeyDown
137
- };
138
- }
139
- function focusFirst(candidates, { select = false } = {}) {
140
- const previouslyFocusedElement = document.activeElement;
141
- for (const candidate of candidates) {
142
- focus(candidate, { select });
143
- if (document.activeElement !== previouslyFocusedElement)
144
- return;
145
- }
146
- }
147
- function getTabbableEdges(container) {
148
- const candidates = getTabbableCandidates(container);
149
- const first = findVisible(candidates, container);
150
- const last = findVisible(candidates.reverse(), container);
151
- return [first, last];
152
- }
153
- function getTabbableCandidates(container) {
154
- const nodes = [];
155
- const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
156
- acceptNode: (node) => {
157
- const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
158
- if (node.disabled || node.hidden || isHiddenInput)
159
- return NodeFilter.FILTER_SKIP;
160
- return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
161
- }
162
- });
163
- while (walker.nextNode())
164
- nodes.push(walker.currentNode);
165
- return nodes;
166
- }
167
- function findVisible(elements, container) {
168
- for (const element of elements) {
169
- if (!isHidden(element, { upTo: container }))
170
- return element;
171
- }
172
- }
173
- function isHidden(node, { upTo }) {
174
- if (getComputedStyle(node).visibility === "hidden")
175
- return true;
176
- while (node) {
177
- if (upTo !== void 0 && node === upTo)
178
- return false;
179
- if (getComputedStyle(node).display === "none")
180
- return true;
181
- node = node.parentElement;
182
- }
183
- return false;
184
- }
185
- function isSelectableInput(element) {
186
- return element instanceof HTMLInputElement && "select" in element;
187
- }
188
- function focus(element, { select = false } = {}) {
189
- if (element?.focus) {
190
- const previouslyFocusedElement = document.activeElement;
191
- element.focus({ preventScroll: true });
192
- if (element !== previouslyFocusedElement && isSelectableInput(element) && select)
193
- element.select();
194
- }
195
- }
196
- const focusScopesStack = createFocusScopesStack();
197
- function createFocusScopesStack() {
198
- let stack = [];
199
- return {
200
- add(focusScope) {
201
- const activeFocusScope = stack[0];
202
- if (focusScope !== activeFocusScope) {
203
- activeFocusScope?.pause();
204
- }
205
- stack = arrayRemove(stack, focusScope);
206
- stack.unshift(focusScope);
207
- },
208
- remove(focusScope) {
209
- stack = arrayRemove(stack, focusScope);
210
- stack[0]?.resume();
211
- }
212
- };
213
- }
214
- function arrayRemove(array, item) {
215
- const updatedArray = [...array];
216
- const index = updatedArray.indexOf(item);
217
- if (index !== -1) {
218
- updatedArray.splice(index, 1);
219
- }
220
- return updatedArray;
221
- }
222
- function removeLinks(items) {
223
- return items.filter((item) => item.tagName !== "A");
224
- }
225
- export {
226
- FocusScope,
227
- useFocusScope
228
- };
229
- //# sourceMappingURL=FocusScope.mjs.map
@@ -1,6 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/FocusScope.tsx"],
4
- "mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,gBAAgB;AACzB,YAAY,WAAW;AAIvB,MAAM,qBAAqB;AAC3B,MAAM,uBAAuB;AAC7B,MAAM,gBAAgB,EAAE,SAAS,OAAO,YAAY,KAAK;AAUzD,MAAM,aAAa,MAAM;AAAA,EACvB,SAASA,YAAW,OAAO,cAAc;AACvC,UAAM,aAAa,cAAc,OAAO,YAAY;AAEpD,QAAI,OAAO,MAAM,aAAa,YAAY;AACxC,aAAO,GAAG,MAAM,SAAS,UAAU,EAAE;AAAA,IACvC;AAEA,WAAO,MAAM,aAAa,MAAM,SAAS,KAAK,MAAM,QAAQ,GAAU,UAAU;AAAA,EAClF;AACF;AAMO,SAAS,cACd,OACA,cACA;AACA,QAAM;AAAA,IACJ,OAAO;AAAA,IACP,UAAU;AAAA,IACV,kBAAkB;AAAA,IAClB,oBAAoB;AAAA,IACpB;AAAA,IACA;AAAA,IACA,GAAG;AAAA,EACL,IAAI;AACJ,QAAM,CAAC,WAAW,YAAY,IAAI,MAAM,SAA6B,IAAI;AACzE,QAAM,mBAAmB,SAAS,oBAAoB;AACtD,QAAM,qBAAqB,SAAS,sBAAsB;AAC1D,QAAM,wBAAwB,MAAM,OAA2B,IAAI;AACnE,QAAM,eAAe,gBAAgB,cAAc,CAAC,SAAS,aAAa,IAAI,CAAC;AAE/E,QAAM,aAAa,MAAM,OAAO;AAAA,IAC9B,QAAQ;AAAA,IACR,QAAQ;AACN,WAAK,SAAS;AAAA,IAChB;AAAA,IACA,SAAS;AACP,WAAK,SAAS;AAAA,IAChB;AAAA,EACF,CAAC,EAAE;AAGH,QAAM,UAAU,MAAM;AACpB,QAAI,CAAC;AAAS;AACd,aAAS,cAAc,OAAmB;A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AE,SAAS,MAAM,IAAI,CAAC,GAAG;AAExE,MAAI,SAAS,OAAO;AAClB,UAAM,2BAA2B,SAAS;AAE1C,YAAQ,MAAM,EAAE,eAAe,KAAK,CAAC;AAErC,QAAI,YAAY,4BAA4B,kBAAkB,OAAO,KAAK;AACxE,cAAQ,OAAO;AAAA,EACnB;AACF;AAOA,MAAM,mBAAmB,uBAAuB;AAEhD,SAAS,yBAAyB;AAEhC,MAAI,QAAyB,CAAC;AAE9B,SAAO;AAAA,IACL,IAAI,YAA2B;AAE7B,YAAM,mBAAmB,MAAM,CAAC;AAChC,UAAI,eAAe,kBAAkB;AACnC,0BAAkB,MAAM;AAAA,MAC1B;AAEA,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,QAAQ,UAAU;AAAA,IAC1B;AAAA,IAEA,OAAO,YAA2B;AAChC,cAAQ,YAAY,OAAO,UAAU;AACrC,YAAM,CAAC,GAAG,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AAEA,SAAS,YAAe,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,SAAS,YAAY,OAAsB;AACzC,SAAO,MAAM,OAAO,CAAC,SAAS,KAAK,YAAY,GAAG;AACpD;",
5
- "names": ["FocusScope", "container"]
6
- }
@@ -1,8 +0,0 @@
1
- import { forwardRef } from "react";
2
- const FocusScope = forwardRef((props, _ref) => {
3
- return props.children;
4
- });
5
- export {
6
- FocusScope
7
- };
8
- //# sourceMappingURL=FocusScope.native.mjs.map
@@ -1,6 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/FocusScope.native.tsx"],
4
- "mappings": "AAAA,SAAS,kBAAkB;AAIpB,MAAM,aAAa,WAAW,CAAC,OAAwB,SAAS;AACrE,SAAO,MAAM;AACf,CAAC;",
5
- "names": []
6
- }
@@ -1 +0,0 @@
1
- //# sourceMappingURL=FocusScopeProps.mjs.map
@@ -1,6 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": [],
4
- "mappings": "",
5
- "names": []
6
- }
@@ -1,2 +0,0 @@
1
- export * from "./FocusScope";
2
- //# sourceMappingURL=index.mjs.map
@@ -1,6 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": ["../../src/index.tsx"],
4
- "mappings": "AAAA,cAAc;",
5
- "names": []
6
- }