@octanejs/radix 0.1.32 → 0.1.33
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/dist/cjs/AccessibleIcon.cjs +56 -0
- package/dist/cjs/Accordion.cjs +272 -0
- package/dist/cjs/AlertDialog.cjs +144 -0
- package/dist/cjs/Arrow.cjs +43 -0
- package/dist/cjs/AspectRatio.cjs +56 -0
- package/dist/cjs/Avatar.cjs +150 -0
- package/dist/cjs/Checkbox.cjs +281 -0
- package/dist/cjs/Collapsible.cjs +176 -0
- package/dist/cjs/ContextMenu.cjs +315 -0
- package/dist/cjs/Dialog.cjs +291 -0
- package/dist/cjs/DismissableLayer.cjs +382 -0
- package/dist/cjs/DropdownMenu.cjs +296 -0
- package/dist/cjs/FocusScope.cjs +257 -0
- package/dist/cjs/Form.cjs +537 -0
- package/dist/cjs/HoverCard.cjs +309 -0
- package/dist/cjs/Label.cjs +42 -0
- package/dist/cjs/Menu.cjs +925 -0
- package/dist/cjs/Menubar.cjs +475 -0
- package/dist/cjs/NavigationMenu.cjs +942 -0
- package/dist/cjs/OneTimePasswordField.cjs +729 -0
- package/dist/cjs/PasswordToggleField.cjs +377 -0
- package/dist/cjs/Popover.cjs +340 -0
- package/dist/cjs/Popper.cjs +368 -0
- package/dist/cjs/Portal.cjs +40 -0
- package/dist/cjs/Presence.cjs +183 -0
- package/dist/cjs/Primitive.cjs +63 -0
- package/dist/cjs/Progress.cjs +111 -0
- package/dist/cjs/Radio.cjs +240 -0
- package/dist/cjs/RadioGroup.cjs +216 -0
- package/dist/cjs/RovingFocusGroup.cjs +253 -0
- package/dist/cjs/ScrollArea.cjs +828 -0
- package/dist/cjs/Select.cjs +1429 -0
- package/dist/cjs/Separator.cjs +51 -0
- package/dist/cjs/Slider.cjs +634 -0
- package/dist/cjs/Slot.cjs +97 -0
- package/dist/cjs/Switch.cjs +253 -0
- package/dist/cjs/Tabs.cjs +195 -0
- package/dist/cjs/Toast.cjs +733 -0
- package/dist/cjs/Toggle.cjs +54 -0
- package/dist/cjs/ToggleGroup.cjs +174 -0
- package/dist/cjs/Toolbar.cjs +165 -0
- package/dist/cjs/Tooltip.cjs +628 -0
- package/dist/cjs/VisuallyHidden.cjs +52 -0
- package/dist/cjs/collection.cjs +95 -0
- package/dist/cjs/compose-event-handlers.cjs +35 -0
- package/dist/cjs/compose-refs.cjs +68 -0
- package/dist/cjs/context.cjs +96 -0
- package/dist/cjs/direction.cjs +41 -0
- package/dist/cjs/focus-guards.cjs +76 -0
- package/dist/cjs/index.cjs +174 -0
- package/dist/cjs/internal.cjs +51 -0
- package/dist/cjs/scroll-lock.cjs +67 -0
- package/dist/cjs/use-callback-ref.cjs +47 -0
- package/dist/cjs/use-effect-event.cjs +52 -0
- package/dist/cjs/use-is-hydrated.cjs +43 -0
- package/dist/cjs/use-previous.cjs +47 -0
- package/dist/cjs/use-size.cjs +69 -0
- package/dist/cjs/useControllableState.cjs +80 -0
- package/dist/cjs/useId.cjs +36 -0
- package/package.json +14 -7
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var FocusScope_exports = {};
|
|
20
|
+
__export(FocusScope_exports, {
|
|
21
|
+
FocusScope: () => FocusScope,
|
|
22
|
+
Root: () => FocusScope
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(FocusScope_exports);
|
|
25
|
+
var import_octane = require("octane");
|
|
26
|
+
var import_compose_refs = require("./compose-refs.cjs");
|
|
27
|
+
var import_internal = require("./internal.cjs");
|
|
28
|
+
var import_Primitive = require("./Primitive.cjs");
|
|
29
|
+
const AUTOFOCUS_ON_MOUNT = "focusScope.autoFocusOnMount";
|
|
30
|
+
const AUTOFOCUS_ON_UNMOUNT = "focusScope.autoFocusOnUnmount";
|
|
31
|
+
const EVENT_OPTIONS = { bubbles: false, cancelable: true };
|
|
32
|
+
function FocusScope(props) {
|
|
33
|
+
const slot = (0, import_internal.S)("FocusScope");
|
|
34
|
+
const {
|
|
35
|
+
loop = false,
|
|
36
|
+
trapped = false,
|
|
37
|
+
onMountAutoFocus: onMountAutoFocusProp,
|
|
38
|
+
onUnmountAutoFocus: onUnmountAutoFocusProp,
|
|
39
|
+
ref: forwardedRef,
|
|
40
|
+
...scopeProps
|
|
41
|
+
} = props ?? {};
|
|
42
|
+
const [container, setContainer] = (0, import_octane.useState)(null, (0, import_internal.subSlot)(slot, "node"));
|
|
43
|
+
const onMountAutoFocus = (0, import_octane.useEffectEvent)(
|
|
44
|
+
onMountAutoFocusProp ?? (() => {
|
|
45
|
+
}),
|
|
46
|
+
(0, import_internal.subSlot)(slot, "mountCb")
|
|
47
|
+
);
|
|
48
|
+
const onUnmountAutoFocus = (0, import_octane.useEffectEvent)(
|
|
49
|
+
onUnmountAutoFocusProp ?? (() => {
|
|
50
|
+
}),
|
|
51
|
+
(0, import_internal.subSlot)(slot, "unmountCb")
|
|
52
|
+
);
|
|
53
|
+
const lastFocusedElementRef = (0, import_octane.useRef)(null, (0, import_internal.subSlot)(slot, "last"));
|
|
54
|
+
const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, setContainer, (0, import_internal.subSlot)(slot, "refs"));
|
|
55
|
+
const focusScope = (0, import_octane.useRef)(
|
|
56
|
+
{
|
|
57
|
+
paused: false,
|
|
58
|
+
pause() {
|
|
59
|
+
this.paused = true;
|
|
60
|
+
},
|
|
61
|
+
resume() {
|
|
62
|
+
this.paused = false;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
(0, import_internal.subSlot)(slot, "api")
|
|
66
|
+
).current;
|
|
67
|
+
(0, import_octane.useEffect)(
|
|
68
|
+
() => {
|
|
69
|
+
if (!trapped) return;
|
|
70
|
+
function handleFocusIn(event) {
|
|
71
|
+
if (focusScope.paused || !container) return;
|
|
72
|
+
const target = event.target;
|
|
73
|
+
if (container.contains(target)) {
|
|
74
|
+
lastFocusedElementRef.current = target;
|
|
75
|
+
} else {
|
|
76
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
function handleFocusOut(event) {
|
|
80
|
+
if (focusScope.paused || !container) return;
|
|
81
|
+
const relatedTarget = event.relatedTarget;
|
|
82
|
+
if (relatedTarget === null) return;
|
|
83
|
+
if (!container.contains(relatedTarget)) {
|
|
84
|
+
focus(lastFocusedElementRef.current, { select: true });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function handleMutations(mutations) {
|
|
88
|
+
const focusedElement = document.activeElement;
|
|
89
|
+
if (focusedElement !== document.body) return;
|
|
90
|
+
for (const mutation of mutations) {
|
|
91
|
+
if (mutation.removedNodes.length > 0) focus(container);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
document.addEventListener("focusin", handleFocusIn);
|
|
95
|
+
document.addEventListener("focusout", handleFocusOut);
|
|
96
|
+
const mutationObserver = new MutationObserver(handleMutations);
|
|
97
|
+
if (container) mutationObserver.observe(container, { childList: true, subtree: true });
|
|
98
|
+
return () => {
|
|
99
|
+
document.removeEventListener("focusin", handleFocusIn);
|
|
100
|
+
document.removeEventListener("focusout", handleFocusOut);
|
|
101
|
+
mutationObserver.disconnect();
|
|
102
|
+
};
|
|
103
|
+
},
|
|
104
|
+
[trapped, container],
|
|
105
|
+
(0, import_internal.subSlot)(slot, "e:trap")
|
|
106
|
+
);
|
|
107
|
+
(0, import_octane.useEffect)(
|
|
108
|
+
() => {
|
|
109
|
+
if (!container) return;
|
|
110
|
+
focusScopesStack.add(focusScope);
|
|
111
|
+
const previouslyFocusedElement = document.activeElement;
|
|
112
|
+
const hasFocusedCandidate = container.contains(previouslyFocusedElement);
|
|
113
|
+
if (!hasFocusedCandidate) {
|
|
114
|
+
const mountEvent = new CustomEvent(AUTOFOCUS_ON_MOUNT, EVENT_OPTIONS);
|
|
115
|
+
container.addEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
116
|
+
container.dispatchEvent(mountEvent);
|
|
117
|
+
if (!mountEvent.defaultPrevented) {
|
|
118
|
+
focusFirst(removeLinks(getTabbableCandidates(container)), { select: true });
|
|
119
|
+
if (document.activeElement === previouslyFocusedElement) {
|
|
120
|
+
focus(container);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return () => {
|
|
125
|
+
container.removeEventListener(AUTOFOCUS_ON_MOUNT, onMountAutoFocus);
|
|
126
|
+
setTimeout(() => {
|
|
127
|
+
const unmountEvent = new CustomEvent(AUTOFOCUS_ON_UNMOUNT, EVENT_OPTIONS);
|
|
128
|
+
container.addEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
129
|
+
container.dispatchEvent(unmountEvent);
|
|
130
|
+
if (!unmountEvent.defaultPrevented) {
|
|
131
|
+
focus(previouslyFocusedElement ?? document.body, { select: true });
|
|
132
|
+
}
|
|
133
|
+
container.removeEventListener(AUTOFOCUS_ON_UNMOUNT, onUnmountAutoFocus);
|
|
134
|
+
focusScopesStack.remove(focusScope);
|
|
135
|
+
}, 0);
|
|
136
|
+
};
|
|
137
|
+
},
|
|
138
|
+
[container],
|
|
139
|
+
(0, import_internal.subSlot)(slot, "e:mount")
|
|
140
|
+
);
|
|
141
|
+
const handleKeyDown = (0, import_octane.useCallback)(
|
|
142
|
+
(event) => {
|
|
143
|
+
if (!loop && !trapped) return;
|
|
144
|
+
if (focusScope.paused) return;
|
|
145
|
+
const isTabKey = event.key === "Tab" && !event.altKey && !event.ctrlKey && !event.metaKey;
|
|
146
|
+
const focusedElement = document.activeElement;
|
|
147
|
+
if (isTabKey && focusedElement) {
|
|
148
|
+
const scopeContainer = event.currentTarget;
|
|
149
|
+
const [first, last] = getTabbableEdges(scopeContainer);
|
|
150
|
+
const hasTabbableElementsInside = first && last;
|
|
151
|
+
if (!hasTabbableElementsInside) {
|
|
152
|
+
if (focusedElement === scopeContainer) event.preventDefault();
|
|
153
|
+
} else {
|
|
154
|
+
if (!event.shiftKey && focusedElement === last) {
|
|
155
|
+
event.preventDefault();
|
|
156
|
+
if (loop) focus(first, { select: true });
|
|
157
|
+
} else if (event.shiftKey && focusedElement === first) {
|
|
158
|
+
event.preventDefault();
|
|
159
|
+
if (loop) focus(last, { select: true });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
[loop, trapped],
|
|
165
|
+
(0, import_internal.subSlot)(slot, "keydown")
|
|
166
|
+
);
|
|
167
|
+
return (0, import_octane.createElement)(import_Primitive.Primitive.div, {
|
|
168
|
+
tabIndex: -1,
|
|
169
|
+
...scopeProps,
|
|
170
|
+
ref: composedRefs,
|
|
171
|
+
onKeyDown: handleKeyDown
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
function focusFirst(candidates, { select = false } = {}) {
|
|
175
|
+
const previouslyFocusedElement = document.activeElement;
|
|
176
|
+
for (const candidate of candidates) {
|
|
177
|
+
focus(candidate, { select });
|
|
178
|
+
if (document.activeElement !== previouslyFocusedElement) return;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function getTabbableEdges(container) {
|
|
182
|
+
const candidates = getTabbableCandidates(container);
|
|
183
|
+
const first = findVisible(candidates, container);
|
|
184
|
+
const last = findVisible(candidates.reverse(), container);
|
|
185
|
+
return [first, last];
|
|
186
|
+
}
|
|
187
|
+
function getTabbableCandidates(container) {
|
|
188
|
+
const nodes = [];
|
|
189
|
+
const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
|
|
190
|
+
acceptNode: (node) => {
|
|
191
|
+
const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
|
|
192
|
+
if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP;
|
|
193
|
+
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
while (walker.nextNode()) nodes.push(walker.currentNode);
|
|
197
|
+
return nodes;
|
|
198
|
+
}
|
|
199
|
+
function findVisible(elements, container) {
|
|
200
|
+
for (const element of elements) {
|
|
201
|
+
if (!isHidden(element, { upTo: container })) return element;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function isHidden(node, { upTo }) {
|
|
205
|
+
if (!node) return false;
|
|
206
|
+
if (getComputedStyle(node).visibility === "hidden") return true;
|
|
207
|
+
while (node) {
|
|
208
|
+
if (upTo !== void 0 && node === upTo) return false;
|
|
209
|
+
if (getComputedStyle(node).display === "none") return true;
|
|
210
|
+
node = node.parentElement;
|
|
211
|
+
}
|
|
212
|
+
return false;
|
|
213
|
+
}
|
|
214
|
+
function isSelectableInput(element) {
|
|
215
|
+
return element instanceof HTMLInputElement && "select" in element;
|
|
216
|
+
}
|
|
217
|
+
function focus(element, { select = false } = {}) {
|
|
218
|
+
if (element && element.focus) {
|
|
219
|
+
const previouslyFocusedElement = document.activeElement;
|
|
220
|
+
element.focus({ preventScroll: true });
|
|
221
|
+
if (element !== previouslyFocusedElement && isSelectableInput(element) && select) {
|
|
222
|
+
element.select();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const focusScopesStack = createFocusScopesStack();
|
|
227
|
+
function createFocusScopesStack() {
|
|
228
|
+
let stack = [];
|
|
229
|
+
return {
|
|
230
|
+
add(focusScope) {
|
|
231
|
+
const activeFocusScope = stack[0];
|
|
232
|
+
if (focusScope !== activeFocusScope) {
|
|
233
|
+
activeFocusScope?.pause();
|
|
234
|
+
}
|
|
235
|
+
stack = arrayRemove(stack, focusScope);
|
|
236
|
+
stack.unshift(focusScope);
|
|
237
|
+
},
|
|
238
|
+
remove(focusScope) {
|
|
239
|
+
stack = arrayRemove(stack, focusScope);
|
|
240
|
+
stack[0]?.resume();
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function arrayRemove(array, item) {
|
|
245
|
+
const updatedArray = [...array];
|
|
246
|
+
const index = updatedArray.indexOf(item);
|
|
247
|
+
if (index !== -1) updatedArray.splice(index, 1);
|
|
248
|
+
return updatedArray;
|
|
249
|
+
}
|
|
250
|
+
function removeLinks(items) {
|
|
251
|
+
return items.filter((item) => item.tagName !== "A");
|
|
252
|
+
}
|
|
253
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
254
|
+
0 && (module.exports = {
|
|
255
|
+
FocusScope,
|
|
256
|
+
Root
|
|
257
|
+
});
|