@inertiaui/vanilla 0.4.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 +64 -0
- package/dist/index.js +156 -2
- 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
|
@@ -21,6 +21,12 @@ export declare interface AnimateOptions {
|
|
|
21
21
|
|
|
22
22
|
export declare function autoUpdate(reference: HTMLElement, floating: HTMLElement, update: () => void): CleanupFunction;
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Port of Laravel's `blank` function.
|
|
26
|
+
* Returns true if the value is "empty" — null, undefined, empty string, empty array/object, etc.
|
|
27
|
+
*/
|
|
28
|
+
export declare function blank(value: unknown): boolean;
|
|
29
|
+
|
|
24
30
|
/**
|
|
25
31
|
* Cancel any running animations on an element
|
|
26
32
|
*/
|
|
@@ -34,6 +40,23 @@ export declare function createFocusTrap(container: HTMLElement, options?: FocusT
|
|
|
34
40
|
|
|
35
41
|
export declare function createMenuNavigation(container: HTMLElement, options?: MenuNavigationOptions): CleanupFunction;
|
|
36
42
|
|
|
43
|
+
export declare type DarkModeStrategy = 'auto' | 'class' | 'selector' | 'media' | (() => boolean);
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Debounce a function using requestAnimationFrame.
|
|
47
|
+
* Ensures the function runs at most once per animation frame.
|
|
48
|
+
*
|
|
49
|
+
* @see https://pqina.nl/blog/applying-styles-based-on-the-user-scroll-position-with-smart-css/
|
|
50
|
+
*/
|
|
51
|
+
export declare function debounce<T extends (...args: unknown[]) => void>(fn: T): (...args: Parameters<T>) => void;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Detect the browser's current framerate.
|
|
55
|
+
* Returns a Promise that resolves with the detected FPS (capped to 30–240 range).
|
|
56
|
+
* Falls back to 60 if requestAnimationFrame is unavailable or detection times out.
|
|
57
|
+
*/
|
|
58
|
+
export declare function detectFramerate(): Promise<number>;
|
|
59
|
+
|
|
37
60
|
export declare type EasingName = keyof typeof easings;
|
|
38
61
|
|
|
39
62
|
/**
|
|
@@ -63,6 +86,30 @@ export declare interface FocusTrapOptions {
|
|
|
63
86
|
|
|
64
87
|
export declare function generateId(prefix?: string): string;
|
|
65
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
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Check whether the document is currently in RTL direction.
|
|
110
|
+
*/
|
|
111
|
+
export declare function isRtl(): boolean;
|
|
112
|
+
|
|
66
113
|
export declare function isStandardDomEvent(eventName: string): boolean;
|
|
67
114
|
|
|
68
115
|
export declare function kebabCase(string: string): string;
|
|
@@ -89,6 +136,12 @@ export declare function only<T extends Record<string, unknown>>(target: T, keys:
|
|
|
89
136
|
|
|
90
137
|
export declare function only(target: string[], keys: string[], ignoreCase?: boolean): string[];
|
|
91
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Observe changes to the document's `dir` attribute and invoke the callback
|
|
141
|
+
* whenever it changes. Returns a cleanup function to stop observing.
|
|
142
|
+
*/
|
|
143
|
+
export declare function onRtlChange(callback: (rtl: boolean) => void): CleanupFunction;
|
|
144
|
+
|
|
92
145
|
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
146
|
|
|
94
147
|
declare interface PositionOptions_2 {
|
|
@@ -104,6 +157,17 @@ export declare interface PositionResult {
|
|
|
104
157
|
placement: Placement;
|
|
105
158
|
}
|
|
106
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Detect whether the user prefers dark mode.
|
|
162
|
+
*
|
|
163
|
+
* Strategies:
|
|
164
|
+
* - `'class'` / `'selector'` — checks `<html class="dark">`
|
|
165
|
+
* - `'media'` — checks `prefers-color-scheme: dark` media query
|
|
166
|
+
* - `'auto'` (default) — checks class first, then media query
|
|
167
|
+
* - custom function — called directly for full control
|
|
168
|
+
*/
|
|
169
|
+
export declare function prefersDarkMode(strategy?: DarkModeStrategy): boolean;
|
|
170
|
+
|
|
107
171
|
export declare function rejectNullValues<T>(target: T[]): Exclude<T, null>[];
|
|
108
172
|
|
|
109
173
|
export declare function rejectNullValues<T extends Record<string, unknown>>(target: T): Partial<T>;
|
package/dist/index.js
CHANGED
|
@@ -467,7 +467,7 @@ function supportsAnchorPositioning() {
|
|
|
467
467
|
}
|
|
468
468
|
return _supportsAnchor;
|
|
469
469
|
}
|
|
470
|
-
function isRtl() {
|
|
470
|
+
function isRtl$1() {
|
|
471
471
|
return typeof document !== "undefined" && document.documentElement.dir === "rtl";
|
|
472
472
|
}
|
|
473
473
|
function getOppositePlacement(placement) {
|
|
@@ -491,7 +491,7 @@ function computePositionFromRect(refRect, floatingRect, placement, offset) {
|
|
|
491
491
|
let x = 0;
|
|
492
492
|
let y = 0;
|
|
493
493
|
const [side, alignment] = placement.split("-");
|
|
494
|
-
const rtl = isRtl();
|
|
494
|
+
const rtl = isRtl$1();
|
|
495
495
|
switch (side) {
|
|
496
496
|
case "bottom":
|
|
497
497
|
y = refRect.bottom + offset;
|
|
@@ -719,24 +719,178 @@ function autoUpdate(reference, floating, update) {
|
|
|
719
719
|
}
|
|
720
720
|
};
|
|
721
721
|
}
|
|
722
|
+
function blank(value) {
|
|
723
|
+
if (value === null || value === void 0) {
|
|
724
|
+
return true;
|
|
725
|
+
}
|
|
726
|
+
if (typeof value === "string") {
|
|
727
|
+
return value.trim() === "";
|
|
728
|
+
}
|
|
729
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
730
|
+
return false;
|
|
731
|
+
}
|
|
732
|
+
if (Array.isArray(value) || typeof value === "object" && value !== null && typeof value.length === "number") {
|
|
733
|
+
return value.length === 0;
|
|
734
|
+
}
|
|
735
|
+
return !value;
|
|
736
|
+
}
|
|
737
|
+
function debounce(fn) {
|
|
738
|
+
let frame;
|
|
739
|
+
return (...params) => {
|
|
740
|
+
if (frame) {
|
|
741
|
+
cancelAnimationFrame(frame);
|
|
742
|
+
}
|
|
743
|
+
frame = requestAnimationFrame(() => {
|
|
744
|
+
fn(...params);
|
|
745
|
+
});
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
function detectFramerate() {
|
|
749
|
+
return new Promise((resolve) => {
|
|
750
|
+
if (typeof window === "undefined" || !window.requestAnimationFrame) {
|
|
751
|
+
return resolve(60);
|
|
752
|
+
}
|
|
753
|
+
const timestamps = [];
|
|
754
|
+
let rafId;
|
|
755
|
+
function measure(now) {
|
|
756
|
+
timestamps.unshift(now);
|
|
757
|
+
if (timestamps.length > 10) {
|
|
758
|
+
const lastTime = timestamps.pop();
|
|
759
|
+
const fps = Math.floor(1e3 * 10 / (now - lastTime));
|
|
760
|
+
cancelAnimationFrame(rafId);
|
|
761
|
+
resolve(Math.min(Math.max(fps, 30), 240));
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
rafId = window.requestAnimationFrame(measure);
|
|
765
|
+
}
|
|
766
|
+
rafId = window.requestAnimationFrame(measure);
|
|
767
|
+
setTimeout(() => {
|
|
768
|
+
if (rafId !== void 0) {
|
|
769
|
+
cancelAnimationFrame(rafId);
|
|
770
|
+
}
|
|
771
|
+
resolve(60);
|
|
772
|
+
}, 1e3);
|
|
773
|
+
});
|
|
774
|
+
}
|
|
775
|
+
function prefersDarkMode(strategy = "auto") {
|
|
776
|
+
if (typeof strategy === "function") {
|
|
777
|
+
return strategy();
|
|
778
|
+
}
|
|
779
|
+
const containsDarkClass = () => document.documentElement.classList.contains("dark");
|
|
780
|
+
const matchesDarkMediaQuery = () => window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
781
|
+
if (strategy === "class" || strategy === "selector") {
|
|
782
|
+
return containsDarkClass();
|
|
783
|
+
}
|
|
784
|
+
if (strategy === "media") {
|
|
785
|
+
return matchesDarkMediaQuery();
|
|
786
|
+
}
|
|
787
|
+
return containsDarkClass() || matchesDarkMediaQuery();
|
|
788
|
+
}
|
|
789
|
+
function isRtl() {
|
|
790
|
+
return document.documentElement.dir === "rtl";
|
|
791
|
+
}
|
|
792
|
+
function onRtlChange(callback) {
|
|
793
|
+
const observer = new MutationObserver(() => {
|
|
794
|
+
callback(document.documentElement.dir === "rtl");
|
|
795
|
+
});
|
|
796
|
+
observer.observe(document.documentElement, {
|
|
797
|
+
attributes: true,
|
|
798
|
+
attributeFilter: ["dir"]
|
|
799
|
+
});
|
|
800
|
+
return () => observer.disconnect();
|
|
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
|
+
}
|
|
722
868
|
export {
|
|
723
869
|
animate,
|
|
724
870
|
autoUpdate,
|
|
871
|
+
blank,
|
|
725
872
|
cancelAnimations,
|
|
726
873
|
computePosition,
|
|
727
874
|
createFocusTrap,
|
|
728
875
|
createMenuNavigation,
|
|
876
|
+
debounce,
|
|
877
|
+
detectFramerate,
|
|
729
878
|
easings,
|
|
730
879
|
except,
|
|
731
880
|
generateId,
|
|
881
|
+
hexToHsl,
|
|
882
|
+
hslToHex,
|
|
883
|
+
isRtl,
|
|
732
884
|
isStandardDomEvent,
|
|
733
885
|
kebabCase,
|
|
734
886
|
lockScroll,
|
|
735
887
|
markAriaHidden,
|
|
736
888
|
onClickOutside,
|
|
737
889
|
onEscapeKey,
|
|
890
|
+
onRtlChange,
|
|
738
891
|
onceChildrenRendered,
|
|
739
892
|
only,
|
|
893
|
+
prefersDarkMode,
|
|
740
894
|
rejectNullValues,
|
|
741
895
|
sameUrlPath,
|
|
742
896
|
supportsAnchorPositioning
|