@inertiaui/vanilla 0.5.0 → 1.0.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/README.md +126 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +749 -759
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,829 +1,819 @@
|
|
|
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
|
-
(el) => el.getAttribute("aria-hidden") !== "true"
|
|
158
|
-
);
|
|
159
|
-
}
|
|
160
|
-
const focusTrapStack = [];
|
|
155
|
+
if (!container) return [];
|
|
156
|
+
return Array.from(container.querySelectorAll(FOCUSABLE_SELECTORS)).filter((el) => el.getAttribute("aria-hidden") !== "true");
|
|
157
|
+
}
|
|
158
|
+
var focusTrapStack = [];
|
|
161
159
|
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
|
-
}
|
|
160
|
+
if (event.key !== "Tab" || focusTrapStack.length === 0) return;
|
|
161
|
+
const { container } = focusTrapStack[focusTrapStack.length - 1];
|
|
162
|
+
const focusableElements = getFocusableElements(container);
|
|
163
|
+
if (focusableElements.length === 0) return;
|
|
164
|
+
const firstElement = focusableElements[0];
|
|
165
|
+
const lastElement = focusableElements[focusableElements.length - 1];
|
|
166
|
+
if (event.shiftKey) {
|
|
167
|
+
if (document.activeElement === firstElement || !container.contains(document.activeElement)) {
|
|
168
|
+
event.preventDefault();
|
|
169
|
+
lastElement.focus();
|
|
170
|
+
}
|
|
171
|
+
} else if (document.activeElement === lastElement || !container.contains(document.activeElement)) {
|
|
172
|
+
event.preventDefault();
|
|
173
|
+
firstElement.focus();
|
|
174
|
+
}
|
|
183
175
|
}
|
|
184
176
|
function handleTrapFocusIn(event) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
if (focusableElements.length > 0) {
|
|
192
|
-
focusableElements[0].focus();
|
|
193
|
-
}
|
|
194
|
-
}
|
|
177
|
+
if (focusTrapStack.length === 0) return;
|
|
178
|
+
const { container } = focusTrapStack[focusTrapStack.length - 1];
|
|
179
|
+
if (!container.contains(event.target)) {
|
|
180
|
+
const focusableElements = getFocusableElements(container);
|
|
181
|
+
if (focusableElements.length > 0) focusableElements[0].focus();
|
|
182
|
+
}
|
|
195
183
|
}
|
|
196
184
|
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
|
-
cleaned = true;
|
|
239
|
-
if (rafId !== void 0) {
|
|
240
|
-
cancelAnimationFrame(rafId);
|
|
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
|
-
};
|
|
185
|
+
const { initialFocus = true, initialFocusElement = null, returnFocus = true } = options;
|
|
186
|
+
const previouslyFocused = document.activeElement;
|
|
187
|
+
const entry = { container };
|
|
188
|
+
if (focusTrapStack.length === 0) {
|
|
189
|
+
document.addEventListener("keydown", handleTrapKeyDown);
|
|
190
|
+
document.addEventListener("focusin", handleTrapFocusIn);
|
|
191
|
+
}
|
|
192
|
+
focusTrapStack.push(entry);
|
|
193
|
+
function handleFocusOut(event) {
|
|
194
|
+
if (event.relatedTarget !== null) return;
|
|
195
|
+
queueMicrotask(() => {
|
|
196
|
+
if (!container.contains(container.ownerDocument.activeElement)) {
|
|
197
|
+
const focusableElements = getFocusableElements(container);
|
|
198
|
+
if (focusableElements.length > 0) focusableElements[0].focus();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
container.addEventListener("focusout", handleFocusOut);
|
|
203
|
+
let rafId;
|
|
204
|
+
if (initialFocus) rafId = requestAnimationFrame(() => {
|
|
205
|
+
rafId = void 0;
|
|
206
|
+
if (initialFocusElement && container.contains(initialFocusElement)) initialFocusElement.focus();
|
|
207
|
+
else {
|
|
208
|
+
const focusableElements = getFocusableElements(container);
|
|
209
|
+
if (focusableElements.length > 0) focusableElements[0].focus();
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
let cleaned = false;
|
|
213
|
+
return function cleanup() {
|
|
214
|
+
if (cleaned) return;
|
|
215
|
+
cleaned = true;
|
|
216
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
217
|
+
container.removeEventListener("focusout", handleFocusOut);
|
|
218
|
+
const index = focusTrapStack.indexOf(entry);
|
|
219
|
+
if (index >= 0) focusTrapStack.splice(index, 1);
|
|
220
|
+
if (focusTrapStack.length === 0) {
|
|
221
|
+
document.removeEventListener("keydown", handleTrapKeyDown);
|
|
222
|
+
document.removeEventListener("focusin", handleTrapFocusIn);
|
|
223
|
+
}
|
|
224
|
+
if (returnFocus && previouslyFocused && typeof previouslyFocused.focus === "function" && previouslyFocused.isConnected) previouslyFocused.focus();
|
|
225
|
+
};
|
|
255
226
|
}
|
|
256
227
|
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 = [];
|
|
228
|
+
const { preventDefault = false, stopPropagation = false } = options;
|
|
229
|
+
function handler(event) {
|
|
230
|
+
if (event.key === "Escape") {
|
|
231
|
+
if (preventDefault) event.preventDefault();
|
|
232
|
+
if (stopPropagation) event.stopPropagation();
|
|
233
|
+
callback(event);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
document.addEventListener("keydown", handler);
|
|
237
|
+
return function cleanup() {
|
|
238
|
+
document.removeEventListener("keydown", handler);
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
var ariaHiddenStack = [];
|
|
275
242
|
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
|
-
};
|
|
243
|
+
const element = typeof elementOrSelector === "string" ? document.querySelector(elementOrSelector) : elementOrSelector;
|
|
244
|
+
if (!element) return function noop() {};
|
|
245
|
+
const existingIndex = ariaHiddenStack.findIndex((item) => item.element === element);
|
|
246
|
+
if (existingIndex >= 0) ariaHiddenStack[existingIndex].count++;
|
|
247
|
+
else {
|
|
248
|
+
const originalValue = element.getAttribute("aria-hidden");
|
|
249
|
+
ariaHiddenStack.push({
|
|
250
|
+
element,
|
|
251
|
+
originalValue,
|
|
252
|
+
count: 1
|
|
253
|
+
});
|
|
254
|
+
element.setAttribute("aria-hidden", "true");
|
|
255
|
+
}
|
|
256
|
+
let cleaned = false;
|
|
257
|
+
return function cleanup() {
|
|
258
|
+
if (cleaned) return;
|
|
259
|
+
cleaned = true;
|
|
260
|
+
unmarkAriaHidden(element);
|
|
261
|
+
};
|
|
297
262
|
}
|
|
298
263
|
function unmarkAriaHidden(element) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
ariaHiddenStack.splice(index, 1);
|
|
312
|
-
}
|
|
313
|
-
}
|
|
264
|
+
const index = ariaHiddenStack.findIndex((item) => item.element === element);
|
|
265
|
+
if (index < 0) return;
|
|
266
|
+
ariaHiddenStack[index].count--;
|
|
267
|
+
if (ariaHiddenStack[index].count <= 0) {
|
|
268
|
+
const { originalValue } = ariaHiddenStack[index];
|
|
269
|
+
if (originalValue === null) element.removeAttribute("aria-hidden");
|
|
270
|
+
else element.setAttribute("aria-hidden", originalValue);
|
|
271
|
+
ariaHiddenStack.splice(index, 1);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region src/clickOutside.ts
|
|
314
276
|
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
|
-
}
|
|
277
|
+
const targets = Array.isArray(elements) ? elements : [elements];
|
|
278
|
+
function handler(event) {
|
|
279
|
+
const target = event.target;
|
|
280
|
+
for (const el of targets) if (el.contains(target)) return;
|
|
281
|
+
let node = target;
|
|
282
|
+
while (node) {
|
|
283
|
+
if (node instanceof HTMLElement && node.hasAttribute("data-inertiaui-portal")) return;
|
|
284
|
+
node = node.parentNode;
|
|
285
|
+
}
|
|
286
|
+
callback(event);
|
|
287
|
+
}
|
|
288
|
+
const timeoutId = setTimeout(() => {
|
|
289
|
+
document.addEventListener("pointerdown", handler, true);
|
|
290
|
+
}, 0);
|
|
291
|
+
let cleaned = false;
|
|
292
|
+
return function cleanup() {
|
|
293
|
+
if (cleaned) return;
|
|
294
|
+
cleaned = true;
|
|
295
|
+
clearTimeout(timeoutId);
|
|
296
|
+
document.removeEventListener("pointerdown", handler, true);
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/menu.ts
|
|
345
301
|
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;
|
|
302
|
+
const { itemSelector = "[role=\"menuitem\"]:not([disabled]):not([aria-disabled=\"true\"])", orientation = "vertical", loop = true, typeAhead = true, onActivate } = options;
|
|
303
|
+
let typeAheadBuffer = "";
|
|
304
|
+
let typeAheadTimeout;
|
|
305
|
+
function getItems() {
|
|
306
|
+
return Array.from(container.querySelectorAll(itemSelector));
|
|
307
|
+
}
|
|
308
|
+
function getActiveIndex() {
|
|
309
|
+
const items = getItems();
|
|
310
|
+
const focused = document.activeElement;
|
|
311
|
+
return items.indexOf(focused);
|
|
312
|
+
}
|
|
313
|
+
function focusItem(index) {
|
|
314
|
+
const items = getItems();
|
|
315
|
+
if (items.length === 0) return;
|
|
316
|
+
const item = items[loop ? (index % items.length + items.length) % items.length : Math.max(0, Math.min(index, items.length - 1))];
|
|
317
|
+
items.forEach((el) => el.setAttribute("tabindex", "-1"));
|
|
318
|
+
item.setAttribute("tabindex", "0");
|
|
319
|
+
item.focus();
|
|
320
|
+
}
|
|
321
|
+
function handleKeyDown(event) {
|
|
322
|
+
const items = getItems();
|
|
323
|
+
if (items.length === 0) return;
|
|
324
|
+
const nextKey = orientation === "vertical" ? "ArrowDown" : "ArrowRight";
|
|
325
|
+
const prevKey = orientation === "vertical" ? "ArrowUp" : "ArrowLeft";
|
|
326
|
+
switch (event.key) {
|
|
327
|
+
case nextKey:
|
|
328
|
+
event.preventDefault();
|
|
329
|
+
focusItem(getActiveIndex() + 1);
|
|
330
|
+
break;
|
|
331
|
+
case prevKey:
|
|
332
|
+
event.preventDefault();
|
|
333
|
+
focusItem(getActiveIndex() - 1);
|
|
334
|
+
break;
|
|
335
|
+
case "Home":
|
|
336
|
+
event.preventDefault();
|
|
337
|
+
focusItem(0);
|
|
338
|
+
break;
|
|
339
|
+
case "End":
|
|
340
|
+
event.preventDefault();
|
|
341
|
+
focusItem(items.length - 1);
|
|
342
|
+
break;
|
|
343
|
+
case "Enter":
|
|
344
|
+
case " ": {
|
|
345
|
+
event.preventDefault();
|
|
346
|
+
const focused = document.activeElement;
|
|
347
|
+
if (items.includes(focused)) {
|
|
348
|
+
focused.click();
|
|
349
|
+
onActivate?.(focused);
|
|
350
|
+
}
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
default:
|
|
354
|
+
if (typeAhead && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
355
|
+
event.preventDefault();
|
|
356
|
+
typeAheadBuffer += event.key.toLowerCase();
|
|
357
|
+
if (typeAheadTimeout) clearTimeout(typeAheadTimeout);
|
|
358
|
+
typeAheadTimeout = setTimeout(() => {
|
|
359
|
+
typeAheadBuffer = "";
|
|
360
|
+
}, 350);
|
|
361
|
+
const match = items.find((item) => {
|
|
362
|
+
return (item.textContent || "").trim().toLowerCase().startsWith(typeAheadBuffer);
|
|
363
|
+
});
|
|
364
|
+
if (match) focusItem(items.indexOf(match));
|
|
365
|
+
}
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
container.addEventListener("keydown", handleKeyDown);
|
|
370
|
+
const items = getItems();
|
|
371
|
+
items.forEach((el, i) => el.setAttribute("tabindex", i === 0 ? "0" : "-1"));
|
|
372
|
+
let rafId;
|
|
373
|
+
if (items.length > 0) rafId = requestAnimationFrame(() => {
|
|
374
|
+
rafId = void 0;
|
|
375
|
+
items[0].focus();
|
|
376
|
+
});
|
|
377
|
+
let cleaned = false;
|
|
378
|
+
return function cleanup() {
|
|
379
|
+
if (cleaned) return;
|
|
380
|
+
cleaned = true;
|
|
381
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
382
|
+
if (typeAheadTimeout) clearTimeout(typeAheadTimeout);
|
|
383
|
+
container.removeEventListener("keydown", handleKeyDown);
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
//#endregion
|
|
387
|
+
//#region src/position.ts
|
|
388
|
+
var ANCHOR_CLASS_PREFIX = "iui-anchor-";
|
|
389
|
+
var _supportsAnchor = null;
|
|
464
390
|
function supportsAnchorPositioning() {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
}
|
|
468
|
-
return _supportsAnchor;
|
|
391
|
+
if (_supportsAnchor === null) _supportsAnchor = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("anchor-name", "--a");
|
|
392
|
+
return _supportsAnchor;
|
|
469
393
|
}
|
|
470
394
|
function isRtl$1() {
|
|
471
|
-
|
|
395
|
+
return typeof document !== "undefined" && document.documentElement.dir === "rtl";
|
|
472
396
|
}
|
|
473
397
|
function getOppositePlacement(placement) {
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
return map[placement] || placement;
|
|
398
|
+
return {
|
|
399
|
+
top: "bottom",
|
|
400
|
+
"top-start": "bottom-start",
|
|
401
|
+
"top-end": "bottom-end",
|
|
402
|
+
bottom: "top",
|
|
403
|
+
"bottom-start": "top-start",
|
|
404
|
+
"bottom-end": "top-end",
|
|
405
|
+
left: "right",
|
|
406
|
+
"left-start": "right-start",
|
|
407
|
+
"left-end": "right-end",
|
|
408
|
+
right: "left",
|
|
409
|
+
"right-start": "left-start",
|
|
410
|
+
"right-end": "left-end"
|
|
411
|
+
}[placement] || placement;
|
|
489
412
|
}
|
|
490
413
|
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
|
-
|
|
414
|
+
let x = 0;
|
|
415
|
+
let y = 0;
|
|
416
|
+
const [side, alignment] = placement.split("-");
|
|
417
|
+
const rtl = isRtl$1();
|
|
418
|
+
switch (side) {
|
|
419
|
+
case "bottom":
|
|
420
|
+
y = refRect.bottom + offset;
|
|
421
|
+
break;
|
|
422
|
+
case "top":
|
|
423
|
+
y = refRect.top - floatingRect.height - offset;
|
|
424
|
+
break;
|
|
425
|
+
case "left":
|
|
426
|
+
x = refRect.left - floatingRect.width - offset;
|
|
427
|
+
break;
|
|
428
|
+
case "right":
|
|
429
|
+
x = refRect.right + offset;
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
432
|
+
if (side === "bottom" || side === "top") {
|
|
433
|
+
const startIsLeft = !rtl;
|
|
434
|
+
switch (alignment) {
|
|
435
|
+
case "start":
|
|
436
|
+
x = startIsLeft ? refRect.left : refRect.right - floatingRect.width;
|
|
437
|
+
break;
|
|
438
|
+
case "end":
|
|
439
|
+
x = startIsLeft ? refRect.right - floatingRect.width : refRect.left;
|
|
440
|
+
break;
|
|
441
|
+
default:
|
|
442
|
+
x = refRect.left + (refRect.width - floatingRect.width) / 2;
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
} else switch (alignment) {
|
|
446
|
+
case "start":
|
|
447
|
+
y = refRect.top;
|
|
448
|
+
break;
|
|
449
|
+
case "end":
|
|
450
|
+
y = refRect.bottom - floatingRect.height;
|
|
451
|
+
break;
|
|
452
|
+
default:
|
|
453
|
+
y = refRect.top + (refRect.height - floatingRect.height) / 2;
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
x,
|
|
458
|
+
y
|
|
459
|
+
};
|
|
536
460
|
}
|
|
537
461
|
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
|
-
}
|
|
462
|
+
const [side] = placement.split("-");
|
|
463
|
+
const vw = window.innerWidth;
|
|
464
|
+
const vh = window.innerHeight;
|
|
465
|
+
switch (side) {
|
|
466
|
+
case "bottom": return y + floatingRect.height > vh;
|
|
467
|
+
case "top": return y < 0;
|
|
468
|
+
case "left": return x < 0;
|
|
469
|
+
case "right": return x + floatingRect.width > vw;
|
|
470
|
+
default: return false;
|
|
471
|
+
}
|
|
553
472
|
}
|
|
554
473
|
function clampToViewport(x, y, floatingRect) {
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
474
|
+
const vw = window.innerWidth;
|
|
475
|
+
const vh = window.innerHeight;
|
|
476
|
+
const margin = 4;
|
|
477
|
+
return {
|
|
478
|
+
x: Math.max(margin, Math.min(x, vw - floatingRect.width - margin)),
|
|
479
|
+
y: Math.max(margin, Math.min(y, vh - floatingRect.height - margin))
|
|
480
|
+
};
|
|
562
481
|
}
|
|
563
482
|
function computeWithFallback(reference, floating, options = {}) {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
483
|
+
const { placement = "bottom-start", offset = 0, flip = true } = options;
|
|
484
|
+
const refRect = reference.getBoundingClientRect();
|
|
485
|
+
const floatingRect = floating.getBoundingClientRect();
|
|
486
|
+
let { x, y } = computePositionFromRect(refRect, floatingRect, placement, offset);
|
|
487
|
+
let actualPlacement = placement;
|
|
488
|
+
if (flip && wouldOverflow(x, y, floatingRect, placement)) {
|
|
489
|
+
const flipped = getOppositePlacement(placement);
|
|
490
|
+
const flippedPos = computePositionFromRect(refRect, floatingRect, flipped, offset);
|
|
491
|
+
if (!wouldOverflow(flippedPos.x, flippedPos.y, floatingRect, flipped)) {
|
|
492
|
+
x = flippedPos.x;
|
|
493
|
+
y = flippedPos.y;
|
|
494
|
+
actualPlacement = flipped;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
const clamped = clampToViewport(x, y, floatingRect);
|
|
498
|
+
return {
|
|
499
|
+
x: clamped.x,
|
|
500
|
+
y: clamped.y,
|
|
501
|
+
placement: actualPlacement
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
var anchorStyleId = "inertiaui-anchor-styles";
|
|
582
505
|
function ensureAnchorStylesheet() {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
506
|
+
let style = document.getElementById(anchorStyleId);
|
|
507
|
+
if (!style) {
|
|
508
|
+
style = document.createElement("style");
|
|
509
|
+
style.id = anchorStyleId;
|
|
510
|
+
document.head.appendChild(style);
|
|
511
|
+
}
|
|
512
|
+
return style.sheet;
|
|
590
513
|
}
|
|
591
514
|
function findAnchorClass(element) {
|
|
592
|
-
|
|
515
|
+
return Array.from(element.classList).find((c) => c.startsWith(ANCHOR_CLASS_PREFIX));
|
|
593
516
|
}
|
|
594
517
|
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;
|
|
518
|
+
const id = generateId(ANCHOR_CLASS_PREFIX);
|
|
519
|
+
const anchorName = `--${id}`;
|
|
520
|
+
reference.style.anchorName = anchorName;
|
|
521
|
+
floating.style.positionAnchor = anchorName;
|
|
522
|
+
floating.style.position = "fixed";
|
|
523
|
+
const [side, alignment] = placement.split("-");
|
|
524
|
+
const sheet = ensureAnchorStylesheet();
|
|
525
|
+
const className = id;
|
|
526
|
+
let positionRules = "";
|
|
527
|
+
let tryFallback = "";
|
|
528
|
+
switch (side) {
|
|
529
|
+
case "bottom":
|
|
530
|
+
positionRules += `top: anchor(bottom);`;
|
|
531
|
+
positionRules += `margin-top: ${offset}px;`;
|
|
532
|
+
break;
|
|
533
|
+
case "top":
|
|
534
|
+
positionRules += `bottom: anchor(top);`;
|
|
535
|
+
positionRules += `margin-bottom: ${offset}px;`;
|
|
536
|
+
break;
|
|
537
|
+
case "left":
|
|
538
|
+
positionRules += `right: anchor(left);`;
|
|
539
|
+
positionRules += `margin-right: ${offset}px;`;
|
|
540
|
+
break;
|
|
541
|
+
case "right":
|
|
542
|
+
positionRules += `left: anchor(right);`;
|
|
543
|
+
positionRules += `margin-left: ${offset}px;`;
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
if (side === "bottom" || side === "top") switch (alignment) {
|
|
547
|
+
case "start":
|
|
548
|
+
positionRules += `left: anchor(left);`;
|
|
549
|
+
break;
|
|
550
|
+
case "end":
|
|
551
|
+
positionRules += `right: anchor(right);`;
|
|
552
|
+
break;
|
|
553
|
+
default:
|
|
554
|
+
positionRules += `left: anchor(center); translate: -50% 0;`;
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
else switch (alignment) {
|
|
558
|
+
case "start":
|
|
559
|
+
positionRules += `top: anchor(top);`;
|
|
560
|
+
break;
|
|
561
|
+
case "end":
|
|
562
|
+
positionRules += `bottom: anchor(bottom);`;
|
|
563
|
+
break;
|
|
564
|
+
default:
|
|
565
|
+
positionRules += `top: anchor(center); translate: 0 -50%;`;
|
|
566
|
+
break;
|
|
567
|
+
}
|
|
568
|
+
if (flip) tryFallback = `position-try-fallbacks: flip-block, flip-inline;`;
|
|
569
|
+
const rule = `.${className} { ${positionRules} ${tryFallback} }`;
|
|
570
|
+
sheet.insertRule(rule, sheet.cssRules.length);
|
|
571
|
+
floating.classList.add(className);
|
|
572
|
+
return className;
|
|
655
573
|
}
|
|
656
574
|
function cleanupAnchorPositioning(reference, floating, className) {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
}
|
|
575
|
+
reference.style.anchorName = "";
|
|
576
|
+
floating.style.positionAnchor = "";
|
|
577
|
+
floating.style.position = "";
|
|
578
|
+
floating.classList.remove(className);
|
|
579
|
+
const style = document.getElementById(anchorStyleId);
|
|
580
|
+
if (style?.sheet) {
|
|
581
|
+
for (let i = 0; i < style.sheet.cssRules.length; i++) if (style.sheet.cssRules[i].selectorText === `.${className}`) {
|
|
582
|
+
style.sheet.deleteRule(i);
|
|
583
|
+
break;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
671
586
|
}
|
|
672
587
|
function computePosition(reference, floating, options = {}) {
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
588
|
+
const { placement = "bottom-start", offset = 0, flip = true } = options;
|
|
589
|
+
if (supportsAnchorPositioning()) {
|
|
590
|
+
if (!findAnchorClass(floating)) setupAnchorPositioning(reference, floating, placement, offset, flip);
|
|
591
|
+
const rect = floating.getBoundingClientRect();
|
|
592
|
+
return {
|
|
593
|
+
x: rect.left,
|
|
594
|
+
y: rect.top,
|
|
595
|
+
placement
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
const result = computeWithFallback(reference, floating, {
|
|
599
|
+
placement,
|
|
600
|
+
offset,
|
|
601
|
+
flip
|
|
602
|
+
});
|
|
603
|
+
floating.style.position = "fixed";
|
|
604
|
+
floating.style.top = `${result.y}px`;
|
|
605
|
+
floating.style.left = `${result.x}px`;
|
|
606
|
+
return result;
|
|
687
607
|
}
|
|
688
608
|
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
|
-
}
|
|
609
|
+
let rafId;
|
|
610
|
+
function scheduleUpdate() {
|
|
611
|
+
if (rafId !== void 0) return;
|
|
612
|
+
rafId = requestAnimationFrame(() => {
|
|
613
|
+
rafId = void 0;
|
|
614
|
+
update();
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
window.addEventListener("resize", scheduleUpdate);
|
|
618
|
+
window.addEventListener("scroll", scheduleUpdate, true);
|
|
619
|
+
const resizeObserver = new ResizeObserver(scheduleUpdate);
|
|
620
|
+
resizeObserver.observe(reference);
|
|
621
|
+
resizeObserver.observe(floating);
|
|
622
|
+
let cleaned = false;
|
|
623
|
+
return function cleanup() {
|
|
624
|
+
if (cleaned) return;
|
|
625
|
+
cleaned = true;
|
|
626
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
627
|
+
window.removeEventListener("resize", scheduleUpdate);
|
|
628
|
+
window.removeEventListener("scroll", scheduleUpdate, true);
|
|
629
|
+
resizeObserver.disconnect();
|
|
630
|
+
const anchorClass = findAnchorClass(floating);
|
|
631
|
+
if (anchorClass) cleanupAnchorPositioning(reference, floating, anchorClass);
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
//#endregion
|
|
635
|
+
//#region src/blank.ts
|
|
636
|
+
/**
|
|
637
|
+
* Port of Laravel's `blank` function.
|
|
638
|
+
* Returns true if the value is "empty" — null, undefined, empty string, empty array/object, etc.
|
|
639
|
+
*/
|
|
722
640
|
function blank(value) {
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
641
|
+
if (value === null || value === void 0) return true;
|
|
642
|
+
if (typeof value === "string") return value.trim() === "";
|
|
643
|
+
if (typeof value === "number" || typeof value === "boolean") return false;
|
|
644
|
+
if (Array.isArray(value) || typeof value === "object" && value !== null && typeof value.length === "number") return value.length === 0;
|
|
645
|
+
return !value;
|
|
646
|
+
}
|
|
647
|
+
//#endregion
|
|
648
|
+
//#region src/debounce.ts
|
|
649
|
+
/**
|
|
650
|
+
* Debounce a function using requestAnimationFrame.
|
|
651
|
+
* Ensures the function runs at most once per animation frame.
|
|
652
|
+
*
|
|
653
|
+
* @see https://pqina.nl/blog/applying-styles-based-on-the-user-scroll-position-with-smart-css/
|
|
654
|
+
*/
|
|
737
655
|
function debounce(fn) {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
656
|
+
let frame;
|
|
657
|
+
return (...params) => {
|
|
658
|
+
if (frame) cancelAnimationFrame(frame);
|
|
659
|
+
frame = requestAnimationFrame(() => {
|
|
660
|
+
fn(...params);
|
|
661
|
+
});
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
/**
|
|
665
|
+
* Detect the browser's current framerate.
|
|
666
|
+
* Returns a Promise that resolves with the detected FPS (capped to 30–240 range).
|
|
667
|
+
* Falls back to 60 if requestAnimationFrame is unavailable or detection times out.
|
|
668
|
+
*/
|
|
748
669
|
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
|
-
|
|
670
|
+
return new Promise((resolve) => {
|
|
671
|
+
if (typeof window === "undefined" || !window.requestAnimationFrame) return resolve(60);
|
|
672
|
+
const timestamps = [];
|
|
673
|
+
let rafId;
|
|
674
|
+
function measure(now) {
|
|
675
|
+
timestamps.unshift(now);
|
|
676
|
+
if (timestamps.length > 10) {
|
|
677
|
+
const lastTime = timestamps.pop();
|
|
678
|
+
const fps = Math.floor(1e3 * 10 / (now - lastTime));
|
|
679
|
+
cancelAnimationFrame(rafId);
|
|
680
|
+
resolve(Math.min(Math.max(fps, 30), 240));
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
rafId = window.requestAnimationFrame(measure);
|
|
684
|
+
}
|
|
685
|
+
rafId = window.requestAnimationFrame(measure);
|
|
686
|
+
setTimeout(() => {
|
|
687
|
+
if (rafId !== void 0) cancelAnimationFrame(rafId);
|
|
688
|
+
resolve(60);
|
|
689
|
+
}, 1e3);
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
//#endregion
|
|
693
|
+
//#region src/darkMode.ts
|
|
694
|
+
/**
|
|
695
|
+
* Detect whether the user prefers dark mode.
|
|
696
|
+
*
|
|
697
|
+
* Strategies:
|
|
698
|
+
* - `'class'` / `'selector'` — checks `<html class="dark">`
|
|
699
|
+
* - `'media'` — checks `prefers-color-scheme: dark` media query
|
|
700
|
+
* - `'auto'` (default) — checks class first, then media query
|
|
701
|
+
* - custom function — called directly for full control
|
|
702
|
+
*/
|
|
775
703
|
function prefersDarkMode(strategy = "auto") {
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}
|
|
704
|
+
if (typeof strategy === "function") return strategy();
|
|
705
|
+
const containsDarkClass = () => document.documentElement.classList.contains("dark");
|
|
706
|
+
const matchesDarkMediaQuery = () => window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
707
|
+
if (strategy === "class" || strategy === "selector") return containsDarkClass();
|
|
708
|
+
if (strategy === "media") return matchesDarkMediaQuery();
|
|
709
|
+
return containsDarkClass() || matchesDarkMediaQuery();
|
|
710
|
+
}
|
|
711
|
+
//#endregion
|
|
712
|
+
//#region src/rtl.ts
|
|
713
|
+
/**
|
|
714
|
+
* Check whether the document is currently in RTL direction.
|
|
715
|
+
*/
|
|
789
716
|
function isRtl() {
|
|
790
|
-
|
|
717
|
+
return document.documentElement.dir === "rtl";
|
|
791
718
|
}
|
|
719
|
+
/**
|
|
720
|
+
* Observe changes to the document's `dir` attribute and invoke the callback
|
|
721
|
+
* whenever it changes. Returns a cleanup function to stop observing.
|
|
722
|
+
*/
|
|
792
723
|
function onRtlChange(callback) {
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
}
|
|
802
|
-
|
|
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
|
-
|
|
724
|
+
const observer = new MutationObserver(() => {
|
|
725
|
+
callback(document.documentElement.dir === "rtl");
|
|
726
|
+
});
|
|
727
|
+
observer.observe(document.documentElement, {
|
|
728
|
+
attributes: true,
|
|
729
|
+
attributeFilter: ["dir"]
|
|
730
|
+
});
|
|
731
|
+
return () => observer.disconnect();
|
|
732
|
+
}
|
|
733
|
+
//#endregion
|
|
734
|
+
//#region src/color.ts
|
|
735
|
+
/**
|
|
736
|
+
* Convert a hex color string to HSL values.
|
|
737
|
+
* Accepts with or without leading '#'.
|
|
738
|
+
*/
|
|
739
|
+
function hexToHsl(hex) {
|
|
740
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
741
|
+
if (!result) return {
|
|
742
|
+
h: 0,
|
|
743
|
+
s: 100,
|
|
744
|
+
l: 50
|
|
745
|
+
};
|
|
746
|
+
let r = parseInt(result[1], 16) / 255;
|
|
747
|
+
let g = parseInt(result[2], 16) / 255;
|
|
748
|
+
let b = parseInt(result[3], 16) / 255;
|
|
749
|
+
const max = Math.max(r, g, b);
|
|
750
|
+
const min = Math.min(r, g, b);
|
|
751
|
+
let h = 0;
|
|
752
|
+
let s = 0;
|
|
753
|
+
const l = (max + min) / 2;
|
|
754
|
+
if (max !== min) {
|
|
755
|
+
const d = max - min;
|
|
756
|
+
s = l > .5 ? d / (2 - max - min) : d / (max + min);
|
|
757
|
+
switch (max) {
|
|
758
|
+
case r:
|
|
759
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
760
|
+
break;
|
|
761
|
+
case g:
|
|
762
|
+
h = ((b - r) / d + 2) / 6;
|
|
763
|
+
break;
|
|
764
|
+
case b:
|
|
765
|
+
h = ((r - g) / d + 4) / 6;
|
|
766
|
+
break;
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
return {
|
|
770
|
+
h: Math.round(h * 360),
|
|
771
|
+
s: Math.round(s * 100),
|
|
772
|
+
l: Math.round(l * 100)
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
/**
|
|
776
|
+
* Convert HSL values to a hex color string.
|
|
777
|
+
* h: 0-360, s: 0-100, l: 0-100
|
|
778
|
+
* Returns string with leading '#'.
|
|
779
|
+
*/
|
|
780
|
+
function hslToHex(h, s, l) {
|
|
781
|
+
s /= 100;
|
|
782
|
+
l /= 100;
|
|
783
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
784
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
785
|
+
const m = l - c / 2;
|
|
786
|
+
let r = 0, g = 0, b = 0;
|
|
787
|
+
if (0 <= h && h < 60) {
|
|
788
|
+
r = c;
|
|
789
|
+
g = x;
|
|
790
|
+
b = 0;
|
|
791
|
+
} else if (60 <= h && h < 120) {
|
|
792
|
+
r = x;
|
|
793
|
+
g = c;
|
|
794
|
+
b = 0;
|
|
795
|
+
} else if (120 <= h && h < 180) {
|
|
796
|
+
r = 0;
|
|
797
|
+
g = c;
|
|
798
|
+
b = x;
|
|
799
|
+
} else if (180 <= h && h < 240) {
|
|
800
|
+
r = 0;
|
|
801
|
+
g = x;
|
|
802
|
+
b = c;
|
|
803
|
+
} else if (240 <= h && h < 300) {
|
|
804
|
+
r = x;
|
|
805
|
+
g = 0;
|
|
806
|
+
b = c;
|
|
807
|
+
} else if (300 <= h && h < 360) {
|
|
808
|
+
r = c;
|
|
809
|
+
g = 0;
|
|
810
|
+
b = x;
|
|
811
|
+
}
|
|
812
|
+
const toHex = (n) => {
|
|
813
|
+
const hex = Math.round((n + m) * 255).toString(16);
|
|
814
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
815
|
+
};
|
|
816
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
|
|
817
|
+
}
|
|
818
|
+
//#endregion
|
|
819
|
+
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 };
|