@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,266 @@
|
|
|
1
|
+
// Ported from @radix-ui/react-focus-scope. Traps and/or loops Tab focus within its
|
|
2
|
+
// subtree, autofocuses the first tabbable on mount and restores focus on unmount (both
|
|
3
|
+
// preventable via the `focusScope.autoFocusOnMount`/`autoFocusOnUnmount` custom events),
|
|
4
|
+
// and pauses lower scopes via a module-level stack. Pure DOM + octane hooks; React's
|
|
5
|
+
// `useCallbackRef` → octane `useEffectEvent`.
|
|
6
|
+
import { createElement, useCallback, useEffect, useEffectEvent, useRef, useState } from 'octane';
|
|
7
|
+
|
|
8
|
+
import { useComposedRefs } from './compose-refs';
|
|
9
|
+
import { S, subSlot } from './internal';
|
|
10
|
+
import { Primitive } from './Primitive';
|
|
11
|
+
|
|
12
|
+
const AUTOFOCUS_ON_MOUNT = 'focusScope.autoFocusOnMount';
|
|
13
|
+
const AUTOFOCUS_ON_UNMOUNT = 'focusScope.autoFocusOnUnmount';
|
|
14
|
+
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
15
|
+
|
|
16
|
+
interface FocusScopeAPI {
|
|
17
|
+
paused: boolean;
|
|
18
|
+
pause(): void;
|
|
19
|
+
resume(): void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function FocusScope(props: any): any {
|
|
23
|
+
const slot = S('FocusScope');
|
|
24
|
+
const {
|
|
25
|
+
loop = false,
|
|
26
|
+
trapped = false,
|
|
27
|
+
onMountAutoFocus: onMountAutoFocusProp,
|
|
28
|
+
onUnmountAutoFocus: onUnmountAutoFocusProp,
|
|
29
|
+
ref: forwardedRef,
|
|
30
|
+
...scopeProps
|
|
31
|
+
} = props ?? {};
|
|
32
|
+
const [container, setContainer] = useState<HTMLElement | null>(null, subSlot(slot, 'node'));
|
|
33
|
+
const onMountAutoFocus = useEffectEvent(
|
|
34
|
+
onMountAutoFocusProp ?? (() => {}),
|
|
35
|
+
subSlot(slot, 'mountCb'),
|
|
36
|
+
);
|
|
37
|
+
const onUnmountAutoFocus = useEffectEvent(
|
|
38
|
+
onUnmountAutoFocusProp ?? (() => {}),
|
|
39
|
+
subSlot(slot, 'unmountCb'),
|
|
40
|
+
);
|
|
41
|
+
const lastFocusedElementRef = useRef<HTMLElement | null>(null, subSlot(slot, 'last'));
|
|
42
|
+
const composedRefs = useComposedRefs(forwardedRef, setContainer, subSlot(slot, 'refs'));
|
|
43
|
+
|
|
44
|
+
const focusScope = useRef<FocusScopeAPI>(
|
|
45
|
+
{
|
|
46
|
+
paused: false,
|
|
47
|
+
pause() {
|
|
48
|
+
this.paused = true;
|
|
49
|
+
},
|
|
50
|
+
resume() {
|
|
51
|
+
this.paused = false;
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
subSlot(slot, 'api'),
|
|
55
|
+
).current;
|
|
56
|
+
|
|
57
|
+
// Focus containment: keep focus inside `container` while trapped.
|
|
58
|
+
useEffect(
|
|
59
|
+
() => {
|
|
60
|
+
if (!trapped) return;
|
|
61
|
+
function handleFocusIn(event: FocusEvent): void {
|
|
62
|
+
if (focusScope.paused || !container) return;
|
|
63
|
+
const target = event.target as HTMLElement | null;
|
|
64
|
+
if (container.contains(target)) {
|
|
65
|
+
lastFocusedElementRef.current = target;
|
|
66
|
+
} else {
|
|
67
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function handleFocusOut(event: FocusEvent): void {
|
|
71
|
+
if (focusScope.paused || !container) return;
|
|
72
|
+
const relatedTarget = event.relatedTarget as HTMLElement | null;
|
|
73
|
+
if (relatedTarget === null) return;
|
|
74
|
+
if (!container.contains(relatedTarget)) {
|
|
75
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
// When the focused element gets removed from the DOM, move focus back to the
|
|
79
|
+
// container (browsers move it to body).
|
|
80
|
+
function handleMutations(mutations: MutationRecord[]): void {
|
|
81
|
+
const focusedElement = document.activeElement;
|
|
82
|
+
if (focusedElement !== document.body) return;
|
|
83
|
+
for (const mutation of mutations) {
|
|
84
|
+
if (mutation.removedNodes.length > 0) focus(container);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
document.addEventListener('focusin', handleFocusIn);
|
|
88
|
+
document.addEventListener('focusout', handleFocusOut);
|
|
89
|
+
const mutationObserver = new MutationObserver(handleMutations);
|
|
90
|
+
if (container) mutationObserver.observe(container, { childList: true, subtree: true });
|
|
91
|
+
return () => {
|
|
92
|
+
document.removeEventListener('focusin', handleFocusIn);
|
|
93
|
+
document.removeEventListener('focusout', handleFocusOut);
|
|
94
|
+
mutationObserver.disconnect();
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
[trapped, container],
|
|
98
|
+
subSlot(slot, 'e:trap'),
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
// Mount/unmount autofocus + scope-stack registration.
|
|
102
|
+
useEffect(
|
|
103
|
+
() => {
|
|
104
|
+
if (!container) return;
|
|
105
|
+
focusScopesStack.add(focusScope);
|
|
106
|
+
const previouslyFocusedElement = document.activeElement as HTMLElement | null;
|
|
107
|
+
const hasFocusedCandidate = container.contains(previouslyFocusedElement);
|
|
108
|
+
if (!hasFocusedCandidate) {
|
|
109
|
+
const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
|
|
110
|
+
container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
111
|
+
container.dispatchEvent(mountEvent);
|
|
112
|
+
if (!mountEvent.defaultPrevented) {
|
|
113
|
+
focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
|
|
114
|
+
if (document.activeElement === previouslyFocusedElement) {
|
|
115
|
+
focus(container);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return () => {
|
|
120
|
+
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
121
|
+
setTimeout(() => {
|
|
122
|
+
const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
|
|
123
|
+
container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
124
|
+
container.dispatchEvent(unmountEvent);
|
|
125
|
+
if (!unmountEvent.defaultPrevented) {
|
|
126
|
+
focus(previouslyFocusedElement ?? document.body, { select: true });
|
|
127
|
+
}
|
|
128
|
+
container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
129
|
+
focusScopesStack.remove(focusScope);
|
|
130
|
+
}, 0);
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
[container],
|
|
134
|
+
subSlot(slot, 'e:mount'),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
const handleKeyDown = useCallback(
|
|
138
|
+
(event: KeyboardEvent) => {
|
|
139
|
+
if (!loop && !trapped) return;
|
|
140
|
+
if (focusScope.paused) return;
|
|
141
|
+
const isTabKey = event.key === 'Tab' && !event.altKey && !event.ctrlKey && !event.metaKey;
|
|
142
|
+
const focusedElement = document.activeElement as HTMLElement | null;
|
|
143
|
+
if (isTabKey && focusedElement) {
|
|
144
|
+
const scopeContainer = event.currentTarget as HTMLElement;
|
|
145
|
+
const [first, last] = getTabbableEdges(scopeContainer);
|
|
146
|
+
const hasTabbableElementsInside = first && last;
|
|
147
|
+
if (!hasTabbableElementsInside) {
|
|
148
|
+
if (focusedElement === scopeContainer) event.preventDefault();
|
|
149
|
+
} else {
|
|
150
|
+
if (!event.shiftKey && focusedElement === last) {
|
|
151
|
+
event.preventDefault();
|
|
152
|
+
if (loop) focus(first, { select: true });
|
|
153
|
+
} else if (event.shiftKey && focusedElement === first) {
|
|
154
|
+
event.preventDefault();
|
|
155
|
+
if (loop) focus(last, { select: true });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
[loop, trapped],
|
|
161
|
+
subSlot(slot, 'keydown'),
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
return createElement(Primitive.div, {
|
|
165
|
+
tabIndex: -1,
|
|
166
|
+
...scopeProps,
|
|
167
|
+
ref: composedRefs,
|
|
168
|
+
onKeyDown: handleKeyDown,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export { FocusScope as Root };
|
|
173
|
+
|
|
174
|
+
function focusFirst(candidates: HTMLElement[], { select = false } = {}): void {
|
|
175
|
+
const previouslyFocusedElement = document.activeElement;
|
|
176
|
+
for (const candidate of candidates) {
|
|
177
|
+
focus(candidate, { select });
|
|
178
|
+
if (document.activeElement !== previouslyFocusedElement) return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function getTabbableEdges(
|
|
183
|
+
container: HTMLElement,
|
|
184
|
+
): [HTMLElement | undefined, HTMLElement | undefined] {
|
|
185
|
+
const candidates = getTabbableCandidates(container);
|
|
186
|
+
const first = findVisible(candidates, container);
|
|
187
|
+
const last = findVisible(candidates.reverse(), container);
|
|
188
|
+
return [first, last];
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function getTabbableCandidates(container: HTMLElement): HTMLElement[] {
|
|
192
|
+
const nodes: HTMLElement[] = [];
|
|
193
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
|
|
194
|
+
acceptNode: (node: any) => {
|
|
195
|
+
const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden';
|
|
196
|
+
if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;
|
|
197
|
+
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
|
|
198
|
+
},
|
|
199
|
+
});
|
|
200
|
+
while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement);
|
|
201
|
+
return nodes;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function findVisible(elements: HTMLElement[], container: HTMLElement): HTMLElement | undefined {
|
|
205
|
+
for (const element of elements) {
|
|
206
|
+
if (!isHidden(element, { upTo: container })) return element;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function isHidden(node: HTMLElement | null, { upTo }: { upTo?: HTMLElement }): boolean {
|
|
211
|
+
if (!node) return false;
|
|
212
|
+
if (getComputedStyle(node).visibility === 'hidden') return true;
|
|
213
|
+
while (node) {
|
|
214
|
+
if (upTo !== undefined && node === upTo) return false;
|
|
215
|
+
if (getComputedStyle(node).display === 'none') return true;
|
|
216
|
+
node = node.parentElement;
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function isSelectableInput(element: any): element is HTMLInputElement {
|
|
222
|
+
return element instanceof HTMLInputElement && 'select' in element;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function focus(element: HTMLElement | null, { select = false } = {}): void {
|
|
226
|
+
if (element && element.focus) {
|
|
227
|
+
const previouslyFocusedElement = document.activeElement;
|
|
228
|
+
element.focus({ preventScroll: true });
|
|
229
|
+
if (element !== previouslyFocusedElement && isSelectableInput(element) && select) {
|
|
230
|
+
element.select();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const focusScopesStack = createFocusScopesStack();
|
|
236
|
+
function createFocusScopesStack(): {
|
|
237
|
+
add(focusScope: FocusScopeAPI): void;
|
|
238
|
+
remove(focusScope: FocusScopeAPI): void;
|
|
239
|
+
} {
|
|
240
|
+
let stack: FocusScopeAPI[] = [];
|
|
241
|
+
return {
|
|
242
|
+
add(focusScope) {
|
|
243
|
+
const activeFocusScope = stack[0];
|
|
244
|
+
if (focusScope !== activeFocusScope) {
|
|
245
|
+
activeFocusScope?.pause();
|
|
246
|
+
}
|
|
247
|
+
stack = arrayRemove(stack, focusScope);
|
|
248
|
+
stack.unshift(focusScope);
|
|
249
|
+
},
|
|
250
|
+
remove(focusScope) {
|
|
251
|
+
stack = arrayRemove(stack, focusScope);
|
|
252
|
+
stack[0]?.resume();
|
|
253
|
+
},
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function arrayRemove<T>(array: T[], item: T): T[] {
|
|
258
|
+
const updatedArray = [...array];
|
|
259
|
+
const index = updatedArray.indexOf(item);
|
|
260
|
+
if (index !== -1) updatedArray.splice(index, 1);
|
|
261
|
+
return updatedArray;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function removeLinks(items: HTMLElement[]): HTMLElement[] {
|
|
265
|
+
return items.filter((item) => item.tagName !== 'A');
|
|
266
|
+
}
|