@inertiaui/vanilla 0.1.2 → 0.3.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 +375 -41
- package/dist/index.d.ts +113 -5
- package/dist/index.js +492 -45
- package/package.json +23 -3
- package/dist/animate.d.ts +0 -33
- package/dist/dialog.d.ts +0 -14
- package/dist/helpers.d.ts +0 -10
package/dist/index.js
CHANGED
|
@@ -2,10 +2,10 @@ function generateId(prefix = "inertiaui_") {
|
|
|
2
2
|
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
3
3
|
return `${prefix}${crypto.randomUUID()}`;
|
|
4
4
|
}
|
|
5
|
-
return `${prefix}${Date.now().toString(36)}_${Math.random().toString(36).
|
|
5
|
+
return `${prefix}${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 11)}`;
|
|
6
6
|
}
|
|
7
7
|
function strToLowercase(key) {
|
|
8
|
-
return
|
|
8
|
+
return key.toLowerCase();
|
|
9
9
|
}
|
|
10
10
|
function except(target, keys, ignoreCase = false) {
|
|
11
11
|
if (ignoreCase) {
|
|
@@ -48,14 +48,7 @@ function rejectNullValues(target) {
|
|
|
48
48
|
}
|
|
49
49
|
function kebabCase(string) {
|
|
50
50
|
if (!string) return "";
|
|
51
|
-
|
|
52
|
-
string = string.replace(/-+/g, "-");
|
|
53
|
-
if (!/[A-Z]/.test(string)) {
|
|
54
|
-
return string;
|
|
55
|
-
}
|
|
56
|
-
string = string.replace(/\s+/g, "").replace(/_/g, "").replace(/(?:^|\s|-)+([A-Za-z])/g, (_m, p1) => p1.toUpperCase());
|
|
57
|
-
string = string.replace(/(.)(?=[A-Z])/g, "$1-");
|
|
58
|
-
return string.toLowerCase();
|
|
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();
|
|
59
52
|
}
|
|
60
53
|
function isStandardDomEvent(eventName) {
|
|
61
54
|
const lowerEventName = eventName.toLowerCase();
|
|
@@ -100,7 +93,7 @@ function animate(element, keyframes, options = {}) {
|
|
|
100
93
|
easing: resolvedEasing,
|
|
101
94
|
fill
|
|
102
95
|
});
|
|
103
|
-
return animation.finished;
|
|
96
|
+
return animation.finished.catch(() => animation);
|
|
104
97
|
}
|
|
105
98
|
function cancelAnimations(element) {
|
|
106
99
|
element.getAnimations().forEach((animation) => animation.cancel());
|
|
@@ -148,46 +141,72 @@ function getFocusableElements(container) {
|
|
|
148
141
|
return [];
|
|
149
142
|
}
|
|
150
143
|
return Array.from(container.querySelectorAll(FOCUSABLE_SELECTORS)).filter(
|
|
151
|
-
(el) =>
|
|
144
|
+
(el) => el.getAttribute("aria-hidden") !== "true"
|
|
152
145
|
);
|
|
153
146
|
}
|
|
147
|
+
const focusTrapStack = [];
|
|
148
|
+
function handleTrapKeyDown(event) {
|
|
149
|
+
if (event.key !== "Tab" || focusTrapStack.length === 0) {
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const { container } = focusTrapStack[focusTrapStack.length - 1];
|
|
153
|
+
const focusableElements = getFocusableElements(container);
|
|
154
|
+
if (focusableElements.length === 0) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const firstElement = focusableElements[0];
|
|
158
|
+
const lastElement = focusableElements[focusableElements.length - 1];
|
|
159
|
+
if (event.shiftKey) {
|
|
160
|
+
if (document.activeElement === firstElement || !container.contains(document.activeElement)) {
|
|
161
|
+
event.preventDefault();
|
|
162
|
+
lastElement.focus();
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
if (document.activeElement === lastElement || !container.contains(document.activeElement)) {
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
firstElement.focus();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function handleTrapFocusIn(event) {
|
|
172
|
+
if (focusTrapStack.length === 0) {
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const { container } = focusTrapStack[focusTrapStack.length - 1];
|
|
176
|
+
if (!container.contains(event.target)) {
|
|
177
|
+
const focusableElements = getFocusableElements(container);
|
|
178
|
+
if (focusableElements.length > 0) {
|
|
179
|
+
focusableElements[0].focus();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
154
183
|
function createFocusTrap(container, options = {}) {
|
|
155
184
|
const { initialFocus = true, initialFocusElement = null, returnFocus = true } = options;
|
|
156
185
|
const previouslyFocused = document.activeElement;
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
186
|
+
const entry = { container };
|
|
187
|
+
if (focusTrapStack.length === 0) {
|
|
188
|
+
document.addEventListener("keydown", handleTrapKeyDown);
|
|
189
|
+
document.addEventListener("focusin", handleTrapFocusIn);
|
|
190
|
+
}
|
|
191
|
+
focusTrapStack.push(entry);
|
|
192
|
+
function handleFocusOut(event) {
|
|
193
|
+
if (event.relatedTarget !== null) {
|
|
163
194
|
return;
|
|
164
195
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
} else {
|
|
173
|
-
if (document.activeElement === lastElement || !container.contains(document.activeElement)) {
|
|
174
|
-
event.preventDefault();
|
|
175
|
-
firstElement.focus();
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
function handleFocusIn(event) {
|
|
180
|
-
if (!container.contains(event.target)) {
|
|
181
|
-
const focusableElements = getFocusableElements(container);
|
|
182
|
-
if (focusableElements.length > 0) {
|
|
183
|
-
focusableElements[0].focus();
|
|
196
|
+
queueMicrotask(() => {
|
|
197
|
+
if (!container.contains(container.ownerDocument.activeElement)) {
|
|
198
|
+
const focusableElements = getFocusableElements(container);
|
|
199
|
+
if (focusableElements.length > 0) {
|
|
200
|
+
focusableElements[0].focus();
|
|
201
|
+
}
|
|
184
202
|
}
|
|
185
|
-
}
|
|
203
|
+
});
|
|
186
204
|
}
|
|
187
|
-
|
|
188
|
-
|
|
205
|
+
container.addEventListener("focusout", handleFocusOut);
|
|
206
|
+
let rafId;
|
|
189
207
|
if (initialFocus) {
|
|
190
|
-
requestAnimationFrame(() => {
|
|
208
|
+
rafId = requestAnimationFrame(() => {
|
|
209
|
+
rafId = void 0;
|
|
191
210
|
if (initialFocusElement && container.contains(initialFocusElement)) {
|
|
192
211
|
initialFocusElement.focus();
|
|
193
212
|
} else {
|
|
@@ -198,10 +217,25 @@ function createFocusTrap(container, options = {}) {
|
|
|
198
217
|
}
|
|
199
218
|
});
|
|
200
219
|
}
|
|
220
|
+
let cleaned = false;
|
|
201
221
|
return function cleanup() {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
222
|
+
if (cleaned) {
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
cleaned = true;
|
|
226
|
+
if (rafId !== void 0) {
|
|
227
|
+
cancelAnimationFrame(rafId);
|
|
228
|
+
}
|
|
229
|
+
container.removeEventListener("focusout", handleFocusOut);
|
|
230
|
+
const index = focusTrapStack.indexOf(entry);
|
|
231
|
+
if (index >= 0) {
|
|
232
|
+
focusTrapStack.splice(index, 1);
|
|
233
|
+
}
|
|
234
|
+
if (focusTrapStack.length === 0) {
|
|
235
|
+
document.removeEventListener("keydown", handleTrapKeyDown);
|
|
236
|
+
document.removeEventListener("focusin", handleTrapFocusIn);
|
|
237
|
+
}
|
|
238
|
+
if (returnFocus && previouslyFocused && typeof previouslyFocused.focus === "function" && previouslyFocused.isConnected) {
|
|
205
239
|
previouslyFocused.focus();
|
|
206
240
|
}
|
|
207
241
|
};
|
|
@@ -264,10 +298,421 @@ function unmarkAriaHidden(element) {
|
|
|
264
298
|
ariaHiddenStack.splice(index, 1);
|
|
265
299
|
}
|
|
266
300
|
}
|
|
301
|
+
function onClickOutside(elements, callback) {
|
|
302
|
+
const targets = Array.isArray(elements) ? elements : [elements];
|
|
303
|
+
function handler(event) {
|
|
304
|
+
const target = event.target;
|
|
305
|
+
for (const el of targets) {
|
|
306
|
+
if (el.contains(target)) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
let node = target;
|
|
311
|
+
while (node) {
|
|
312
|
+
if (node instanceof HTMLElement && node.hasAttribute("data-inertiaui-portal")) {
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
node = node.parentNode;
|
|
316
|
+
}
|
|
317
|
+
callback(event);
|
|
318
|
+
}
|
|
319
|
+
const timeoutId = setTimeout(() => {
|
|
320
|
+
document.addEventListener("pointerdown", handler, true);
|
|
321
|
+
}, 0);
|
|
322
|
+
let cleaned = false;
|
|
323
|
+
return function cleanup() {
|
|
324
|
+
if (cleaned) {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
cleaned = true;
|
|
328
|
+
clearTimeout(timeoutId);
|
|
329
|
+
document.removeEventListener("pointerdown", handler, true);
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
function createMenuNavigation(container, options = {}) {
|
|
333
|
+
const {
|
|
334
|
+
itemSelector = '[role="menuitem"]:not([disabled]):not([aria-disabled="true"])',
|
|
335
|
+
orientation = "vertical",
|
|
336
|
+
loop = true,
|
|
337
|
+
typeAhead = true,
|
|
338
|
+
onActivate
|
|
339
|
+
} = options;
|
|
340
|
+
let typeAheadBuffer = "";
|
|
341
|
+
let typeAheadTimeout;
|
|
342
|
+
function getItems() {
|
|
343
|
+
return Array.from(container.querySelectorAll(itemSelector));
|
|
344
|
+
}
|
|
345
|
+
function getActiveIndex() {
|
|
346
|
+
const items2 = getItems();
|
|
347
|
+
const focused = document.activeElement;
|
|
348
|
+
return items2.indexOf(focused);
|
|
349
|
+
}
|
|
350
|
+
function focusItem(index) {
|
|
351
|
+
const items2 = getItems();
|
|
352
|
+
if (items2.length === 0) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const targetIndex = loop ? (index % items2.length + items2.length) % items2.length : Math.max(0, Math.min(index, items2.length - 1));
|
|
356
|
+
const item = items2[targetIndex];
|
|
357
|
+
items2.forEach((el) => el.setAttribute("tabindex", "-1"));
|
|
358
|
+
item.setAttribute("tabindex", "0");
|
|
359
|
+
item.focus();
|
|
360
|
+
}
|
|
361
|
+
function handleKeyDown(event) {
|
|
362
|
+
const items2 = getItems();
|
|
363
|
+
if (items2.length === 0) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
const nextKey = orientation === "vertical" ? "ArrowDown" : "ArrowRight";
|
|
367
|
+
const prevKey = orientation === "vertical" ? "ArrowUp" : "ArrowLeft";
|
|
368
|
+
switch (event.key) {
|
|
369
|
+
case nextKey: {
|
|
370
|
+
event.preventDefault();
|
|
371
|
+
const currentIndex = getActiveIndex();
|
|
372
|
+
focusItem(currentIndex + 1);
|
|
373
|
+
break;
|
|
374
|
+
}
|
|
375
|
+
case prevKey: {
|
|
376
|
+
event.preventDefault();
|
|
377
|
+
const currentIndex = getActiveIndex();
|
|
378
|
+
focusItem(currentIndex - 1);
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
case "Home": {
|
|
382
|
+
event.preventDefault();
|
|
383
|
+
focusItem(0);
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "End": {
|
|
387
|
+
event.preventDefault();
|
|
388
|
+
focusItem(items2.length - 1);
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
case "Enter":
|
|
392
|
+
case " ": {
|
|
393
|
+
event.preventDefault();
|
|
394
|
+
const focused = document.activeElement;
|
|
395
|
+
if (items2.includes(focused)) {
|
|
396
|
+
focused.click();
|
|
397
|
+
onActivate == null ? void 0 : onActivate(focused);
|
|
398
|
+
}
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
default: {
|
|
402
|
+
if (typeAhead && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
403
|
+
event.preventDefault();
|
|
404
|
+
typeAheadBuffer += event.key.toLowerCase();
|
|
405
|
+
if (typeAheadTimeout) {
|
|
406
|
+
clearTimeout(typeAheadTimeout);
|
|
407
|
+
}
|
|
408
|
+
typeAheadTimeout = setTimeout(() => {
|
|
409
|
+
typeAheadBuffer = "";
|
|
410
|
+
}, 350);
|
|
411
|
+
const match = items2.find((item) => {
|
|
412
|
+
const text = (item.textContent || "").trim().toLowerCase();
|
|
413
|
+
return text.startsWith(typeAheadBuffer);
|
|
414
|
+
});
|
|
415
|
+
if (match) {
|
|
416
|
+
const index = items2.indexOf(match);
|
|
417
|
+
focusItem(index);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
break;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
container.addEventListener("keydown", handleKeyDown);
|
|
425
|
+
const items = getItems();
|
|
426
|
+
items.forEach((el, i) => el.setAttribute("tabindex", i === 0 ? "0" : "-1"));
|
|
427
|
+
let rafId;
|
|
428
|
+
if (items.length > 0) {
|
|
429
|
+
rafId = requestAnimationFrame(() => {
|
|
430
|
+
rafId = void 0;
|
|
431
|
+
items[0].focus();
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
let cleaned = false;
|
|
435
|
+
return function cleanup() {
|
|
436
|
+
if (cleaned) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
cleaned = true;
|
|
440
|
+
if (rafId !== void 0) {
|
|
441
|
+
cancelAnimationFrame(rafId);
|
|
442
|
+
}
|
|
443
|
+
if (typeAheadTimeout) {
|
|
444
|
+
clearTimeout(typeAheadTimeout);
|
|
445
|
+
}
|
|
446
|
+
container.removeEventListener("keydown", handleKeyDown);
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
const ANCHOR_CLASS_PREFIX = "iui-anchor-";
|
|
450
|
+
let _supportsAnchor = null;
|
|
451
|
+
function supportsAnchorPositioning() {
|
|
452
|
+
if (_supportsAnchor === null) {
|
|
453
|
+
_supportsAnchor = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("anchor-name", "--a");
|
|
454
|
+
}
|
|
455
|
+
return _supportsAnchor;
|
|
456
|
+
}
|
|
457
|
+
function isRtl() {
|
|
458
|
+
return typeof document !== "undefined" && document.documentElement.dir === "rtl";
|
|
459
|
+
}
|
|
460
|
+
function getOppositePlacement(placement) {
|
|
461
|
+
const map = {
|
|
462
|
+
top: "bottom",
|
|
463
|
+
"top-start": "bottom-start",
|
|
464
|
+
"top-end": "bottom-end",
|
|
465
|
+
bottom: "top",
|
|
466
|
+
"bottom-start": "top-start",
|
|
467
|
+
"bottom-end": "top-end",
|
|
468
|
+
left: "right",
|
|
469
|
+
"left-start": "right-start",
|
|
470
|
+
"left-end": "right-end",
|
|
471
|
+
right: "left",
|
|
472
|
+
"right-start": "left-start",
|
|
473
|
+
"right-end": "left-end"
|
|
474
|
+
};
|
|
475
|
+
return map[placement] || placement;
|
|
476
|
+
}
|
|
477
|
+
function computePositionFromRect(refRect, floatingRect, placement, offset) {
|
|
478
|
+
let x = 0;
|
|
479
|
+
let y = 0;
|
|
480
|
+
const [side, alignment] = placement.split("-");
|
|
481
|
+
const rtl = isRtl();
|
|
482
|
+
switch (side) {
|
|
483
|
+
case "bottom":
|
|
484
|
+
y = refRect.bottom + offset;
|
|
485
|
+
break;
|
|
486
|
+
case "top":
|
|
487
|
+
y = refRect.top - floatingRect.height - offset;
|
|
488
|
+
break;
|
|
489
|
+
case "left":
|
|
490
|
+
x = refRect.left - floatingRect.width - offset;
|
|
491
|
+
break;
|
|
492
|
+
case "right":
|
|
493
|
+
x = refRect.right + offset;
|
|
494
|
+
break;
|
|
495
|
+
}
|
|
496
|
+
if (side === "bottom" || side === "top") {
|
|
497
|
+
const startIsLeft = !rtl;
|
|
498
|
+
switch (alignment) {
|
|
499
|
+
case "start":
|
|
500
|
+
x = startIsLeft ? refRect.left : refRect.right - floatingRect.width;
|
|
501
|
+
break;
|
|
502
|
+
case "end":
|
|
503
|
+
x = startIsLeft ? refRect.right - floatingRect.width : refRect.left;
|
|
504
|
+
break;
|
|
505
|
+
default:
|
|
506
|
+
x = refRect.left + (refRect.width - floatingRect.width) / 2;
|
|
507
|
+
break;
|
|
508
|
+
}
|
|
509
|
+
} else {
|
|
510
|
+
switch (alignment) {
|
|
511
|
+
case "start":
|
|
512
|
+
y = refRect.top;
|
|
513
|
+
break;
|
|
514
|
+
case "end":
|
|
515
|
+
y = refRect.bottom - floatingRect.height;
|
|
516
|
+
break;
|
|
517
|
+
default:
|
|
518
|
+
y = refRect.top + (refRect.height - floatingRect.height) / 2;
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return { x, y };
|
|
523
|
+
}
|
|
524
|
+
function wouldOverflow(x, y, floatingRect, placement) {
|
|
525
|
+
const [side] = placement.split("-");
|
|
526
|
+
const vw = window.innerWidth;
|
|
527
|
+
const vh = window.innerHeight;
|
|
528
|
+
switch (side) {
|
|
529
|
+
case "bottom":
|
|
530
|
+
return y + floatingRect.height > vh;
|
|
531
|
+
case "top":
|
|
532
|
+
return y < 0;
|
|
533
|
+
case "left":
|
|
534
|
+
return x < 0;
|
|
535
|
+
case "right":
|
|
536
|
+
return x + floatingRect.width > vw;
|
|
537
|
+
default:
|
|
538
|
+
return false;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
function clampToViewport(x, y, floatingRect) {
|
|
542
|
+
const vw = window.innerWidth;
|
|
543
|
+
const vh = window.innerHeight;
|
|
544
|
+
const margin = 4;
|
|
545
|
+
return {
|
|
546
|
+
x: Math.max(margin, Math.min(x, vw - floatingRect.width - margin)),
|
|
547
|
+
y: Math.max(margin, Math.min(y, vh - floatingRect.height - margin))
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
function computeWithFallback(reference, floating, options = {}) {
|
|
551
|
+
const { placement = "bottom-start", offset = 0, flip = true } = options;
|
|
552
|
+
const refRect = reference.getBoundingClientRect();
|
|
553
|
+
const floatingRect = floating.getBoundingClientRect();
|
|
554
|
+
let { x, y } = computePositionFromRect(refRect, floatingRect, placement, offset);
|
|
555
|
+
let actualPlacement = placement;
|
|
556
|
+
if (flip && wouldOverflow(x, y, floatingRect, placement)) {
|
|
557
|
+
const flipped = getOppositePlacement(placement);
|
|
558
|
+
const flippedPos = computePositionFromRect(refRect, floatingRect, flipped, offset);
|
|
559
|
+
if (!wouldOverflow(flippedPos.x, flippedPos.y, floatingRect, flipped)) {
|
|
560
|
+
x = flippedPos.x;
|
|
561
|
+
y = flippedPos.y;
|
|
562
|
+
actualPlacement = flipped;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const clamped = clampToViewport(x, y, floatingRect);
|
|
566
|
+
return { x: clamped.x, y: clamped.y, placement: actualPlacement };
|
|
567
|
+
}
|
|
568
|
+
const anchorStyleId = "inertiaui-anchor-styles";
|
|
569
|
+
function ensureAnchorStylesheet() {
|
|
570
|
+
let style = document.getElementById(anchorStyleId);
|
|
571
|
+
if (!style) {
|
|
572
|
+
style = document.createElement("style");
|
|
573
|
+
style.id = anchorStyleId;
|
|
574
|
+
document.head.appendChild(style);
|
|
575
|
+
}
|
|
576
|
+
return style.sheet;
|
|
577
|
+
}
|
|
578
|
+
function findAnchorClass(element) {
|
|
579
|
+
return Array.from(element.classList).find((c) => c.startsWith(ANCHOR_CLASS_PREFIX));
|
|
580
|
+
}
|
|
581
|
+
function setupAnchorPositioning(reference, floating, placement, offset, flip) {
|
|
582
|
+
const id = generateId(ANCHOR_CLASS_PREFIX);
|
|
583
|
+
const anchorName = `--${id}`;
|
|
584
|
+
reference.style.anchorName = anchorName;
|
|
585
|
+
floating.style.positionAnchor = anchorName;
|
|
586
|
+
floating.style.position = "fixed";
|
|
587
|
+
const [side, alignment] = placement.split("-");
|
|
588
|
+
const sheet = ensureAnchorStylesheet();
|
|
589
|
+
const className = id;
|
|
590
|
+
let positionRules = "";
|
|
591
|
+
let tryFallback = "";
|
|
592
|
+
switch (side) {
|
|
593
|
+
case "bottom":
|
|
594
|
+
positionRules += `top: anchor(bottom);`;
|
|
595
|
+
positionRules += `margin-top: ${offset}px;`;
|
|
596
|
+
break;
|
|
597
|
+
case "top":
|
|
598
|
+
positionRules += `bottom: anchor(top);`;
|
|
599
|
+
positionRules += `margin-bottom: ${offset}px;`;
|
|
600
|
+
break;
|
|
601
|
+
case "left":
|
|
602
|
+
positionRules += `right: anchor(left);`;
|
|
603
|
+
positionRules += `margin-right: ${offset}px;`;
|
|
604
|
+
break;
|
|
605
|
+
case "right":
|
|
606
|
+
positionRules += `left: anchor(right);`;
|
|
607
|
+
positionRules += `margin-left: ${offset}px;`;
|
|
608
|
+
break;
|
|
609
|
+
}
|
|
610
|
+
if (side === "bottom" || side === "top") {
|
|
611
|
+
switch (alignment) {
|
|
612
|
+
case "start":
|
|
613
|
+
positionRules += `left: anchor(left);`;
|
|
614
|
+
break;
|
|
615
|
+
case "end":
|
|
616
|
+
positionRules += `right: anchor(right);`;
|
|
617
|
+
break;
|
|
618
|
+
default:
|
|
619
|
+
positionRules += `left: anchor(center); translate: -50% 0;`;
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
} else {
|
|
623
|
+
switch (alignment) {
|
|
624
|
+
case "start":
|
|
625
|
+
positionRules += `top: anchor(top);`;
|
|
626
|
+
break;
|
|
627
|
+
case "end":
|
|
628
|
+
positionRules += `bottom: anchor(bottom);`;
|
|
629
|
+
break;
|
|
630
|
+
default:
|
|
631
|
+
positionRules += `top: anchor(center); translate: 0 -50%;`;
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
if (flip) {
|
|
636
|
+
tryFallback = `position-try-fallbacks: flip-block, flip-inline;`;
|
|
637
|
+
}
|
|
638
|
+
const rule = `.${className} { ${positionRules} ${tryFallback} }`;
|
|
639
|
+
sheet.insertRule(rule, sheet.cssRules.length);
|
|
640
|
+
floating.classList.add(className);
|
|
641
|
+
return className;
|
|
642
|
+
}
|
|
643
|
+
function cleanupAnchorPositioning(reference, floating, className) {
|
|
644
|
+
reference.style.anchorName = "";
|
|
645
|
+
floating.style.positionAnchor = "";
|
|
646
|
+
floating.style.position = "";
|
|
647
|
+
floating.classList.remove(className);
|
|
648
|
+
const style = document.getElementById(anchorStyleId);
|
|
649
|
+
if (style == null ? void 0 : style.sheet) {
|
|
650
|
+
for (let i = 0; i < style.sheet.cssRules.length; i++) {
|
|
651
|
+
const rule = style.sheet.cssRules[i];
|
|
652
|
+
if (rule.selectorText === `.${className}`) {
|
|
653
|
+
style.sheet.deleteRule(i);
|
|
654
|
+
break;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
function computePosition(reference, floating, options = {}) {
|
|
660
|
+
const { placement = "bottom-start", offset = 0, flip = true } = options;
|
|
661
|
+
if (supportsAnchorPositioning()) {
|
|
662
|
+
const existingClass = findAnchorClass(floating);
|
|
663
|
+
if (!existingClass) {
|
|
664
|
+
setupAnchorPositioning(reference, floating, placement, offset, flip);
|
|
665
|
+
}
|
|
666
|
+
const rect = floating.getBoundingClientRect();
|
|
667
|
+
return { x: rect.left, y: rect.top, placement };
|
|
668
|
+
}
|
|
669
|
+
const result = computeWithFallback(reference, floating, { placement, offset, flip });
|
|
670
|
+
floating.style.position = "fixed";
|
|
671
|
+
floating.style.top = `${result.y}px`;
|
|
672
|
+
floating.style.left = `${result.x}px`;
|
|
673
|
+
return result;
|
|
674
|
+
}
|
|
675
|
+
function autoUpdate(reference, floating, update) {
|
|
676
|
+
let rafId;
|
|
677
|
+
function scheduleUpdate() {
|
|
678
|
+
if (rafId !== void 0) {
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
rafId = requestAnimationFrame(() => {
|
|
682
|
+
rafId = void 0;
|
|
683
|
+
update();
|
|
684
|
+
});
|
|
685
|
+
}
|
|
686
|
+
window.addEventListener("resize", scheduleUpdate);
|
|
687
|
+
window.addEventListener("scroll", scheduleUpdate, true);
|
|
688
|
+
const resizeObserver = new ResizeObserver(scheduleUpdate);
|
|
689
|
+
resizeObserver.observe(reference);
|
|
690
|
+
resizeObserver.observe(floating);
|
|
691
|
+
let cleaned = false;
|
|
692
|
+
return function cleanup() {
|
|
693
|
+
if (cleaned) {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
cleaned = true;
|
|
697
|
+
if (rafId !== void 0) {
|
|
698
|
+
cancelAnimationFrame(rafId);
|
|
699
|
+
}
|
|
700
|
+
window.removeEventListener("resize", scheduleUpdate);
|
|
701
|
+
window.removeEventListener("scroll", scheduleUpdate, true);
|
|
702
|
+
resizeObserver.disconnect();
|
|
703
|
+
const anchorClass = findAnchorClass(floating);
|
|
704
|
+
if (anchorClass) {
|
|
705
|
+
cleanupAnchorPositioning(reference, floating, anchorClass);
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
}
|
|
267
709
|
export {
|
|
268
710
|
animate,
|
|
711
|
+
autoUpdate,
|
|
269
712
|
cancelAnimations,
|
|
713
|
+
computePosition,
|
|
270
714
|
createFocusTrap,
|
|
715
|
+
createMenuNavigation,
|
|
271
716
|
easings,
|
|
272
717
|
except,
|
|
273
718
|
generateId,
|
|
@@ -275,8 +720,10 @@ export {
|
|
|
275
720
|
kebabCase,
|
|
276
721
|
lockScroll,
|
|
277
722
|
markAriaHidden,
|
|
723
|
+
onClickOutside,
|
|
278
724
|
onEscapeKey,
|
|
279
725
|
only,
|
|
280
726
|
rejectNullValues,
|
|
281
|
-
sameUrlPath
|
|
727
|
+
sameUrlPath,
|
|
728
|
+
supportsAnchorPositioning
|
|
282
729
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inertiaui/vanilla",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"author": "Pascal Baljet <pascal@protone.media>",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
8
9
|
"files": [
|
|
9
10
|
"dist"
|
|
10
11
|
],
|
|
@@ -16,15 +17,34 @@
|
|
|
16
17
|
"import": "./dist/index.js"
|
|
17
18
|
}
|
|
18
19
|
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "https://github.com/inertiaui/vanilla"
|
|
23
|
+
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"dialog",
|
|
26
|
+
"focus-trap",
|
|
27
|
+
"scroll-lock",
|
|
28
|
+
"aria-hidden",
|
|
29
|
+
"animation",
|
|
30
|
+
"web-animations-api",
|
|
31
|
+
"accessibility",
|
|
32
|
+
"a11y",
|
|
33
|
+
"vanilla-js",
|
|
34
|
+
"typescript"
|
|
35
|
+
],
|
|
19
36
|
"scripts": {
|
|
20
37
|
"dev": "vite build --watch",
|
|
21
|
-
"build": "vite build
|
|
38
|
+
"build": "vite build",
|
|
22
39
|
"lint": "eslint src/",
|
|
23
|
-
"format": "prettier --write src/
|
|
40
|
+
"format": "prettier --write src/ e2e/",
|
|
41
|
+
"format:check": "prettier --check src/ e2e/",
|
|
24
42
|
"test": "vitest",
|
|
43
|
+
"test:e2e": "playwright test",
|
|
25
44
|
"prepublishOnly": "npm run build"
|
|
26
45
|
},
|
|
27
46
|
"devDependencies": {
|
|
47
|
+
"@playwright/test": "^1.58.2",
|
|
28
48
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
29
49
|
"@typescript-eslint/parser": "^8.0.0",
|
|
30
50
|
"@vitest/coverage-v8": "^3.1.0",
|
package/dist/animate.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tailwind CSS easing functions
|
|
3
|
-
*/
|
|
4
|
-
export declare const easings: {
|
|
5
|
-
readonly linear: "linear";
|
|
6
|
-
readonly in: "cubic-bezier(0.4, 0, 1, 1)";
|
|
7
|
-
readonly out: "cubic-bezier(0, 0, 0.2, 1)";
|
|
8
|
-
readonly inOut: "cubic-bezier(0.4, 0, 0.2, 1)";
|
|
9
|
-
};
|
|
10
|
-
export type EasingName = keyof typeof easings;
|
|
11
|
-
export interface AnimateOptions {
|
|
12
|
-
duration?: number;
|
|
13
|
-
easing?: string | EasingName;
|
|
14
|
-
fill?: FillMode;
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Animate an element using the Web Animations API.
|
|
18
|
-
* Returns a promise that resolves when the animation completes.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* await animate(element, [
|
|
22
|
-
* { transform: 'scale(0.95)', opacity: 0 },
|
|
23
|
-
* { transform: 'scale(1)', opacity: 1 }
|
|
24
|
-
* ])
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* await animate(element, keyframes.fadeIn, { duration: 200 })
|
|
28
|
-
*/
|
|
29
|
-
export declare function animate(element: HTMLElement, keyframes: Keyframe[], options?: AnimateOptions): Promise<Animation>;
|
|
30
|
-
/**
|
|
31
|
-
* Cancel any running animations on an element
|
|
32
|
-
*/
|
|
33
|
-
export declare function cancelAnimations(element: HTMLElement): void;
|
package/dist/dialog.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
export type CleanupFunction = () => void;
|
|
2
|
-
export declare function lockScroll(): CleanupFunction;
|
|
3
|
-
export interface FocusTrapOptions {
|
|
4
|
-
initialFocus?: boolean;
|
|
5
|
-
initialFocusElement?: HTMLElement | null;
|
|
6
|
-
returnFocus?: boolean;
|
|
7
|
-
}
|
|
8
|
-
export declare function createFocusTrap(container: HTMLElement, options?: FocusTrapOptions): CleanupFunction;
|
|
9
|
-
export interface EscapeKeyOptions {
|
|
10
|
-
preventDefault?: boolean;
|
|
11
|
-
stopPropagation?: boolean;
|
|
12
|
-
}
|
|
13
|
-
export declare function onEscapeKey(callback: (event: KeyboardEvent) => void, options?: EscapeKeyOptions): CleanupFunction;
|
|
14
|
-
export declare function markAriaHidden(elementOrSelector: Element | string): CleanupFunction;
|