@inertiaui/vanilla 1.0.3 → 1.2.1
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 +445 -7
- package/dist/index.d.ts +357 -0
- package/dist/index.js +1320 -13
- 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,113 @@ 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 { createReorderableList, REORDERABLE_LIST_HANDLE_ATTRIBUTE } from '@inertiaui/vanilla'
|
|
415
|
+
|
|
416
|
+
let items = ['Apple', 'Banana', 'Cherry']
|
|
417
|
+
|
|
418
|
+
const controller = createReorderableList({
|
|
419
|
+
getItems: () => items,
|
|
420
|
+
setItems: (nextItems) => {
|
|
421
|
+
items = nextItems
|
|
422
|
+
render()
|
|
423
|
+
},
|
|
424
|
+
getBounds: () => listElement.getBoundingClientRect(),
|
|
425
|
+
autoScroll: true,
|
|
426
|
+
onChange: () => render(),
|
|
427
|
+
onBeforeReorder: (move, context) => {
|
|
428
|
+
if (!context.alreadyPreviewed) {
|
|
429
|
+
captureAnimationSnapshot()
|
|
430
|
+
}
|
|
431
|
+
},
|
|
432
|
+
onReorder: (move) => {
|
|
433
|
+
announce(`Moved ${move.item}`)
|
|
434
|
+
},
|
|
435
|
+
})
|
|
436
|
+
|
|
437
|
+
controller.setListElement(listElement)
|
|
438
|
+
|
|
439
|
+
items.forEach((_, index) => {
|
|
440
|
+
controller.setItemElement(index, itemElements[index])
|
|
441
|
+
handles[index].setAttribute(REORDERABLE_LIST_HANDLE_ATTRIBUTE, 'true')
|
|
442
|
+
handles[index].addEventListener('pointerdown', (event) => controller.pointerDown(index, event))
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
moveUpButton.addEventListener('click', () => controller.moveItem(index, 'up'))
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
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.
|
|
449
|
+
|
|
450
|
+
#### Reorderable List Options
|
|
451
|
+
|
|
452
|
+
| Option | Type | Default | Description |
|
|
453
|
+
|--------|------|---------|-------------|
|
|
454
|
+
| `getItems` | `() => readonly T[]` | Required | Current ordered items |
|
|
455
|
+
| `setItems` | `(items: T[]) => void` | `undefined` | Optional writer used by `moveItem` and pointer commits |
|
|
456
|
+
| `canReorder` | `() => boolean` | `true` | Guard before starting or committing a reorder |
|
|
457
|
+
| `getBounds` | `() => ReorderBounds \| null` | Registered list rect | Optional outer active area |
|
|
458
|
+
| `autoScroll` | `boolean \| AutoScrollerOptions` | `false` | Edge auto-scroll while dragging |
|
|
459
|
+
| `onChange` | `(state) => void` | `undefined` | Called when pointer drag state changes |
|
|
460
|
+
| `onBeforeReorder` | `(move, context) => void` | `undefined` | Called before writing items or firing `onReorder` |
|
|
461
|
+
| `onReorder` | `(move, context) => void` | `undefined` | Called after a valid keyboard or pointer reorder |
|
|
462
|
+
|
|
337
463
|
## Positioning
|
|
338
464
|
|
|
339
465
|
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 +500,7 @@ The function applies positioning styles to the floating element automatically (`
|
|
|
374
500
|
| `placement` | `Placement` | `'bottom-start'` | Where to position the floating element |
|
|
375
501
|
| `offset` | `number` | `0` | Distance in pixels between reference and floating element |
|
|
376
502
|
| `flip` | `boolean` | `true` | Flip to opposite side when overflowing viewport |
|
|
503
|
+
| `autoSize` | `boolean` | `false` | Constrain the floating element to the available viewport space (sets `max-height` and `overflow-y: auto`) |
|
|
377
504
|
|
|
378
505
|
#### Placements
|
|
379
506
|
|
|
@@ -413,6 +540,26 @@ const result = computePosition(reference, floating, {
|
|
|
413
540
|
|
|
414
541
|
The floating element is always clamped to stay within the viewport with a 4px margin, even after flipping.
|
|
415
542
|
|
|
543
|
+
#### Auto Sizing
|
|
544
|
+
|
|
545
|
+
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.
|
|
546
|
+
|
|
547
|
+
```typescript
|
|
548
|
+
computePosition(reference, floating, {
|
|
549
|
+
placement: 'bottom-start',
|
|
550
|
+
offset: 8,
|
|
551
|
+
autoSize: true,
|
|
552
|
+
})
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
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`):
|
|
556
|
+
|
|
557
|
+
```css
|
|
558
|
+
.menu {
|
|
559
|
+
--iui-max-height: 20rem;
|
|
560
|
+
}
|
|
561
|
+
```
|
|
562
|
+
|
|
416
563
|
### autoUpdate
|
|
417
564
|
|
|
418
565
|
Automatically reposition the floating element when the layout changes:
|
|
@@ -443,6 +590,63 @@ Updates are batched using `requestAnimationFrame` to avoid layout thrashing.
|
|
|
443
590
|
|
|
444
591
|
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
592
|
|
|
593
|
+
Use `supportsTopLayerAnchorPositioning()` to check whether the browser also supports anchor positioning/sizing for top-layer popovers:
|
|
594
|
+
|
|
595
|
+
```typescript
|
|
596
|
+
import { supportsTopLayerAnchorPositioning } from '@inertiaui/vanilla'
|
|
597
|
+
|
|
598
|
+
if (supportsTopLayerAnchorPositioning()) {
|
|
599
|
+
// Native anchor positioning can be used for popover/top-layer content.
|
|
600
|
+
}
|
|
601
|
+
```
|
|
602
|
+
|
|
603
|
+
### positionTopLayerPopover
|
|
604
|
+
|
|
605
|
+
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.
|
|
606
|
+
|
|
607
|
+
```typescript
|
|
608
|
+
import { positionTopLayerPopover } from '@inertiaui/vanilla'
|
|
609
|
+
|
|
610
|
+
popover.showPopover()
|
|
611
|
+
|
|
612
|
+
const result = positionTopLayerPopover(button, popover, {
|
|
613
|
+
placement: 'bottom-start',
|
|
614
|
+
offset: 4,
|
|
615
|
+
matchReferenceWidth: true,
|
|
616
|
+
viewportMargin: 8,
|
|
617
|
+
})
|
|
618
|
+
```
|
|
619
|
+
|
|
620
|
+
#### Top-Layer Options
|
|
621
|
+
|
|
622
|
+
| Option | Type | Default | Description |
|
|
623
|
+
|--------|------|---------|-------------|
|
|
624
|
+
| `placement` | `Placement` | `'bottom-start'` | Preferred placement |
|
|
625
|
+
| `offset` | `number` | `4` | Distance from the reference |
|
|
626
|
+
| `flip` | `boolean` | `true` | Flip when the preferred side overflows |
|
|
627
|
+
| `matchReferenceWidth` | `boolean` | `false` | Set width to the reference width, clamped to viewport |
|
|
628
|
+
| `viewportMargin` | `number` | `8` | Space kept between the popover and viewport edge |
|
|
629
|
+
| `anchorPositioning` | `boolean` | `true` | Use CSS Anchor Positioning when supported |
|
|
630
|
+
|
|
631
|
+
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.
|
|
632
|
+
|
|
633
|
+
### autoUpdateTopLayerPopover
|
|
634
|
+
|
|
635
|
+
Like `autoUpdate`, but also listens to `visualViewport` scroll/resize events, which are important for top-layer popovers on mobile browsers.
|
|
636
|
+
|
|
637
|
+
```typescript
|
|
638
|
+
import { autoUpdateTopLayerPopover, positionTopLayerPopover } from '@inertiaui/vanilla'
|
|
639
|
+
|
|
640
|
+
const cleanup = autoUpdateTopLayerPopover(button, popover, () => {
|
|
641
|
+
positionTopLayerPopover(button, popover, {
|
|
642
|
+
placement: 'bottom-start',
|
|
643
|
+
matchReferenceWidth: true,
|
|
644
|
+
})
|
|
645
|
+
})
|
|
646
|
+
|
|
647
|
+
cleanup()
|
|
648
|
+
```
|
|
649
|
+
|
|
446
650
|
### Full Example
|
|
447
651
|
|
|
448
652
|
```typescript
|
|
@@ -468,6 +672,67 @@ function setupTooltip(trigger: HTMLElement, tooltip: HTMLElement) {
|
|
|
468
672
|
}
|
|
469
673
|
```
|
|
470
674
|
|
|
675
|
+
## Native Popovers
|
|
676
|
+
|
|
677
|
+
`createNativePopoverDisclosure` coordinates open state, native `showPopover()`/`hidePopover()`, top-layer positioning, auto-updates, toggle-event sync, and optional focus-out dismissal.
|
|
678
|
+
|
|
679
|
+
```typescript
|
|
680
|
+
import { createNativePopoverDisclosure } from '@inertiaui/vanilla'
|
|
681
|
+
|
|
682
|
+
const disclosure = createNativePopoverDisclosure({
|
|
683
|
+
reference: () => button,
|
|
684
|
+
popover: () => popover,
|
|
685
|
+
position: {
|
|
686
|
+
placement: 'bottom-start',
|
|
687
|
+
offset: 4,
|
|
688
|
+
matchReferenceWidth: true,
|
|
689
|
+
},
|
|
690
|
+
focusOut: {
|
|
691
|
+
container: () => popover,
|
|
692
|
+
onDismiss: () => disclosure.closePopover(),
|
|
693
|
+
},
|
|
694
|
+
onOpenChange: (open) => {
|
|
695
|
+
button.setAttribute('aria-expanded', String(open))
|
|
696
|
+
},
|
|
697
|
+
})
|
|
698
|
+
|
|
699
|
+
button.addEventListener('click', () => {
|
|
700
|
+
disclosure.togglePopover(
|
|
701
|
+
() => {
|
|
702
|
+
disclosure.openPopover()
|
|
703
|
+
disclosure.showPopover()
|
|
704
|
+
disclosure.updatePosition()
|
|
705
|
+
disclosure.startAutoUpdate()
|
|
706
|
+
},
|
|
707
|
+
() => {
|
|
708
|
+
disclosure.closePopover()
|
|
709
|
+
},
|
|
710
|
+
)
|
|
711
|
+
})
|
|
712
|
+
|
|
713
|
+
popover.addEventListener('toggle', (event) => {
|
|
714
|
+
disclosure.handlePopoverToggle(event)
|
|
715
|
+
})
|
|
716
|
+
|
|
717
|
+
popover.addEventListener('focusout', (event) => {
|
|
718
|
+
disclosure.handleFocusOut(event)
|
|
719
|
+
})
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
### Controller Methods
|
|
723
|
+
|
|
724
|
+
| Method | Description |
|
|
725
|
+
|--------|-------------|
|
|
726
|
+
| `openPopover({ onBeforeOpen })` | Marks the controller open and cancels pending focus-out dismissal |
|
|
727
|
+
| `closePopover({ onClose, hide })` | Marks closed, optionally hides natively, and stops auto-updates |
|
|
728
|
+
| `togglePopover(open, close)` | Calls one of two callbacks based on current state |
|
|
729
|
+
| `showPopover()` / `hidePopover()` | Delegates to native popover methods |
|
|
730
|
+
| `handlePopoverToggle(event, { onClose })` | Syncs controller state when the browser closes the native popover |
|
|
731
|
+
| `handleFocusOut(event)` | Delegates to the configured focus-out dismiss controller |
|
|
732
|
+
| `updatePosition()` | Runs `positionTopLayerPopover` |
|
|
733
|
+
| `startAutoUpdate()` | Starts `autoUpdateTopLayerPopover` |
|
|
734
|
+
| `cleanupPopover()` | Cleans focus-out and positioning listeners |
|
|
735
|
+
|
|
471
736
|
## Accessibility
|
|
472
737
|
|
|
473
738
|
Accessibility utilities for managing `aria-hidden` attributes with reference counting support.
|
|
@@ -543,18 +808,33 @@ cleanup()
|
|
|
543
808
|
element.getAttribute('aria-hidden') // null (removed)
|
|
544
809
|
```
|
|
545
810
|
|
|
811
|
+
### markInert
|
|
812
|
+
|
|
813
|
+
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"`.
|
|
814
|
+
|
|
815
|
+
```typescript
|
|
816
|
+
import { markInert } from '@inertiaui/vanilla'
|
|
817
|
+
|
|
818
|
+
const cleanup = markInert('#app')
|
|
819
|
+
|
|
820
|
+
// Later, restore the original inert/aria-hidden state
|
|
821
|
+
cleanup()
|
|
822
|
+
```
|
|
823
|
+
|
|
824
|
+
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.
|
|
825
|
+
|
|
546
826
|
### Use with Dialogs
|
|
547
827
|
|
|
548
828
|
When a dialog opens, the main content should be marked as `aria-hidden` to prevent screen readers from reading background content:
|
|
549
829
|
|
|
550
830
|
```typescript
|
|
551
|
-
import {
|
|
831
|
+
import { markInert, lockScroll, createFocusTrap, onEscapeKey } from '@inertiaui/vanilla'
|
|
552
832
|
|
|
553
833
|
function openDialog(dialogElement: HTMLElement) {
|
|
554
834
|
const closeDialog = () => cleanups.forEach(fn => fn())
|
|
555
835
|
|
|
556
836
|
const cleanups = [
|
|
557
|
-
|
|
837
|
+
markInert('#app'),
|
|
558
838
|
lockScroll(),
|
|
559
839
|
createFocusTrap(dialogElement),
|
|
560
840
|
onEscapeKey(closeDialog),
|
|
@@ -737,6 +1017,25 @@ const handleScroll = debounce(() => {
|
|
|
737
1017
|
window.addEventListener('scroll', handleScroll)
|
|
738
1018
|
```
|
|
739
1019
|
|
|
1020
|
+
### createDebouncer
|
|
1021
|
+
|
|
1022
|
+
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.
|
|
1023
|
+
|
|
1024
|
+
```typescript
|
|
1025
|
+
import { createDebouncer } from '@inertiaui/vanilla'
|
|
1026
|
+
|
|
1027
|
+
const debouncer = createDebouncer(250)
|
|
1028
|
+
|
|
1029
|
+
input.addEventListener('input', () => {
|
|
1030
|
+
debouncer.schedule(() => {
|
|
1031
|
+
search(input.value)
|
|
1032
|
+
})
|
|
1033
|
+
})
|
|
1034
|
+
|
|
1035
|
+
// Cancel a pending scheduled call
|
|
1036
|
+
debouncer.cancel()
|
|
1037
|
+
```
|
|
1038
|
+
|
|
740
1039
|
### detectFramerate
|
|
741
1040
|
|
|
742
1041
|
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 +1047,117 @@ const fps = await detectFramerate()
|
|
|
748
1047
|
console.log(`Running at ${fps} FPS`)
|
|
749
1048
|
```
|
|
750
1049
|
|
|
1050
|
+
## Color
|
|
1051
|
+
|
|
1052
|
+
Convert colors between hex and HSL.
|
|
1053
|
+
|
|
1054
|
+
### hexToHsl
|
|
1055
|
+
|
|
1056
|
+
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 }`.
|
|
1057
|
+
|
|
1058
|
+
```typescript
|
|
1059
|
+
import { hexToHsl } from '@inertiaui/vanilla'
|
|
1060
|
+
|
|
1061
|
+
hexToHsl('#3490dc') // { h: 207, s: 71, l: 53 }
|
|
1062
|
+
hexToHsl('3490dc') // same — leading '#' is optional
|
|
1063
|
+
```
|
|
1064
|
+
|
|
1065
|
+
### hslToHex
|
|
1066
|
+
|
|
1067
|
+
Convert HSL values to a hex color string (with leading `#`). Expects `h: 0–360`, `s: 0–100`, `l: 0–100`.
|
|
1068
|
+
|
|
1069
|
+
```typescript
|
|
1070
|
+
import { hslToHex } from '@inertiaui/vanilla'
|
|
1071
|
+
|
|
1072
|
+
hslToHex(207, 71, 53) // '#3290dc'
|
|
1073
|
+
```
|
|
1074
|
+
|
|
1075
|
+
The `HslColor` type is also exported:
|
|
1076
|
+
|
|
1077
|
+
```typescript
|
|
1078
|
+
import type { HslColor } from '@inertiaui/vanilla'
|
|
1079
|
+
```
|
|
1080
|
+
|
|
1081
|
+
### RGB and Normalization Helpers
|
|
1082
|
+
|
|
1083
|
+
```typescript
|
|
1084
|
+
import {
|
|
1085
|
+
formatAlpha,
|
|
1086
|
+
hexToRgb,
|
|
1087
|
+
normalizeAlpha,
|
|
1088
|
+
normalizeHue,
|
|
1089
|
+
normalizePercent,
|
|
1090
|
+
rgbToHex,
|
|
1091
|
+
} from '@inertiaui/vanilla'
|
|
1092
|
+
|
|
1093
|
+
hexToRgb('#3490dc') // { r: 52, g: 144, b: 220 }
|
|
1094
|
+
rgbToHex(52, 144, 220) // '#3490dc'
|
|
1095
|
+
normalizeHue(-10) // 350
|
|
1096
|
+
normalizePercent(120) // 100
|
|
1097
|
+
normalizeAlpha(1.5) // 1
|
|
1098
|
+
formatAlpha(0.5) // '0.5'
|
|
1099
|
+
```
|
|
1100
|
+
|
|
1101
|
+
### Color Parsing
|
|
1102
|
+
|
|
1103
|
+
Parse hex, RGB/RGBA, or HSL/HSLA strings into a normalized `ParsedColor`.
|
|
1104
|
+
|
|
1105
|
+
```typescript
|
|
1106
|
+
import {
|
|
1107
|
+
parseColorString,
|
|
1108
|
+
parseHexColor,
|
|
1109
|
+
parseHslColor,
|
|
1110
|
+
parseRgbColor,
|
|
1111
|
+
} from '@inertiaui/vanilla'
|
|
1112
|
+
|
|
1113
|
+
parseColorString('#3490dc')
|
|
1114
|
+
// { hex: '#3490dc', h: 207, s: 71, l: 53, alpha: 1 }
|
|
1115
|
+
|
|
1116
|
+
parseColorString('rgba(52, 144, 220, 0.5)')
|
|
1117
|
+
// { hex: '#3490dc', h: 207, s: 71, l: 53, alpha: 0.5 }
|
|
1118
|
+
|
|
1119
|
+
parseColorString('hsla(207, 71%, 53%, 50%)')
|
|
1120
|
+
// { hex: '#3290dc', h: 207, s: 71, l: 53, alpha: 0.5 }
|
|
1121
|
+
```
|
|
1122
|
+
|
|
1123
|
+
The lower-level parsers return `null` when the input is invalid:
|
|
1124
|
+
|
|
1125
|
+
```typescript
|
|
1126
|
+
parseHexColor('#fff')
|
|
1127
|
+
parseRgbColor('rgb(52, 144, 220)')
|
|
1128
|
+
parseHslColor('hsl(207, 71%, 53%)')
|
|
1129
|
+
```
|
|
1130
|
+
|
|
1131
|
+
### Channel Parsing
|
|
1132
|
+
|
|
1133
|
+
```typescript
|
|
1134
|
+
import { parseAlphaChannel, parseRgbChannel } from '@inertiaui/vanilla'
|
|
1135
|
+
|
|
1136
|
+
parseRgbChannel('50%') // 128
|
|
1137
|
+
parseRgbChannel('255') // 255
|
|
1138
|
+
parseAlphaChannel('50%') // 0.5
|
|
1139
|
+
parseAlphaChannel('0.25') // 0.25
|
|
1140
|
+
```
|
|
1141
|
+
|
|
1142
|
+
### formatColor
|
|
1143
|
+
|
|
1144
|
+
Format a parsed color as hex, RGB/RGBA, or HSL/HSLA. Alpha is included automatically when `alpha < 1`, or explicitly via `includeAlpha`.
|
|
1145
|
+
|
|
1146
|
+
```typescript
|
|
1147
|
+
import { formatColor, parseColorString } from '@inertiaui/vanilla'
|
|
1148
|
+
|
|
1149
|
+
const color = parseColorString('rgba(52, 144, 220, 0.5)')!
|
|
1150
|
+
|
|
1151
|
+
formatColor(color, 'hex') // '#3490dc80'
|
|
1152
|
+
formatColor(color, 'rgb') // 'rgba(52, 144, 220, 0.5)'
|
|
1153
|
+
formatColor(color, 'hsl') // 'hsla(207, 71%, 53%, 0.5)'
|
|
1154
|
+
|
|
1155
|
+
formatColor(color, 'hex', { includeAlpha: false })
|
|
1156
|
+
// '#3490dc'
|
|
1157
|
+
```
|
|
1158
|
+
|
|
1159
|
+
Types exported by the color module include `HslColor`, `RgbColor`, `ParsedColor`, `ColorFormat`, and `FormatColorOptions`.
|
|
1160
|
+
|
|
751
1161
|
## Helpers
|
|
752
1162
|
|
|
753
1163
|
### generateId
|
|
@@ -1097,16 +1507,44 @@ This library is written in TypeScript and exports the following types:
|
|
|
1097
1507
|
|
|
1098
1508
|
```typescript
|
|
1099
1509
|
import type {
|
|
1510
|
+
AnimateOptions,
|
|
1511
|
+
AutoScrollAxis,
|
|
1512
|
+
AutoScrollContainer,
|
|
1513
|
+
AutoScrollerOptions,
|
|
1100
1514
|
CleanupFunction,
|
|
1101
|
-
|
|
1515
|
+
ColorFormat,
|
|
1516
|
+
DarkModeStrategy,
|
|
1517
|
+
Debouncer,
|
|
1102
1518
|
EscapeKeyOptions,
|
|
1103
|
-
|
|
1519
|
+
FocusOutDismissController,
|
|
1520
|
+
FocusOutDismissOptions,
|
|
1521
|
+
FocusTrapOptions,
|
|
1104
1522
|
EasingName,
|
|
1523
|
+
FormatColorOptions,
|
|
1524
|
+
HslColor,
|
|
1525
|
+
ListboxNavigationOptions,
|
|
1526
|
+
ListboxNavigationResult,
|
|
1105
1527
|
MenuNavigationOptions,
|
|
1528
|
+
NativePopoverCloseOptions,
|
|
1529
|
+
NativePopoverDisclosureController,
|
|
1530
|
+
NativePopoverDisclosureOptions,
|
|
1531
|
+
NativePopoverOpenOptions,
|
|
1532
|
+
NativePopoverToggleOptions,
|
|
1106
1533
|
Placement,
|
|
1534
|
+
ParsedColor,
|
|
1107
1535
|
PositionOptions,
|
|
1108
1536
|
PositionResult,
|
|
1109
|
-
|
|
1537
|
+
ReorderBounds,
|
|
1538
|
+
ReorderCommitContext,
|
|
1539
|
+
ReorderDirection,
|
|
1540
|
+
ReorderMove,
|
|
1541
|
+
ReorderPreviewItem,
|
|
1542
|
+
ReorderableListController,
|
|
1543
|
+
ReorderableListOptions,
|
|
1544
|
+
ReorderableListState,
|
|
1545
|
+
ReorderSource,
|
|
1546
|
+
RgbColor,
|
|
1547
|
+
TopLayerPopoverPositionOptions,
|
|
1110
1548
|
} from '@inertiaui/vanilla'
|
|
1111
1549
|
```
|
|
1112
1550
|
|