@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/README.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
# Inertia UI Vanilla
|
|
2
2
|
|
|
3
|
-
A lightweight vanilla TypeScript library providing UI utilities for dialogs, animations, focus management, and common helper functions. Framework-agnostic and designed to integrate seamlessly with Vue, React, or any JavaScript application.
|
|
3
|
+
A lightweight vanilla TypeScript library providing UI utilities for dialogs, animations, focus management, menu navigation, click outside detection, floating element positioning, and common helper functions. Framework-agnostic and designed to integrate seamlessly with Vue, React, or any JavaScript application.
|
|
4
|
+
|
|
5
|
+
This package is part of the [Inertia UI](https://inertiaui.com) suite. Check out our other packages:
|
|
6
|
+
|
|
7
|
+
- **[Inertia Modal](https://inertiaui.com/inertia-modal/docs/)**: Turn any Laravel route into a modal or slideover with a single component. No backend changes needed, with support for nested/stacked modals and inter-modal communication. Works with Vue and React.
|
|
8
|
+
- **[Inertia Table](https://inertiaui.com/inertia-table)**: The most complete data table package for Laravel and Inertia.js. Sorting, searching, and filtering across relationships, bulk actions, CSV/Excel/PDF exports, sticky headers, and much more. Works with Vue and React.
|
|
9
|
+
|
|
10
|
+
[](https://inertiaui.com)
|
|
4
11
|
|
|
5
12
|
## Installation
|
|
6
13
|
|
|
@@ -13,13 +20,16 @@ npm install @inertiaui/vanilla
|
|
|
13
20
|
- [Scroll Locking](#scroll-locking)
|
|
14
21
|
- [Focus Management](#focus-management)
|
|
15
22
|
- [Keyboard Events](#keyboard-events)
|
|
23
|
+
- [Click Outside](#click-outside)
|
|
24
|
+
- [Menu Navigation](#menu-navigation)
|
|
25
|
+
- [Positioning](#positioning)
|
|
16
26
|
- [Accessibility](#accessibility)
|
|
17
27
|
- [Animation](#animation)
|
|
18
28
|
- [Helpers](#helpers)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
29
|
+
- [generateId](#generateid)
|
|
30
|
+
- [Object Filtering](#object-filtering)
|
|
31
|
+
- [String Utilities](#string-utilities)
|
|
32
|
+
- [URL Utilities](#url-utilities)
|
|
23
33
|
|
|
24
34
|
## Scroll Locking
|
|
25
35
|
|
|
@@ -37,6 +47,7 @@ unlock()
|
|
|
37
47
|
```
|
|
38
48
|
|
|
39
49
|
The function:
|
|
50
|
+
|
|
40
51
|
- Sets `document.body.style.overflow` to `'hidden'`
|
|
41
52
|
- Adds padding to compensate for scrollbar width (prevents layout shift)
|
|
42
53
|
- Returns a cleanup function that can only unlock once
|
|
@@ -82,11 +93,7 @@ Creates a focus trap within a container element.
|
|
|
82
93
|
```typescript
|
|
83
94
|
import { createFocusTrap } from '@inertiaui/vanilla'
|
|
84
95
|
|
|
85
|
-
const cleanup = createFocusTrap(
|
|
86
|
-
initialFocus: true,
|
|
87
|
-
initialFocusElement: null,
|
|
88
|
-
returnFocus: true,
|
|
89
|
-
})
|
|
96
|
+
const cleanup = createFocusTrap(dialogElement)
|
|
90
97
|
|
|
91
98
|
// Later, remove the focus trap
|
|
92
99
|
cleanup()
|
|
@@ -103,26 +110,27 @@ cleanup()
|
|
|
103
110
|
#### Behavior
|
|
104
111
|
|
|
105
112
|
The focus trap:
|
|
113
|
+
|
|
106
114
|
- Listens for Tab key and wraps focus at container boundaries
|
|
107
115
|
- Prevents focus from leaving the container via Tab or Shift+Tab
|
|
108
116
|
- Catches focus that escapes (e.g., via mouse click outside)
|
|
109
117
|
- Optionally focuses the first focusable element on creation
|
|
110
118
|
- Optionally returns focus to the previously focused element on cleanup
|
|
119
|
+
- 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
120
|
|
|
112
121
|
```typescript
|
|
113
|
-
const container = document.getElementById('dialog')
|
|
122
|
+
const container = document.getElementById('dialog')!
|
|
114
123
|
const submitButton = document.getElementById('submit')
|
|
115
124
|
|
|
116
125
|
const cleanup = createFocusTrap(container, {
|
|
117
|
-
initialFocus: true,
|
|
118
126
|
initialFocusElement: submitButton, // Focus submit button instead of first element
|
|
119
|
-
returnFocus: true,
|
|
120
127
|
})
|
|
121
128
|
```
|
|
122
129
|
|
|
123
130
|
#### Focusable Elements
|
|
124
131
|
|
|
125
132
|
The focus trap recognizes these elements as focusable:
|
|
133
|
+
|
|
126
134
|
- `a[href]`
|
|
127
135
|
- `button:not([disabled])`
|
|
128
136
|
- `textarea:not([disabled])`
|
|
@@ -130,7 +138,7 @@ The focus trap recognizes these elements as focusable:
|
|
|
130
138
|
- `select:not([disabled])`
|
|
131
139
|
- `[tabindex]:not([tabindex="-1"])`
|
|
132
140
|
|
|
133
|
-
Elements with `aria-hidden`
|
|
141
|
+
Elements with `aria-hidden="true"` are excluded. (Elements with `disabled` are already filtered by the selectors above.)
|
|
134
142
|
|
|
135
143
|
## Keyboard Events
|
|
136
144
|
|
|
@@ -168,21 +176,293 @@ const cleanup = onEscapeKey(handleEscape, {
|
|
|
168
176
|
The cleanup function pattern integrates well with framework lifecycle hooks:
|
|
169
177
|
|
|
170
178
|
```typescript
|
|
171
|
-
// Vue
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
})
|
|
175
|
-
onUnmounted(() => {
|
|
176
|
-
cleanup()
|
|
177
|
-
})
|
|
179
|
+
// Vue (<script setup>)
|
|
180
|
+
const cleanup = onEscapeKey(closeDialog)
|
|
181
|
+
onUnmounted(() => cleanup())
|
|
178
182
|
|
|
179
183
|
// React
|
|
180
184
|
useEffect(() => {
|
|
181
|
-
|
|
182
|
-
return cleanup
|
|
185
|
+
return onEscapeKey(closeDialog)
|
|
183
186
|
}, [])
|
|
184
187
|
```
|
|
185
188
|
|
|
189
|
+
## Click Outside
|
|
190
|
+
|
|
191
|
+
The `onClickOutside` function detects clicks outside one or more elements and calls a callback. Useful for closing dropdowns, popovers, and modals when the user clicks elsewhere.
|
|
192
|
+
|
|
193
|
+
### Basic Usage
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import { onClickOutside } from '@inertiaui/vanilla'
|
|
197
|
+
|
|
198
|
+
const cleanup = onClickOutside(dropdownElement, (event) => {
|
|
199
|
+
closeDropdown()
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
// Later, remove the listener
|
|
203
|
+
cleanup()
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Multiple Elements
|
|
207
|
+
|
|
208
|
+
Pass an array of elements to ignore clicks inside any of them:
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
const cleanup = onClickOutside([triggerButton, dropdownPanel], () => {
|
|
212
|
+
closeDropdown()
|
|
213
|
+
})
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### Portal Support
|
|
217
|
+
|
|
218
|
+
Clicks inside elements with the `data-inertiaui-portal` attribute (or their descendants) are automatically ignored. This prevents portalled content like dropdown menus from being considered "outside":
|
|
219
|
+
|
|
220
|
+
```html
|
|
221
|
+
<div data-inertiaui-portal>
|
|
222
|
+
<!-- Clicks here won't trigger the callback -->
|
|
223
|
+
</div>
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### Same-Tick Protection
|
|
227
|
+
|
|
228
|
+
The listener registration is deferred by one tick, so the click that triggered the element to open won't immediately close it:
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
openButton.addEventListener('click', () => {
|
|
232
|
+
dropdown.hidden = false
|
|
233
|
+
// The click on openButton won't trigger the outside handler
|
|
234
|
+
onClickOutside(dropdown, () => { dropdown.hidden = true })
|
|
235
|
+
})
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Menu Navigation
|
|
239
|
+
|
|
240
|
+
The `createMenuNavigation` function adds keyboard navigation to menu containers, implementing the [WAI-ARIA Menu Pattern](https://www.w3.org/WAI/ARIA/apg/patterns/menu/). It supports arrow key navigation, roving tabindex, type-ahead search, and item activation.
|
|
241
|
+
|
|
242
|
+
### Basic Usage
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
import { createMenuNavigation } from '@inertiaui/vanilla'
|
|
246
|
+
|
|
247
|
+
const cleanup = createMenuNavigation(menuElement)
|
|
248
|
+
|
|
249
|
+
// Later, remove navigation
|
|
250
|
+
cleanup()
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Options
|
|
254
|
+
|
|
255
|
+
| Option | Type | Default | Description |
|
|
256
|
+
|--------|------|---------|-------------|
|
|
257
|
+
| `itemSelector` | `string` | `'[role="menuitem"]:not([disabled]):not([aria-disabled="true"])'` | CSS selector for menu items |
|
|
258
|
+
| `orientation` | `'vertical' \| 'horizontal'` | `'vertical'` | Arrow key direction |
|
|
259
|
+
| `loop` | `boolean` | `true` | Wrap focus from last to first item |
|
|
260
|
+
| `typeAhead` | `boolean` | `true` | Enable type-ahead character search |
|
|
261
|
+
| `onActivate` | `(item: HTMLElement) => void` | `undefined` | Called when an item is activated via Enter or Space |
|
|
262
|
+
|
|
263
|
+
### Keyboard Support
|
|
264
|
+
|
|
265
|
+
| Key | Action |
|
|
266
|
+
|-----|--------|
|
|
267
|
+
| `ArrowDown` / `ArrowRight` | Focus next item (depending on orientation) |
|
|
268
|
+
| `ArrowUp` / `ArrowLeft` | Focus previous item (depending on orientation) |
|
|
269
|
+
| `Home` | Focus first item |
|
|
270
|
+
| `End` | Focus last item |
|
|
271
|
+
| `Enter` / `Space` | Activate (click) the focused item |
|
|
272
|
+
| Any character | Type-ahead: focus the first item whose text starts with the typed characters |
|
|
273
|
+
|
|
274
|
+
### Roving Tabindex
|
|
275
|
+
|
|
276
|
+
The focused item receives `tabindex="0"` while all other items receive `tabindex="-1"`. This allows the menu to participate in the page's tab order with a single tab stop:
|
|
277
|
+
|
|
278
|
+
```typescript
|
|
279
|
+
const cleanup = createMenuNavigation(menuElement)
|
|
280
|
+
|
|
281
|
+
// First item has tabindex="0", rest have tabindex="-1"
|
|
282
|
+
// Arrow keys move focus and update tabindex accordingly
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### Horizontal Menus
|
|
286
|
+
|
|
287
|
+
For horizontal menus like toolbars, use `orientation: 'horizontal'`:
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
const cleanup = createMenuNavigation(toolbar, {
|
|
291
|
+
orientation: 'horizontal',
|
|
292
|
+
})
|
|
293
|
+
// ArrowRight/ArrowLeft navigate instead of ArrowDown/ArrowUp
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### Custom Item Selector
|
|
297
|
+
|
|
298
|
+
Use a custom selector for non-standard menu structures:
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
const cleanup = createMenuNavigation(container, {
|
|
302
|
+
itemSelector: '.menu-item:not(.disabled)',
|
|
303
|
+
})
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
### Full Example
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
import { createMenuNavigation, onClickOutside, onEscapeKey } from '@inertiaui/vanilla'
|
|
310
|
+
|
|
311
|
+
function openMenu(menuElement: HTMLElement) {
|
|
312
|
+
menuElement.hidden = false
|
|
313
|
+
|
|
314
|
+
const cleanups = [
|
|
315
|
+
createMenuNavigation(menuElement, {
|
|
316
|
+
onActivate: (item) => {
|
|
317
|
+
handleMenuAction(item.dataset.action!)
|
|
318
|
+
closeMenu()
|
|
319
|
+
},
|
|
320
|
+
}),
|
|
321
|
+
onClickOutside(menuElement, closeMenu),
|
|
322
|
+
onEscapeKey(closeMenu),
|
|
323
|
+
]
|
|
324
|
+
|
|
325
|
+
function closeMenu() {
|
|
326
|
+
cleanups.forEach((fn) => fn())
|
|
327
|
+
menuElement.hidden = true
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
## Positioning
|
|
333
|
+
|
|
334
|
+
Utilities for positioning floating elements (dropdowns, tooltips, popovers) relative to a reference element. Uses [CSS Anchor Positioning](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_anchor_positioning) when supported, with an automatic JavaScript fallback.
|
|
335
|
+
|
|
336
|
+
### supportsAnchorPositioning
|
|
337
|
+
|
|
338
|
+
Check if the browser supports CSS Anchor Positioning:
|
|
339
|
+
|
|
340
|
+
```typescript
|
|
341
|
+
import { supportsAnchorPositioning } from '@inertiaui/vanilla'
|
|
342
|
+
|
|
343
|
+
if (supportsAnchorPositioning()) {
|
|
344
|
+
// Browser handles positioning via CSS
|
|
345
|
+
} else {
|
|
346
|
+
// JavaScript fallback is used
|
|
347
|
+
}
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
The result is cached after the first call.
|
|
351
|
+
|
|
352
|
+
### computePosition
|
|
353
|
+
|
|
354
|
+
Position a floating element relative to a reference element:
|
|
355
|
+
|
|
356
|
+
```typescript
|
|
357
|
+
import { computePosition } from '@inertiaui/vanilla'
|
|
358
|
+
|
|
359
|
+
const result = computePosition(referenceElement, floatingElement)
|
|
360
|
+
// { x: 100, y: 140, placement: 'bottom-start' }
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
The function applies positioning styles to the floating element automatically (`position: fixed` with `top` and `left` values).
|
|
364
|
+
|
|
365
|
+
#### Options
|
|
366
|
+
|
|
367
|
+
| Option | Type | Default | Description |
|
|
368
|
+
|--------|------|---------|-------------|
|
|
369
|
+
| `placement` | `Placement` | `'bottom-start'` | Where to position the floating element |
|
|
370
|
+
| `offset` | `number` | `0` | Distance in pixels between reference and floating element |
|
|
371
|
+
| `flip` | `boolean` | `true` | Flip to opposite side when overflowing viewport |
|
|
372
|
+
|
|
373
|
+
#### Placements
|
|
374
|
+
|
|
375
|
+
Twelve placement options are available, combining a side with an optional alignment:
|
|
376
|
+
|
|
377
|
+
| Side | Center | Start | End |
|
|
378
|
+
|------|--------|-------|-----|
|
|
379
|
+
| `top` | `top` | `top-start` | `top-end` |
|
|
380
|
+
| `bottom` | `bottom` | `bottom-start` | `bottom-end` |
|
|
381
|
+
| `left` | `left` | `left-start` | `left-end` |
|
|
382
|
+
| `right` | `right` | `right-start` | `right-end` |
|
|
383
|
+
|
|
384
|
+
The `start` and `end` alignments are RTL-aware for `top` and `bottom` placements.
|
|
385
|
+
|
|
386
|
+
#### Flip Behavior
|
|
387
|
+
|
|
388
|
+
When the floating element would overflow the viewport, it automatically flips to the opposite side:
|
|
389
|
+
|
|
390
|
+
```typescript
|
|
391
|
+
// If there's no room below, flips to top
|
|
392
|
+
const result = computePosition(reference, floating, {
|
|
393
|
+
placement: 'bottom-start',
|
|
394
|
+
})
|
|
395
|
+
// result.placement may be 'top-start' if it flipped
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
Disable flipping to keep the element on the specified side:
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
const result = computePosition(reference, floating, {
|
|
402
|
+
placement: 'bottom-start',
|
|
403
|
+
flip: false,
|
|
404
|
+
})
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
#### Viewport Clamping
|
|
408
|
+
|
|
409
|
+
The floating element is always clamped to stay within the viewport with a 4px margin, even after flipping.
|
|
410
|
+
|
|
411
|
+
### autoUpdate
|
|
412
|
+
|
|
413
|
+
Automatically reposition the floating element when the layout changes:
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
import { computePosition, autoUpdate } from '@inertiaui/vanilla'
|
|
417
|
+
|
|
418
|
+
const cleanup = autoUpdate(referenceElement, floatingElement, () => {
|
|
419
|
+
computePosition(referenceElement, floatingElement, {
|
|
420
|
+
placement: 'bottom-start',
|
|
421
|
+
offset: 8,
|
|
422
|
+
})
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
// Later, stop updating
|
|
426
|
+
cleanup()
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
`autoUpdate` listens for:
|
|
430
|
+
|
|
431
|
+
- Window `resize` events
|
|
432
|
+
- Window `scroll` events (including nested scrollable containers)
|
|
433
|
+
- Size changes on both the reference and floating elements (via `ResizeObserver`)
|
|
434
|
+
|
|
435
|
+
Updates are batched using `requestAnimationFrame` to avoid layout thrashing.
|
|
436
|
+
|
|
437
|
+
### CSS Anchor Positioning
|
|
438
|
+
|
|
439
|
+
When the browser supports CSS Anchor Positioning, `computePosition` uses native CSS for positioning. This provides better performance and handles edge cases like scrolling and resizing without JavaScript recalculation. The `autoUpdate` cleanup function automatically removes CSS anchor styles when called.
|
|
440
|
+
|
|
441
|
+
### Full Example
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
import { computePosition, autoUpdate } from '@inertiaui/vanilla'
|
|
445
|
+
|
|
446
|
+
function setupTooltip(trigger: HTMLElement, tooltip: HTMLElement) {
|
|
447
|
+
tooltip.style.display = 'block'
|
|
448
|
+
|
|
449
|
+
const cleanup = autoUpdate(trigger, tooltip, () => {
|
|
450
|
+
computePosition(trigger, tooltip, {
|
|
451
|
+
placement: 'top',
|
|
452
|
+
offset: 8,
|
|
453
|
+
})
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
// Initial position
|
|
457
|
+
computePosition(trigger, tooltip, {
|
|
458
|
+
placement: 'top',
|
|
459
|
+
offset: 8,
|
|
460
|
+
})
|
|
461
|
+
|
|
462
|
+
return cleanup
|
|
463
|
+
}
|
|
464
|
+
```
|
|
465
|
+
|
|
186
466
|
## Accessibility
|
|
187
467
|
|
|
188
468
|
Accessibility utilities for managing `aria-hidden` attributes with reference counting support.
|
|
@@ -204,11 +484,11 @@ Accepts either an element or a CSS selector:
|
|
|
204
484
|
|
|
205
485
|
```typescript
|
|
206
486
|
// Using selector
|
|
207
|
-
const
|
|
487
|
+
const cleanup1 = markAriaHidden('#app')
|
|
208
488
|
|
|
209
489
|
// Using element
|
|
210
|
-
const element = document.getElementById('app')
|
|
211
|
-
const
|
|
490
|
+
const element = document.getElementById('app')!
|
|
491
|
+
const cleanup2 = markAriaHidden(element)
|
|
212
492
|
```
|
|
213
493
|
|
|
214
494
|
### Reference Counting
|
|
@@ -235,27 +515,27 @@ cleanup2()
|
|
|
235
515
|
The original `aria-hidden` value is preserved and restored:
|
|
236
516
|
|
|
237
517
|
```typescript
|
|
238
|
-
const element = document.getElementById('sidebar')
|
|
518
|
+
const element = document.getElementById('sidebar')!
|
|
239
519
|
element.setAttribute('aria-hidden', 'false')
|
|
240
520
|
|
|
241
521
|
const cleanup = markAriaHidden(element)
|
|
242
|
-
|
|
522
|
+
element.getAttribute('aria-hidden') // 'true'
|
|
243
523
|
|
|
244
524
|
cleanup()
|
|
245
|
-
|
|
525
|
+
element.getAttribute('aria-hidden') // 'false' (restored)
|
|
246
526
|
```
|
|
247
527
|
|
|
248
528
|
If the element had no `aria-hidden` attribute, the attribute is removed on cleanup:
|
|
249
529
|
|
|
250
530
|
```typescript
|
|
251
|
-
const element = document.getElementById('main')
|
|
531
|
+
const element = document.getElementById('main')!
|
|
252
532
|
// No aria-hidden attribute
|
|
253
533
|
|
|
254
534
|
const cleanup = markAriaHidden(element)
|
|
255
|
-
|
|
535
|
+
element.getAttribute('aria-hidden') // 'true'
|
|
256
536
|
|
|
257
537
|
cleanup()
|
|
258
|
-
|
|
538
|
+
element.getAttribute('aria-hidden') // null (removed)
|
|
259
539
|
```
|
|
260
540
|
|
|
261
541
|
### Use with Dialogs
|
|
@@ -266,14 +546,16 @@ When a dialog opens, the main content should be marked as `aria-hidden` to preve
|
|
|
266
546
|
import { markAriaHidden, lockScroll, createFocusTrap, onEscapeKey } from '@inertiaui/vanilla'
|
|
267
547
|
|
|
268
548
|
function openDialog(dialogElement: HTMLElement) {
|
|
549
|
+
const closeDialog = () => cleanups.forEach(fn => fn())
|
|
550
|
+
|
|
269
551
|
const cleanups = [
|
|
270
552
|
markAriaHidden('#app'),
|
|
271
553
|
lockScroll(),
|
|
272
554
|
createFocusTrap(dialogElement),
|
|
273
|
-
onEscapeKey(
|
|
555
|
+
onEscapeKey(closeDialog),
|
|
274
556
|
]
|
|
275
557
|
|
|
276
|
-
return
|
|
558
|
+
return closeDialog
|
|
277
559
|
}
|
|
278
560
|
```
|
|
279
561
|
|
|
@@ -283,7 +565,7 @@ The animation module provides a simple wrapper around the Web Animations API wit
|
|
|
283
565
|
|
|
284
566
|
### animate
|
|
285
567
|
|
|
286
|
-
Animate an element using the Web Animations API. Returns a promise that resolves when the animation completes.
|
|
568
|
+
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
569
|
|
|
288
570
|
```typescript
|
|
289
571
|
import { animate } from '@inertiaui/vanilla'
|
|
@@ -387,10 +669,10 @@ const id = generateId()
|
|
|
387
669
|
#### Custom Prefix
|
|
388
670
|
|
|
389
671
|
```typescript
|
|
390
|
-
|
|
672
|
+
generateId('modal_')
|
|
391
673
|
// 'modal_550e8400-e29b-41d4-a716-446655440000'
|
|
392
674
|
|
|
393
|
-
|
|
675
|
+
generateId('dialog-')
|
|
394
676
|
// 'dialog-550e8400-e29b-41d4-a716-446655440000'
|
|
395
677
|
```
|
|
396
678
|
|
|
@@ -407,6 +689,7 @@ In environments where `crypto.randomUUID()` is not available, the function falls
|
|
|
407
689
|
#### Use Cases
|
|
408
690
|
|
|
409
691
|
Useful for generating unique IDs for:
|
|
692
|
+
|
|
410
693
|
- Dialog instances
|
|
411
694
|
- Form elements requiring unique IDs
|
|
412
695
|
- Accessibility attributes (`aria-labelledby`, `aria-describedby`)
|
|
@@ -533,7 +816,9 @@ kebabCase('already-kebab') // 'already-kebab'
|
|
|
533
816
|
```typescript
|
|
534
817
|
kebabCase('user123Name') // 'user123-name'
|
|
535
818
|
kebabCase('multiple__underscores') // 'multiple-underscores'
|
|
536
|
-
kebabCase('UPPERCASE') // '
|
|
819
|
+
kebabCase('UPPERCASE') // 'uppercase'
|
|
820
|
+
kebabCase('XMLDocument') // 'xml-document'
|
|
821
|
+
kebabCase('hello world') // 'hello-world'
|
|
537
822
|
```
|
|
538
823
|
|
|
539
824
|
#### isStandardDomEvent
|
|
@@ -574,15 +859,15 @@ isStandardDomEvent('OnClick') // true
|
|
|
574
859
|
Useful for distinguishing between standard DOM events and custom events when processing event handlers:
|
|
575
860
|
|
|
576
861
|
```typescript
|
|
577
|
-
const props = {
|
|
862
|
+
const props: Record<string, Function> = {
|
|
578
863
|
onClick: handleClick,
|
|
579
864
|
onMouseOver: handleHover,
|
|
580
865
|
onModalReady: handleModalReady,
|
|
581
866
|
onUserUpdated: handleUserUpdated,
|
|
582
867
|
}
|
|
583
868
|
|
|
584
|
-
const domEvents = {}
|
|
585
|
-
const customEvents = {}
|
|
869
|
+
const domEvents: Record<string, Function> = {}
|
|
870
|
+
const customEvents: Record<string, Function> = {}
|
|
586
871
|
|
|
587
872
|
for (const [key, value] of Object.entries(props)) {
|
|
588
873
|
if (isStandardDomEvent(key)) {
|
|
@@ -636,6 +921,51 @@ Useful for determining active navigation states or comparing the current route w
|
|
|
636
921
|
const isActive = sameUrlPath(window.location.href, linkHref)
|
|
637
922
|
```
|
|
638
923
|
|
|
924
|
+
## Development
|
|
925
|
+
|
|
926
|
+
### Running the dev server
|
|
927
|
+
|
|
928
|
+
Start Vite to browse the interactive test pages:
|
|
929
|
+
|
|
930
|
+
```bash
|
|
931
|
+
npx vite --port 3333
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
Then open `http://localhost:3333` for an overview linking to all test pages.
|
|
935
|
+
|
|
936
|
+
### Running E2E tests
|
|
937
|
+
|
|
938
|
+
The test suite uses Playwright with Chromium. It automatically starts a Vite dev server:
|
|
939
|
+
|
|
940
|
+
```bash
|
|
941
|
+
npm run test:e2e
|
|
942
|
+
```
|
|
943
|
+
|
|
944
|
+
Run a single spec:
|
|
945
|
+
|
|
946
|
+
```bash
|
|
947
|
+
npx playwright test e2e/menu.spec.ts
|
|
948
|
+
```
|
|
949
|
+
|
|
950
|
+
Run tests matching a name:
|
|
951
|
+
|
|
952
|
+
```bash
|
|
953
|
+
npx playwright test -g "ArrowDown"
|
|
954
|
+
```
|
|
955
|
+
|
|
956
|
+
### Test structure
|
|
957
|
+
|
|
958
|
+
Each feature has a spec file and a matching HTML page:
|
|
959
|
+
|
|
960
|
+
```
|
|
961
|
+
e2e/click-outside.spec.ts → e2e/pages/click-outside.html
|
|
962
|
+
e2e/menu.spec.ts → e2e/pages/menu.html
|
|
963
|
+
e2e/focus-trap.spec.ts → e2e/pages/focus-trap.html
|
|
964
|
+
...
|
|
965
|
+
```
|
|
966
|
+
|
|
967
|
+
The mapping is handled by a custom fixture in `e2e/test.ts`. HTML pages import directly from the TypeScript source via Vite.
|
|
968
|
+
|
|
639
969
|
## TypeScript
|
|
640
970
|
|
|
641
971
|
This library is written in TypeScript and exports the following types:
|
|
@@ -647,6 +977,10 @@ import type {
|
|
|
647
977
|
EscapeKeyOptions,
|
|
648
978
|
AnimateOptions,
|
|
649
979
|
EasingName,
|
|
980
|
+
MenuNavigationOptions,
|
|
981
|
+
Placement,
|
|
982
|
+
PositionOptions,
|
|
983
|
+
PositionResult,
|
|
650
984
|
} from '@inertiaui/vanilla'
|
|
651
985
|
```
|
|
652
986
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,113 @@
|
|
|
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
|
+
export declare function autoUpdate(reference: HTMLElement, floating: HTMLElement, update: () => void): CleanupFunction;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Cancel any running animations on an element
|
|
26
|
+
*/
|
|
27
|
+
export declare function cancelAnimations(element: HTMLElement): void;
|
|
28
|
+
|
|
29
|
+
export declare type CleanupFunction = () => void;
|
|
30
|
+
|
|
31
|
+
export declare function computePosition(reference: HTMLElement, floating: HTMLElement, options?: PositionOptions_2): PositionResult;
|
|
32
|
+
|
|
33
|
+
export declare function createFocusTrap(container: HTMLElement, options?: FocusTrapOptions): CleanupFunction;
|
|
34
|
+
|
|
35
|
+
export declare function createMenuNavigation(container: HTMLElement, options?: MenuNavigationOptions): CleanupFunction;
|
|
36
|
+
|
|
37
|
+
export declare type EasingName = keyof typeof easings;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Tailwind CSS easing functions
|
|
41
|
+
*/
|
|
42
|
+
export declare const easings: {
|
|
43
|
+
readonly linear: "linear";
|
|
44
|
+
readonly in: "cubic-bezier(0.4, 0, 1, 1)";
|
|
45
|
+
readonly out: "cubic-bezier(0, 0, 0.2, 1)";
|
|
46
|
+
readonly inOut: "cubic-bezier(0.4, 0, 0.2, 1)";
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export declare interface EscapeKeyOptions {
|
|
50
|
+
preventDefault?: boolean;
|
|
51
|
+
stopPropagation?: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export declare function except<T extends Record<string, unknown>>(target: T, keys: string[], ignoreCase?: boolean): Partial<T>;
|
|
55
|
+
|
|
56
|
+
export declare function except(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
57
|
+
|
|
58
|
+
export declare interface FocusTrapOptions {
|
|
59
|
+
initialFocus?: boolean;
|
|
60
|
+
initialFocusElement?: HTMLElement | null;
|
|
61
|
+
returnFocus?: boolean;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export declare function generateId(prefix?: string): string;
|
|
65
|
+
|
|
66
|
+
export declare function isStandardDomEvent(eventName: string): boolean;
|
|
67
|
+
|
|
68
|
+
export declare function kebabCase(string: string): string;
|
|
69
|
+
|
|
70
|
+
export declare function lockScroll(): CleanupFunction;
|
|
71
|
+
|
|
72
|
+
export declare function markAriaHidden(elementOrSelector: Element | string): CleanupFunction;
|
|
73
|
+
|
|
74
|
+
export declare interface MenuNavigationOptions {
|
|
75
|
+
itemSelector?: string;
|
|
76
|
+
orientation?: 'vertical' | 'horizontal';
|
|
77
|
+
loop?: boolean;
|
|
78
|
+
typeAhead?: boolean;
|
|
79
|
+
onActivate?: (item: HTMLElement) => void;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export declare function onClickOutside(elements: HTMLElement | HTMLElement[], callback: (event: PointerEvent) => void): CleanupFunction;
|
|
83
|
+
|
|
84
|
+
export declare function onEscapeKey(callback: (event: KeyboardEvent) => void, options?: EscapeKeyOptions): CleanupFunction;
|
|
85
|
+
|
|
86
|
+
export declare function only<T extends Record<string, unknown>>(target: T, keys: string[], ignoreCase?: boolean): Partial<T>;
|
|
87
|
+
|
|
88
|
+
export declare function only(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
89
|
+
|
|
90
|
+
export declare type Placement = 'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' | 'right' | 'right-start' | 'right-end';
|
|
91
|
+
|
|
92
|
+
declare interface PositionOptions_2 {
|
|
93
|
+
placement?: Placement;
|
|
94
|
+
offset?: number;
|
|
95
|
+
flip?: boolean;
|
|
96
|
+
}
|
|
97
|
+
export { PositionOptions_2 as PositionOptions }
|
|
98
|
+
|
|
99
|
+
export declare interface PositionResult {
|
|
100
|
+
x: number;
|
|
101
|
+
y: number;
|
|
102
|
+
placement: Placement;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export declare function rejectNullValues<T>(target: T[]): Exclude<T, null>[];
|
|
106
|
+
|
|
107
|
+
export declare function rejectNullValues<T extends Record<string, unknown>>(target: T): Partial<T>;
|
|
108
|
+
|
|
109
|
+
export declare function sameUrlPath(url1: string | URL | undefined | null, url2: string | URL | undefined | null): boolean;
|
|
110
|
+
|
|
111
|
+
export declare function supportsAnchorPositioning(): boolean;
|
|
112
|
+
|
|
113
|
+
export { }
|