@inertiaui/vanilla 0.6.0 → 1.0.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/README.md +4 -4
- package/dist/index.js +758 -825
- package/package.json +57 -58
package/dist/index.js
CHANGED
|
@@ -1,897 +1,830 @@
|
|
|
1
|
+
//#region src/helpers.ts
|
|
1
2
|
function generateId(prefix = "inertiaui_") {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
5
|
-
return `${prefix}${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 11)}`;
|
|
3
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") return `${prefix}${crypto.randomUUID()}`;
|
|
4
|
+
return `${prefix}${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 11)}`;
|
|
6
5
|
}
|
|
7
6
|
function strToLowercase(key) {
|
|
8
|
-
|
|
7
|
+
return key.toLowerCase();
|
|
9
8
|
}
|
|
10
9
|
function except(target, keys, ignoreCase = false) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
return Object.keys(target).reduce((acc, key) => {
|
|
18
|
-
if (!keys.includes(ignoreCase ? strToLowercase(key) : key)) {
|
|
19
|
-
acc[key] = target[key];
|
|
20
|
-
}
|
|
21
|
-
return acc;
|
|
22
|
-
}, {});
|
|
10
|
+
if (ignoreCase) keys = keys.map(strToLowercase);
|
|
11
|
+
if (Array.isArray(target)) return target.filter((key) => !keys.includes(ignoreCase ? strToLowercase(key) : key));
|
|
12
|
+
return Object.keys(target).reduce((acc, key) => {
|
|
13
|
+
if (!keys.includes(ignoreCase ? strToLowercase(key) : key)) acc[key] = target[key];
|
|
14
|
+
return acc;
|
|
15
|
+
}, {});
|
|
23
16
|
}
|
|
24
17
|
function only(target, keys, ignoreCase = false) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return Object.keys(target).reduce((acc, key) => {
|
|
32
|
-
if (keys.includes(ignoreCase ? strToLowercase(key) : key)) {
|
|
33
|
-
acc[key] = target[key];
|
|
34
|
-
}
|
|
35
|
-
return acc;
|
|
36
|
-
}, {});
|
|
18
|
+
if (ignoreCase) keys = keys.map(strToLowercase);
|
|
19
|
+
if (Array.isArray(target)) return target.filter((key) => keys.includes(ignoreCase ? strToLowercase(key) : key));
|
|
20
|
+
return Object.keys(target).reduce((acc, key) => {
|
|
21
|
+
if (keys.includes(ignoreCase ? strToLowercase(key) : key)) acc[key] = target[key];
|
|
22
|
+
return acc;
|
|
23
|
+
}, {});
|
|
37
24
|
}
|
|
38
25
|
function rejectNullValues(target) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
acc[key] = target[key];
|
|
45
|
-
}
|
|
46
|
-
return acc;
|
|
47
|
-
}, {});
|
|
26
|
+
if (Array.isArray(target)) return target.filter((item) => item !== null);
|
|
27
|
+
return Object.keys(target).reduce((acc, key) => {
|
|
28
|
+
if (key in target && target[key] !== null) acc[key] = target[key];
|
|
29
|
+
return acc;
|
|
30
|
+
}, {});
|
|
48
31
|
}
|
|
49
32
|
function kebabCase(string) {
|
|
50
|
-
|
|
51
|
-
|
|
33
|
+
if (!string) return "";
|
|
34
|
+
return string.replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").replace(/([a-z\d])([A-Z])/g, "$1-$2").replace(/[\s_]+/g, "-").replace(/-+/g, "-").toLowerCase();
|
|
52
35
|
}
|
|
53
36
|
function isStandardDomEvent(eventName) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
return standardPatterns.some((pattern) => pattern.test(lowerEventName));
|
|
37
|
+
const lowerEventName = eventName.toLowerCase();
|
|
38
|
+
return [
|
|
39
|
+
/^on(click|dblclick|mousedown|mouseup|mouseover|mouseout|mousemove|mouseenter|mouseleave)$/,
|
|
40
|
+
/^on(keydown|keyup|keypress)$/,
|
|
41
|
+
/^on(focus|blur|change|input|submit|reset)$/,
|
|
42
|
+
/^on(load|unload|error|resize|scroll)$/,
|
|
43
|
+
/^on(touchstart|touchend|touchmove|touchcancel)$/,
|
|
44
|
+
/^on(pointerdown|pointerup|pointermove|pointerenter|pointerleave|pointercancel)$/,
|
|
45
|
+
/^on(drag|dragstart|dragend|dragenter|dragleave|dragover|drop)$/,
|
|
46
|
+
/^on(animationstart|animationend|animationiteration)$/,
|
|
47
|
+
/^on(transitionstart|transitionend|transitionrun|transitioncancel)$/
|
|
48
|
+
].some((pattern) => pattern.test(lowerEventName));
|
|
67
49
|
}
|
|
68
50
|
function onceChildrenRendered(element, callback) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
51
|
+
if (element.children.length > 0) {
|
|
52
|
+
callback();
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const observer = new MutationObserver(() => {
|
|
56
|
+
if (element.children.length > 0) {
|
|
57
|
+
observer.disconnect();
|
|
58
|
+
callback();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
observer.observe(element, { childList: true });
|
|
80
62
|
}
|
|
81
63
|
function sameUrlPath(url1, url2) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
64
|
+
if (!url1 || !url2) return false;
|
|
65
|
+
const origin = typeof window !== "undefined" ? window.location.origin : "http://localhost";
|
|
66
|
+
const parsed1 = typeof url1 === "string" ? new URL(url1, origin) : url1;
|
|
67
|
+
const parsed2 = typeof url2 === "string" ? new URL(url2, origin) : url2;
|
|
68
|
+
return `${parsed1.origin}${parsed1.pathname}` === `${parsed2.origin}${parsed2.pathname}`;
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/animate.ts
|
|
72
|
+
/**
|
|
73
|
+
* Tailwind CSS easing functions
|
|
74
|
+
*/
|
|
75
|
+
var easings = {
|
|
76
|
+
linear: "linear",
|
|
77
|
+
in: "cubic-bezier(0.4, 0, 1, 1)",
|
|
78
|
+
out: "cubic-bezier(0, 0, 0.2, 1)",
|
|
79
|
+
inOut: "cubic-bezier(0.4, 0, 0.2, 1)"
|
|
95
80
|
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
81
|
+
var defaultOptions = {
|
|
82
|
+
duration: 300,
|
|
83
|
+
easing: "inOut",
|
|
84
|
+
fill: "forwards"
|
|
100
85
|
};
|
|
86
|
+
/**
|
|
87
|
+
* Animate an element using the Web Animations API.
|
|
88
|
+
* Returns a promise that resolves when the animation completes.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* await animate(element, [
|
|
92
|
+
* { transform: 'scale(0.95)', opacity: 0 },
|
|
93
|
+
* { transform: 'scale(1)', opacity: 1 }
|
|
94
|
+
* ])
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* await animate(element, keyframes.fadeIn, { duration: 200 })
|
|
98
|
+
*/
|
|
101
99
|
function animate(element, keyframes, options = {}) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
100
|
+
const { duration, easing, fill } = {
|
|
101
|
+
...defaultOptions,
|
|
102
|
+
...options
|
|
103
|
+
};
|
|
104
|
+
const resolvedEasing = easing in easings ? easings[easing] : easing;
|
|
105
|
+
const animation = element.animate(keyframes, {
|
|
106
|
+
duration,
|
|
107
|
+
easing: resolvedEasing,
|
|
108
|
+
fill
|
|
109
|
+
});
|
|
110
|
+
return animation.finished.catch(() => animation);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Cancel any running animations on an element
|
|
114
|
+
*/
|
|
111
115
|
function cancelAnimations(element) {
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
element.getAnimations().forEach((animation) => animation.cancel());
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
//#region src/dialog.ts
|
|
120
|
+
var FOCUSABLE_SELECTORS = [
|
|
121
|
+
"a[href]",
|
|
122
|
+
"button:not([disabled])",
|
|
123
|
+
"textarea:not([disabled])",
|
|
124
|
+
"input:not([disabled])",
|
|
125
|
+
"select:not([disabled])",
|
|
126
|
+
"[tabindex]:not([tabindex=\"-1\"])"
|
|
121
127
|
].join(", ");
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
128
|
+
var scrollLockCount = 0;
|
|
129
|
+
var originalOverflow = "";
|
|
130
|
+
var originalPaddingRight = "";
|
|
125
131
|
function lockScroll() {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}
|
|
141
|
-
unlocked = true;
|
|
142
|
-
unlockScroll();
|
|
143
|
-
};
|
|
132
|
+
if (scrollLockCount === 0) {
|
|
133
|
+
originalOverflow = document.body.style.overflow;
|
|
134
|
+
originalPaddingRight = document.body.style.paddingRight;
|
|
135
|
+
const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
|
|
136
|
+
document.body.style.overflow = "hidden";
|
|
137
|
+
if (scrollbarWidth > 0) document.body.style.paddingRight = `${scrollbarWidth}px`;
|
|
138
|
+
}
|
|
139
|
+
scrollLockCount++;
|
|
140
|
+
let unlocked = false;
|
|
141
|
+
return function unlock() {
|
|
142
|
+
if (unlocked) return;
|
|
143
|
+
unlocked = true;
|
|
144
|
+
unlockScroll();
|
|
145
|
+
};
|
|
144
146
|
}
|
|
145
147
|
function unlockScroll() {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
scrollLockCount = Math.max(0, scrollLockCount - 1);
|
|
149
|
+
if (scrollLockCount === 0) {
|
|
150
|
+
document.body.style.overflow = originalOverflow;
|
|
151
|
+
document.body.style.paddingRight = originalPaddingRight;
|
|
152
|
+
}
|
|
151
153
|
}
|
|
152
154
|
function getFocusableElements(container) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
155
|
+
if (!container) return [];
|
|
156
|
+
return Array.from(container.querySelectorAll(FOCUSABLE_SELECTORS)).filter((el) => el.getAttribute("aria-hidden") !== "true");
|
|
157
|
+
}
|
|
158
|
+
var focusTrapStack = [];
|
|
159
|
+
var lastPointerDownTarget = null;
|
|
160
|
+
function trackPointerDown(event) {
|
|
161
|
+
lastPointerDownTarget = event.target;
|
|
162
|
+
setTimeout(() => {
|
|
163
|
+
if (lastPointerDownTarget === event.target) lastPointerDownTarget = null;
|
|
164
|
+
}, 0);
|
|
165
|
+
}
|
|
161
166
|
function handleTrapKeyDown(event) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
} else {
|
|
178
|
-
if (document.activeElement === lastElement || !container.contains(document.activeElement)) {
|
|
179
|
-
event.preventDefault();
|
|
180
|
-
firstElement.focus();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
167
|
+
if (event.key !== "Tab" || focusTrapStack.length === 0) return;
|
|
168
|
+
const { container } = focusTrapStack[focusTrapStack.length - 1];
|
|
169
|
+
const focusableElements = getFocusableElements(container);
|
|
170
|
+
if (focusableElements.length === 0) return;
|
|
171
|
+
const firstElement = focusableElements[0];
|
|
172
|
+
const lastElement = focusableElements[focusableElements.length - 1];
|
|
173
|
+
if (event.shiftKey) {
|
|
174
|
+
if (document.activeElement === firstElement || !container.contains(document.activeElement)) {
|
|
175
|
+
event.preventDefault();
|
|
176
|
+
lastElement.focus();
|
|
177
|
+
}
|
|
178
|
+
} else if (document.activeElement === lastElement || !container.contains(document.activeElement)) {
|
|
179
|
+
event.preventDefault();
|
|
180
|
+
firstElement.focus();
|
|
181
|
+
}
|
|
183
182
|
}
|
|
184
183
|
function handleTrapFocusIn(event) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (focusableElements.length > 0) {
|
|
192
|
-
focusableElements[0].focus();
|
|
193
|
-
}
|
|
194
|
-
}
|
|
184
|
+
if (focusTrapStack.length === 0) return;
|
|
185
|
+
const { container } = focusTrapStack[focusTrapStack.length - 1];
|
|
186
|
+
if (!container.contains(event.target)) {
|
|
187
|
+
const focusableElements = getFocusableElements(container);
|
|
188
|
+
if (focusableElements.length > 0) focusableElements[0].focus({ preventScroll: true });
|
|
189
|
+
}
|
|
195
190
|
}
|
|
196
191
|
function createFocusTrap(container, options = {}) {
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
container.removeEventListener("focusout", handleFocusOut);
|
|
243
|
-
const index = focusTrapStack.indexOf(entry);
|
|
244
|
-
if (index >= 0) {
|
|
245
|
-
focusTrapStack.splice(index, 1);
|
|
246
|
-
}
|
|
247
|
-
if (focusTrapStack.length === 0) {
|
|
248
|
-
document.removeEventListener("keydown", handleTrapKeyDown);
|
|
249
|
-
document.removeEventListener("focusin", handleTrapFocusIn);
|
|
250
|
-
}
|
|
251
|
-
if (returnFocus && previouslyFocused && typeof previouslyFocused.focus === "function" && previouslyFocused.isConnected) {
|
|
252
|
-
previouslyFocused.focus();
|
|
253
|
-
}
|
|
254
|
-
};
|
|
192
|
+
const { initialFocus = true, initialFocusElement = null, returnFocus = true } = options;
|
|
193
|
+
const previouslyFocused = document.activeElement;
|
|
194
|
+
const entry = { container };
|
|
195
|
+
if (focusTrapStack.length === 0) {
|
|
196
|
+
document.addEventListener("keydown", handleTrapKeyDown);
|
|
197
|
+
document.addEventListener("focusin", handleTrapFocusIn);
|
|
198
|
+
document.addEventListener("pointerdown", trackPointerDown, true);
|
|
199
|
+
}
|
|
200
|
+
focusTrapStack.push(entry);
|
|
201
|
+
function handleFocusOut(event) {
|
|
202
|
+
if (event.relatedTarget !== null) return;
|
|
203
|
+
const pointerTarget = lastPointerDownTarget;
|
|
204
|
+
queueMicrotask(() => {
|
|
205
|
+
if (container.contains(container.ownerDocument.activeElement)) return;
|
|
206
|
+
if (pointerTarget instanceof Node && container.contains(pointerTarget)) return;
|
|
207
|
+
const focusableElements = getFocusableElements(container);
|
|
208
|
+
if (focusableElements.length > 0) focusableElements[0].focus({ preventScroll: true });
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
container.addEventListener("focusout", handleFocusOut);
|
|
212
|
+
let rafId;
|
|
213
|
+
if (initialFocus) rafId = requestAnimationFrame(() => {
|
|
214
|
+
rafId = void 0;
|
|
215
|
+
if (initialFocusElement && container.contains(initialFocusElement)) initialFocusElement.focus();
|
|
216
|
+
else {
|
|
217
|
+
const focusableElements = getFocusableElements(container);
|
|
218
|
+
if (focusableElements.length > 0) focusableElements[0].focus();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
let cleaned = false;
|
|
222
|
+
return function cleanup() {
|
|
223
|
+
if (cleaned) return;
|
|
224
|
+
cleaned = true;
|
|
225
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
226
|
+
container.removeEventListener("focusout", handleFocusOut);
|
|
227
|
+
const index = focusTrapStack.indexOf(entry);
|
|
228
|
+
if (index >= 0) focusTrapStack.splice(index, 1);
|
|
229
|
+
if (focusTrapStack.length === 0) {
|
|
230
|
+
document.removeEventListener("keydown", handleTrapKeyDown);
|
|
231
|
+
document.removeEventListener("focusin", handleTrapFocusIn);
|
|
232
|
+
document.removeEventListener("pointerdown", trackPointerDown, true);
|
|
233
|
+
lastPointerDownTarget = null;
|
|
234
|
+
}
|
|
235
|
+
if (returnFocus && previouslyFocused && typeof previouslyFocused.focus === "function" && previouslyFocused.isConnected) previouslyFocused.focus();
|
|
236
|
+
};
|
|
255
237
|
}
|
|
256
238
|
function onEscapeKey(callback, options = {}) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
document.removeEventListener("keydown", handler);
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
const ariaHiddenStack = [];
|
|
239
|
+
const { preventDefault = false, stopPropagation = false } = options;
|
|
240
|
+
function handler(event) {
|
|
241
|
+
if (event.key === "Escape") {
|
|
242
|
+
if (preventDefault) event.preventDefault();
|
|
243
|
+
if (stopPropagation) event.stopPropagation();
|
|
244
|
+
callback(event);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
document.addEventListener("keydown", handler);
|
|
248
|
+
return function cleanup() {
|
|
249
|
+
document.removeEventListener("keydown", handler);
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
var ariaHiddenStack = [];
|
|
275
253
|
function markAriaHidden(elementOrSelector) {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
unmarkAriaHidden(element);
|
|
296
|
-
};
|
|
254
|
+
const element = typeof elementOrSelector === "string" ? document.querySelector(elementOrSelector) : elementOrSelector;
|
|
255
|
+
if (!element) return function noop() {};
|
|
256
|
+
const existingIndex = ariaHiddenStack.findIndex((item) => item.element === element);
|
|
257
|
+
if (existingIndex >= 0) ariaHiddenStack[existingIndex].count++;
|
|
258
|
+
else {
|
|
259
|
+
const originalValue = element.getAttribute("aria-hidden");
|
|
260
|
+
ariaHiddenStack.push({
|
|
261
|
+
element,
|
|
262
|
+
originalValue,
|
|
263
|
+
count: 1
|
|
264
|
+
});
|
|
265
|
+
element.setAttribute("aria-hidden", "true");
|
|
266
|
+
}
|
|
267
|
+
let cleaned = false;
|
|
268
|
+
return function cleanup() {
|
|
269
|
+
if (cleaned) return;
|
|
270
|
+
cleaned = true;
|
|
271
|
+
unmarkAriaHidden(element);
|
|
272
|
+
};
|
|
297
273
|
}
|
|
298
274
|
function unmarkAriaHidden(element) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
ariaHiddenStack.splice(index, 1);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
275
|
+
const index = ariaHiddenStack.findIndex((item) => item.element === element);
|
|
276
|
+
if (index < 0) return;
|
|
277
|
+
ariaHiddenStack[index].count--;
|
|
278
|
+
if (ariaHiddenStack[index].count <= 0) {
|
|
279
|
+
const { originalValue } = ariaHiddenStack[index];
|
|
280
|
+
if (originalValue === null) element.removeAttribute("aria-hidden");
|
|
281
|
+
else element.setAttribute("aria-hidden", originalValue);
|
|
282
|
+
ariaHiddenStack.splice(index, 1);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
//#endregion
|
|
286
|
+
//#region src/clickOutside.ts
|
|
314
287
|
function onClickOutside(elements, callback) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
cleaned = true;
|
|
341
|
-
clearTimeout(timeoutId);
|
|
342
|
-
document.removeEventListener("pointerdown", handler, true);
|
|
343
|
-
};
|
|
344
|
-
}
|
|
288
|
+
const targets = Array.isArray(elements) ? elements : [elements];
|
|
289
|
+
function handler(event) {
|
|
290
|
+
const target = event.target;
|
|
291
|
+
for (const el of targets) if (el.contains(target)) return;
|
|
292
|
+
let node = target;
|
|
293
|
+
while (node) {
|
|
294
|
+
if (node instanceof HTMLElement && node.hasAttribute("data-inertiaui-portal")) return;
|
|
295
|
+
node = node.parentNode;
|
|
296
|
+
}
|
|
297
|
+
callback(event);
|
|
298
|
+
}
|
|
299
|
+
const timeoutId = setTimeout(() => {
|
|
300
|
+
document.addEventListener("pointerdown", handler, true);
|
|
301
|
+
}, 0);
|
|
302
|
+
let cleaned = false;
|
|
303
|
+
return function cleanup() {
|
|
304
|
+
if (cleaned) return;
|
|
305
|
+
cleaned = true;
|
|
306
|
+
clearTimeout(timeoutId);
|
|
307
|
+
document.removeEventListener("pointerdown", handler, true);
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
//#endregion
|
|
311
|
+
//#region src/menu.ts
|
|
345
312
|
function createMenuNavigation(container, options = {}) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
container.addEventListener("keydown", handleKeyDown);
|
|
438
|
-
const items = getItems();
|
|
439
|
-
items.forEach((el, i) => el.setAttribute("tabindex", i === 0 ? "0" : "-1"));
|
|
440
|
-
let rafId;
|
|
441
|
-
if (items.length > 0) {
|
|
442
|
-
rafId = requestAnimationFrame(() => {
|
|
443
|
-
rafId = void 0;
|
|
444
|
-
items[0].focus();
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
let cleaned = false;
|
|
448
|
-
return function cleanup() {
|
|
449
|
-
if (cleaned) {
|
|
450
|
-
return;
|
|
451
|
-
}
|
|
452
|
-
cleaned = true;
|
|
453
|
-
if (rafId !== void 0) {
|
|
454
|
-
cancelAnimationFrame(rafId);
|
|
455
|
-
}
|
|
456
|
-
if (typeAheadTimeout) {
|
|
457
|
-
clearTimeout(typeAheadTimeout);
|
|
458
|
-
}
|
|
459
|
-
container.removeEventListener("keydown", handleKeyDown);
|
|
460
|
-
};
|
|
461
|
-
}
|
|
462
|
-
const ANCHOR_CLASS_PREFIX = "iui-anchor-";
|
|
463
|
-
let _supportsAnchor = null;
|
|
313
|
+
const { itemSelector = "[role=\"menuitem\"]:not([disabled]):not([aria-disabled=\"true\"])", orientation = "vertical", loop = true, typeAhead = true, onActivate } = options;
|
|
314
|
+
let typeAheadBuffer = "";
|
|
315
|
+
let typeAheadTimeout;
|
|
316
|
+
function getItems() {
|
|
317
|
+
return Array.from(container.querySelectorAll(itemSelector));
|
|
318
|
+
}
|
|
319
|
+
function getActiveIndex() {
|
|
320
|
+
const items = getItems();
|
|
321
|
+
const focused = document.activeElement;
|
|
322
|
+
return items.indexOf(focused);
|
|
323
|
+
}
|
|
324
|
+
function focusItem(index) {
|
|
325
|
+
const items = getItems();
|
|
326
|
+
if (items.length === 0) return;
|
|
327
|
+
const item = items[loop ? (index % items.length + items.length) % items.length : Math.max(0, Math.min(index, items.length - 1))];
|
|
328
|
+
items.forEach((el) => el.setAttribute("tabindex", "-1"));
|
|
329
|
+
item.setAttribute("tabindex", "0");
|
|
330
|
+
item.focus();
|
|
331
|
+
}
|
|
332
|
+
function handleKeyDown(event) {
|
|
333
|
+
const items = getItems();
|
|
334
|
+
if (items.length === 0) return;
|
|
335
|
+
const nextKey = orientation === "vertical" ? "ArrowDown" : "ArrowRight";
|
|
336
|
+
const prevKey = orientation === "vertical" ? "ArrowUp" : "ArrowLeft";
|
|
337
|
+
switch (event.key) {
|
|
338
|
+
case nextKey:
|
|
339
|
+
event.preventDefault();
|
|
340
|
+
focusItem(getActiveIndex() + 1);
|
|
341
|
+
break;
|
|
342
|
+
case prevKey:
|
|
343
|
+
event.preventDefault();
|
|
344
|
+
focusItem(getActiveIndex() - 1);
|
|
345
|
+
break;
|
|
346
|
+
case "Home":
|
|
347
|
+
event.preventDefault();
|
|
348
|
+
focusItem(0);
|
|
349
|
+
break;
|
|
350
|
+
case "End":
|
|
351
|
+
event.preventDefault();
|
|
352
|
+
focusItem(items.length - 1);
|
|
353
|
+
break;
|
|
354
|
+
case "Enter":
|
|
355
|
+
case " ": {
|
|
356
|
+
event.preventDefault();
|
|
357
|
+
const focused = document.activeElement;
|
|
358
|
+
if (items.includes(focused)) {
|
|
359
|
+
focused.click();
|
|
360
|
+
onActivate?.(focused);
|
|
361
|
+
}
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
default:
|
|
365
|
+
if (typeAhead && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
366
|
+
event.preventDefault();
|
|
367
|
+
typeAheadBuffer += event.key.toLowerCase();
|
|
368
|
+
if (typeAheadTimeout) clearTimeout(typeAheadTimeout);
|
|
369
|
+
typeAheadTimeout = setTimeout(() => {
|
|
370
|
+
typeAheadBuffer = "";
|
|
371
|
+
}, 350);
|
|
372
|
+
const match = items.find((item) => {
|
|
373
|
+
return (item.textContent || "").trim().toLowerCase().startsWith(typeAheadBuffer);
|
|
374
|
+
});
|
|
375
|
+
if (match) focusItem(items.indexOf(match));
|
|
376
|
+
}
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
container.addEventListener("keydown", handleKeyDown);
|
|
381
|
+
const items = getItems();
|
|
382
|
+
items.forEach((el, i) => el.setAttribute("tabindex", i === 0 ? "0" : "-1"));
|
|
383
|
+
let rafId;
|
|
384
|
+
if (items.length > 0) rafId = requestAnimationFrame(() => {
|
|
385
|
+
rafId = void 0;
|
|
386
|
+
items[0].focus();
|
|
387
|
+
});
|
|
388
|
+
let cleaned = false;
|
|
389
|
+
return function cleanup() {
|
|
390
|
+
if (cleaned) return;
|
|
391
|
+
cleaned = true;
|
|
392
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
393
|
+
if (typeAheadTimeout) clearTimeout(typeAheadTimeout);
|
|
394
|
+
container.removeEventListener("keydown", handleKeyDown);
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
//#endregion
|
|
398
|
+
//#region src/position.ts
|
|
399
|
+
var ANCHOR_CLASS_PREFIX = "iui-anchor-";
|
|
400
|
+
var _supportsAnchor = null;
|
|
464
401
|
function supportsAnchorPositioning() {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
}
|
|
468
|
-
return _supportsAnchor;
|
|
402
|
+
if (_supportsAnchor === null) _supportsAnchor = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("anchor-name", "--a");
|
|
403
|
+
return _supportsAnchor;
|
|
469
404
|
}
|
|
470
405
|
function isRtl$1() {
|
|
471
|
-
|
|
406
|
+
return typeof document !== "undefined" && document.documentElement.dir === "rtl";
|
|
472
407
|
}
|
|
473
408
|
function getOppositePlacement(placement) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
return map[placement] || placement;
|
|
409
|
+
return {
|
|
410
|
+
top: "bottom",
|
|
411
|
+
"top-start": "bottom-start",
|
|
412
|
+
"top-end": "bottom-end",
|
|
413
|
+
bottom: "top",
|
|
414
|
+
"bottom-start": "top-start",
|
|
415
|
+
"bottom-end": "top-end",
|
|
416
|
+
left: "right",
|
|
417
|
+
"left-start": "right-start",
|
|
418
|
+
"left-end": "right-end",
|
|
419
|
+
right: "left",
|
|
420
|
+
"right-start": "left-start",
|
|
421
|
+
"right-end": "left-end"
|
|
422
|
+
}[placement] || placement;
|
|
489
423
|
}
|
|
490
424
|
function computePositionFromRect(refRect, floatingRect, placement, offset) {
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
425
|
+
let x = 0;
|
|
426
|
+
let y = 0;
|
|
427
|
+
const [side, alignment] = placement.split("-");
|
|
428
|
+
const rtl = isRtl$1();
|
|
429
|
+
switch (side) {
|
|
430
|
+
case "bottom":
|
|
431
|
+
y = refRect.bottom + offset;
|
|
432
|
+
break;
|
|
433
|
+
case "top":
|
|
434
|
+
y = refRect.top - floatingRect.height - offset;
|
|
435
|
+
break;
|
|
436
|
+
case "left":
|
|
437
|
+
x = refRect.left - floatingRect.width - offset;
|
|
438
|
+
break;
|
|
439
|
+
case "right":
|
|
440
|
+
x = refRect.right + offset;
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
if (side === "bottom" || side === "top") {
|
|
444
|
+
const startIsLeft = !rtl;
|
|
445
|
+
switch (alignment) {
|
|
446
|
+
case "start":
|
|
447
|
+
x = startIsLeft ? refRect.left : refRect.right - floatingRect.width;
|
|
448
|
+
break;
|
|
449
|
+
case "end":
|
|
450
|
+
x = startIsLeft ? refRect.right - floatingRect.width : refRect.left;
|
|
451
|
+
break;
|
|
452
|
+
default:
|
|
453
|
+
x = refRect.left + (refRect.width - floatingRect.width) / 2;
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
} else switch (alignment) {
|
|
457
|
+
case "start":
|
|
458
|
+
y = refRect.top;
|
|
459
|
+
break;
|
|
460
|
+
case "end":
|
|
461
|
+
y = refRect.bottom - floatingRect.height;
|
|
462
|
+
break;
|
|
463
|
+
default:
|
|
464
|
+
y = refRect.top + (refRect.height - floatingRect.height) / 2;
|
|
465
|
+
break;
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
x,
|
|
469
|
+
y
|
|
470
|
+
};
|
|
536
471
|
}
|
|
537
472
|
function wouldOverflow(x, y, floatingRect, placement) {
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
case "right":
|
|
549
|
-
return x + floatingRect.width > vw;
|
|
550
|
-
default:
|
|
551
|
-
return false;
|
|
552
|
-
}
|
|
473
|
+
const [side] = placement.split("-");
|
|
474
|
+
const vw = window.innerWidth;
|
|
475
|
+
const vh = window.innerHeight;
|
|
476
|
+
switch (side) {
|
|
477
|
+
case "bottom": return y + floatingRect.height > vh;
|
|
478
|
+
case "top": return y < 0;
|
|
479
|
+
case "left": return x < 0;
|
|
480
|
+
case "right": return x + floatingRect.width > vw;
|
|
481
|
+
default: return false;
|
|
482
|
+
}
|
|
553
483
|
}
|
|
554
484
|
function clampToViewport(x, y, floatingRect) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
485
|
+
const vw = window.innerWidth;
|
|
486
|
+
const vh = window.innerHeight;
|
|
487
|
+
const margin = 4;
|
|
488
|
+
return {
|
|
489
|
+
x: Math.max(margin, Math.min(x, vw - floatingRect.width - margin)),
|
|
490
|
+
y: Math.max(margin, Math.min(y, vh - floatingRect.height - margin))
|
|
491
|
+
};
|
|
562
492
|
}
|
|
563
493
|
function computeWithFallback(reference, floating, options = {}) {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
494
|
+
const { placement = "bottom-start", offset = 0, flip = true } = options;
|
|
495
|
+
const refRect = reference.getBoundingClientRect();
|
|
496
|
+
const floatingRect = floating.getBoundingClientRect();
|
|
497
|
+
let { x, y } = computePositionFromRect(refRect, floatingRect, placement, offset);
|
|
498
|
+
let actualPlacement = placement;
|
|
499
|
+
if (flip && wouldOverflow(x, y, floatingRect, placement)) {
|
|
500
|
+
const flipped = getOppositePlacement(placement);
|
|
501
|
+
const flippedPos = computePositionFromRect(refRect, floatingRect, flipped, offset);
|
|
502
|
+
if (!wouldOverflow(flippedPos.x, flippedPos.y, floatingRect, flipped)) {
|
|
503
|
+
x = flippedPos.x;
|
|
504
|
+
y = flippedPos.y;
|
|
505
|
+
actualPlacement = flipped;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
const clamped = clampToViewport(x, y, floatingRect);
|
|
509
|
+
return {
|
|
510
|
+
x: clamped.x,
|
|
511
|
+
y: clamped.y,
|
|
512
|
+
placement: actualPlacement
|
|
513
|
+
};
|
|
514
|
+
}
|
|
515
|
+
var anchorStyleId = "inertiaui-anchor-styles";
|
|
582
516
|
function ensureAnchorStylesheet() {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
517
|
+
let style = document.getElementById(anchorStyleId);
|
|
518
|
+
if (!style) {
|
|
519
|
+
style = document.createElement("style");
|
|
520
|
+
style.id = anchorStyleId;
|
|
521
|
+
document.head.appendChild(style);
|
|
522
|
+
}
|
|
523
|
+
return style.sheet;
|
|
590
524
|
}
|
|
591
525
|
function findAnchorClass(element) {
|
|
592
|
-
|
|
526
|
+
return Array.from(element.classList).find((c) => c.startsWith(ANCHOR_CLASS_PREFIX));
|
|
593
527
|
}
|
|
594
528
|
function setupAnchorPositioning(reference, floating, placement, offset, flip) {
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
}
|
|
651
|
-
const rule = `.${className} { ${positionRules} ${tryFallback} }`;
|
|
652
|
-
sheet.insertRule(rule, sheet.cssRules.length);
|
|
653
|
-
floating.classList.add(className);
|
|
654
|
-
return className;
|
|
529
|
+
const id = generateId(ANCHOR_CLASS_PREFIX);
|
|
530
|
+
const anchorName = `--${id}`;
|
|
531
|
+
reference.style.anchorName = anchorName;
|
|
532
|
+
floating.style.positionAnchor = anchorName;
|
|
533
|
+
floating.style.position = "fixed";
|
|
534
|
+
const [side, alignment] = placement.split("-");
|
|
535
|
+
const sheet = ensureAnchorStylesheet();
|
|
536
|
+
const className = id;
|
|
537
|
+
let positionRules = "";
|
|
538
|
+
let tryFallback = "";
|
|
539
|
+
switch (side) {
|
|
540
|
+
case "bottom":
|
|
541
|
+
positionRules += `top: anchor(bottom);`;
|
|
542
|
+
positionRules += `margin-top: ${offset}px;`;
|
|
543
|
+
break;
|
|
544
|
+
case "top":
|
|
545
|
+
positionRules += `bottom: anchor(top);`;
|
|
546
|
+
positionRules += `margin-bottom: ${offset}px;`;
|
|
547
|
+
break;
|
|
548
|
+
case "left":
|
|
549
|
+
positionRules += `right: anchor(left);`;
|
|
550
|
+
positionRules += `margin-right: ${offset}px;`;
|
|
551
|
+
break;
|
|
552
|
+
case "right":
|
|
553
|
+
positionRules += `left: anchor(right);`;
|
|
554
|
+
positionRules += `margin-left: ${offset}px;`;
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
if (side === "bottom" || side === "top") switch (alignment) {
|
|
558
|
+
case "start":
|
|
559
|
+
positionRules += `left: anchor(left);`;
|
|
560
|
+
break;
|
|
561
|
+
case "end":
|
|
562
|
+
positionRules += `right: anchor(right);`;
|
|
563
|
+
break;
|
|
564
|
+
default:
|
|
565
|
+
positionRules += `left: anchor(center); translate: -50% 0;`;
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
else switch (alignment) {
|
|
569
|
+
case "start":
|
|
570
|
+
positionRules += `top: anchor(top);`;
|
|
571
|
+
break;
|
|
572
|
+
case "end":
|
|
573
|
+
positionRules += `bottom: anchor(bottom);`;
|
|
574
|
+
break;
|
|
575
|
+
default:
|
|
576
|
+
positionRules += `top: anchor(center); translate: 0 -50%;`;
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
if (flip) tryFallback = `position-try-fallbacks: flip-block, flip-inline;`;
|
|
580
|
+
const rule = `.${className} { ${positionRules} ${tryFallback} }`;
|
|
581
|
+
sheet.insertRule(rule, sheet.cssRules.length);
|
|
582
|
+
floating.classList.add(className);
|
|
583
|
+
return className;
|
|
655
584
|
}
|
|
656
585
|
function cleanupAnchorPositioning(reference, floating, className) {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
}
|
|
586
|
+
reference.style.anchorName = "";
|
|
587
|
+
floating.style.positionAnchor = "";
|
|
588
|
+
floating.style.position = "";
|
|
589
|
+
floating.classList.remove(className);
|
|
590
|
+
const style = document.getElementById(anchorStyleId);
|
|
591
|
+
if (style?.sheet) {
|
|
592
|
+
for (let i = 0; i < style.sheet.cssRules.length; i++) if (style.sheet.cssRules[i].selectorText === `.${className}`) {
|
|
593
|
+
style.sheet.deleteRule(i);
|
|
594
|
+
break;
|
|
595
|
+
}
|
|
596
|
+
}
|
|
671
597
|
}
|
|
672
598
|
function computePosition(reference, floating, options = {}) {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
599
|
+
const { placement = "bottom-start", offset = 0, flip = true } = options;
|
|
600
|
+
if (supportsAnchorPositioning()) {
|
|
601
|
+
if (!findAnchorClass(floating)) setupAnchorPositioning(reference, floating, placement, offset, flip);
|
|
602
|
+
const rect = floating.getBoundingClientRect();
|
|
603
|
+
return {
|
|
604
|
+
x: rect.left,
|
|
605
|
+
y: rect.top,
|
|
606
|
+
placement
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
const result = computeWithFallback(reference, floating, {
|
|
610
|
+
placement,
|
|
611
|
+
offset,
|
|
612
|
+
flip
|
|
613
|
+
});
|
|
614
|
+
floating.style.position = "fixed";
|
|
615
|
+
floating.style.top = `${result.y}px`;
|
|
616
|
+
floating.style.left = `${result.x}px`;
|
|
617
|
+
return result;
|
|
687
618
|
}
|
|
688
619
|
function autoUpdate(reference, floating, update) {
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
};
|
|
721
|
-
}
|
|
620
|
+
let rafId;
|
|
621
|
+
function scheduleUpdate() {
|
|
622
|
+
if (rafId !== void 0) return;
|
|
623
|
+
rafId = requestAnimationFrame(() => {
|
|
624
|
+
rafId = void 0;
|
|
625
|
+
update();
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
window.addEventListener("resize", scheduleUpdate);
|
|
629
|
+
window.addEventListener("scroll", scheduleUpdate, true);
|
|
630
|
+
const resizeObserver = new ResizeObserver(scheduleUpdate);
|
|
631
|
+
resizeObserver.observe(reference);
|
|
632
|
+
resizeObserver.observe(floating);
|
|
633
|
+
let cleaned = false;
|
|
634
|
+
return function cleanup() {
|
|
635
|
+
if (cleaned) return;
|
|
636
|
+
cleaned = true;
|
|
637
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
638
|
+
window.removeEventListener("resize", scheduleUpdate);
|
|
639
|
+
window.removeEventListener("scroll", scheduleUpdate, true);
|
|
640
|
+
resizeObserver.disconnect();
|
|
641
|
+
const anchorClass = findAnchorClass(floating);
|
|
642
|
+
if (anchorClass) cleanupAnchorPositioning(reference, floating, anchorClass);
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
//#endregion
|
|
646
|
+
//#region src/blank.ts
|
|
647
|
+
/**
|
|
648
|
+
* Port of Laravel's `blank` function.
|
|
649
|
+
* Returns true if the value is "empty" — null, undefined, empty string, empty array/object, etc.
|
|
650
|
+
*/
|
|
722
651
|
function blank(value) {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
652
|
+
if (value === null || value === void 0) return true;
|
|
653
|
+
if (typeof value === "string") return value.trim() === "";
|
|
654
|
+
if (typeof value === "number" || typeof value === "boolean") return false;
|
|
655
|
+
if (Array.isArray(value) || typeof value === "object" && value !== null && typeof value.length === "number") return value.length === 0;
|
|
656
|
+
return !value;
|
|
657
|
+
}
|
|
658
|
+
//#endregion
|
|
659
|
+
//#region src/debounce.ts
|
|
660
|
+
/**
|
|
661
|
+
* Debounce a function using requestAnimationFrame.
|
|
662
|
+
* Ensures the function runs at most once per animation frame.
|
|
663
|
+
*
|
|
664
|
+
* @see https://pqina.nl/blog/applying-styles-based-on-the-user-scroll-position-with-smart-css/
|
|
665
|
+
*/
|
|
737
666
|
function debounce(fn) {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
667
|
+
let frame;
|
|
668
|
+
return (...params) => {
|
|
669
|
+
if (frame) cancelAnimationFrame(frame);
|
|
670
|
+
frame = requestAnimationFrame(() => {
|
|
671
|
+
fn(...params);
|
|
672
|
+
});
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Detect the browser's current framerate.
|
|
677
|
+
* Returns a Promise that resolves with the detected FPS (capped to 30–240 range).
|
|
678
|
+
* Falls back to 60 if requestAnimationFrame is unavailable or detection times out.
|
|
679
|
+
*/
|
|
748
680
|
function detectFramerate() {
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
681
|
+
return new Promise((resolve) => {
|
|
682
|
+
if (typeof window === "undefined" || !window.requestAnimationFrame) return resolve(60);
|
|
683
|
+
const timestamps = [];
|
|
684
|
+
let rafId;
|
|
685
|
+
function measure(now) {
|
|
686
|
+
timestamps.unshift(now);
|
|
687
|
+
if (timestamps.length > 10) {
|
|
688
|
+
const lastTime = timestamps.pop();
|
|
689
|
+
const fps = Math.floor(1e3 * 10 / (now - lastTime));
|
|
690
|
+
cancelAnimationFrame(rafId);
|
|
691
|
+
resolve(Math.min(Math.max(fps, 30), 240));
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
rafId = window.requestAnimationFrame(measure);
|
|
695
|
+
}
|
|
696
|
+
rafId = window.requestAnimationFrame(measure);
|
|
697
|
+
setTimeout(() => {
|
|
698
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
699
|
+
resolve(60);
|
|
700
|
+
}, 1e3);
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
//#endregion
|
|
704
|
+
//#region src/darkMode.ts
|
|
705
|
+
/**
|
|
706
|
+
* Detect whether the user prefers dark mode.
|
|
707
|
+
*
|
|
708
|
+
* Strategies:
|
|
709
|
+
* - `'class'` / `'selector'` — checks `<html class="dark">`
|
|
710
|
+
* - `'media'` — checks `prefers-color-scheme: dark` media query
|
|
711
|
+
* - `'auto'` (default) — checks class first, then media query
|
|
712
|
+
* - custom function — called directly for full control
|
|
713
|
+
*/
|
|
775
714
|
function prefersDarkMode(strategy = "auto") {
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}
|
|
715
|
+
if (typeof strategy === "function") return strategy();
|
|
716
|
+
const containsDarkClass = () => document.documentElement.classList.contains("dark");
|
|
717
|
+
const matchesDarkMediaQuery = () => window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
718
|
+
if (strategy === "class" || strategy === "selector") return containsDarkClass();
|
|
719
|
+
if (strategy === "media") return matchesDarkMediaQuery();
|
|
720
|
+
return containsDarkClass() || matchesDarkMediaQuery();
|
|
721
|
+
}
|
|
722
|
+
//#endregion
|
|
723
|
+
//#region src/rtl.ts
|
|
724
|
+
/**
|
|
725
|
+
* Check whether the document is currently in RTL direction.
|
|
726
|
+
*/
|
|
789
727
|
function isRtl() {
|
|
790
|
-
|
|
728
|
+
return document.documentElement.dir === "rtl";
|
|
791
729
|
}
|
|
730
|
+
/**
|
|
731
|
+
* Observe changes to the document's `dir` attribute and invoke the callback
|
|
732
|
+
* whenever it changes. Returns a cleanup function to stop observing.
|
|
733
|
+
*/
|
|
792
734
|
function onRtlChange(callback) {
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
}
|
|
735
|
+
const observer = new MutationObserver(() => {
|
|
736
|
+
callback(document.documentElement.dir === "rtl");
|
|
737
|
+
});
|
|
738
|
+
observer.observe(document.documentElement, {
|
|
739
|
+
attributes: true,
|
|
740
|
+
attributeFilter: ["dir"]
|
|
741
|
+
});
|
|
742
|
+
return () => observer.disconnect();
|
|
743
|
+
}
|
|
744
|
+
//#endregion
|
|
745
|
+
//#region src/color.ts
|
|
746
|
+
/**
|
|
747
|
+
* Convert a hex color string to HSL values.
|
|
748
|
+
* Accepts with or without leading '#'.
|
|
749
|
+
*/
|
|
802
750
|
function hexToHsl(hex) {
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
751
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
752
|
+
if (!result) return {
|
|
753
|
+
h: 0,
|
|
754
|
+
s: 100,
|
|
755
|
+
l: 50
|
|
756
|
+
};
|
|
757
|
+
let r = parseInt(result[1], 16) / 255;
|
|
758
|
+
let g = parseInt(result[2], 16) / 255;
|
|
759
|
+
let b = parseInt(result[3], 16) / 255;
|
|
760
|
+
const max = Math.max(r, g, b);
|
|
761
|
+
const min = Math.min(r, g, b);
|
|
762
|
+
let h = 0;
|
|
763
|
+
let s = 0;
|
|
764
|
+
const l = (max + min) / 2;
|
|
765
|
+
if (max !== min) {
|
|
766
|
+
const d = max - min;
|
|
767
|
+
s = l > .5 ? d / (2 - max - min) : d / (max + min);
|
|
768
|
+
switch (max) {
|
|
769
|
+
case r:
|
|
770
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
771
|
+
break;
|
|
772
|
+
case g:
|
|
773
|
+
h = ((b - r) / d + 2) / 6;
|
|
774
|
+
break;
|
|
775
|
+
case b:
|
|
776
|
+
h = ((r - g) / d + 4) / 6;
|
|
777
|
+
break;
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
return {
|
|
781
|
+
h: Math.round(h * 360),
|
|
782
|
+
s: Math.round(s * 100),
|
|
783
|
+
l: Math.round(l * 100)
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Convert HSL values to a hex color string.
|
|
788
|
+
* h: 0-360, s: 0-100, l: 0-100
|
|
789
|
+
* Returns string with leading '#'.
|
|
790
|
+
*/
|
|
830
791
|
function hslToHex(h, s, l) {
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
autoUpdate,
|
|
871
|
-
blank,
|
|
872
|
-
cancelAnimations,
|
|
873
|
-
computePosition,
|
|
874
|
-
createFocusTrap,
|
|
875
|
-
createMenuNavigation,
|
|
876
|
-
debounce,
|
|
877
|
-
detectFramerate,
|
|
878
|
-
easings,
|
|
879
|
-
except,
|
|
880
|
-
generateId,
|
|
881
|
-
hexToHsl,
|
|
882
|
-
hslToHex,
|
|
883
|
-
isRtl,
|
|
884
|
-
isStandardDomEvent,
|
|
885
|
-
kebabCase,
|
|
886
|
-
lockScroll,
|
|
887
|
-
markAriaHidden,
|
|
888
|
-
onClickOutside,
|
|
889
|
-
onEscapeKey,
|
|
890
|
-
onRtlChange,
|
|
891
|
-
onceChildrenRendered,
|
|
892
|
-
only,
|
|
893
|
-
prefersDarkMode,
|
|
894
|
-
rejectNullValues,
|
|
895
|
-
sameUrlPath,
|
|
896
|
-
supportsAnchorPositioning
|
|
897
|
-
};
|
|
792
|
+
s /= 100;
|
|
793
|
+
l /= 100;
|
|
794
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
795
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
796
|
+
const m = l - c / 2;
|
|
797
|
+
let r = 0, g = 0, b = 0;
|
|
798
|
+
if (0 <= h && h < 60) {
|
|
799
|
+
r = c;
|
|
800
|
+
g = x;
|
|
801
|
+
b = 0;
|
|
802
|
+
} else if (60 <= h && h < 120) {
|
|
803
|
+
r = x;
|
|
804
|
+
g = c;
|
|
805
|
+
b = 0;
|
|
806
|
+
} else if (120 <= h && h < 180) {
|
|
807
|
+
r = 0;
|
|
808
|
+
g = c;
|
|
809
|
+
b = x;
|
|
810
|
+
} else if (180 <= h && h < 240) {
|
|
811
|
+
r = 0;
|
|
812
|
+
g = x;
|
|
813
|
+
b = c;
|
|
814
|
+
} else if (240 <= h && h < 300) {
|
|
815
|
+
r = x;
|
|
816
|
+
g = 0;
|
|
817
|
+
b = c;
|
|
818
|
+
} else if (300 <= h && h < 360) {
|
|
819
|
+
r = c;
|
|
820
|
+
g = 0;
|
|
821
|
+
b = x;
|
|
822
|
+
}
|
|
823
|
+
const toHex = (n) => {
|
|
824
|
+
const hex = Math.round((n + m) * 255).toString(16);
|
|
825
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
826
|
+
};
|
|
827
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
|
|
828
|
+
}
|
|
829
|
+
//#endregion
|
|
830
|
+
export { animate, autoUpdate, blank, cancelAnimations, computePosition, createFocusTrap, createMenuNavigation, debounce, detectFramerate, easings, except, generateId, hexToHsl, hslToHex, isRtl, isStandardDomEvent, kebabCase, lockScroll, markAriaHidden, onClickOutside, onEscapeKey, onRtlChange, onceChildrenRendered, only, prefersDarkMode, rejectNullValues, sameUrlPath, supportsAnchorPositioning };
|