@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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/dist/index.js +758 -825
  3. 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
- if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
3
- return `${prefix}${crypto.randomUUID()}`;
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
- return key.toLowerCase();
7
+ return key.toLowerCase();
9
8
  }
10
9
  function except(target, keys, ignoreCase = false) {
11
- if (ignoreCase) {
12
- keys = keys.map(strToLowercase);
13
- }
14
- if (Array.isArray(target)) {
15
- return target.filter((key) => !keys.includes(ignoreCase ? strToLowercase(key) : key));
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
- if (ignoreCase) {
26
- keys = keys.map(strToLowercase);
27
- }
28
- if (Array.isArray(target)) {
29
- return target.filter((key) => keys.includes(ignoreCase ? strToLowercase(key) : key));
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
- if (Array.isArray(target)) {
40
- return target.filter((item) => item !== null);
41
- }
42
- return Object.keys(target).reduce((acc, key) => {
43
- if (key in target && target[key] !== null) {
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
- if (!string) return "";
51
- 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();
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
- const lowerEventName = eventName.toLowerCase();
55
- const standardPatterns = [
56
- /^on(click|dblclick|mousedown|mouseup|mouseover|mouseout|mousemove|mouseenter|mouseleave)$/,
57
- /^on(keydown|keyup|keypress)$/,
58
- /^on(focus|blur|change|input|submit|reset)$/,
59
- /^on(load|unload|error|resize|scroll)$/,
60
- /^on(touchstart|touchend|touchmove|touchcancel)$/,
61
- /^on(pointerdown|pointerup|pointermove|pointerenter|pointerleave|pointercancel)$/,
62
- /^on(drag|dragstart|dragend|dragenter|dragleave|dragover|drop)$/,
63
- /^on(animationstart|animationend|animationiteration)$/,
64
- /^on(transitionstart|transitionend|transitionrun|transitioncancel)$/
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
- if (element.children.length > 0) {
70
- callback();
71
- return;
72
- }
73
- const observer = new MutationObserver(() => {
74
- if (element.children.length > 0) {
75
- observer.disconnect();
76
- callback();
77
- }
78
- });
79
- observer.observe(element, { childList: true });
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
- if (!url1 || !url2) {
83
- return false;
84
- }
85
- const origin = typeof window !== "undefined" ? window.location.origin : "http://localhost";
86
- const parsed1 = typeof url1 === "string" ? new URL(url1, origin) : url1;
87
- const parsed2 = typeof url2 === "string" ? new URL(url2, origin) : url2;
88
- return `${parsed1.origin}${parsed1.pathname}` === `${parsed2.origin}${parsed2.pathname}`;
89
- }
90
- const easings = {
91
- linear: "linear",
92
- in: "cubic-bezier(0.4, 0, 1, 1)",
93
- out: "cubic-bezier(0, 0, 0.2, 1)",
94
- inOut: "cubic-bezier(0.4, 0, 0.2, 1)"
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
- const defaultOptions = {
97
- duration: 300,
98
- easing: "inOut",
99
- fill: "forwards"
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
- const { duration, easing, fill } = { ...defaultOptions, ...options };
103
- const resolvedEasing = easing in easings ? easings[easing] : easing;
104
- const animation = element.animate(keyframes, {
105
- duration,
106
- easing: resolvedEasing,
107
- fill
108
- });
109
- return animation.finished.catch(() => animation);
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
- element.getAnimations().forEach((animation) => animation.cancel());
113
- }
114
- const FOCUSABLE_SELECTORS = [
115
- "a[href]",
116
- "button:not([disabled])",
117
- "textarea:not([disabled])",
118
- "input:not([disabled])",
119
- "select:not([disabled])",
120
- '[tabindex]:not([tabindex="-1"])'
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
- let scrollLockCount = 0;
123
- let originalOverflow = "";
124
- let originalPaddingRight = "";
128
+ var scrollLockCount = 0;
129
+ var originalOverflow = "";
130
+ var originalPaddingRight = "";
125
131
  function lockScroll() {
126
- if (scrollLockCount === 0) {
127
- originalOverflow = document.body.style.overflow;
128
- originalPaddingRight = document.body.style.paddingRight;
129
- const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
130
- document.body.style.overflow = "hidden";
131
- if (scrollbarWidth > 0) {
132
- document.body.style.paddingRight = `${scrollbarWidth}px`;
133
- }
134
- }
135
- scrollLockCount++;
136
- let unlocked = false;
137
- return function unlock() {
138
- if (unlocked) {
139
- return;
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
- scrollLockCount = Math.max(0, scrollLockCount - 1);
147
- if (scrollLockCount === 0) {
148
- document.body.style.overflow = originalOverflow;
149
- document.body.style.paddingRight = originalPaddingRight;
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
- if (!container) {
154
- return [];
155
- }
156
- return Array.from(container.querySelectorAll(FOCUSABLE_SELECTORS)).filter(
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 = [];
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
- if (event.key !== "Tab" || focusTrapStack.length === 0) {
163
- return;
164
- }
165
- const { container } = focusTrapStack[focusTrapStack.length - 1];
166
- const focusableElements = getFocusableElements(container);
167
- if (focusableElements.length === 0) {
168
- return;
169
- }
170
- const firstElement = focusableElements[0];
171
- const lastElement = focusableElements[focusableElements.length - 1];
172
- if (event.shiftKey) {
173
- if (document.activeElement === firstElement || !container.contains(document.activeElement)) {
174
- event.preventDefault();
175
- lastElement.focus();
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
- if (focusTrapStack.length === 0) {
186
- return;
187
- }
188
- const { container } = focusTrapStack[focusTrapStack.length - 1];
189
- if (!container.contains(event.target)) {
190
- const focusableElements = getFocusableElements(container);
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
- const { initialFocus = true, initialFocusElement = null, returnFocus = true } = options;
198
- const previouslyFocused = document.activeElement;
199
- const entry = { container };
200
- if (focusTrapStack.length === 0) {
201
- document.addEventListener("keydown", handleTrapKeyDown);
202
- document.addEventListener("focusin", handleTrapFocusIn);
203
- }
204
- focusTrapStack.push(entry);
205
- function handleFocusOut(event) {
206
- if (event.relatedTarget !== null) {
207
- return;
208
- }
209
- queueMicrotask(() => {
210
- if (!container.contains(container.ownerDocument.activeElement)) {
211
- const focusableElements = getFocusableElements(container);
212
- if (focusableElements.length > 0) {
213
- focusableElements[0].focus();
214
- }
215
- }
216
- });
217
- }
218
- container.addEventListener("focusout", handleFocusOut);
219
- let rafId;
220
- if (initialFocus) {
221
- rafId = requestAnimationFrame(() => {
222
- rafId = void 0;
223
- if (initialFocusElement && container.contains(initialFocusElement)) {
224
- initialFocusElement.focus();
225
- } else {
226
- const focusableElements = getFocusableElements(container);
227
- if (focusableElements.length > 0) {
228
- focusableElements[0].focus();
229
- }
230
- }
231
- });
232
- }
233
- let cleaned = false;
234
- return function cleanup() {
235
- if (cleaned) {
236
- return;
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
- };
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
- const { preventDefault = false, stopPropagation = false } = options;
258
- function handler(event) {
259
- if (event.key === "Escape") {
260
- if (preventDefault) {
261
- event.preventDefault();
262
- }
263
- if (stopPropagation) {
264
- event.stopPropagation();
265
- }
266
- callback(event);
267
- }
268
- }
269
- document.addEventListener("keydown", handler);
270
- return function cleanup() {
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
- const element = typeof elementOrSelector === "string" ? document.querySelector(elementOrSelector) : elementOrSelector;
277
- if (!element) {
278
- return function noop() {
279
- };
280
- }
281
- const existingIndex = ariaHiddenStack.findIndex((item) => item.element === element);
282
- if (existingIndex >= 0) {
283
- ariaHiddenStack[existingIndex].count++;
284
- } else {
285
- const originalValue = element.getAttribute("aria-hidden");
286
- ariaHiddenStack.push({ element, originalValue, count: 1 });
287
- element.setAttribute("aria-hidden", "true");
288
- }
289
- let cleaned = false;
290
- return function cleanup() {
291
- if (cleaned) {
292
- return;
293
- }
294
- cleaned = true;
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
- const index = ariaHiddenStack.findIndex((item) => item.element === element);
300
- if (index < 0) {
301
- return;
302
- }
303
- ariaHiddenStack[index].count--;
304
- if (ariaHiddenStack[index].count <= 0) {
305
- const { originalValue } = ariaHiddenStack[index];
306
- if (originalValue === null) {
307
- element.removeAttribute("aria-hidden");
308
- } else {
309
- element.setAttribute("aria-hidden", originalValue);
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
- const targets = Array.isArray(elements) ? elements : [elements];
316
- function handler(event) {
317
- const target = event.target;
318
- for (const el of targets) {
319
- if (el.contains(target)) {
320
- return;
321
- }
322
- }
323
- let node = target;
324
- while (node) {
325
- if (node instanceof HTMLElement && node.hasAttribute("data-inertiaui-portal")) {
326
- return;
327
- }
328
- node = node.parentNode;
329
- }
330
- callback(event);
331
- }
332
- const timeoutId = setTimeout(() => {
333
- document.addEventListener("pointerdown", handler, true);
334
- }, 0);
335
- let cleaned = false;
336
- return function cleanup() {
337
- if (cleaned) {
338
- return;
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
- const {
347
- itemSelector = '[role="menuitem"]:not([disabled]):not([aria-disabled="true"])',
348
- orientation = "vertical",
349
- loop = true,
350
- typeAhead = true,
351
- onActivate
352
- } = options;
353
- let typeAheadBuffer = "";
354
- let typeAheadTimeout;
355
- function getItems() {
356
- return Array.from(container.querySelectorAll(itemSelector));
357
- }
358
- function getActiveIndex() {
359
- const items2 = getItems();
360
- const focused = document.activeElement;
361
- return items2.indexOf(focused);
362
- }
363
- function focusItem(index) {
364
- const items2 = getItems();
365
- if (items2.length === 0) {
366
- return;
367
- }
368
- const targetIndex = loop ? (index % items2.length + items2.length) % items2.length : Math.max(0, Math.min(index, items2.length - 1));
369
- const item = items2[targetIndex];
370
- items2.forEach((el) => el.setAttribute("tabindex", "-1"));
371
- item.setAttribute("tabindex", "0");
372
- item.focus();
373
- }
374
- function handleKeyDown(event) {
375
- const items2 = getItems();
376
- if (items2.length === 0) {
377
- return;
378
- }
379
- const nextKey = orientation === "vertical" ? "ArrowDown" : "ArrowRight";
380
- const prevKey = orientation === "vertical" ? "ArrowUp" : "ArrowLeft";
381
- switch (event.key) {
382
- case nextKey: {
383
- event.preventDefault();
384
- const currentIndex = getActiveIndex();
385
- focusItem(currentIndex + 1);
386
- break;
387
- }
388
- case prevKey: {
389
- event.preventDefault();
390
- const currentIndex = getActiveIndex();
391
- focusItem(currentIndex - 1);
392
- break;
393
- }
394
- case "Home": {
395
- event.preventDefault();
396
- focusItem(0);
397
- break;
398
- }
399
- case "End": {
400
- event.preventDefault();
401
- focusItem(items2.length - 1);
402
- break;
403
- }
404
- case "Enter":
405
- case " ": {
406
- event.preventDefault();
407
- const focused = document.activeElement;
408
- if (items2.includes(focused)) {
409
- focused.click();
410
- onActivate == null ? void 0 : onActivate(focused);
411
- }
412
- break;
413
- }
414
- default: {
415
- if (typeAhead && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
416
- event.preventDefault();
417
- typeAheadBuffer += event.key.toLowerCase();
418
- if (typeAheadTimeout) {
419
- clearTimeout(typeAheadTimeout);
420
- }
421
- typeAheadTimeout = setTimeout(() => {
422
- typeAheadBuffer = "";
423
- }, 350);
424
- const match = items2.find((item) => {
425
- const text = (item.textContent || "").trim().toLowerCase();
426
- return text.startsWith(typeAheadBuffer);
427
- });
428
- if (match) {
429
- const index = items2.indexOf(match);
430
- focusItem(index);
431
- }
432
- }
433
- break;
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
- if (_supportsAnchor === null) {
466
- _supportsAnchor = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("anchor-name", "--a");
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
- return typeof document !== "undefined" && document.documentElement.dir === "rtl";
406
+ return typeof document !== "undefined" && document.documentElement.dir === "rtl";
472
407
  }
473
408
  function getOppositePlacement(placement) {
474
- const map = {
475
- top: "bottom",
476
- "top-start": "bottom-start",
477
- "top-end": "bottom-end",
478
- bottom: "top",
479
- "bottom-start": "top-start",
480
- "bottom-end": "top-end",
481
- left: "right",
482
- "left-start": "right-start",
483
- "left-end": "right-end",
484
- right: "left",
485
- "right-start": "left-start",
486
- "right-end": "left-end"
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
- let x = 0;
492
- let y = 0;
493
- const [side, alignment] = placement.split("-");
494
- const rtl = isRtl$1();
495
- switch (side) {
496
- case "bottom":
497
- y = refRect.bottom + offset;
498
- break;
499
- case "top":
500
- y = refRect.top - floatingRect.height - offset;
501
- break;
502
- case "left":
503
- x = refRect.left - floatingRect.width - offset;
504
- break;
505
- case "right":
506
- x = refRect.right + offset;
507
- break;
508
- }
509
- if (side === "bottom" || side === "top") {
510
- const startIsLeft = !rtl;
511
- switch (alignment) {
512
- case "start":
513
- x = startIsLeft ? refRect.left : refRect.right - floatingRect.width;
514
- break;
515
- case "end":
516
- x = startIsLeft ? refRect.right - floatingRect.width : refRect.left;
517
- break;
518
- default:
519
- x = refRect.left + (refRect.width - floatingRect.width) / 2;
520
- break;
521
- }
522
- } else {
523
- switch (alignment) {
524
- case "start":
525
- y = refRect.top;
526
- break;
527
- case "end":
528
- y = refRect.bottom - floatingRect.height;
529
- break;
530
- default:
531
- y = refRect.top + (refRect.height - floatingRect.height) / 2;
532
- break;
533
- }
534
- }
535
- return { x, y };
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
- const [side] = placement.split("-");
539
- const vw = window.innerWidth;
540
- const vh = window.innerHeight;
541
- switch (side) {
542
- case "bottom":
543
- return y + floatingRect.height > vh;
544
- case "top":
545
- return y < 0;
546
- case "left":
547
- return x < 0;
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
- const vw = window.innerWidth;
556
- const vh = window.innerHeight;
557
- const margin = 4;
558
- return {
559
- x: Math.max(margin, Math.min(x, vw - floatingRect.width - margin)),
560
- y: Math.max(margin, Math.min(y, vh - floatingRect.height - margin))
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
- const { placement = "bottom-start", offset = 0, flip = true } = options;
565
- const refRect = reference.getBoundingClientRect();
566
- const floatingRect = floating.getBoundingClientRect();
567
- let { x, y } = computePositionFromRect(refRect, floatingRect, placement, offset);
568
- let actualPlacement = placement;
569
- if (flip && wouldOverflow(x, y, floatingRect, placement)) {
570
- const flipped = getOppositePlacement(placement);
571
- const flippedPos = computePositionFromRect(refRect, floatingRect, flipped, offset);
572
- if (!wouldOverflow(flippedPos.x, flippedPos.y, floatingRect, flipped)) {
573
- x = flippedPos.x;
574
- y = flippedPos.y;
575
- actualPlacement = flipped;
576
- }
577
- }
578
- const clamped = clampToViewport(x, y, floatingRect);
579
- return { x: clamped.x, y: clamped.y, placement: actualPlacement };
580
- }
581
- const anchorStyleId = "inertiaui-anchor-styles";
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
- let style = document.getElementById(anchorStyleId);
584
- if (!style) {
585
- style = document.createElement("style");
586
- style.id = anchorStyleId;
587
- document.head.appendChild(style);
588
- }
589
- return style.sheet;
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
- return Array.from(element.classList).find((c) => c.startsWith(ANCHOR_CLASS_PREFIX));
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
- const id = generateId(ANCHOR_CLASS_PREFIX);
596
- const anchorName = `--${id}`;
597
- reference.style.anchorName = anchorName;
598
- floating.style.positionAnchor = anchorName;
599
- floating.style.position = "fixed";
600
- const [side, alignment] = placement.split("-");
601
- const sheet = ensureAnchorStylesheet();
602
- const className = id;
603
- let positionRules = "";
604
- let tryFallback = "";
605
- switch (side) {
606
- case "bottom":
607
- positionRules += `top: anchor(bottom);`;
608
- positionRules += `margin-top: ${offset}px;`;
609
- break;
610
- case "top":
611
- positionRules += `bottom: anchor(top);`;
612
- positionRules += `margin-bottom: ${offset}px;`;
613
- break;
614
- case "left":
615
- positionRules += `right: anchor(left);`;
616
- positionRules += `margin-right: ${offset}px;`;
617
- break;
618
- case "right":
619
- positionRules += `left: anchor(right);`;
620
- positionRules += `margin-left: ${offset}px;`;
621
- break;
622
- }
623
- if (side === "bottom" || side === "top") {
624
- switch (alignment) {
625
- case "start":
626
- positionRules += `left: anchor(left);`;
627
- break;
628
- case "end":
629
- positionRules += `right: anchor(right);`;
630
- break;
631
- default:
632
- positionRules += `left: anchor(center); translate: -50% 0;`;
633
- break;
634
- }
635
- } else {
636
- switch (alignment) {
637
- case "start":
638
- positionRules += `top: anchor(top);`;
639
- break;
640
- case "end":
641
- positionRules += `bottom: anchor(bottom);`;
642
- break;
643
- default:
644
- positionRules += `top: anchor(center); translate: 0 -50%;`;
645
- break;
646
- }
647
- }
648
- if (flip) {
649
- tryFallback = `position-try-fallbacks: flip-block, flip-inline;`;
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
- reference.style.anchorName = "";
658
- floating.style.positionAnchor = "";
659
- floating.style.position = "";
660
- floating.classList.remove(className);
661
- const style = document.getElementById(anchorStyleId);
662
- if (style == null ? void 0 : style.sheet) {
663
- for (let i = 0; i < style.sheet.cssRules.length; i++) {
664
- const rule = style.sheet.cssRules[i];
665
- if (rule.selectorText === `.${className}`) {
666
- style.sheet.deleteRule(i);
667
- break;
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
- const { placement = "bottom-start", offset = 0, flip = true } = options;
674
- if (supportsAnchorPositioning()) {
675
- const existingClass = findAnchorClass(floating);
676
- if (!existingClass) {
677
- setupAnchorPositioning(reference, floating, placement, offset, flip);
678
- }
679
- const rect = floating.getBoundingClientRect();
680
- return { x: rect.left, y: rect.top, placement };
681
- }
682
- const result = computeWithFallback(reference, floating, { placement, offset, flip });
683
- floating.style.position = "fixed";
684
- floating.style.top = `${result.y}px`;
685
- floating.style.left = `${result.x}px`;
686
- return result;
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
- let rafId;
690
- function scheduleUpdate() {
691
- if (rafId !== void 0) {
692
- return;
693
- }
694
- rafId = requestAnimationFrame(() => {
695
- rafId = void 0;
696
- update();
697
- });
698
- }
699
- window.addEventListener("resize", scheduleUpdate);
700
- window.addEventListener("scroll", scheduleUpdate, true);
701
- const resizeObserver = new ResizeObserver(scheduleUpdate);
702
- resizeObserver.observe(reference);
703
- resizeObserver.observe(floating);
704
- let cleaned = false;
705
- return function cleanup() {
706
- if (cleaned) {
707
- return;
708
- }
709
- cleaned = true;
710
- if (rafId !== void 0) {
711
- cancelAnimationFrame(rafId);
712
- }
713
- window.removeEventListener("resize", scheduleUpdate);
714
- window.removeEventListener("scroll", scheduleUpdate, true);
715
- resizeObserver.disconnect();
716
- const anchorClass = findAnchorClass(floating);
717
- if (anchorClass) {
718
- cleanupAnchorPositioning(reference, floating, anchorClass);
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
- if (value === null || value === void 0) {
724
- return true;
725
- }
726
- if (typeof value === "string") {
727
- return value.trim() === "";
728
- }
729
- if (typeof value === "number" || typeof value === "boolean") {
730
- return false;
731
- }
732
- if (Array.isArray(value) || typeof value === "object" && value !== null && typeof value.length === "number") {
733
- return value.length === 0;
734
- }
735
- return !value;
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
- let frame;
739
- return (...params) => {
740
- if (frame) {
741
- cancelAnimationFrame(frame);
742
- }
743
- frame = requestAnimationFrame(() => {
744
- fn(...params);
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
- return new Promise((resolve) => {
750
- if (typeof window === "undefined" || !window.requestAnimationFrame) {
751
- return resolve(60);
752
- }
753
- const timestamps = [];
754
- let rafId;
755
- function measure(now) {
756
- timestamps.unshift(now);
757
- if (timestamps.length > 10) {
758
- const lastTime = timestamps.pop();
759
- const fps = Math.floor(1e3 * 10 / (now - lastTime));
760
- cancelAnimationFrame(rafId);
761
- resolve(Math.min(Math.max(fps, 30), 240));
762
- return;
763
- }
764
- rafId = window.requestAnimationFrame(measure);
765
- }
766
- rafId = window.requestAnimationFrame(measure);
767
- setTimeout(() => {
768
- if (rafId !== void 0) {
769
- cancelAnimationFrame(rafId);
770
- }
771
- resolve(60);
772
- }, 1e3);
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
- if (typeof strategy === "function") {
777
- return strategy();
778
- }
779
- const containsDarkClass = () => document.documentElement.classList.contains("dark");
780
- const matchesDarkMediaQuery = () => window.matchMedia("(prefers-color-scheme: dark)").matches;
781
- if (strategy === "class" || strategy === "selector") {
782
- return containsDarkClass();
783
- }
784
- if (strategy === "media") {
785
- return matchesDarkMediaQuery();
786
- }
787
- return containsDarkClass() || matchesDarkMediaQuery();
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
- return document.documentElement.dir === "rtl";
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
- const observer = new MutationObserver(() => {
794
- callback(document.documentElement.dir === "rtl");
795
- });
796
- observer.observe(document.documentElement, {
797
- attributes: true,
798
- attributeFilter: ["dir"]
799
- });
800
- return () => observer.disconnect();
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
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
804
- if (!result) return { h: 0, s: 100, l: 50 };
805
- let r = parseInt(result[1], 16) / 255;
806
- let g = parseInt(result[2], 16) / 255;
807
- let b = parseInt(result[3], 16) / 255;
808
- const max = Math.max(r, g, b);
809
- const min = Math.min(r, g, b);
810
- let h = 0;
811
- let s = 0;
812
- const l = (max + min) / 2;
813
- if (max !== min) {
814
- const d = max - min;
815
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
816
- switch (max) {
817
- case r:
818
- h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
819
- break;
820
- case g:
821
- h = ((b - r) / d + 2) / 6;
822
- break;
823
- case b:
824
- h = ((r - g) / d + 4) / 6;
825
- break;
826
- }
827
- }
828
- return { h: Math.round(h * 360), s: Math.round(s * 100), l: Math.round(l * 100) };
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
- s /= 100;
832
- l /= 100;
833
- const c = (1 - Math.abs(2 * l - 1)) * s;
834
- const x = c * (1 - Math.abs(h / 60 % 2 - 1));
835
- const m = l - c / 2;
836
- let r = 0, g = 0, b = 0;
837
- if (0 <= h && h < 60) {
838
- r = c;
839
- g = x;
840
- b = 0;
841
- } else if (60 <= h && h < 120) {
842
- r = x;
843
- g = c;
844
- b = 0;
845
- } else if (120 <= h && h < 180) {
846
- r = 0;
847
- g = c;
848
- b = x;
849
- } else if (180 <= h && h < 240) {
850
- r = 0;
851
- g = x;
852
- b = c;
853
- } else if (240 <= h && h < 300) {
854
- r = x;
855
- g = 0;
856
- b = c;
857
- } else if (300 <= h && h < 360) {
858
- r = c;
859
- g = 0;
860
- b = x;
861
- }
862
- const toHex = (n) => {
863
- const hex = Math.round((n + m) * 255).toString(16);
864
- return hex.length === 1 ? "0" + hex : hex;
865
- };
866
- return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
867
- }
868
- export {
869
- animate,
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 };