@octanejs/radix 0.1.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.
- package/LICENSE +21 -0
- package/README.md +100 -0
- package/package.json +45 -0
- package/src/AccessibleIcon.ts +26 -0
- package/src/Accordion.ts +282 -0
- package/src/AlertDialog.ts +110 -0
- package/src/Arrow.ts +21 -0
- package/src/AspectRatio.ts +34 -0
- package/src/Avatar.ts +152 -0
- package/src/Checkbox.ts +325 -0
- package/src/Collapsible.ts +170 -0
- package/src/ContextMenu.ts +303 -0
- package/src/Dialog.ts +308 -0
- package/src/DismissableLayer.ts +413 -0
- package/src/DropdownMenu.ts +275 -0
- package/src/FocusScope.ts +266 -0
- package/src/Form.ts +621 -0
- package/src/HoverCard.ts +318 -0
- package/src/Label.ts +25 -0
- package/src/Menu.ts +1057 -0
- package/src/Menubar.ts +572 -0
- package/src/NavigationMenu.ts +1236 -0
- package/src/OneTimePasswordField.ts +977 -0
- package/src/PasswordToggleField.ts +495 -0
- package/src/Popover.ts +354 -0
- package/src/Popper.ts +401 -0
- package/src/Portal.ts +19 -0
- package/src/Presence.ts +225 -0
- package/src/Primitive.ts +53 -0
- package/src/Progress.ts +92 -0
- package/src/Radio.ts +262 -0
- package/src/RadioGroup.ts +212 -0
- package/src/RovingFocusGroup.ts +259 -0
- package/src/ScrollArea.ts +966 -0
- package/src/Select.ts +1901 -0
- package/src/Separator.ts +36 -0
- package/src/Slider.ts +745 -0
- package/src/Slot.ts +105 -0
- package/src/Switch.ts +278 -0
- package/src/Tabs.ts +177 -0
- package/src/Toast.ts +923 -0
- package/src/Toggle.ts +31 -0
- package/src/ToggleGroup.ts +157 -0
- package/src/Toolbar.ts +130 -0
- package/src/Tooltip.ts +669 -0
- package/src/VisuallyHidden.ts +29 -0
- package/src/collection.ts +97 -0
- package/src/compose-event-handlers.ts +15 -0
- package/src/compose-refs.ts +53 -0
- package/src/context.ts +109 -0
- package/src/direction.ts +19 -0
- package/src/focus-guards.ts +56 -0
- package/src/index.ts +54 -0
- package/src/internal.ts +42 -0
- package/src/scroll-lock.ts +57 -0
- package/src/use-callback-ref.ts +28 -0
- package/src/use-effect-event.ts +36 -0
- package/src/use-is-hydrated.ts +23 -0
- package/src/use-previous.ts +28 -0
- package/src/use-size.ts +57 -0
- package/src/useControllableState.ts +78 -0
- package/src/useId.ts +15 -0
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// Ported from @radix-ui/react-roving-focus. A single-tab-stop group: one item holds
|
|
2
|
+
// `tabIndex=0` (the roving stop) and arrow keys move focus between items per
|
|
3
|
+
// orientation/direction, with optional looping; tabbing into the group enters at the
|
|
4
|
+
// active/current item (the `rovingFocusGroup.onEntryFocus` custom event, preventable).
|
|
5
|
+
// Built on the Collection primitive; used by ToggleGroup / Tabs / Toolbar / RadioGroup.
|
|
6
|
+
import { createElement, useCallback, useEffect, useEffectEvent, useRef, useState } from 'octane';
|
|
7
|
+
|
|
8
|
+
import { createCollection } from './collection';
|
|
9
|
+
import { composeEventHandlers } from './compose-event-handlers';
|
|
10
|
+
import { useComposedRefs } from './compose-refs';
|
|
11
|
+
import { createContextScope } from './context';
|
|
12
|
+
import { S, subSlot } from './internal';
|
|
13
|
+
import { Primitive } from './Primitive';
|
|
14
|
+
import { useControllableState } from './useControllableState';
|
|
15
|
+
import { useId } from './useId';
|
|
16
|
+
|
|
17
|
+
const ENTRY_FOCUS = 'rovingFocusGroup.onEntryFocus';
|
|
18
|
+
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
19
|
+
|
|
20
|
+
const [Collection, useCollection, createCollectionScope] = createCollection('RovingFocusGroup');
|
|
21
|
+
export const [createRovingFocusGroupContext, createRovingFocusGroupScope] = createContextScope(
|
|
22
|
+
'RovingFocusGroup',
|
|
23
|
+
[createCollectionScope],
|
|
24
|
+
);
|
|
25
|
+
interface RovingContextValue {
|
|
26
|
+
orientation?: 'horizontal' | 'vertical';
|
|
27
|
+
dir: 'ltr' | 'rtl';
|
|
28
|
+
loop: boolean;
|
|
29
|
+
currentTabStopId: string | null;
|
|
30
|
+
onItemFocus: (tabStopId: string) => void;
|
|
31
|
+
onItemShiftTab: () => void;
|
|
32
|
+
onFocusableItemAdd: () => void;
|
|
33
|
+
onFocusableItemRemove: () => void;
|
|
34
|
+
}
|
|
35
|
+
const [RovingFocusProvider, useRovingFocusContext] =
|
|
36
|
+
createRovingFocusGroupContext<RovingContextValue>('RovingFocusGroup');
|
|
37
|
+
|
|
38
|
+
export function Root(props: any): any {
|
|
39
|
+
return createElement(Collection.Provider, {
|
|
40
|
+
scope: props?.__scopeRovingFocusGroup,
|
|
41
|
+
children: createElement(Collection.Slot, {
|
|
42
|
+
scope: props?.__scopeRovingFocusGroup,
|
|
43
|
+
children: createElement(RovingFocusGroupImpl, props),
|
|
44
|
+
}),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function RovingFocusGroupImpl(props: any): any {
|
|
49
|
+
const slot = S('RovingFocusGroupImpl');
|
|
50
|
+
const {
|
|
51
|
+
__scopeRovingFocusGroup,
|
|
52
|
+
orientation,
|
|
53
|
+
loop = false,
|
|
54
|
+
dir,
|
|
55
|
+
currentTabStopId: currentTabStopIdProp,
|
|
56
|
+
defaultCurrentTabStopId,
|
|
57
|
+
onCurrentTabStopIdChange,
|
|
58
|
+
onEntryFocus,
|
|
59
|
+
preventScrollOnEntryFocus = false,
|
|
60
|
+
ref: forwardedRef,
|
|
61
|
+
...groupProps
|
|
62
|
+
} = props ?? {};
|
|
63
|
+
const ref = useRef<HTMLElement | null>(null, subSlot(slot, 'ref'));
|
|
64
|
+
const composedRefs = useComposedRefs(forwardedRef, ref, subSlot(slot, 'refs'));
|
|
65
|
+
const direction: 'ltr' | 'rtl' = dir === 'rtl' ? 'rtl' : 'ltr';
|
|
66
|
+
const [currentTabStopId, setCurrentTabStopId] = useControllableState<string | null>(
|
|
67
|
+
{
|
|
68
|
+
prop: currentTabStopIdProp,
|
|
69
|
+
defaultProp: defaultCurrentTabStopId ?? null,
|
|
70
|
+
onChange: onCurrentTabStopIdChange,
|
|
71
|
+
},
|
|
72
|
+
subSlot(slot, 'stop'),
|
|
73
|
+
);
|
|
74
|
+
const [isTabbingBackOut, setIsTabbingBackOut] = useState(false, subSlot(slot, 'backout'));
|
|
75
|
+
const handleEntryFocus = useEffectEvent(onEntryFocus ?? (() => {}), subSlot(slot, 'entry'));
|
|
76
|
+
const getItems = useCollection(__scopeRovingFocusGroup, subSlot(slot, 'items'));
|
|
77
|
+
const isClickFocusRef = useRef(false, subSlot(slot, 'clickFocus'));
|
|
78
|
+
const [focusableItemsCount, setFocusableItemsCount] = useState(0, subSlot(slot, 'count'));
|
|
79
|
+
|
|
80
|
+
useEffect(
|
|
81
|
+
() => {
|
|
82
|
+
const node = ref.current;
|
|
83
|
+
if (node) {
|
|
84
|
+
node.addEventListener(ENTRY_FOCUS, handleEntryFocus);
|
|
85
|
+
return () => node.removeEventListener(ENTRY_FOCUS, handleEntryFocus);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
[],
|
|
89
|
+
subSlot(slot, 'e:entry'),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
return createElement(RovingFocusProvider, {
|
|
93
|
+
scope: __scopeRovingFocusGroup,
|
|
94
|
+
orientation,
|
|
95
|
+
dir: direction,
|
|
96
|
+
loop,
|
|
97
|
+
currentTabStopId,
|
|
98
|
+
onItemFocus: useCallback(
|
|
99
|
+
(tabStopId: string) => setCurrentTabStopId(tabStopId),
|
|
100
|
+
[setCurrentTabStopId],
|
|
101
|
+
subSlot(slot, 'onFocus'),
|
|
102
|
+
),
|
|
103
|
+
onItemShiftTab: useCallback(() => setIsTabbingBackOut(true), [], subSlot(slot, 'shiftTab')),
|
|
104
|
+
onFocusableItemAdd: useCallback(
|
|
105
|
+
() => setFocusableItemsCount((prev: number) => prev + 1),
|
|
106
|
+
[],
|
|
107
|
+
subSlot(slot, 'add'),
|
|
108
|
+
),
|
|
109
|
+
onFocusableItemRemove: useCallback(
|
|
110
|
+
() => setFocusableItemsCount((prev: number) => prev - 1),
|
|
111
|
+
[],
|
|
112
|
+
subSlot(slot, 'remove'),
|
|
113
|
+
),
|
|
114
|
+
children: createElement(Primitive.div, {
|
|
115
|
+
tabIndex: isTabbingBackOut || focusableItemsCount === 0 ? -1 : 0,
|
|
116
|
+
'data-orientation': orientation,
|
|
117
|
+
...groupProps,
|
|
118
|
+
ref: composedRefs,
|
|
119
|
+
style: { outline: 'none', ...props.style },
|
|
120
|
+
onMouseDown: composeEventHandlers(props.onMouseDown, () => {
|
|
121
|
+
isClickFocusRef.current = true;
|
|
122
|
+
}),
|
|
123
|
+
onFocus: composeEventHandlers(props.onFocus, (event: FocusEvent) => {
|
|
124
|
+
// Tabbing INTO the group (not clicking) enters at the best candidate item.
|
|
125
|
+
const isKeyboardFocus = !isClickFocusRef.current;
|
|
126
|
+
if (event.target === event.currentTarget && isKeyboardFocus && !isTabbingBackOut) {
|
|
127
|
+
const entryFocusEvent = new CustomEvent(ENTRY_FOCUS, EVENT_OPTIONS);
|
|
128
|
+
(event.currentTarget as HTMLElement).dispatchEvent(entryFocusEvent);
|
|
129
|
+
if (!entryFocusEvent.defaultPrevented) {
|
|
130
|
+
const items = getItems().filter((item: any) => item.focusable);
|
|
131
|
+
const activeItem = items.find((item: any) => item.active);
|
|
132
|
+
const currentItem = items.find((item: any) => item.id === currentTabStopId);
|
|
133
|
+
const candidateItems = [activeItem, currentItem, ...items].filter(Boolean);
|
|
134
|
+
const candidateNodes = candidateItems.map((item: any) => item.ref.current);
|
|
135
|
+
focusFirst(candidateNodes, preventScrollOnEntryFocus);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
isClickFocusRef.current = false;
|
|
139
|
+
}),
|
|
140
|
+
onBlur: composeEventHandlers(props.onBlur, () => setIsTabbingBackOut(false)),
|
|
141
|
+
}),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function Item(props: any): any {
|
|
146
|
+
const slot = S('RovingFocusGroupItem');
|
|
147
|
+
const {
|
|
148
|
+
__scopeRovingFocusGroup,
|
|
149
|
+
focusable = true,
|
|
150
|
+
active = false,
|
|
151
|
+
tabStopId,
|
|
152
|
+
children,
|
|
153
|
+
...itemProps
|
|
154
|
+
} = props ?? {};
|
|
155
|
+
const autoId = useId(subSlot(slot, 'id'));
|
|
156
|
+
const id = tabStopId || autoId;
|
|
157
|
+
const context = useRovingFocusContext('RovingFocusGroupItem', __scopeRovingFocusGroup);
|
|
158
|
+
const isCurrentTabStop = context.currentTabStopId === id;
|
|
159
|
+
const getItems = useCollection(__scopeRovingFocusGroup, subSlot(slot, 'items'));
|
|
160
|
+
const { onFocusableItemAdd, onFocusableItemRemove, currentTabStopId } = context;
|
|
161
|
+
|
|
162
|
+
useEffect(
|
|
163
|
+
() => {
|
|
164
|
+
if (focusable) {
|
|
165
|
+
onFocusableItemAdd();
|
|
166
|
+
return () => onFocusableItemRemove();
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
[focusable, onFocusableItemAdd, onFocusableItemRemove],
|
|
170
|
+
subSlot(slot, 'e:count'),
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
return createElement(Collection.ItemSlot, {
|
|
174
|
+
scope: __scopeRovingFocusGroup,
|
|
175
|
+
id,
|
|
176
|
+
focusable,
|
|
177
|
+
active,
|
|
178
|
+
children: createElement(Primitive.span, {
|
|
179
|
+
tabIndex: isCurrentTabStop ? 0 : -1,
|
|
180
|
+
'data-orientation': context.orientation,
|
|
181
|
+
...itemProps,
|
|
182
|
+
onMouseDown: composeEventHandlers(props.onMouseDown, (event: MouseEvent) => {
|
|
183
|
+
// Prevent focusing non-focusable items on pointer down.
|
|
184
|
+
if (!focusable) event.preventDefault();
|
|
185
|
+
else context.onItemFocus(id);
|
|
186
|
+
}),
|
|
187
|
+
onFocus: composeEventHandlers(props.onFocus, () => context.onItemFocus(id)),
|
|
188
|
+
onKeyDown: composeEventHandlers(props.onKeyDown, (event: KeyboardEvent) => {
|
|
189
|
+
if (event.key === 'Tab' && event.shiftKey) {
|
|
190
|
+
context.onItemShiftTab();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (event.target !== event.currentTarget) return;
|
|
194
|
+
const focusIntent = getFocusIntent(event, context.orientation, context.dir);
|
|
195
|
+
if (focusIntent !== undefined) {
|
|
196
|
+
if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) return;
|
|
197
|
+
event.preventDefault();
|
|
198
|
+
const items = getItems().filter((item: any) => item.focusable);
|
|
199
|
+
let candidateNodes = items.map((item: any) => item.ref.current) as HTMLElement[];
|
|
200
|
+
if (focusIntent === 'last') candidateNodes.reverse();
|
|
201
|
+
else if (focusIntent === 'prev' || focusIntent === 'next') {
|
|
202
|
+
if (focusIntent === 'prev') candidateNodes.reverse();
|
|
203
|
+
const currentIndex = candidateNodes.indexOf(event.currentTarget as HTMLElement);
|
|
204
|
+
candidateNodes = context.loop
|
|
205
|
+
? wrapArray(candidateNodes, currentIndex + 1)
|
|
206
|
+
: candidateNodes.slice(currentIndex + 1);
|
|
207
|
+
}
|
|
208
|
+
setTimeout(() => focusFirst(candidateNodes));
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
children:
|
|
212
|
+
typeof children === 'function'
|
|
213
|
+
? children({ isCurrentTabStop, hasTabStop: currentTabStopId != null })
|
|
214
|
+
: children,
|
|
215
|
+
}),
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const MAP_KEY_TO_FOCUS_INTENT: Record<string, 'first' | 'last' | 'prev' | 'next'> = {
|
|
220
|
+
ArrowLeft: 'prev',
|
|
221
|
+
ArrowUp: 'prev',
|
|
222
|
+
ArrowRight: 'next',
|
|
223
|
+
ArrowDown: 'next',
|
|
224
|
+
PageUp: 'first',
|
|
225
|
+
Home: 'first',
|
|
226
|
+
PageDown: 'last',
|
|
227
|
+
End: 'last',
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
function getDirectionAwareKey(key: string, dir?: string): string {
|
|
231
|
+
if (dir !== 'rtl') return key;
|
|
232
|
+
return key === 'ArrowLeft' ? 'ArrowRight' : key === 'ArrowRight' ? 'ArrowLeft' : key;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function getFocusIntent(
|
|
236
|
+
event: KeyboardEvent,
|
|
237
|
+
orientation?: string,
|
|
238
|
+
dir?: string,
|
|
239
|
+
): 'first' | 'last' | 'prev' | 'next' | undefined {
|
|
240
|
+
const key = getDirectionAwareKey(event.key, dir);
|
|
241
|
+
if (orientation === 'vertical' && ['ArrowLeft', 'ArrowRight'].includes(key)) return undefined;
|
|
242
|
+
if (orientation === 'horizontal' && ['ArrowUp', 'ArrowDown'].includes(key)) return undefined;
|
|
243
|
+
return MAP_KEY_TO_FOCUS_INTENT[key];
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function focusFirst(candidates: HTMLElement[], preventScroll = false): void {
|
|
247
|
+
const PREVIOUSLY_FOCUSED_ELEMENT = document.activeElement;
|
|
248
|
+
for (const candidate of candidates) {
|
|
249
|
+
if (candidate === PREVIOUSLY_FOCUSED_ELEMENT) return;
|
|
250
|
+
candidate.focus({ preventScroll });
|
|
251
|
+
if (document.activeElement !== PREVIOUSLY_FOCUSED_ELEMENT) return;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function wrapArray<T>(array: T[], startIndex: number): T[] {
|
|
256
|
+
return array.map((_, index) => array[(startIndex + index) % array.length]);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export { Root as RovingFocusGroup, Item as RovingFocusGroupItem };
|