@versini/ui-hooks 4.4.0 → 4.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/dist/hooks/useClickOutside.js +26 -0
- package/dist/hooks/useInterval.js +12 -0
- package/dist/hooks/useLocalStorage.js +3 -3
- package/dist/hooks/useUncontrolled.js +10 -10
- package/dist/hooks/useViewportSize.js +21 -0
- package/dist/index.d.ts +41 -1
- package/dist/index.js +21 -17
- package/package.json +2 -2
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useRef as f, useEffect as d } from "react";
|
|
2
|
+
const i = ["mousedown", "touchstart"];
|
|
3
|
+
function E(r, e, o) {
|
|
4
|
+
const c = f(null);
|
|
5
|
+
return d(() => {
|
|
6
|
+
const s = (t) => {
|
|
7
|
+
const n = t ? t.target : void 0;
|
|
8
|
+
if (Array.isArray(o)) {
|
|
9
|
+
const a = !document.body.contains(n) && n.tagName !== "HTML";
|
|
10
|
+
o.every(
|
|
11
|
+
(u) => !!u && !t.composedPath().includes(u)
|
|
12
|
+
) && !a && r();
|
|
13
|
+
} else c.current && !c.current.contains(n) && r();
|
|
14
|
+
};
|
|
15
|
+
return (e || i).forEach(
|
|
16
|
+
(t) => document.addEventListener(t, s)
|
|
17
|
+
), () => {
|
|
18
|
+
(e || i).forEach(
|
|
19
|
+
(t) => document.removeEventListener(t, s)
|
|
20
|
+
);
|
|
21
|
+
};
|
|
22
|
+
}, [r, o, e]), c;
|
|
23
|
+
}
|
|
24
|
+
export {
|
|
25
|
+
E as useClickOutside
|
|
26
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useState as i, useRef as o, useCallback as a, useEffect as v } from "react";
|
|
2
|
+
function p(u, c) {
|
|
3
|
+
const [t, s] = i(!1), e = o(null), l = o(null), r = a(() => {
|
|
4
|
+
s((f) => (!f && (!e.current || e.current === -1) && (e.current = window.setInterval(l.current, c)), !0));
|
|
5
|
+
}, [c]), n = a(() => {
|
|
6
|
+
s(!1), window.clearInterval(e.current || -1), e.current = -1;
|
|
7
|
+
}, []);
|
|
8
|
+
return v(() => (l.current = u, t && r(), n), [u, t, r, n]), { start: r, stop: n, active: t };
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
p as useInterval
|
|
12
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useSyncExternalStore as
|
|
1
|
+
import { useSyncExternalStore as d, useCallback as a, useEffect as w } from "react";
|
|
2
2
|
function S(t, e) {
|
|
3
3
|
window.dispatchEvent(new StorageEvent("storage", { key: t, newValue: e }));
|
|
4
4
|
}
|
|
@@ -14,7 +14,7 @@ function E({
|
|
|
14
14
|
key: t,
|
|
15
15
|
initialValue: e
|
|
16
16
|
}) {
|
|
17
|
-
const s =
|
|
17
|
+
const s = d(m, () => l(t)), o = a(
|
|
18
18
|
(n) => {
|
|
19
19
|
try {
|
|
20
20
|
const r = typeof n == "function" ? n(JSON.parse(s)) : n;
|
|
@@ -29,7 +29,7 @@ function E({
|
|
|
29
29
|
}, [e, o]), f = a(() => {
|
|
30
30
|
o(null);
|
|
31
31
|
}, [o]);
|
|
32
|
-
return
|
|
32
|
+
return w(() => {
|
|
33
33
|
try {
|
|
34
34
|
l(t) === null && typeof e < "u" && g(t, e);
|
|
35
35
|
} catch (n) {
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { useState as
|
|
1
|
+
import { useState as u, useEffect as U } from "react";
|
|
2
2
|
function w({
|
|
3
|
-
value:
|
|
4
|
-
defaultValue:
|
|
5
|
-
finalValue:
|
|
6
|
-
onChange:
|
|
3
|
+
value: t,
|
|
4
|
+
defaultValue: n,
|
|
5
|
+
finalValue: c,
|
|
6
|
+
onChange: e = () => {
|
|
7
7
|
},
|
|
8
8
|
initialControlledDelay: r = 0
|
|
9
9
|
}) {
|
|
10
|
-
const [
|
|
11
|
-
|
|
10
|
+
const [f, i] = u(!1), [d, o] = u(
|
|
11
|
+
n !== void 0 ? n : c
|
|
12
12
|
), m = (s) => {
|
|
13
|
-
|
|
13
|
+
o(s), e == null || e(s);
|
|
14
14
|
};
|
|
15
15
|
return U(() => {
|
|
16
|
-
(async () =>
|
|
16
|
+
(async () => t !== void 0 && !f && r > 0 && (await new Promise(
|
|
17
17
|
(s) => setTimeout(s, r)
|
|
18
18
|
), i(!0)))();
|
|
19
|
-
}, [
|
|
19
|
+
}, [t, r, f]), t !== void 0 ? !f && r > 0 ? ["", e, !0] : [t, e, !0] : [d, m, !1];
|
|
20
20
|
}
|
|
21
21
|
export {
|
|
22
22
|
w as useUncontrolled
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useState as w, useCallback as r, useEffect as o } from "react";
|
|
2
|
+
function t(e, i) {
|
|
3
|
+
o(() => (window.addEventListener(e, i, {
|
|
4
|
+
passive: !0
|
|
5
|
+
}), () => window.removeEventListener(e, i)), [e, i]);
|
|
6
|
+
}
|
|
7
|
+
function s() {
|
|
8
|
+
const [e, i] = w({
|
|
9
|
+
width: 0,
|
|
10
|
+
height: 0
|
|
11
|
+
}), n = r(() => {
|
|
12
|
+
i({
|
|
13
|
+
width: window.innerWidth || 0,
|
|
14
|
+
height: window.innerHeight || 0
|
|
15
|
+
});
|
|
16
|
+
}, []);
|
|
17
|
+
return t("resize", n), t("orientationchange", n), o(n, []), e;
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
s as useViewportSize
|
|
21
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
import * as react from 'react';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Custom hooks that triggers a callback when a click is detected
|
|
5
|
+
* outside the target element.
|
|
6
|
+
*
|
|
7
|
+
* @param handler - Function to be called when clicked outside
|
|
8
|
+
* @param events - Array of events to listen to
|
|
9
|
+
* @param nodes - Array of nodes to check against
|
|
10
|
+
* @returns Ref to be attached to the target element
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const ref = useClickOutside(() => {
|
|
14
|
+
* console.log('Clicked outside!');
|
|
15
|
+
* });
|
|
16
|
+
* <div ref={ref}>Click me!</div>
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
declare function useClickOutside<T extends HTMLElement = any>(handler: () => void, events?: string[] | null, nodes?: (HTMLElement | null)[]): react.RefObject<T | null>;
|
|
20
|
+
|
|
3
21
|
interface HotkeyItemOptions {
|
|
4
22
|
preventDefault?: boolean;
|
|
5
23
|
}
|
|
@@ -14,6 +32,28 @@ type HotkeyItem = [
|
|
|
14
32
|
declare function shouldFireEvent(event: KeyboardEvent, tagsToIgnore: string[], triggerOnContentEditable?: boolean): boolean;
|
|
15
33
|
declare function useHotkeys(hotkeys: HotkeyItem[], tagsToIgnore?: string[], triggerOnContentEditable?: boolean): void;
|
|
16
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Custom hook to call a function within a given interval.
|
|
37
|
+
*
|
|
38
|
+
* @param fn Callback function to be executed at each interval
|
|
39
|
+
* @param interval Interval time in milliseconds
|
|
40
|
+
* @returns An object containing start, stop, and active state
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const { start, stop, active } = useInterval(() => {
|
|
44
|
+
* console.log("Interval executed");
|
|
45
|
+
* }, 1000);
|
|
46
|
+
* start(); // To start the interval
|
|
47
|
+
* stop(); // To stop the interval
|
|
48
|
+
* console.log(active); // To check if the interval is active
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
declare function useInterval(fn: () => void, interval: number): {
|
|
52
|
+
start: () => void;
|
|
53
|
+
stop: () => void;
|
|
54
|
+
active: boolean;
|
|
55
|
+
};
|
|
56
|
+
|
|
17
57
|
/**
|
|
18
58
|
* Hook that checks if an element is visible in the viewport.
|
|
19
59
|
* @returns
|
|
@@ -154,4 +194,4 @@ type UseUniqueIdOptions = string | number | {
|
|
|
154
194
|
};
|
|
155
195
|
declare function useUniqueId(options?: UseUniqueIdOptions): string | undefined;
|
|
156
196
|
|
|
157
|
-
export { type HotkeyItem, type HotkeyItemOptions, type StorageProperties, type UseUniqueIdOptions, getHotkeyHandler, shouldFireEvent, useHotkeys, useInViewport, useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId };
|
|
197
|
+
export { type HotkeyItem, type HotkeyItemOptions, type StorageProperties, type UseUniqueIdOptions, getHotkeyHandler, shouldFireEvent, useClickOutside, useHotkeys, useInViewport, useInterval, useIsMounted, useLocalStorage, useMergeRefs, useResizeObserver, useUncontrolled, useUniqueId };
|
package/dist/index.js
CHANGED
|
@@ -1,24 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { useClickOutside as r } from "./hooks/useClickOutside.js";
|
|
2
|
+
import { getHotkeyHandler as s, shouldFireEvent as u, useHotkeys as f } from "./hooks/useHotkeys.js";
|
|
3
|
+
import { useInterval as m } from "./hooks/useInterval.js";
|
|
4
|
+
import { useInViewport as n } from "./hooks/useInViewport.js";
|
|
5
|
+
import { useIsMounted as d } from "./hooks/useIsMounted.js";
|
|
6
|
+
import { useLocalStorage as a } from "./hooks/useLocalStorage.js";
|
|
7
|
+
import { useMergeRefs as c } from "./hooks/useMergeRefs.js";
|
|
8
|
+
import { useResizeObserver as k } from "./hooks/useResizeObserver.js";
|
|
7
9
|
import { useUncontrolled as H } from "./hooks/useUncontrolled.js";
|
|
8
|
-
import { useUniqueId as
|
|
10
|
+
import { useUniqueId as M } from "./hooks/useUniqueId.js";
|
|
9
11
|
/*!
|
|
10
|
-
@versini/ui-hooks v4.
|
|
12
|
+
@versini/ui-hooks v4.6.0
|
|
11
13
|
© 2025 gizmette.com
|
|
12
14
|
*/
|
|
13
15
|
export {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
f as
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
a as
|
|
16
|
+
s as getHotkeyHandler,
|
|
17
|
+
u as shouldFireEvent,
|
|
18
|
+
r as useClickOutside,
|
|
19
|
+
f as useHotkeys,
|
|
20
|
+
n as useInViewport,
|
|
21
|
+
m as useInterval,
|
|
22
|
+
d as useIsMounted,
|
|
23
|
+
a as useLocalStorage,
|
|
24
|
+
c as useMergeRefs,
|
|
25
|
+
k as useResizeObserver,
|
|
22
26
|
H as useUncontrolled,
|
|
23
|
-
|
|
27
|
+
M as useUniqueId
|
|
24
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-hooks",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -37,5 +37,5 @@
|
|
|
37
37
|
"react": "^18.3.1 || ^19.0.0",
|
|
38
38
|
"react-dom": "^18.3.1 || ^19.0.0"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "1199c5a06253a95c103981005f5a1131c841bb00"
|
|
41
41
|
}
|