@inertiaui/vanilla 0.2.0 → 0.4.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 +369 -38
- package/dist/index.d.ts +35 -0
- package/dist/index.js +444 -2
- package/package.json +5 -3
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,6 +110,7 @@ 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)
|
|
@@ -111,19 +119,18 @@ The focus trap:
|
|
|
111
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.
|
|
112
120
|
|
|
113
121
|
```typescript
|
|
114
|
-
const container = document.getElementById('dialog')
|
|
122
|
+
const container = document.getElementById('dialog')!
|
|
115
123
|
const submitButton = document.getElementById('submit')
|
|
116
124
|
|
|
117
125
|
const cleanup = createFocusTrap(container, {
|
|
118
|
-
initialFocus: true,
|
|
119
126
|
initialFocusElement: submitButton, // Focus submit button instead of first element
|
|
120
|
-
returnFocus: true,
|
|
121
127
|
})
|
|
122
128
|
```
|
|
123
129
|
|
|
124
130
|
#### Focusable Elements
|
|
125
131
|
|
|
126
132
|
The focus trap recognizes these elements as focusable:
|
|
133
|
+
|
|
127
134
|
- `a[href]`
|
|
128
135
|
- `button:not([disabled])`
|
|
129
136
|
- `textarea:not([disabled])`
|
|
@@ -169,21 +176,293 @@ const cleanup = onEscapeKey(handleEscape, {
|
|
|
169
176
|
The cleanup function pattern integrates well with framework lifecycle hooks:
|
|
170
177
|
|
|
171
178
|
```typescript
|
|
172
|
-
// Vue
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
})
|
|
176
|
-
onUnmounted(() => {
|
|
177
|
-
cleanup()
|
|
178
|
-
})
|
|
179
|
+
// Vue (<script setup>)
|
|
180
|
+
const cleanup = onEscapeKey(closeDialog)
|
|
181
|
+
onUnmounted(() => cleanup())
|
|
179
182
|
|
|
180
183
|
// React
|
|
181
184
|
useEffect(() => {
|
|
182
|
-
|
|
183
|
-
return cleanup
|
|
185
|
+
return onEscapeKey(closeDialog)
|
|
184
186
|
}, [])
|
|
185
187
|
```
|
|
186
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
|
+
|
|
187
466
|
## Accessibility
|
|
188
467
|
|
|
189
468
|
Accessibility utilities for managing `aria-hidden` attributes with reference counting support.
|
|
@@ -205,11 +484,11 @@ Accepts either an element or a CSS selector:
|
|
|
205
484
|
|
|
206
485
|
```typescript
|
|
207
486
|
// Using selector
|
|
208
|
-
const
|
|
487
|
+
const cleanup1 = markAriaHidden('#app')
|
|
209
488
|
|
|
210
489
|
// Using element
|
|
211
|
-
const element = document.getElementById('app')
|
|
212
|
-
const
|
|
490
|
+
const element = document.getElementById('app')!
|
|
491
|
+
const cleanup2 = markAriaHidden(element)
|
|
213
492
|
```
|
|
214
493
|
|
|
215
494
|
### Reference Counting
|
|
@@ -236,27 +515,27 @@ cleanup2()
|
|
|
236
515
|
The original `aria-hidden` value is preserved and restored:
|
|
237
516
|
|
|
238
517
|
```typescript
|
|
239
|
-
const element = document.getElementById('sidebar')
|
|
518
|
+
const element = document.getElementById('sidebar')!
|
|
240
519
|
element.setAttribute('aria-hidden', 'false')
|
|
241
520
|
|
|
242
521
|
const cleanup = markAriaHidden(element)
|
|
243
|
-
|
|
522
|
+
element.getAttribute('aria-hidden') // 'true'
|
|
244
523
|
|
|
245
524
|
cleanup()
|
|
246
|
-
|
|
525
|
+
element.getAttribute('aria-hidden') // 'false' (restored)
|
|
247
526
|
```
|
|
248
527
|
|
|
249
528
|
If the element had no `aria-hidden` attribute, the attribute is removed on cleanup:
|
|
250
529
|
|
|
251
530
|
```typescript
|
|
252
|
-
const element = document.getElementById('main')
|
|
531
|
+
const element = document.getElementById('main')!
|
|
253
532
|
// No aria-hidden attribute
|
|
254
533
|
|
|
255
534
|
const cleanup = markAriaHidden(element)
|
|
256
|
-
|
|
535
|
+
element.getAttribute('aria-hidden') // 'true'
|
|
257
536
|
|
|
258
537
|
cleanup()
|
|
259
|
-
|
|
538
|
+
element.getAttribute('aria-hidden') // null (removed)
|
|
260
539
|
```
|
|
261
540
|
|
|
262
541
|
### Use with Dialogs
|
|
@@ -267,14 +546,16 @@ When a dialog opens, the main content should be marked as `aria-hidden` to preve
|
|
|
267
546
|
import { markAriaHidden, lockScroll, createFocusTrap, onEscapeKey } from '@inertiaui/vanilla'
|
|
268
547
|
|
|
269
548
|
function openDialog(dialogElement: HTMLElement) {
|
|
549
|
+
const closeDialog = () => cleanups.forEach(fn => fn())
|
|
550
|
+
|
|
270
551
|
const cleanups = [
|
|
271
552
|
markAriaHidden('#app'),
|
|
272
553
|
lockScroll(),
|
|
273
554
|
createFocusTrap(dialogElement),
|
|
274
|
-
onEscapeKey(
|
|
555
|
+
onEscapeKey(closeDialog),
|
|
275
556
|
]
|
|
276
557
|
|
|
277
|
-
return
|
|
558
|
+
return closeDialog
|
|
278
559
|
}
|
|
279
560
|
```
|
|
280
561
|
|
|
@@ -388,10 +669,10 @@ const id = generateId()
|
|
|
388
669
|
#### Custom Prefix
|
|
389
670
|
|
|
390
671
|
```typescript
|
|
391
|
-
|
|
672
|
+
generateId('modal_')
|
|
392
673
|
// 'modal_550e8400-e29b-41d4-a716-446655440000'
|
|
393
674
|
|
|
394
|
-
|
|
675
|
+
generateId('dialog-')
|
|
395
676
|
// 'dialog-550e8400-e29b-41d4-a716-446655440000'
|
|
396
677
|
```
|
|
397
678
|
|
|
@@ -408,6 +689,7 @@ In environments where `crypto.randomUUID()` is not available, the function falls
|
|
|
408
689
|
#### Use Cases
|
|
409
690
|
|
|
410
691
|
Useful for generating unique IDs for:
|
|
692
|
+
|
|
411
693
|
- Dialog instances
|
|
412
694
|
- Form elements requiring unique IDs
|
|
413
695
|
- Accessibility attributes (`aria-labelledby`, `aria-describedby`)
|
|
@@ -577,15 +859,15 @@ isStandardDomEvent('OnClick') // true
|
|
|
577
859
|
Useful for distinguishing between standard DOM events and custom events when processing event handlers:
|
|
578
860
|
|
|
579
861
|
```typescript
|
|
580
|
-
const props = {
|
|
862
|
+
const props: Record<string, Function> = {
|
|
581
863
|
onClick: handleClick,
|
|
582
864
|
onMouseOver: handleHover,
|
|
583
865
|
onModalReady: handleModalReady,
|
|
584
866
|
onUserUpdated: handleUserUpdated,
|
|
585
867
|
}
|
|
586
868
|
|
|
587
|
-
const domEvents = {}
|
|
588
|
-
const customEvents = {}
|
|
869
|
+
const domEvents: Record<string, Function> = {}
|
|
870
|
+
const customEvents: Record<string, Function> = {}
|
|
589
871
|
|
|
590
872
|
for (const [key, value] of Object.entries(props)) {
|
|
591
873
|
if (isStandardDomEvent(key)) {
|
|
@@ -639,6 +921,51 @@ Useful for determining active navigation states or comparing the current route w
|
|
|
639
921
|
const isActive = sameUrlPath(window.location.href, linkHref)
|
|
640
922
|
```
|
|
641
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
|
+
|
|
642
969
|
## TypeScript
|
|
643
970
|
|
|
644
971
|
This library is written in TypeScript and exports the following types:
|
|
@@ -650,6 +977,10 @@ import type {
|
|
|
650
977
|
EscapeKeyOptions,
|
|
651
978
|
AnimateOptions,
|
|
652
979
|
EasingName,
|
|
980
|
+
MenuNavigationOptions,
|
|
981
|
+
Placement,
|
|
982
|
+
PositionOptions,
|
|
983
|
+
PositionResult,
|
|
653
984
|
} from '@inertiaui/vanilla'
|
|
654
985
|
```
|
|
655
986
|
|
package/dist/index.d.ts
CHANGED
|
@@ -19,6 +19,8 @@ export declare interface AnimateOptions {
|
|
|
19
19
|
fill?: FillMode;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
export declare function autoUpdate(reference: HTMLElement, floating: HTMLElement, update: () => void): CleanupFunction;
|
|
23
|
+
|
|
22
24
|
/**
|
|
23
25
|
* Cancel any running animations on an element
|
|
24
26
|
*/
|
|
@@ -26,8 +28,12 @@ export declare function cancelAnimations(element: HTMLElement): void;
|
|
|
26
28
|
|
|
27
29
|
export declare type CleanupFunction = () => void;
|
|
28
30
|
|
|
31
|
+
export declare function computePosition(reference: HTMLElement, floating: HTMLElement, options?: PositionOptions_2): PositionResult;
|
|
32
|
+
|
|
29
33
|
export declare function createFocusTrap(container: HTMLElement, options?: FocusTrapOptions): CleanupFunction;
|
|
30
34
|
|
|
35
|
+
export declare function createMenuNavigation(container: HTMLElement, options?: MenuNavigationOptions): CleanupFunction;
|
|
36
|
+
|
|
31
37
|
export declare type EasingName = keyof typeof easings;
|
|
32
38
|
|
|
33
39
|
/**
|
|
@@ -65,16 +71,45 @@ export declare function lockScroll(): CleanupFunction;
|
|
|
65
71
|
|
|
66
72
|
export declare function markAriaHidden(elementOrSelector: Element | string): CleanupFunction;
|
|
67
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 onceChildrenRendered(element: Element, callback: () => void): void;
|
|
83
|
+
|
|
84
|
+
export declare function onClickOutside(elements: HTMLElement | HTMLElement[], callback: (event: PointerEvent) => void): CleanupFunction;
|
|
85
|
+
|
|
68
86
|
export declare function onEscapeKey(callback: (event: KeyboardEvent) => void, options?: EscapeKeyOptions): CleanupFunction;
|
|
69
87
|
|
|
70
88
|
export declare function only<T extends Record<string, unknown>>(target: T, keys: string[], ignoreCase?: boolean): Partial<T>;
|
|
71
89
|
|
|
72
90
|
export declare function only(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
73
91
|
|
|
92
|
+
export declare type Placement = 'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' | 'right' | 'right-start' | 'right-end';
|
|
93
|
+
|
|
94
|
+
declare interface PositionOptions_2 {
|
|
95
|
+
placement?: Placement;
|
|
96
|
+
offset?: number;
|
|
97
|
+
flip?: boolean;
|
|
98
|
+
}
|
|
99
|
+
export { PositionOptions_2 as PositionOptions }
|
|
100
|
+
|
|
101
|
+
export declare interface PositionResult {
|
|
102
|
+
x: number;
|
|
103
|
+
y: number;
|
|
104
|
+
placement: Placement;
|
|
105
|
+
}
|
|
106
|
+
|
|
74
107
|
export declare function rejectNullValues<T>(target: T[]): Exclude<T, null>[];
|
|
75
108
|
|
|
76
109
|
export declare function rejectNullValues<T extends Record<string, unknown>>(target: T): Partial<T>;
|
|
77
110
|
|
|
78
111
|
export declare function sameUrlPath(url1: string | URL | undefined | null, url2: string | URL | undefined | null): boolean;
|
|
79
112
|
|
|
113
|
+
export declare function supportsAnchorPositioning(): boolean;
|
|
114
|
+
|
|
80
115
|
export { }
|
package/dist/index.js
CHANGED
|
@@ -65,6 +65,19 @@ function isStandardDomEvent(eventName) {
|
|
|
65
65
|
];
|
|
66
66
|
return standardPatterns.some((pattern) => pattern.test(lowerEventName));
|
|
67
67
|
}
|
|
68
|
+
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 });
|
|
80
|
+
}
|
|
68
81
|
function sameUrlPath(url1, url2) {
|
|
69
82
|
if (!url1 || !url2) {
|
|
70
83
|
return false;
|
|
@@ -189,6 +202,20 @@ function createFocusTrap(container, options = {}) {
|
|
|
189
202
|
document.addEventListener("focusin", handleTrapFocusIn);
|
|
190
203
|
}
|
|
191
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);
|
|
192
219
|
let rafId;
|
|
193
220
|
if (initialFocus) {
|
|
194
221
|
rafId = requestAnimationFrame(() => {
|
|
@@ -212,6 +239,7 @@ function createFocusTrap(container, options = {}) {
|
|
|
212
239
|
if (rafId !== void 0) {
|
|
213
240
|
cancelAnimationFrame(rafId);
|
|
214
241
|
}
|
|
242
|
+
container.removeEventListener("focusout", handleFocusOut);
|
|
215
243
|
const index = focusTrapStack.indexOf(entry);
|
|
216
244
|
if (index >= 0) {
|
|
217
245
|
focusTrapStack.splice(index, 1);
|
|
@@ -220,7 +248,7 @@ function createFocusTrap(container, options = {}) {
|
|
|
220
248
|
document.removeEventListener("keydown", handleTrapKeyDown);
|
|
221
249
|
document.removeEventListener("focusin", handleTrapFocusIn);
|
|
222
250
|
}
|
|
223
|
-
if (returnFocus && previouslyFocused && typeof previouslyFocused.focus === "function") {
|
|
251
|
+
if (returnFocus && previouslyFocused && typeof previouslyFocused.focus === "function" && previouslyFocused.isConnected) {
|
|
224
252
|
previouslyFocused.focus();
|
|
225
253
|
}
|
|
226
254
|
};
|
|
@@ -283,10 +311,421 @@ function unmarkAriaHidden(element) {
|
|
|
283
311
|
ariaHiddenStack.splice(index, 1);
|
|
284
312
|
}
|
|
285
313
|
}
|
|
314
|
+
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
|
+
}
|
|
345
|
+
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;
|
|
464
|
+
function supportsAnchorPositioning() {
|
|
465
|
+
if (_supportsAnchor === null) {
|
|
466
|
+
_supportsAnchor = typeof CSS !== "undefined" && typeof CSS.supports === "function" && CSS.supports("anchor-name", "--a");
|
|
467
|
+
}
|
|
468
|
+
return _supportsAnchor;
|
|
469
|
+
}
|
|
470
|
+
function isRtl() {
|
|
471
|
+
return typeof document !== "undefined" && document.documentElement.dir === "rtl";
|
|
472
|
+
}
|
|
473
|
+
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;
|
|
489
|
+
}
|
|
490
|
+
function computePositionFromRect(refRect, floatingRect, placement, offset) {
|
|
491
|
+
let x = 0;
|
|
492
|
+
let y = 0;
|
|
493
|
+
const [side, alignment] = placement.split("-");
|
|
494
|
+
const rtl = isRtl();
|
|
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 };
|
|
536
|
+
}
|
|
537
|
+
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
|
+
}
|
|
553
|
+
}
|
|
554
|
+
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
|
+
};
|
|
562
|
+
}
|
|
563
|
+
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";
|
|
582
|
+
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;
|
|
590
|
+
}
|
|
591
|
+
function findAnchorClass(element) {
|
|
592
|
+
return Array.from(element.classList).find((c) => c.startsWith(ANCHOR_CLASS_PREFIX));
|
|
593
|
+
}
|
|
594
|
+
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;
|
|
655
|
+
}
|
|
656
|
+
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
|
+
}
|
|
671
|
+
}
|
|
672
|
+
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;
|
|
687
|
+
}
|
|
688
|
+
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
|
+
}
|
|
286
722
|
export {
|
|
287
723
|
animate,
|
|
724
|
+
autoUpdate,
|
|
288
725
|
cancelAnimations,
|
|
726
|
+
computePosition,
|
|
289
727
|
createFocusTrap,
|
|
728
|
+
createMenuNavigation,
|
|
290
729
|
easings,
|
|
291
730
|
except,
|
|
292
731
|
generateId,
|
|
@@ -294,8 +733,11 @@ export {
|
|
|
294
733
|
kebabCase,
|
|
295
734
|
lockScroll,
|
|
296
735
|
markAriaHidden,
|
|
736
|
+
onClickOutside,
|
|
297
737
|
onEscapeKey,
|
|
738
|
+
onceChildrenRendered,
|
|
298
739
|
only,
|
|
299
740
|
rejectNullValues,
|
|
300
|
-
sameUrlPath
|
|
741
|
+
sameUrlPath,
|
|
742
|
+
supportsAnchorPositioning
|
|
301
743
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inertiaui/vanilla",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"author": "Pascal Baljet <pascal@protone.media>",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,12 +37,14 @@
|
|
|
37
37
|
"dev": "vite build --watch",
|
|
38
38
|
"build": "vite build",
|
|
39
39
|
"lint": "eslint src/",
|
|
40
|
-
"format": "prettier --write src/
|
|
41
|
-
"format:check": "prettier --check src/
|
|
40
|
+
"format": "prettier --write src/ e2e/",
|
|
41
|
+
"format:check": "prettier --check src/ e2e/",
|
|
42
42
|
"test": "vitest",
|
|
43
|
+
"test:e2e": "playwright test",
|
|
43
44
|
"prepublishOnly": "npm run build"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
47
|
+
"@playwright/test": "^1.58.2",
|
|
46
48
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
47
49
|
"@typescript-eslint/parser": "^8.0.0",
|
|
48
50
|
"@vitest/coverage-v8": "^3.1.0",
|