@inertiaui/vanilla 0.1.2 → 0.2.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 +6 -3
- package/dist/index.d.ts +80 -5
- package/dist/index.js +64 -45
- package/package.json +20 -2
- package/dist/animate.d.ts +0 -33
- package/dist/dialog.d.ts +0 -14
- package/dist/helpers.d.ts +0 -10
package/README.md
CHANGED
|
@@ -108,6 +108,7 @@ The focus trap:
|
|
|
108
108
|
- Catches focus that escapes (e.g., via mouse click outside)
|
|
109
109
|
- Optionally focuses the first focusable element on creation
|
|
110
110
|
- Optionally returns focus to the previously focused element on cleanup
|
|
111
|
+
- Supports nesting: when multiple traps are active, only the most recently created trap receives focus. Cleaning up the inner trap restores the outer trap.
|
|
111
112
|
|
|
112
113
|
```typescript
|
|
113
114
|
const container = document.getElementById('dialog')
|
|
@@ -130,7 +131,7 @@ The focus trap recognizes these elements as focusable:
|
|
|
130
131
|
- `select:not([disabled])`
|
|
131
132
|
- `[tabindex]:not([tabindex="-1"])`
|
|
132
133
|
|
|
133
|
-
Elements with `aria-hidden`
|
|
134
|
+
Elements with `aria-hidden="true"` are excluded. (Elements with `disabled` are already filtered by the selectors above.)
|
|
134
135
|
|
|
135
136
|
## Keyboard Events
|
|
136
137
|
|
|
@@ -283,7 +284,7 @@ The animation module provides a simple wrapper around the Web Animations API wit
|
|
|
283
284
|
|
|
284
285
|
### animate
|
|
285
286
|
|
|
286
|
-
Animate an element using the Web Animations API. Returns a promise that resolves when the animation completes.
|
|
287
|
+
Animate an element using the Web Animations API. Returns a promise that resolves when the animation completes. If the animation is cancelled (e.g., by calling `cancelAnimations`), the promise resolves with the `Animation` object instead of rejecting.
|
|
287
288
|
|
|
288
289
|
```typescript
|
|
289
290
|
import { animate } from '@inertiaui/vanilla'
|
|
@@ -533,7 +534,9 @@ kebabCase('already-kebab') // 'already-kebab'
|
|
|
533
534
|
```typescript
|
|
534
535
|
kebabCase('user123Name') // 'user123-name'
|
|
535
536
|
kebabCase('multiple__underscores') // 'multiple-underscores'
|
|
536
|
-
kebabCase('UPPERCASE') // '
|
|
537
|
+
kebabCase('UPPERCASE') // 'uppercase'
|
|
538
|
+
kebabCase('XMLDocument') // 'xml-document'
|
|
539
|
+
kebabCase('hello world') // 'hello-world'
|
|
537
540
|
```
|
|
538
541
|
|
|
539
542
|
#### isStandardDomEvent
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Animate an element using the Web Animations API.
|
|
3
|
+
* Returns a promise that resolves when the animation completes.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* await animate(element, [
|
|
7
|
+
* { transform: 'scale(0.95)', opacity: 0 },
|
|
8
|
+
* { transform: 'scale(1)', opacity: 1 }
|
|
9
|
+
* ])
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* await animate(element, keyframes.fadeIn, { duration: 200 })
|
|
13
|
+
*/
|
|
14
|
+
export declare function animate(element: HTMLElement, keyframes: Keyframe[], options?: AnimateOptions): Promise<Animation>;
|
|
15
|
+
|
|
16
|
+
export declare interface AnimateOptions {
|
|
17
|
+
duration?: number;
|
|
18
|
+
easing?: string | EasingName;
|
|
19
|
+
fill?: FillMode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Cancel any running animations on an element
|
|
24
|
+
*/
|
|
25
|
+
export declare function cancelAnimations(element: HTMLElement): void;
|
|
26
|
+
|
|
27
|
+
export declare type CleanupFunction = () => void;
|
|
28
|
+
|
|
29
|
+
export declare function createFocusTrap(container: HTMLElement, options?: FocusTrapOptions): CleanupFunction;
|
|
30
|
+
|
|
31
|
+
export declare type EasingName = keyof typeof easings;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Tailwind CSS easing functions
|
|
35
|
+
*/
|
|
36
|
+
export declare const easings: {
|
|
37
|
+
readonly linear: "linear";
|
|
38
|
+
readonly in: "cubic-bezier(0.4, 0, 1, 1)";
|
|
39
|
+
readonly out: "cubic-bezier(0, 0, 0.2, 1)";
|
|
40
|
+
readonly inOut: "cubic-bezier(0.4, 0, 0.2, 1)";
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export declare interface EscapeKeyOptions {
|
|
44
|
+
preventDefault?: boolean;
|
|
45
|
+
stopPropagation?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export declare function except<T extends Record<string, unknown>>(target: T, keys: string[], ignoreCase?: boolean): Partial<T>;
|
|
49
|
+
|
|
50
|
+
export declare function except(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
51
|
+
|
|
52
|
+
export declare interface FocusTrapOptions {
|
|
53
|
+
initialFocus?: boolean;
|
|
54
|
+
initialFocusElement?: HTMLElement | null;
|
|
55
|
+
returnFocus?: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export declare function generateId(prefix?: string): string;
|
|
59
|
+
|
|
60
|
+
export declare function isStandardDomEvent(eventName: string): boolean;
|
|
61
|
+
|
|
62
|
+
export declare function kebabCase(string: string): string;
|
|
63
|
+
|
|
64
|
+
export declare function lockScroll(): CleanupFunction;
|
|
65
|
+
|
|
66
|
+
export declare function markAriaHidden(elementOrSelector: Element | string): CleanupFunction;
|
|
67
|
+
|
|
68
|
+
export declare function onEscapeKey(callback: (event: KeyboardEvent) => void, options?: EscapeKeyOptions): CleanupFunction;
|
|
69
|
+
|
|
70
|
+
export declare function only<T extends Record<string, unknown>>(target: T, keys: string[], ignoreCase?: boolean): Partial<T>;
|
|
71
|
+
|
|
72
|
+
export declare function only(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
73
|
+
|
|
74
|
+
export declare function rejectNullValues<T>(target: T[]): Exclude<T, null>[];
|
|
75
|
+
|
|
76
|
+
export declare function rejectNullValues<T extends Record<string, unknown>>(target: T): Partial<T>;
|
|
77
|
+
|
|
78
|
+
export declare function sameUrlPath(url1: string | URL | undefined | null, url2: string | URL | undefined | null): boolean;
|
|
79
|
+
|
|
80
|
+
export { }
|
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,58 @@ 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
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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();
|
|
164
163
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
event.preventDefault();
|
|
170
|
-
lastElement.focus();
|
|
171
|
-
}
|
|
172
|
-
} else {
|
|
173
|
-
if (document.activeElement === lastElement || !container.contains(document.activeElement)) {
|
|
174
|
-
event.preventDefault();
|
|
175
|
-
firstElement.focus();
|
|
176
|
-
}
|
|
164
|
+
} else {
|
|
165
|
+
if (document.activeElement === lastElement || !container.contains(document.activeElement)) {
|
|
166
|
+
event.preventDefault();
|
|
167
|
+
firstElement.focus();
|
|
177
168
|
}
|
|
178
169
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
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();
|
|
185
180
|
}
|
|
186
181
|
}
|
|
187
|
-
|
|
188
|
-
|
|
182
|
+
}
|
|
183
|
+
function createFocusTrap(container, options = {}) {
|
|
184
|
+
const { initialFocus = true, initialFocusElement = null, returnFocus = true } = options;
|
|
185
|
+
const previouslyFocused = document.activeElement;
|
|
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
|
+
let rafId;
|
|
189
193
|
if (initialFocus) {
|
|
190
|
-
requestAnimationFrame(() => {
|
|
194
|
+
rafId = requestAnimationFrame(() => {
|
|
195
|
+
rafId = void 0;
|
|
191
196
|
if (initialFocusElement && container.contains(initialFocusElement)) {
|
|
192
197
|
initialFocusElement.focus();
|
|
193
198
|
} else {
|
|
@@ -198,9 +203,23 @@ function createFocusTrap(container, options = {}) {
|
|
|
198
203
|
}
|
|
199
204
|
});
|
|
200
205
|
}
|
|
206
|
+
let cleaned = false;
|
|
201
207
|
return function cleanup() {
|
|
202
|
-
|
|
203
|
-
|
|
208
|
+
if (cleaned) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
cleaned = true;
|
|
212
|
+
if (rafId !== void 0) {
|
|
213
|
+
cancelAnimationFrame(rafId);
|
|
214
|
+
}
|
|
215
|
+
const index = focusTrapStack.indexOf(entry);
|
|
216
|
+
if (index >= 0) {
|
|
217
|
+
focusTrapStack.splice(index, 1);
|
|
218
|
+
}
|
|
219
|
+
if (focusTrapStack.length === 0) {
|
|
220
|
+
document.removeEventListener("keydown", handleTrapKeyDown);
|
|
221
|
+
document.removeEventListener("focusin", handleTrapFocusIn);
|
|
222
|
+
}
|
|
204
223
|
if (returnFocus && previouslyFocused && typeof previouslyFocused.focus === "function") {
|
|
205
224
|
previouslyFocused.focus();
|
|
206
225
|
}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inertiaui/vanilla",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.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,11 +17,28 @@
|
|
|
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
40
|
"format": "prettier --write src/ tests/",
|
|
41
|
+
"format:check": "prettier --check src/ tests/",
|
|
24
42
|
"test": "vitest",
|
|
25
43
|
"prepublishOnly": "npm run build"
|
|
26
44
|
},
|
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;
|
package/dist/helpers.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export declare function generateId(prefix?: string): string;
|
|
2
|
-
export declare function except<T extends Record<string, unknown>>(target: T, keys: string[], ignoreCase?: boolean): Partial<T>;
|
|
3
|
-
export declare function except(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
4
|
-
export declare function only<T extends Record<string, unknown>>(target: T, keys: string[], ignoreCase?: boolean): Partial<T>;
|
|
5
|
-
export declare function only(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
6
|
-
export declare function rejectNullValues<T>(target: T[]): NonNullable<T>[];
|
|
7
|
-
export declare function rejectNullValues<T extends Record<string, unknown>>(target: T): Partial<T>;
|
|
8
|
-
export declare function kebabCase(string: string): string;
|
|
9
|
-
export declare function isStandardDomEvent(eventName: string): boolean;
|
|
10
|
-
export declare function sameUrlPath(url1: string | URL | undefined | null, url2: string | URL | undefined | null): boolean;
|