@tamagui/roving-focus 1.27.2 → 1.28.0
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/roving-focus",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.28.0",
|
|
4
4
|
"sideEffects": [
|
|
5
5
|
"*.css"
|
|
6
6
|
],
|
|
@@ -27,19 +27,19 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@tamagui/collection": "1.
|
|
31
|
-
"@tamagui/compose-refs": "1.
|
|
32
|
-
"@tamagui/core": "1.
|
|
33
|
-
"@tamagui/create-context": "1.
|
|
34
|
-
"@tamagui/use-controllable-state": "1.
|
|
35
|
-
"@tamagui/use-direction": "1.
|
|
36
|
-
"@tamagui/use-event": "1.
|
|
30
|
+
"@tamagui/collection": "1.28.0",
|
|
31
|
+
"@tamagui/compose-refs": "1.28.0",
|
|
32
|
+
"@tamagui/core": "1.28.0",
|
|
33
|
+
"@tamagui/create-context": "1.28.0",
|
|
34
|
+
"@tamagui/use-controllable-state": "1.28.0",
|
|
35
|
+
"@tamagui/use-direction": "1.28.0",
|
|
36
|
+
"@tamagui/use-event": "1.28.0"
|
|
37
37
|
},
|
|
38
38
|
"peerDependencies": {
|
|
39
39
|
"react": "*"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@tamagui/build": "1.
|
|
42
|
+
"@tamagui/build": "1.28.0",
|
|
43
43
|
"react": "^18.2.0"
|
|
44
44
|
},
|
|
45
45
|
"publishConfig": {
|
|
@@ -1,242 +0,0 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createCollection } from "@tamagui/collection";
|
|
3
|
-
import { useComposedRefs } from "@tamagui/compose-refs";
|
|
4
|
-
import {
|
|
5
|
-
Stack,
|
|
6
|
-
composeEventHandlers,
|
|
7
|
-
isWeb,
|
|
8
|
-
useEvent,
|
|
9
|
-
withStaticProperties
|
|
10
|
-
} from "@tamagui/core";
|
|
11
|
-
import { createContextScope } from "@tamagui/create-context";
|
|
12
|
-
import { useControllableState } from "@tamagui/use-controllable-state";
|
|
13
|
-
import { useDirection } from "@tamagui/use-direction";
|
|
14
|
-
import * as React from "react";
|
|
15
|
-
const ENTRY_FOCUS = "rovingFocusGroup.onEntryFocus";
|
|
16
|
-
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
17
|
-
const RovingFocusGroupImpl = React.forwardRef((props, forwardedRef) => {
|
|
18
|
-
const {
|
|
19
|
-
__scopeRovingFocusGroup,
|
|
20
|
-
orientation,
|
|
21
|
-
loop = false,
|
|
22
|
-
dir,
|
|
23
|
-
currentTabStopId: currentTabStopIdProp,
|
|
24
|
-
defaultCurrentTabStopId,
|
|
25
|
-
onCurrentTabStopIdChange,
|
|
26
|
-
onEntryFocus,
|
|
27
|
-
...groupProps
|
|
28
|
-
} = props;
|
|
29
|
-
const ref = React.useRef(null);
|
|
30
|
-
const composedRefs = useComposedRefs(forwardedRef, ref);
|
|
31
|
-
const direction = useDirection(dir);
|
|
32
|
-
const [currentTabStopId = null, setCurrentTabStopId] = useControllableState({
|
|
33
|
-
prop: currentTabStopIdProp,
|
|
34
|
-
defaultProp: defaultCurrentTabStopId ?? null,
|
|
35
|
-
onChange: onCurrentTabStopIdChange
|
|
36
|
-
});
|
|
37
|
-
const [isTabbingBackOut, setIsTabbingBackOut] = React.useState(false);
|
|
38
|
-
const handleEntryFocus = useEvent(onEntryFocus);
|
|
39
|
-
const getItems = useCollection(__scopeRovingFocusGroup);
|
|
40
|
-
const isClickFocusRef = React.useRef(false);
|
|
41
|
-
const [focusableItemsCount, setFocusableItemsCount] = React.useState(0);
|
|
42
|
-
React.useEffect(() => {
|
|
43
|
-
const node = ref.current;
|
|
44
|
-
if (node) {
|
|
45
|
-
node.addEventListener(ENTRY_FOCUS, handleEntryFocus);
|
|
46
|
-
return () => node.removeEventListener(ENTRY_FOCUS, handleEntryFocus);
|
|
47
|
-
}
|
|
48
|
-
}, [handleEntryFocus]);
|
|
49
|
-
return /* @__PURE__ */ jsx(
|
|
50
|
-
RovingFocusProvider,
|
|
51
|
-
{
|
|
52
|
-
scope: __scopeRovingFocusGroup,
|
|
53
|
-
orientation,
|
|
54
|
-
dir: direction,
|
|
55
|
-
loop,
|
|
56
|
-
currentTabStopId,
|
|
57
|
-
onItemFocus: React.useCallback(
|
|
58
|
-
(tabStopId) => setCurrentTabStopId(tabStopId),
|
|
59
|
-
[setCurrentTabStopId]
|
|
60
|
-
),
|
|
61
|
-
onItemShiftTab: React.useCallback(() => setIsTabbingBackOut(true), []),
|
|
62
|
-
onFocusableItemAdd: React.useCallback(
|
|
63
|
-
() => setFocusableItemsCount((prevCount) => prevCount + 1),
|
|
64
|
-
[]
|
|
65
|
-
),
|
|
66
|
-
onFocusableItemRemove: React.useCallback(
|
|
67
|
-
() => setFocusableItemsCount((prevCount) => prevCount - 1),
|
|
68
|
-
[]
|
|
69
|
-
),
|
|
70
|
-
children: /* @__PURE__ */ jsx(
|
|
71
|
-
Stack,
|
|
72
|
-
{
|
|
73
|
-
tabIndex: isTabbingBackOut || focusableItemsCount === 0 ? -1 : 0,
|
|
74
|
-
"data-orientation": orientation,
|
|
75
|
-
...groupProps,
|
|
76
|
-
ref: composedRefs,
|
|
77
|
-
style: [{ outline: "none" }, props.style],
|
|
78
|
-
onMouseDown: composeEventHandlers(props.onMouseDown, () => {
|
|
79
|
-
isClickFocusRef.current = true;
|
|
80
|
-
}),
|
|
81
|
-
onFocus: composeEventHandlers(props.onFocus, (event) => {
|
|
82
|
-
const isKeyboardFocus = !isClickFocusRef.current;
|
|
83
|
-
if (event.target === event.currentTarget && isKeyboardFocus && !isTabbingBackOut) {
|
|
84
|
-
const entryFocusEvent = new CustomEvent(ENTRY_FOCUS, EVENT_OPTIONS);
|
|
85
|
-
event.currentTarget.dispatchEvent(entryFocusEvent);
|
|
86
|
-
if (!entryFocusEvent.defaultPrevented) {
|
|
87
|
-
const items = getItems().filter((item) => item.focusable);
|
|
88
|
-
const activeItem = items.find((item) => item.active);
|
|
89
|
-
const currentItem = items.find((item) => item.id === currentTabStopId);
|
|
90
|
-
const candidateItems = [activeItem, currentItem, ...items].filter(
|
|
91
|
-
Boolean
|
|
92
|
-
);
|
|
93
|
-
const candidateNodes = candidateItems.map((item) => item.ref.current);
|
|
94
|
-
focusFirst(candidateNodes);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
isClickFocusRef.current = false;
|
|
98
|
-
}),
|
|
99
|
-
onBlur: composeEventHandlers(
|
|
100
|
-
props.onBlur,
|
|
101
|
-
() => setIsTabbingBackOut(false)
|
|
102
|
-
)
|
|
103
|
-
}
|
|
104
|
-
)
|
|
105
|
-
}
|
|
106
|
-
);
|
|
107
|
-
});
|
|
108
|
-
const ITEM_NAME = "RovingFocusGroupItem";
|
|
109
|
-
const RovingFocusGroupItem = React.forwardRef((props, forwardedRef) => {
|
|
110
|
-
const {
|
|
111
|
-
__scopeRovingFocusGroup,
|
|
112
|
-
focusable = true,
|
|
113
|
-
active = false,
|
|
114
|
-
tabStopId,
|
|
115
|
-
...itemProps
|
|
116
|
-
} = props;
|
|
117
|
-
const autoId = React.useId();
|
|
118
|
-
const id = tabStopId || autoId;
|
|
119
|
-
const context = useRovingFocusContext(ITEM_NAME, __scopeRovingFocusGroup);
|
|
120
|
-
const isCurrentTabStop = context.currentTabStopId === id;
|
|
121
|
-
const getItems = useCollection(__scopeRovingFocusGroup);
|
|
122
|
-
const { onFocusableItemAdd, onFocusableItemRemove } = context;
|
|
123
|
-
React.useEffect(() => {
|
|
124
|
-
if (focusable) {
|
|
125
|
-
onFocusableItemAdd();
|
|
126
|
-
return () => onFocusableItemRemove();
|
|
127
|
-
}
|
|
128
|
-
}, [focusable, onFocusableItemAdd, onFocusableItemRemove]);
|
|
129
|
-
return /* @__PURE__ */ jsx(
|
|
130
|
-
Collection.ItemSlot,
|
|
131
|
-
{
|
|
132
|
-
scope: __scopeRovingFocusGroup,
|
|
133
|
-
id,
|
|
134
|
-
focusable,
|
|
135
|
-
active,
|
|
136
|
-
children: /* @__PURE__ */ jsx(
|
|
137
|
-
Stack,
|
|
138
|
-
{
|
|
139
|
-
tabIndex: isCurrentTabStop ? 0 : -1,
|
|
140
|
-
"data-orientation": context.orientation,
|
|
141
|
-
...itemProps,
|
|
142
|
-
ref: forwardedRef,
|
|
143
|
-
onMouseDown: composeEventHandlers(props.onMouseDown, (event) => {
|
|
144
|
-
if (!focusable)
|
|
145
|
-
event.preventDefault();
|
|
146
|
-
else
|
|
147
|
-
context.onItemFocus(id);
|
|
148
|
-
}),
|
|
149
|
-
onFocus: composeEventHandlers(props.onFocus, () => context.onItemFocus(id)),
|
|
150
|
-
...isWeb && {
|
|
151
|
-
onKeyDown: composeEventHandlers(
|
|
152
|
-
props.onKeyDown,
|
|
153
|
-
(event) => {
|
|
154
|
-
if (event.key === "Tab" && event.shiftKey) {
|
|
155
|
-
context.onItemShiftTab();
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
if (event.target !== event.currentTarget)
|
|
159
|
-
return;
|
|
160
|
-
const focusIntent = getFocusIntent(event, context.orientation, context.dir);
|
|
161
|
-
if (focusIntent !== void 0) {
|
|
162
|
-
event.preventDefault();
|
|
163
|
-
const items = getItems().filter((item) => item.focusable);
|
|
164
|
-
let candidateNodes = items.map((item) => item.ref.current);
|
|
165
|
-
if (focusIntent === "last")
|
|
166
|
-
candidateNodes.reverse();
|
|
167
|
-
else if (focusIntent === "prev" || focusIntent === "next") {
|
|
168
|
-
if (focusIntent === "prev")
|
|
169
|
-
candidateNodes.reverse();
|
|
170
|
-
const currentIndex = candidateNodes.indexOf(event.currentTarget);
|
|
171
|
-
candidateNodes = context.loop ? wrapArray(candidateNodes, currentIndex + 1) : candidateNodes.slice(currentIndex + 1);
|
|
172
|
-
}
|
|
173
|
-
setTimeout(() => focusFirst(candidateNodes));
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
)
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
)
|
|
180
|
-
}
|
|
181
|
-
);
|
|
182
|
-
});
|
|
183
|
-
RovingFocusGroupItem.displayName = ITEM_NAME;
|
|
184
|
-
const GROUP_NAME = "RovingFocusGroup";
|
|
185
|
-
const [Collection, useCollection, createCollectionScope] = createCollection(GROUP_NAME);
|
|
186
|
-
const [createRovingFocusGroupContext, createRovingFocusGroupScope] = createContextScope(
|
|
187
|
-
GROUP_NAME,
|
|
188
|
-
[createCollectionScope]
|
|
189
|
-
);
|
|
190
|
-
const [RovingFocusProvider, useRovingFocusContext] = createRovingFocusGroupContext(GROUP_NAME);
|
|
191
|
-
const RovingFocusGroup = withStaticProperties(
|
|
192
|
-
React.forwardRef(
|
|
193
|
-
(props, forwardedRef) => {
|
|
194
|
-
return /* @__PURE__ */ jsx(Collection.Provider, { scope: props.__scopeRovingFocusGroup, children: /* @__PURE__ */ jsx(Collection.Slot, { scope: props.__scopeRovingFocusGroup, children: /* @__PURE__ */ jsx(RovingFocusGroupImpl, { ...props, ref: forwardedRef }) }) });
|
|
195
|
-
}
|
|
196
|
-
),
|
|
197
|
-
{
|
|
198
|
-
Item: RovingFocusGroupItem
|
|
199
|
-
}
|
|
200
|
-
);
|
|
201
|
-
RovingFocusGroup.displayName = GROUP_NAME;
|
|
202
|
-
const MAP_KEY_TO_FOCUS_INTENT = {
|
|
203
|
-
ArrowLeft: "prev",
|
|
204
|
-
ArrowUp: "prev",
|
|
205
|
-
ArrowRight: "next",
|
|
206
|
-
ArrowDown: "next",
|
|
207
|
-
PageUp: "first",
|
|
208
|
-
Home: "first",
|
|
209
|
-
PageDown: "last",
|
|
210
|
-
End: "last"
|
|
211
|
-
};
|
|
212
|
-
function getDirectionAwareKey(key, dir) {
|
|
213
|
-
if (dir !== "rtl")
|
|
214
|
-
return key;
|
|
215
|
-
return key === "ArrowLeft" ? "ArrowRight" : key === "ArrowRight" ? "ArrowLeft" : key;
|
|
216
|
-
}
|
|
217
|
-
function getFocusIntent(event, orientation, dir) {
|
|
218
|
-
const key = getDirectionAwareKey(event.key, dir);
|
|
219
|
-
if (orientation === "vertical" && ["ArrowLeft", "ArrowRight"].includes(key))
|
|
220
|
-
return void 0;
|
|
221
|
-
if (orientation === "horizontal" && ["ArrowUp", "ArrowDown"].includes(key))
|
|
222
|
-
return void 0;
|
|
223
|
-
return MAP_KEY_TO_FOCUS_INTENT[key];
|
|
224
|
-
}
|
|
225
|
-
function focusFirst(candidates) {
|
|
226
|
-
const PREVIOUSLY_FOCUSED_ELEMENT = document.activeElement;
|
|
227
|
-
for (const candidate of candidates) {
|
|
228
|
-
if (candidate === PREVIOUSLY_FOCUSED_ELEMENT)
|
|
229
|
-
return;
|
|
230
|
-
candidate.focus();
|
|
231
|
-
if (document.activeElement !== PREVIOUSLY_FOCUSED_ELEMENT)
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
function wrapArray(array, startIndex) {
|
|
236
|
-
return array.map((_, index) => array[(startIndex + index) % array.length]);
|
|
237
|
-
}
|
|
238
|
-
export {
|
|
239
|
-
RovingFocusGroup,
|
|
240
|
-
createRovingFocusGroupScope
|
|
241
|
-
};
|
|
242
|
-
//# sourceMappingURL=RovingFocusGroup.mjs.map
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Stack, withStaticProperties } from "@tamagui/core";
|
|
3
|
-
import React from "react";
|
|
4
|
-
const ITEM_NAME = "RovingFocusGroupItem";
|
|
5
|
-
const RovingFocusGroupItem = React.forwardRef(({ children, ...props }, _ref) => /* @__PURE__ */ jsx(Stack, { ...props, children }));
|
|
6
|
-
RovingFocusGroupItem.displayName = ITEM_NAME;
|
|
7
|
-
const GROUP_NAME = "RovingFocusGroup";
|
|
8
|
-
const RovingFocusGroup = withStaticProperties(
|
|
9
|
-
React.forwardRef(({ children, ...props }, _ref) => /* @__PURE__ */ jsx(Stack, { ...props, children })),
|
|
10
|
-
{
|
|
11
|
-
Item: RovingFocusGroupItem
|
|
12
|
-
}
|
|
13
|
-
);
|
|
14
|
-
RovingFocusGroup.displayName = GROUP_NAME;
|
|
15
|
-
const createRovingFocusGroupScope = () => () => ({});
|
|
16
|
-
export {
|
|
17
|
-
RovingFocusGroup,
|
|
18
|
-
createRovingFocusGroupScope
|
|
19
|
-
};
|
|
20
|
-
//# sourceMappingURL=RovingFocusGroup.native.mjs.map
|
package/dist/esm/index.mjs
DELETED