@inertiaui/vanilla 1.1.0 → 1.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 +507 -7
- package/dist/index.d.ts +374 -0
- package/dist/index.js +1394 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Inertia UI Vanilla
|
|
2
2
|
|
|
3
|
-
A lightweight vanilla TypeScript library providing UI utilities for dialogs, animations, focus management, menu navigation, click outside detection, floating
|
|
3
|
+
A lightweight vanilla TypeScript library providing UI utilities for dialogs, animations, focus management, menu/listbox navigation, click outside detection, floating and native popover positioning, reorder interactions, color parsing, debounce helpers, and common helper functions. Framework-agnostic and designed to integrate seamlessly with Vue, React, or any JavaScript application.
|
|
4
4
|
|
|
5
5
|
This package is part of the [Inertia UI](https://inertiaui.com) suite. Check out our other packages:
|
|
6
6
|
|
|
@@ -22,12 +22,16 @@ npm install @inertiaui/vanilla
|
|
|
22
22
|
- [Keyboard Events](#keyboard-events)
|
|
23
23
|
- [Click Outside](#click-outside)
|
|
24
24
|
- [Menu Navigation](#menu-navigation)
|
|
25
|
+
- [Listbox Helpers](#listbox-helpers)
|
|
26
|
+
- [Reorder](#reorder)
|
|
25
27
|
- [Positioning](#positioning)
|
|
28
|
+
- [Native Popovers](#native-popovers)
|
|
26
29
|
- [Accessibility](#accessibility)
|
|
27
30
|
- [Animation](#animation)
|
|
28
31
|
- [Dark Mode Detection](#dark-mode-detection)
|
|
29
32
|
- [RTL Support](#rtl-support)
|
|
30
33
|
- [Debounce](#debounce)
|
|
34
|
+
- [Color](#color)
|
|
31
35
|
- [Helpers](#helpers)
|
|
32
36
|
- [generateId](#generateid)
|
|
33
37
|
- [blank](#blank)
|
|
@@ -54,7 +58,7 @@ unlock()
|
|
|
54
58
|
The function:
|
|
55
59
|
|
|
56
60
|
- Sets `document.body.style.overflow` to `'hidden'`
|
|
57
|
-
-
|
|
61
|
+
- Uses `scrollbar-gutter: stable` when supported, otherwise adds body padding to compensate for scrollbar width
|
|
58
62
|
- Returns a cleanup function that can only unlock once
|
|
59
63
|
|
|
60
64
|
### Reference Counting
|
|
@@ -145,6 +149,21 @@ The focus trap recognizes these elements as focusable:
|
|
|
145
149
|
|
|
146
150
|
Elements with `aria-hidden="true"` are excluded. (Elements with `disabled` are already filtered by the selectors above.)
|
|
147
151
|
|
|
152
|
+
### focusFirstEnabledElement
|
|
153
|
+
|
|
154
|
+
Focuses the first non-disabled element in a list of candidates. Nullish values are ignored, and the function returns whether anything was focused.
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
import { focusFirstEnabledElement } from '@inertiaui/vanilla'
|
|
158
|
+
|
|
159
|
+
const focused = focusFirstEnabledElement([
|
|
160
|
+
document.getElementById('primary') as HTMLButtonElement | null,
|
|
161
|
+
document.getElementById('fallback') as HTMLButtonElement | null,
|
|
162
|
+
])
|
|
163
|
+
|
|
164
|
+
// true when an enabled element was focused
|
|
165
|
+
```
|
|
166
|
+
|
|
148
167
|
## Keyboard Events
|
|
149
168
|
|
|
150
169
|
### onEscapeKey
|
|
@@ -334,6 +353,122 @@ function openMenu(menuElement: HTMLElement) {
|
|
|
334
353
|
}
|
|
335
354
|
```
|
|
336
355
|
|
|
356
|
+
## Listbox Helpers
|
|
357
|
+
|
|
358
|
+
Lower-level utilities for combobox/listbox-style controls.
|
|
359
|
+
|
|
360
|
+
### resolveListboxNavigation
|
|
361
|
+
|
|
362
|
+
Resolves common listbox navigation keys (`ArrowDown`, `ArrowUp`, `Home`, `End`) to a new active index.
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
import { resolveListboxNavigation } from '@inertiaui/vanilla'
|
|
366
|
+
|
|
367
|
+
const result = resolveListboxNavigation({
|
|
368
|
+
items,
|
|
369
|
+
currentIndex,
|
|
370
|
+
key: event.key,
|
|
371
|
+
isItemDisabled: (item) => item.disabled,
|
|
372
|
+
})
|
|
373
|
+
|
|
374
|
+
if (result.handled) {
|
|
375
|
+
event.preventDefault()
|
|
376
|
+
currentIndex = result.index
|
|
377
|
+
}
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
### createFocusOutDismiss
|
|
381
|
+
|
|
382
|
+
Schedules dismissal when focus leaves a container. This is useful for comboboxes and popovers where blur should dismiss after the browser has moved focus.
|
|
383
|
+
|
|
384
|
+
```typescript
|
|
385
|
+
import { createFocusOutDismiss } from '@inertiaui/vanilla'
|
|
386
|
+
|
|
387
|
+
const focusOut = createFocusOutDismiss({
|
|
388
|
+
container: () => popoverElement,
|
|
389
|
+
onDismiss: closePopover,
|
|
390
|
+
delay: 150,
|
|
391
|
+
shouldIgnore: () => isPointerDownInsideScrollbar,
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
popoverElement.addEventListener('focusout', (event) => {
|
|
395
|
+
focusOut.schedule(event)
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
// Call when opening to invalidate an old scheduled dismissal.
|
|
399
|
+
focusOut.markOpen()
|
|
400
|
+
|
|
401
|
+
// Later
|
|
402
|
+
focusOut.cleanup()
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
## Reorder
|
|
406
|
+
|
|
407
|
+
Framework-neutral helpers for keyboard and pointer reordering.
|
|
408
|
+
|
|
409
|
+
### createReorderableList
|
|
410
|
+
|
|
411
|
+
Use `createReorderableList` when you want a headless list controller that owns item registration, live pointer preview state, keyboard moves, pointer commits, optional bounds, optional auto-scroll, and cleanup. Rendering, announcements, focus handling, and animations stay in your package or framework adapter.
|
|
412
|
+
|
|
413
|
+
```typescript
|
|
414
|
+
import {
|
|
415
|
+
animateFromSnapshot,
|
|
416
|
+
captureAnimationSnapshot,
|
|
417
|
+
createReorderableList,
|
|
418
|
+
REORDERABLE_LIST_HANDLE_ATTRIBUTE,
|
|
419
|
+
type AnimationSnapshot,
|
|
420
|
+
} from '@inertiaui/vanilla'
|
|
421
|
+
|
|
422
|
+
let items = ['Apple', 'Banana', 'Cherry']
|
|
423
|
+
let animationSnapshot: AnimationSnapshot | null = null
|
|
424
|
+
|
|
425
|
+
const controller = createReorderableList({
|
|
426
|
+
getItems: () => items,
|
|
427
|
+
setItems: (nextItems) => {
|
|
428
|
+
items = nextItems
|
|
429
|
+
render()
|
|
430
|
+
},
|
|
431
|
+
getBounds: () => listElement.getBoundingClientRect(),
|
|
432
|
+
autoScroll: true,
|
|
433
|
+
onChange: () => render(),
|
|
434
|
+
onBeforeReorder: (move, context) => {
|
|
435
|
+
if (!context.alreadyPreviewed) {
|
|
436
|
+
animationSnapshot = captureAnimationSnapshot(itemElements)
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
onReorder: (move) => {
|
|
440
|
+
animateFromSnapshot(animationSnapshot, itemElements)
|
|
441
|
+
animationSnapshot = null
|
|
442
|
+
announce(`Moved ${move.item}`)
|
|
443
|
+
},
|
|
444
|
+
})
|
|
445
|
+
|
|
446
|
+
controller.setListElement(listElement)
|
|
447
|
+
|
|
448
|
+
items.forEach((_, index) => {
|
|
449
|
+
controller.setItemElement(index, itemElements[index])
|
|
450
|
+
handles[index].setAttribute(REORDERABLE_LIST_HANDLE_ATTRIBUTE, 'true')
|
|
451
|
+
handles[index].addEventListener('pointerdown', (event) => controller.pointerDown(index, event))
|
|
452
|
+
})
|
|
453
|
+
|
|
454
|
+
moveUpButton.addEventListener('click', () => controller.moveItem(index, 'up'))
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
Use `getPreviewOrder()` or `getPreviewItems()` during pointer dragging when your renderer needs to show the future order before release. The reorder detail includes `alreadyPreviewed`, so consumers can avoid replaying the same release animation when the list already animated into the committed position.
|
|
458
|
+
|
|
459
|
+
#### Reorderable List Options
|
|
460
|
+
|
|
461
|
+
| Option | Type | Default | Description |
|
|
462
|
+
|--------|------|---------|-------------|
|
|
463
|
+
| `getItems` | `() => readonly T[]` | Required | Current ordered items |
|
|
464
|
+
| `setItems` | `(items: T[]) => void` | `undefined` | Optional writer used by `moveItem` and pointer commits |
|
|
465
|
+
| `canReorder` | `() => boolean` | `true` | Guard before starting or committing a reorder |
|
|
466
|
+
| `getBounds` | `() => ReorderBounds \| null` | Registered list rect | Optional outer active area |
|
|
467
|
+
| `autoScroll` | `boolean \| AutoScrollerOptions` | `false` | Edge auto-scroll while dragging |
|
|
468
|
+
| `onChange` | `(state) => void` | `undefined` | Called when pointer drag state changes |
|
|
469
|
+
| `onBeforeReorder` | `(move, context) => void` | `undefined` | Called before writing items or firing `onReorder` |
|
|
470
|
+
| `onReorder` | `(move, context) => void` | `undefined` | Called after a valid keyboard or pointer reorder |
|
|
471
|
+
|
|
337
472
|
## Positioning
|
|
338
473
|
|
|
339
474
|
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.
|
|
@@ -374,6 +509,7 @@ The function applies positioning styles to the floating element automatically (`
|
|
|
374
509
|
| `placement` | `Placement` | `'bottom-start'` | Where to position the floating element |
|
|
375
510
|
| `offset` | `number` | `0` | Distance in pixels between reference and floating element |
|
|
376
511
|
| `flip` | `boolean` | `true` | Flip to opposite side when overflowing viewport |
|
|
512
|
+
| `autoSize` | `boolean` | `false` | Constrain the floating element to the available viewport space (sets `max-height` and `overflow-y: auto`) |
|
|
377
513
|
|
|
378
514
|
#### Placements
|
|
379
515
|
|
|
@@ -413,6 +549,26 @@ const result = computePosition(reference, floating, {
|
|
|
413
549
|
|
|
414
550
|
The floating element is always clamped to stay within the viewport with a 4px margin, even after flipping.
|
|
415
551
|
|
|
552
|
+
#### Auto Sizing
|
|
553
|
+
|
|
554
|
+
Enable `autoSize` to cap the floating element's height to the space between its anchored edge and the viewport edge, adding a scrollbar when the content is taller. Useful for long menus that would otherwise overflow.
|
|
555
|
+
|
|
556
|
+
```typescript
|
|
557
|
+
computePosition(reference, floating, {
|
|
558
|
+
placement: 'bottom-start',
|
|
559
|
+
offset: 8,
|
|
560
|
+
autoSize: true,
|
|
561
|
+
})
|
|
562
|
+
```
|
|
563
|
+
|
|
564
|
+
This sets `max-height` and `overflow-y: auto` on the floating element. Cap the height further via the `--iui-max-height` CSS variable (mirrors Headless UI's `--anchor-max-height`):
|
|
565
|
+
|
|
566
|
+
```css
|
|
567
|
+
.menu {
|
|
568
|
+
--iui-max-height: 20rem;
|
|
569
|
+
}
|
|
570
|
+
```
|
|
571
|
+
|
|
416
572
|
### autoUpdate
|
|
417
573
|
|
|
418
574
|
Automatically reposition the floating element when the layout changes:
|
|
@@ -443,6 +599,67 @@ Updates are batched using `requestAnimationFrame` to avoid layout thrashing.
|
|
|
443
599
|
|
|
444
600
|
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.
|
|
445
601
|
|
|
602
|
+
Use `supportsTopLayerAnchorPositioning()` to check whether the browser also supports anchor positioning/sizing for top-layer popovers:
|
|
603
|
+
|
|
604
|
+
```typescript
|
|
605
|
+
import { supportsTopLayerAnchorPositioning } from '@inertiaui/vanilla'
|
|
606
|
+
|
|
607
|
+
if (supportsTopLayerAnchorPositioning()) {
|
|
608
|
+
// Native anchor positioning can be used for popover/top-layer content.
|
|
609
|
+
}
|
|
610
|
+
```
|
|
611
|
+
|
|
612
|
+
### positionTopLayerPopover
|
|
613
|
+
|
|
614
|
+
Position a native top-layer popover (`popover="manual"` or `popover="auto"`) relative to a reference element. This helper uses CSS Anchor Positioning when available and falls back to manual fixed positioning.
|
|
615
|
+
|
|
616
|
+
```typescript
|
|
617
|
+
import { positionTopLayerPopover } from '@inertiaui/vanilla'
|
|
618
|
+
|
|
619
|
+
popover.showPopover()
|
|
620
|
+
|
|
621
|
+
const result = positionTopLayerPopover(button, popover, {
|
|
622
|
+
placement: 'bottom-start',
|
|
623
|
+
offset: 4,
|
|
624
|
+
matchReferenceWidth: true,
|
|
625
|
+
viewportMargin: 8,
|
|
626
|
+
})
|
|
627
|
+
```
|
|
628
|
+
|
|
629
|
+
#### Top-Layer Options
|
|
630
|
+
|
|
631
|
+
| Option | Type | Default | Description |
|
|
632
|
+
|--------|------|---------|-------------|
|
|
633
|
+
| `placement` | `Placement` | `'bottom-start'` | Preferred placement |
|
|
634
|
+
| `offset` | `number` | `4` | Distance from the reference |
|
|
635
|
+
| `flip` | `boolean` | `true` | Flip when the preferred side overflows |
|
|
636
|
+
| `matchReferenceWidth` | `boolean` | `false` | Set width to the reference width, clamped to viewport |
|
|
637
|
+
| `viewportMargin` | `number` | `8` | Space kept between the popover and viewport edge |
|
|
638
|
+
| `anchorPositioning` | `boolean` | `true` | Use CSS Anchor Positioning when supported |
|
|
639
|
+
|
|
640
|
+
The helper also sets safe overflow styles (`max-width`, `max-height`, `overflow`, and `overscroll-behavior`) so long top-layer menus scroll instead of overflowing the viewport.
|
|
641
|
+
|
|
642
|
+
When CSS Anchor Positioning is available but the rendered top-layer rectangle
|
|
643
|
+
still overflows the viewport, the helper automatically switches to the same
|
|
644
|
+
manual fixed-positioning fallback used when anchors are unavailable.
|
|
645
|
+
|
|
646
|
+
### autoUpdateTopLayerPopover
|
|
647
|
+
|
|
648
|
+
Like `autoUpdate`, but also listens to `visualViewport` scroll/resize events, which are important for top-layer popovers on mobile browsers.
|
|
649
|
+
|
|
650
|
+
```typescript
|
|
651
|
+
import { autoUpdateTopLayerPopover, positionTopLayerPopover } from '@inertiaui/vanilla'
|
|
652
|
+
|
|
653
|
+
const cleanup = autoUpdateTopLayerPopover(button, popover, () => {
|
|
654
|
+
positionTopLayerPopover(button, popover, {
|
|
655
|
+
placement: 'bottom-start',
|
|
656
|
+
matchReferenceWidth: true,
|
|
657
|
+
})
|
|
658
|
+
})
|
|
659
|
+
|
|
660
|
+
cleanup()
|
|
661
|
+
```
|
|
662
|
+
|
|
446
663
|
### Full Example
|
|
447
664
|
|
|
448
665
|
```typescript
|
|
@@ -468,6 +685,67 @@ function setupTooltip(trigger: HTMLElement, tooltip: HTMLElement) {
|
|
|
468
685
|
}
|
|
469
686
|
```
|
|
470
687
|
|
|
688
|
+
## Native Popovers
|
|
689
|
+
|
|
690
|
+
`createNativePopoverDisclosure` coordinates open state, native `showPopover()`/`hidePopover()`, top-layer positioning, auto-updates, toggle-event sync, and optional focus-out dismissal.
|
|
691
|
+
|
|
692
|
+
```typescript
|
|
693
|
+
import { createNativePopoverDisclosure } from '@inertiaui/vanilla'
|
|
694
|
+
|
|
695
|
+
const disclosure = createNativePopoverDisclosure({
|
|
696
|
+
reference: () => button,
|
|
697
|
+
popover: () => popover,
|
|
698
|
+
position: {
|
|
699
|
+
placement: 'bottom-start',
|
|
700
|
+
offset: 4,
|
|
701
|
+
matchReferenceWidth: true,
|
|
702
|
+
},
|
|
703
|
+
focusOut: {
|
|
704
|
+
container: () => popover,
|
|
705
|
+
onDismiss: () => disclosure.closePopover(),
|
|
706
|
+
},
|
|
707
|
+
onOpenChange: (open) => {
|
|
708
|
+
button.setAttribute('aria-expanded', String(open))
|
|
709
|
+
},
|
|
710
|
+
})
|
|
711
|
+
|
|
712
|
+
button.addEventListener('click', () => {
|
|
713
|
+
disclosure.togglePopover(
|
|
714
|
+
() => {
|
|
715
|
+
disclosure.openPopover()
|
|
716
|
+
disclosure.showPopover()
|
|
717
|
+
disclosure.updatePosition()
|
|
718
|
+
disclosure.startAutoUpdate()
|
|
719
|
+
},
|
|
720
|
+
() => {
|
|
721
|
+
disclosure.closePopover()
|
|
722
|
+
},
|
|
723
|
+
)
|
|
724
|
+
})
|
|
725
|
+
|
|
726
|
+
popover.addEventListener('toggle', (event) => {
|
|
727
|
+
disclosure.handlePopoverToggle(event)
|
|
728
|
+
})
|
|
729
|
+
|
|
730
|
+
popover.addEventListener('focusout', (event) => {
|
|
731
|
+
disclosure.handleFocusOut(event)
|
|
732
|
+
})
|
|
733
|
+
```
|
|
734
|
+
|
|
735
|
+
### Controller Methods
|
|
736
|
+
|
|
737
|
+
| Method | Description |
|
|
738
|
+
|--------|-------------|
|
|
739
|
+
| `openPopover({ onBeforeOpen })` | Marks the controller open and cancels pending focus-out dismissal |
|
|
740
|
+
| `closePopover({ onClose, hide })` | Marks closed, optionally hides natively, and stops auto-updates |
|
|
741
|
+
| `togglePopover(open, close)` | Calls one of two callbacks based on current state |
|
|
742
|
+
| `showPopover()` / `hidePopover()` | Delegates to native popover methods |
|
|
743
|
+
| `handlePopoverToggle(event, { onClose })` | Syncs controller state when the browser closes the native popover |
|
|
744
|
+
| `handleFocusOut(event)` | Delegates to the configured focus-out dismiss controller |
|
|
745
|
+
| `updatePosition()` | Runs `positionTopLayerPopover` |
|
|
746
|
+
| `startAutoUpdate()` | Starts `autoUpdateTopLayerPopover` |
|
|
747
|
+
| `cleanupPopover()` | Cleans focus-out and positioning listeners |
|
|
748
|
+
|
|
471
749
|
## Accessibility
|
|
472
750
|
|
|
473
751
|
Accessibility utilities for managing `aria-hidden` attributes with reference counting support.
|
|
@@ -543,18 +821,33 @@ cleanup()
|
|
|
543
821
|
element.getAttribute('aria-hidden') // null (removed)
|
|
544
822
|
```
|
|
545
823
|
|
|
824
|
+
### markInert
|
|
825
|
+
|
|
826
|
+
Marks an element as inert and returns a cleanup function. Native `HTMLElement.inert` is used when available; older browsers fall back to `aria-hidden="true"`.
|
|
827
|
+
|
|
828
|
+
```typescript
|
|
829
|
+
import { markInert } from '@inertiaui/vanilla'
|
|
830
|
+
|
|
831
|
+
const cleanup = markInert('#app')
|
|
832
|
+
|
|
833
|
+
// Later, restore the original inert/aria-hidden state
|
|
834
|
+
cleanup()
|
|
835
|
+
```
|
|
836
|
+
|
|
837
|
+
Like `markAriaHidden`, this accepts either an element or selector and uses reference counting. Original `aria-hidden` and native `inert` values are restored after the final cleanup.
|
|
838
|
+
|
|
546
839
|
### Use with Dialogs
|
|
547
840
|
|
|
548
841
|
When a dialog opens, the main content should be marked as `aria-hidden` to prevent screen readers from reading background content:
|
|
549
842
|
|
|
550
843
|
```typescript
|
|
551
|
-
import {
|
|
844
|
+
import { markInert, lockScroll, createFocusTrap, onEscapeKey } from '@inertiaui/vanilla'
|
|
552
845
|
|
|
553
846
|
function openDialog(dialogElement: HTMLElement) {
|
|
554
847
|
const closeDialog = () => cleanups.forEach(fn => fn())
|
|
555
848
|
|
|
556
849
|
const cleanups = [
|
|
557
|
-
|
|
850
|
+
markInert('#app'),
|
|
558
851
|
lockScroll(),
|
|
559
852
|
createFocusTrap(dialogElement),
|
|
560
853
|
onEscapeKey(closeDialog),
|
|
@@ -619,6 +912,55 @@ Or provide a custom easing string:
|
|
|
619
912
|
await animate(element, keyframes, { easing: 'cubic-bezier(0.68, -0.55, 0.27, 1.55)' })
|
|
620
913
|
```
|
|
621
914
|
|
|
915
|
+
### captureAnimationSnapshot and animateFromSnapshot
|
|
916
|
+
|
|
917
|
+
Capture element positions before your renderer changes layout, then animate the
|
|
918
|
+
moved elements from their old positions to their new positions. This is useful
|
|
919
|
+
for FLIP-style reorder animations in framework adapters.
|
|
920
|
+
|
|
921
|
+
```typescript
|
|
922
|
+
import { animateFromSnapshot, captureAnimationSnapshot } from '@inertiaui/vanilla'
|
|
923
|
+
|
|
924
|
+
const rows = () => document.querySelectorAll<HTMLElement>('[data-reorder-row]')
|
|
925
|
+
const snapshot = captureAnimationSnapshot(rows())
|
|
926
|
+
|
|
927
|
+
renderItemsInNewOrder()
|
|
928
|
+
|
|
929
|
+
animateFromSnapshot(snapshot, rows(), {
|
|
930
|
+
duration: 160,
|
|
931
|
+
easing: 'cubic-bezier(0.22, 1, 0.36, 1)',
|
|
932
|
+
fill: 'none',
|
|
933
|
+
minimumDelta: 0.5,
|
|
934
|
+
includeCurrentTransform: true,
|
|
935
|
+
})
|
|
936
|
+
```
|
|
937
|
+
|
|
938
|
+
`captureAnimationSnapshot` returns `null` when Web Animations are unavailable or
|
|
939
|
+
the user prefers reduced motion. `animateFromSnapshot` only cancels previous
|
|
940
|
+
snapshot animations it started for the same element, leaving unrelated hover,
|
|
941
|
+
focus, and component animations alone.
|
|
942
|
+
|
|
943
|
+
#### Snapshot Animation Options
|
|
944
|
+
|
|
945
|
+
| Option | Type | Default | Description |
|
|
946
|
+
|--------|------|---------|-------------|
|
|
947
|
+
| `duration` | `number` | `160` | Animation duration in milliseconds |
|
|
948
|
+
| `easing` | `string \| EasingName` | `'out'` | Easing preset name or CSS easing string |
|
|
949
|
+
| `fill` | `FillMode` | `'none'` | Web Animations fill mode |
|
|
950
|
+
| `minimumDelta` | `number` | `0.5` | Ignore moves smaller than this many pixels |
|
|
951
|
+
| `includeCurrentTransform` | `boolean` | `true` | Fold the element's current translate transform into the next animation |
|
|
952
|
+
|
|
953
|
+
Use `prefersReducedMotion()` and `supportsWebAnimations()` when an adapter needs
|
|
954
|
+
to branch before capturing a snapshot:
|
|
955
|
+
|
|
956
|
+
```typescript
|
|
957
|
+
import { prefersReducedMotion, supportsWebAnimations } from '@inertiaui/vanilla'
|
|
958
|
+
|
|
959
|
+
if (!prefersReducedMotion() && supportsWebAnimations()) {
|
|
960
|
+
const snapshot = captureAnimationSnapshot(rows())
|
|
961
|
+
}
|
|
962
|
+
```
|
|
963
|
+
|
|
622
964
|
### cancelAnimations
|
|
623
965
|
|
|
624
966
|
Cancel any running animations on an element:
|
|
@@ -737,6 +1079,25 @@ const handleScroll = debounce(() => {
|
|
|
737
1079
|
window.addEventListener('scroll', handleScroll)
|
|
738
1080
|
```
|
|
739
1081
|
|
|
1082
|
+
### createDebouncer
|
|
1083
|
+
|
|
1084
|
+
Create a timeout-based debouncer with an explicit millisecond delay. This is useful for typeahead, async search, remote option loading, or any work that should wait until rapid calls settle.
|
|
1085
|
+
|
|
1086
|
+
```typescript
|
|
1087
|
+
import { createDebouncer } from '@inertiaui/vanilla'
|
|
1088
|
+
|
|
1089
|
+
const debouncer = createDebouncer(250)
|
|
1090
|
+
|
|
1091
|
+
input.addEventListener('input', () => {
|
|
1092
|
+
debouncer.schedule(() => {
|
|
1093
|
+
search(input.value)
|
|
1094
|
+
})
|
|
1095
|
+
})
|
|
1096
|
+
|
|
1097
|
+
// Cancel a pending scheduled call
|
|
1098
|
+
debouncer.cancel()
|
|
1099
|
+
```
|
|
1100
|
+
|
|
740
1101
|
### detectFramerate
|
|
741
1102
|
|
|
742
1103
|
Detect the browser's current framerate. Returns a Promise that resolves with the detected FPS (capped to the 30–240 range). Falls back to 60 if `requestAnimationFrame` is unavailable or detection times out.
|
|
@@ -748,6 +1109,117 @@ const fps = await detectFramerate()
|
|
|
748
1109
|
console.log(`Running at ${fps} FPS`)
|
|
749
1110
|
```
|
|
750
1111
|
|
|
1112
|
+
## Color
|
|
1113
|
+
|
|
1114
|
+
Convert colors between hex and HSL.
|
|
1115
|
+
|
|
1116
|
+
### hexToHsl
|
|
1117
|
+
|
|
1118
|
+
Convert a hex color string to HSL values. Accepts the string with or without a leading `#`. Returns an `HslColor` object (`{ h, s, l }`) with `h` in `0–360` and `s`/`l` in `0–100`. Invalid input falls back to `{ h: 0, s: 100, l: 50 }`.
|
|
1119
|
+
|
|
1120
|
+
```typescript
|
|
1121
|
+
import { hexToHsl } from '@inertiaui/vanilla'
|
|
1122
|
+
|
|
1123
|
+
hexToHsl('#3490dc') // { h: 207, s: 71, l: 53 }
|
|
1124
|
+
hexToHsl('3490dc') // same — leading '#' is optional
|
|
1125
|
+
```
|
|
1126
|
+
|
|
1127
|
+
### hslToHex
|
|
1128
|
+
|
|
1129
|
+
Convert HSL values to a hex color string (with leading `#`). Expects `h: 0–360`, `s: 0–100`, `l: 0–100`.
|
|
1130
|
+
|
|
1131
|
+
```typescript
|
|
1132
|
+
import { hslToHex } from '@inertiaui/vanilla'
|
|
1133
|
+
|
|
1134
|
+
hslToHex(207, 71, 53) // '#3290dc'
|
|
1135
|
+
```
|
|
1136
|
+
|
|
1137
|
+
The `HslColor` type is also exported:
|
|
1138
|
+
|
|
1139
|
+
```typescript
|
|
1140
|
+
import type { HslColor } from '@inertiaui/vanilla'
|
|
1141
|
+
```
|
|
1142
|
+
|
|
1143
|
+
### RGB and Normalization Helpers
|
|
1144
|
+
|
|
1145
|
+
```typescript
|
|
1146
|
+
import {
|
|
1147
|
+
formatAlpha,
|
|
1148
|
+
hexToRgb,
|
|
1149
|
+
normalizeAlpha,
|
|
1150
|
+
normalizeHue,
|
|
1151
|
+
normalizePercent,
|
|
1152
|
+
rgbToHex,
|
|
1153
|
+
} from '@inertiaui/vanilla'
|
|
1154
|
+
|
|
1155
|
+
hexToRgb('#3490dc') // { r: 52, g: 144, b: 220 }
|
|
1156
|
+
rgbToHex(52, 144, 220) // '#3490dc'
|
|
1157
|
+
normalizeHue(-10) // 350
|
|
1158
|
+
normalizePercent(120) // 100
|
|
1159
|
+
normalizeAlpha(1.5) // 1
|
|
1160
|
+
formatAlpha(0.5) // '0.5'
|
|
1161
|
+
```
|
|
1162
|
+
|
|
1163
|
+
### Color Parsing
|
|
1164
|
+
|
|
1165
|
+
Parse hex, RGB/RGBA, or HSL/HSLA strings into a normalized `ParsedColor`.
|
|
1166
|
+
|
|
1167
|
+
```typescript
|
|
1168
|
+
import {
|
|
1169
|
+
parseColorString,
|
|
1170
|
+
parseHexColor,
|
|
1171
|
+
parseHslColor,
|
|
1172
|
+
parseRgbColor,
|
|
1173
|
+
} from '@inertiaui/vanilla'
|
|
1174
|
+
|
|
1175
|
+
parseColorString('#3490dc')
|
|
1176
|
+
// { hex: '#3490dc', h: 207, s: 71, l: 53, alpha: 1 }
|
|
1177
|
+
|
|
1178
|
+
parseColorString('rgba(52, 144, 220, 0.5)')
|
|
1179
|
+
// { hex: '#3490dc', h: 207, s: 71, l: 53, alpha: 0.5 }
|
|
1180
|
+
|
|
1181
|
+
parseColorString('hsla(207, 71%, 53%, 50%)')
|
|
1182
|
+
// { hex: '#3290dc', h: 207, s: 71, l: 53, alpha: 0.5 }
|
|
1183
|
+
```
|
|
1184
|
+
|
|
1185
|
+
The lower-level parsers return `null` when the input is invalid:
|
|
1186
|
+
|
|
1187
|
+
```typescript
|
|
1188
|
+
parseHexColor('#fff')
|
|
1189
|
+
parseRgbColor('rgb(52, 144, 220)')
|
|
1190
|
+
parseHslColor('hsl(207, 71%, 53%)')
|
|
1191
|
+
```
|
|
1192
|
+
|
|
1193
|
+
### Channel Parsing
|
|
1194
|
+
|
|
1195
|
+
```typescript
|
|
1196
|
+
import { parseAlphaChannel, parseRgbChannel } from '@inertiaui/vanilla'
|
|
1197
|
+
|
|
1198
|
+
parseRgbChannel('50%') // 128
|
|
1199
|
+
parseRgbChannel('255') // 255
|
|
1200
|
+
parseAlphaChannel('50%') // 0.5
|
|
1201
|
+
parseAlphaChannel('0.25') // 0.25
|
|
1202
|
+
```
|
|
1203
|
+
|
|
1204
|
+
### formatColor
|
|
1205
|
+
|
|
1206
|
+
Format a parsed color as hex, RGB/RGBA, or HSL/HSLA. Alpha is included automatically when `alpha < 1`, or explicitly via `includeAlpha`.
|
|
1207
|
+
|
|
1208
|
+
```typescript
|
|
1209
|
+
import { formatColor, parseColorString } from '@inertiaui/vanilla'
|
|
1210
|
+
|
|
1211
|
+
const color = parseColorString('rgba(52, 144, 220, 0.5)')!
|
|
1212
|
+
|
|
1213
|
+
formatColor(color, 'hex') // '#3490dc80'
|
|
1214
|
+
formatColor(color, 'rgb') // 'rgba(52, 144, 220, 0.5)'
|
|
1215
|
+
formatColor(color, 'hsl') // 'hsla(207, 71%, 53%, 0.5)'
|
|
1216
|
+
|
|
1217
|
+
formatColor(color, 'hex', { includeAlpha: false })
|
|
1218
|
+
// '#3490dc'
|
|
1219
|
+
```
|
|
1220
|
+
|
|
1221
|
+
Types exported by the color module include `HslColor`, `RgbColor`, `ParsedColor`, `ColorFormat`, and `FormatColorOptions`.
|
|
1222
|
+
|
|
751
1223
|
## Helpers
|
|
752
1224
|
|
|
753
1225
|
### generateId
|
|
@@ -1097,16 +1569,44 @@ This library is written in TypeScript and exports the following types:
|
|
|
1097
1569
|
|
|
1098
1570
|
```typescript
|
|
1099
1571
|
import type {
|
|
1572
|
+
AnimateOptions,
|
|
1573
|
+
AutoScrollAxis,
|
|
1574
|
+
AutoScrollContainer,
|
|
1575
|
+
AutoScrollerOptions,
|
|
1100
1576
|
CleanupFunction,
|
|
1101
|
-
|
|
1577
|
+
ColorFormat,
|
|
1578
|
+
DarkModeStrategy,
|
|
1579
|
+
Debouncer,
|
|
1102
1580
|
EscapeKeyOptions,
|
|
1103
|
-
|
|
1581
|
+
FocusOutDismissController,
|
|
1582
|
+
FocusOutDismissOptions,
|
|
1583
|
+
FocusTrapOptions,
|
|
1104
1584
|
EasingName,
|
|
1585
|
+
FormatColorOptions,
|
|
1586
|
+
HslColor,
|
|
1587
|
+
ListboxNavigationOptions,
|
|
1588
|
+
ListboxNavigationResult,
|
|
1105
1589
|
MenuNavigationOptions,
|
|
1590
|
+
NativePopoverCloseOptions,
|
|
1591
|
+
NativePopoverDisclosureController,
|
|
1592
|
+
NativePopoverDisclosureOptions,
|
|
1593
|
+
NativePopoverOpenOptions,
|
|
1594
|
+
NativePopoverToggleOptions,
|
|
1106
1595
|
Placement,
|
|
1596
|
+
ParsedColor,
|
|
1107
1597
|
PositionOptions,
|
|
1108
1598
|
PositionResult,
|
|
1109
|
-
|
|
1599
|
+
ReorderBounds,
|
|
1600
|
+
ReorderCommitContext,
|
|
1601
|
+
ReorderDirection,
|
|
1602
|
+
ReorderMove,
|
|
1603
|
+
ReorderPreviewItem,
|
|
1604
|
+
ReorderableListController,
|
|
1605
|
+
ReorderableListOptions,
|
|
1606
|
+
ReorderableListState,
|
|
1607
|
+
ReorderSource,
|
|
1608
|
+
RgbColor,
|
|
1609
|
+
TopLayerPopoverPositionOptions,
|
|
1110
1610
|
} from '@inertiaui/vanilla'
|
|
1111
1611
|
```
|
|
1112
1612
|
|