@inertiaui/vanilla 0.5.0 → 0.6.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 +126 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +68 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,8 +25,13 @@ npm install @inertiaui/vanilla
|
|
|
25
25
|
- [Positioning](#positioning)
|
|
26
26
|
- [Accessibility](#accessibility)
|
|
27
27
|
- [Animation](#animation)
|
|
28
|
+
- [Dark Mode Detection](#dark-mode-detection)
|
|
29
|
+
- [RTL Support](#rtl-support)
|
|
30
|
+
- [Debounce](#debounce)
|
|
28
31
|
- [Helpers](#helpers)
|
|
29
32
|
- [generateId](#generateid)
|
|
33
|
+
- [blank](#blank)
|
|
34
|
+
- [onceChildrenRendered](#oncechildrenrendered)
|
|
30
35
|
- [Object Filtering](#object-filtering)
|
|
31
36
|
- [String Utilities](#string-utilities)
|
|
32
37
|
- [URL Utilities](#url-utilities)
|
|
@@ -653,6 +658,96 @@ function forceHideModal(modal: HTMLElement) {
|
|
|
653
658
|
}
|
|
654
659
|
```
|
|
655
660
|
|
|
661
|
+
## Dark Mode Detection
|
|
662
|
+
|
|
663
|
+
The `prefersDarkMode` function detects whether the user prefers dark mode, with support for multiple detection strategies.
|
|
664
|
+
|
|
665
|
+
### Basic Usage
|
|
666
|
+
|
|
667
|
+
```typescript
|
|
668
|
+
import { prefersDarkMode } from '@inertiaui/vanilla'
|
|
669
|
+
|
|
670
|
+
const isDark = prefersDarkMode()
|
|
671
|
+
```
|
|
672
|
+
|
|
673
|
+
### Strategies
|
|
674
|
+
|
|
675
|
+
| Strategy | Behavior |
|
|
676
|
+
|----------|----------|
|
|
677
|
+
| `'auto'` (default) | Checks `<html class="dark">` first, then `prefers-color-scheme: dark` media query |
|
|
678
|
+
| `'class'` / `'selector'` | Checks `<html class="dark">` |
|
|
679
|
+
| `'media'` | Checks `prefers-color-scheme: dark` media query |
|
|
680
|
+
| Custom function | Called directly for full control |
|
|
681
|
+
|
|
682
|
+
```typescript
|
|
683
|
+
// Class-based detection (e.g., Tailwind dark mode)
|
|
684
|
+
prefersDarkMode('class')
|
|
685
|
+
|
|
686
|
+
// Media query only
|
|
687
|
+
prefersDarkMode('media')
|
|
688
|
+
|
|
689
|
+
// Custom logic
|
|
690
|
+
prefersDarkMode(() => document.body.dataset.theme === 'dark')
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
## RTL Support
|
|
694
|
+
|
|
695
|
+
Utilities for detecting and observing right-to-left document direction.
|
|
696
|
+
|
|
697
|
+
### isRtl
|
|
698
|
+
|
|
699
|
+
Check whether the document is currently in RTL direction:
|
|
700
|
+
|
|
701
|
+
```typescript
|
|
702
|
+
import { isRtl } from '@inertiaui/vanilla'
|
|
703
|
+
|
|
704
|
+
if (isRtl()) {
|
|
705
|
+
// Document is right-to-left
|
|
706
|
+
}
|
|
707
|
+
```
|
|
708
|
+
|
|
709
|
+
### onRtlChange
|
|
710
|
+
|
|
711
|
+
Observe changes to the document's `dir` attribute and invoke a callback whenever it changes. Returns a cleanup function.
|
|
712
|
+
|
|
713
|
+
```typescript
|
|
714
|
+
import { onRtlChange } from '@inertiaui/vanilla'
|
|
715
|
+
|
|
716
|
+
const cleanup = onRtlChange((rtl) => {
|
|
717
|
+
console.log('RTL changed:', rtl)
|
|
718
|
+
})
|
|
719
|
+
|
|
720
|
+
// Later, stop observing
|
|
721
|
+
cleanup()
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
## Debounce
|
|
725
|
+
|
|
726
|
+
### debounce
|
|
727
|
+
|
|
728
|
+
Debounce a function using `requestAnimationFrame`. Ensures the function runs at most once per animation frame.
|
|
729
|
+
|
|
730
|
+
```typescript
|
|
731
|
+
import { debounce } from '@inertiaui/vanilla'
|
|
732
|
+
|
|
733
|
+
const handleScroll = debounce(() => {
|
|
734
|
+
updatePosition()
|
|
735
|
+
})
|
|
736
|
+
|
|
737
|
+
window.addEventListener('scroll', handleScroll)
|
|
738
|
+
```
|
|
739
|
+
|
|
740
|
+
### detectFramerate
|
|
741
|
+
|
|
742
|
+
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.
|
|
743
|
+
|
|
744
|
+
```typescript
|
|
745
|
+
import { detectFramerate } from '@inertiaui/vanilla'
|
|
746
|
+
|
|
747
|
+
const fps = await detectFramerate()
|
|
748
|
+
console.log(`Running at ${fps} FPS`)
|
|
749
|
+
```
|
|
750
|
+
|
|
656
751
|
## Helpers
|
|
657
752
|
|
|
658
753
|
### generateId
|
|
@@ -706,6 +801,36 @@ title.id = titleId
|
|
|
706
801
|
description.id = descId
|
|
707
802
|
```
|
|
708
803
|
|
|
804
|
+
### blank
|
|
805
|
+
|
|
806
|
+
A port of Laravel's `blank` function. Returns `true` if the value is "empty" — `null`, `undefined`, empty string (or whitespace-only), empty array, or empty object.
|
|
807
|
+
|
|
808
|
+
```typescript
|
|
809
|
+
import { blank } from '@inertiaui/vanilla'
|
|
810
|
+
|
|
811
|
+
blank(null) // true
|
|
812
|
+
blank(undefined) // true
|
|
813
|
+
blank('') // true
|
|
814
|
+
blank(' ') // true
|
|
815
|
+
blank([]) // true
|
|
816
|
+
blank('hello') // false
|
|
817
|
+
blank(0) // false
|
|
818
|
+
blank(false) // false
|
|
819
|
+
```
|
|
820
|
+
|
|
821
|
+
### onceChildrenRendered
|
|
822
|
+
|
|
823
|
+
Invokes a callback once the given element has child elements. If the element already has children, the callback fires immediately. Otherwise, it uses a `MutationObserver` to wait for children to appear.
|
|
824
|
+
|
|
825
|
+
```typescript
|
|
826
|
+
import { onceChildrenRendered } from '@inertiaui/vanilla'
|
|
827
|
+
|
|
828
|
+
onceChildrenRendered(containerElement, () => {
|
|
829
|
+
// Children are now present in the DOM
|
|
830
|
+
initializeContent()
|
|
831
|
+
})
|
|
832
|
+
```
|
|
833
|
+
|
|
709
834
|
### Object Filtering
|
|
710
835
|
|
|
711
836
|
#### except
|
|
@@ -981,6 +1106,7 @@ import type {
|
|
|
981
1106
|
Placement,
|
|
982
1107
|
PositionOptions,
|
|
983
1108
|
PositionResult,
|
|
1109
|
+
DarkModeStrategy,
|
|
984
1110
|
} from '@inertiaui/vanilla'
|
|
985
1111
|
```
|
|
986
1112
|
|
package/dist/index.d.ts
CHANGED
|
@@ -86,6 +86,25 @@ export declare interface FocusTrapOptions {
|
|
|
86
86
|
|
|
87
87
|
export declare function generateId(prefix?: string): string;
|
|
88
88
|
|
|
89
|
+
/**
|
|
90
|
+
* Convert a hex color string to HSL values.
|
|
91
|
+
* Accepts with or without leading '#'.
|
|
92
|
+
*/
|
|
93
|
+
export declare function hexToHsl(hex: string): HslColor;
|
|
94
|
+
|
|
95
|
+
export declare interface HslColor {
|
|
96
|
+
h: number;
|
|
97
|
+
s: number;
|
|
98
|
+
l: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Convert HSL values to a hex color string.
|
|
103
|
+
* h: 0-360, s: 0-100, l: 0-100
|
|
104
|
+
* Returns string with leading '#'.
|
|
105
|
+
*/
|
|
106
|
+
export declare function hslToHex(h: number, s: number, l: number): string;
|
|
107
|
+
|
|
89
108
|
/**
|
|
90
109
|
* Check whether the document is currently in RTL direction.
|
|
91
110
|
*/
|
package/dist/index.js
CHANGED
|
@@ -799,6 +799,72 @@ function onRtlChange(callback) {
|
|
|
799
799
|
});
|
|
800
800
|
return () => observer.disconnect();
|
|
801
801
|
}
|
|
802
|
+
function hexToHsl(hex) {
|
|
803
|
+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
|
|
804
|
+
if (!result) return { h: 0, s: 100, l: 50 };
|
|
805
|
+
let r = parseInt(result[1], 16) / 255;
|
|
806
|
+
let g = parseInt(result[2], 16) / 255;
|
|
807
|
+
let b = parseInt(result[3], 16) / 255;
|
|
808
|
+
const max = Math.max(r, g, b);
|
|
809
|
+
const min = Math.min(r, g, b);
|
|
810
|
+
let h = 0;
|
|
811
|
+
let s = 0;
|
|
812
|
+
const l = (max + min) / 2;
|
|
813
|
+
if (max !== min) {
|
|
814
|
+
const d = max - min;
|
|
815
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
816
|
+
switch (max) {
|
|
817
|
+
case r:
|
|
818
|
+
h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
|
|
819
|
+
break;
|
|
820
|
+
case g:
|
|
821
|
+
h = ((b - r) / d + 2) / 6;
|
|
822
|
+
break;
|
|
823
|
+
case b:
|
|
824
|
+
h = ((r - g) / d + 4) / 6;
|
|
825
|
+
break;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
return { h: Math.round(h * 360), s: Math.round(s * 100), l: Math.round(l * 100) };
|
|
829
|
+
}
|
|
830
|
+
function hslToHex(h, s, l) {
|
|
831
|
+
s /= 100;
|
|
832
|
+
l /= 100;
|
|
833
|
+
const c = (1 - Math.abs(2 * l - 1)) * s;
|
|
834
|
+
const x = c * (1 - Math.abs(h / 60 % 2 - 1));
|
|
835
|
+
const m = l - c / 2;
|
|
836
|
+
let r = 0, g = 0, b = 0;
|
|
837
|
+
if (0 <= h && h < 60) {
|
|
838
|
+
r = c;
|
|
839
|
+
g = x;
|
|
840
|
+
b = 0;
|
|
841
|
+
} else if (60 <= h && h < 120) {
|
|
842
|
+
r = x;
|
|
843
|
+
g = c;
|
|
844
|
+
b = 0;
|
|
845
|
+
} else if (120 <= h && h < 180) {
|
|
846
|
+
r = 0;
|
|
847
|
+
g = c;
|
|
848
|
+
b = x;
|
|
849
|
+
} else if (180 <= h && h < 240) {
|
|
850
|
+
r = 0;
|
|
851
|
+
g = x;
|
|
852
|
+
b = c;
|
|
853
|
+
} else if (240 <= h && h < 300) {
|
|
854
|
+
r = x;
|
|
855
|
+
g = 0;
|
|
856
|
+
b = c;
|
|
857
|
+
} else if (300 <= h && h < 360) {
|
|
858
|
+
r = c;
|
|
859
|
+
g = 0;
|
|
860
|
+
b = x;
|
|
861
|
+
}
|
|
862
|
+
const toHex = (n) => {
|
|
863
|
+
const hex = Math.round((n + m) * 255).toString(16);
|
|
864
|
+
return hex.length === 1 ? "0" + hex : hex;
|
|
865
|
+
};
|
|
866
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
|
|
867
|
+
}
|
|
802
868
|
export {
|
|
803
869
|
animate,
|
|
804
870
|
autoUpdate,
|
|
@@ -812,6 +878,8 @@ export {
|
|
|
812
878
|
easings,
|
|
813
879
|
except,
|
|
814
880
|
generateId,
|
|
881
|
+
hexToHsl,
|
|
882
|
+
hslToHex,
|
|
815
883
|
isRtl,
|
|
816
884
|
isStandardDomEvent,
|
|
817
885
|
kebabCase,
|