@versini/ui-hooks 5.1.0 → 5.2.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/index.d.ts +259 -246
- package/dist/index.js +826 -30
- package/package.json +9 -9
- package/dist/hooks/useClickOutside.js +0 -26
- package/dist/hooks/useHaptic.js +0 -49
- package/dist/hooks/useHotkeys.js +0 -56
- package/dist/hooks/useInViewport.js +0 -12
- package/dist/hooks/useInterval.js +0 -12
- package/dist/hooks/useIsMounted.js +0 -10
- package/dist/hooks/useLocalStorage.js +0 -42
- package/dist/hooks/useMergeRefs.js +0 -12
- package/dist/hooks/useResizeObserver.js +0 -26
- package/dist/hooks/useUncontrolled.js +0 -23
- package/dist/hooks/useUniqueId.js +0 -15
- package/dist/hooks/useViewportSize.js +0 -21
- package/dist/hooks/useVisualViewportSize.js +0 -22
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/ui-hooks",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -20,13 +20,13 @@
|
|
|
20
20
|
],
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build:check": "tsc",
|
|
23
|
-
"build:js": "
|
|
24
|
-
"build:types": "
|
|
25
|
-
"build": "npm-run-all --serial clean build:check build:js
|
|
26
|
-
"clean": "rimraf dist",
|
|
27
|
-
"dev:js": "
|
|
28
|
-
"dev:types": "
|
|
29
|
-
"dev": "
|
|
23
|
+
"build:js": "rslib build",
|
|
24
|
+
"build:types": "echo 'Types now built with rslib'",
|
|
25
|
+
"build": "npm-run-all --serial clean build:check build:js",
|
|
26
|
+
"clean": "rimraf dist tmp",
|
|
27
|
+
"dev:js": "rslib build --watch",
|
|
28
|
+
"dev:types": "echo 'Types now watched with rslib'",
|
|
29
|
+
"dev": "rslib build --watch",
|
|
30
30
|
"lint": "biome lint src",
|
|
31
31
|
"lint:fix": "biome check src --write --no-errors-on-unmatched",
|
|
32
32
|
"prettier": "biome check --write --no-errors-on-unmatched",
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"test:watch": "vitest",
|
|
37
37
|
"test": "vitest run"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "7484ad443b77ef31e52ae3a7d88b8129bc6cdf1d"
|
|
40
40
|
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { useRef as f, useEffect as d } from "react";
|
|
2
|
-
const i = ["mousedown", "touchstart"];
|
|
3
|
-
function E(r, o, e) {
|
|
4
|
-
const c = f(null);
|
|
5
|
-
return d(() => {
|
|
6
|
-
const n = (t) => {
|
|
7
|
-
const s = t ? t.target : void 0;
|
|
8
|
-
if (Array.isArray(e)) {
|
|
9
|
-
const a = !document.body.contains(s) && s.tagName !== "HTML";
|
|
10
|
-
e.every(
|
|
11
|
-
(u) => !!u && !t.composedPath().includes(u)
|
|
12
|
-
) && !a && r();
|
|
13
|
-
} else c.current && !c.current.contains(s) && r();
|
|
14
|
-
};
|
|
15
|
-
return (o || i).forEach(
|
|
16
|
-
(t) => document.addEventListener(t, n)
|
|
17
|
-
), () => {
|
|
18
|
-
(o || i).forEach(
|
|
19
|
-
(t) => document.removeEventListener(t, n)
|
|
20
|
-
);
|
|
21
|
-
};
|
|
22
|
-
}, [r, e, o]), c;
|
|
23
|
-
}
|
|
24
|
-
export {
|
|
25
|
-
E as useClickOutside
|
|
26
|
-
};
|
package/dist/hooks/useHaptic.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { useRef as u, useEffect as d, useCallback as l } from "react";
|
|
2
|
-
const o = 50, s = 120, m = () => {
|
|
3
|
-
const c = u(null), r = u(null), i = u(/* @__PURE__ */ new Set());
|
|
4
|
-
d(() => typeof window > "u" ? void 0 : ((() => {
|
|
5
|
-
if (c.current && r.current)
|
|
6
|
-
return;
|
|
7
|
-
const e = document.createElement("input");
|
|
8
|
-
e.type = "checkbox", e.setAttribute("switch", ""), e.style.display = "none", e.setAttribute("aria-hidden", "true");
|
|
9
|
-
const t = document.createElement("label");
|
|
10
|
-
t.style.display = "none", t.setAttribute("aria-hidden", "true"), t.appendChild(e), document.body.appendChild(t), c.current = e, r.current = t;
|
|
11
|
-
})(), () => {
|
|
12
|
-
for (const e of i.current)
|
|
13
|
-
clearTimeout(e);
|
|
14
|
-
i.current.clear(), r.current && document.body.contains(r.current) && document.body.removeChild(r.current), c.current = null, r.current = null;
|
|
15
|
-
}), []);
|
|
16
|
-
const a = l(() => {
|
|
17
|
-
try {
|
|
18
|
-
if (navigator?.vibrate) {
|
|
19
|
-
navigator.vibrate(o);
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
r.current?.click();
|
|
23
|
-
} catch {
|
|
24
|
-
}
|
|
25
|
-
}, []);
|
|
26
|
-
return { haptic: l(
|
|
27
|
-
(n = 1) => {
|
|
28
|
-
if (!(typeof window > "u") && !(n < 1)) {
|
|
29
|
-
if (navigator?.vibrate && n > 1) {
|
|
30
|
-
const e = [];
|
|
31
|
-
for (let t = 0; t < n; t++)
|
|
32
|
-
e.push(o), t < n - 1 && e.push(s - o);
|
|
33
|
-
navigator.vibrate(e);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
for (let e = 0; e < n; e++) {
|
|
37
|
-
const t = setTimeout(() => {
|
|
38
|
-
a(), i.current.delete(t);
|
|
39
|
-
}, e * s);
|
|
40
|
-
i.current.add(t);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
},
|
|
44
|
-
[a]
|
|
45
|
-
) };
|
|
46
|
-
};
|
|
47
|
-
export {
|
|
48
|
-
m as useHaptic
|
|
49
|
-
};
|
package/dist/hooks/useHotkeys.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import { useEffect as y } from "react";
|
|
2
|
-
function E(t) {
|
|
3
|
-
const e = t.toLowerCase().split("+").map((s) => s.trim()), r = {
|
|
4
|
-
alt: e.includes("alt"),
|
|
5
|
-
ctrl: e.includes("ctrl"),
|
|
6
|
-
meta: e.includes("meta"),
|
|
7
|
-
mod: e.includes("mod"),
|
|
8
|
-
shift: e.includes("shift")
|
|
9
|
-
}, a = ["alt", "ctrl", "meta", "shift", "mod"], n = e.find((s) => !a.includes(s));
|
|
10
|
-
return {
|
|
11
|
-
...r,
|
|
12
|
-
key: n
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
function p(t, e) {
|
|
16
|
-
const { alt: r, ctrl: a, meta: n, mod: s, shift: i, key: f } = t, { altKey: c, ctrlKey: o, metaKey: u, shiftKey: d, key: m } = e;
|
|
17
|
-
if (r !== c)
|
|
18
|
-
return !1;
|
|
19
|
-
if (s) {
|
|
20
|
-
if (!o && !u)
|
|
21
|
-
return !1;
|
|
22
|
-
} else if (a !== o || n !== u)
|
|
23
|
-
return !1;
|
|
24
|
-
return i !== d ? !1 : !!(f && (m.toLowerCase() === f.toLowerCase() || e.code.replace("Key", "").toLowerCase() === f.toLowerCase()));
|
|
25
|
-
}
|
|
26
|
-
function l(t) {
|
|
27
|
-
return (e) => p(E(t), e);
|
|
28
|
-
}
|
|
29
|
-
function h(t) {
|
|
30
|
-
return (e) => {
|
|
31
|
-
const r = "nativeEvent" in e ? e.nativeEvent : e;
|
|
32
|
-
t.forEach(([a, n, s = { preventDefault: !0 }]) => {
|
|
33
|
-
l(a)(r) && (s.preventDefault && e.preventDefault(), n(r));
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
function k(t, e, r = !1) {
|
|
38
|
-
return t.target instanceof HTMLElement ? (r || !t.target.isContentEditable) && !e.includes(t.target.tagName) : !0;
|
|
39
|
-
}
|
|
40
|
-
function w(t, e = ["INPUT", "TEXTAREA", "SELECT"], r = !1) {
|
|
41
|
-
y(() => {
|
|
42
|
-
const a = (n) => {
|
|
43
|
-
t.forEach(
|
|
44
|
-
([s, i, f = { preventDefault: !0 }]) => {
|
|
45
|
-
l(s)(n) && k(n, e, r) && (f.preventDefault && n.preventDefault(), i(n));
|
|
46
|
-
}
|
|
47
|
-
);
|
|
48
|
-
};
|
|
49
|
-
return document.documentElement.addEventListener("keydown", a), () => document.documentElement.removeEventListener("keydown", a);
|
|
50
|
-
}, [t, e, r]);
|
|
51
|
-
}
|
|
52
|
-
export {
|
|
53
|
-
h as getHotkeyHandler,
|
|
54
|
-
k as shouldFireEvent,
|
|
55
|
-
w as useHotkeys
|
|
56
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { useRef as o, useState as c, useCallback as f } from "react";
|
|
2
|
-
function p() {
|
|
3
|
-
const e = o(null), [n, t] = c(!1);
|
|
4
|
-
return { ref: f((r) => {
|
|
5
|
-
typeof IntersectionObserver < "u" && (r && !e.current ? e.current = new IntersectionObserver(
|
|
6
|
-
(s) => t(s.some((i) => i.isIntersecting))
|
|
7
|
-
) : e.current?.disconnect(), r ? e.current?.observe(r) : t(!1));
|
|
8
|
-
}, []), inViewport: n };
|
|
9
|
-
}
|
|
10
|
-
export {
|
|
11
|
-
p as useInViewport
|
|
12
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
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,42 +0,0 @@
|
|
|
1
|
-
import { useSyncExternalStore as w, useCallback as a, useEffect as d } from "react";
|
|
2
|
-
function S(t, e) {
|
|
3
|
-
window.dispatchEvent(new StorageEvent("storage", { key: t, newValue: e }));
|
|
4
|
-
}
|
|
5
|
-
const g = (t, e) => {
|
|
6
|
-
const c = JSON.stringify(
|
|
7
|
-
typeof e == "function" ? e() : e
|
|
8
|
-
);
|
|
9
|
-
window.localStorage.setItem(t, c), S(t, c);
|
|
10
|
-
}, i = (t) => {
|
|
11
|
-
window.localStorage.removeItem(t), S(t, null);
|
|
12
|
-
}, l = (t) => window.localStorage.getItem(t), m = (t) => (window.addEventListener("storage", t), () => window.removeEventListener("storage", t));
|
|
13
|
-
function E({
|
|
14
|
-
key: t,
|
|
15
|
-
initialValue: e
|
|
16
|
-
}) {
|
|
17
|
-
const s = w(m, () => l(t)), o = a(
|
|
18
|
-
(n) => {
|
|
19
|
-
try {
|
|
20
|
-
const r = typeof n == "function" ? n(JSON.parse(s)) : n;
|
|
21
|
-
r == null ? i(t) : g(t, r);
|
|
22
|
-
} catch (r) {
|
|
23
|
-
console.warn(r);
|
|
24
|
-
}
|
|
25
|
-
},
|
|
26
|
-
[t, s]
|
|
27
|
-
), u = a(() => {
|
|
28
|
-
o(e);
|
|
29
|
-
}, [e, o]), f = a(() => {
|
|
30
|
-
o(null);
|
|
31
|
-
}, [o]);
|
|
32
|
-
return d(() => {
|
|
33
|
-
try {
|
|
34
|
-
l(t) === null && typeof e < "u" && g(t, e);
|
|
35
|
-
} catch (n) {
|
|
36
|
-
console.warn(n);
|
|
37
|
-
}
|
|
38
|
-
}, [t, e]), [s ? JSON.parse(s) : null, o, u, f];
|
|
39
|
-
}
|
|
40
|
-
export {
|
|
41
|
-
E as useLocalStorage
|
|
42
|
-
};
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { useMemo as t } from "react";
|
|
2
|
-
function o(r) {
|
|
3
|
-
return t(() => r.every((e) => e == null) ? () => {
|
|
4
|
-
} : (e) => {
|
|
5
|
-
r.forEach((n) => {
|
|
6
|
-
typeof n == "function" ? n(e) : n != null && (n.current = e);
|
|
7
|
-
});
|
|
8
|
-
}, [...r]);
|
|
9
|
-
}
|
|
10
|
-
export {
|
|
11
|
-
o as useMergeRefs
|
|
12
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { useRef as s, useState as m, useMemo as a, useEffect as l } from "react";
|
|
2
|
-
import { useIsMounted as d } from "./useIsMounted.js";
|
|
3
|
-
const b = {
|
|
4
|
-
x: 0,
|
|
5
|
-
y: 0,
|
|
6
|
-
width: 0,
|
|
7
|
-
height: 0,
|
|
8
|
-
top: 0,
|
|
9
|
-
left: 0,
|
|
10
|
-
bottom: 0,
|
|
11
|
-
right: 0
|
|
12
|
-
};
|
|
13
|
-
function h(n) {
|
|
14
|
-
const c = d(), e = s(0), t = s(null), [u, i] = m(b), r = a(() => typeof ResizeObserver > "u" ? null : new ResizeObserver((f) => {
|
|
15
|
-
const o = f[0];
|
|
16
|
-
o && (cancelAnimationFrame(e.current), e.current = requestAnimationFrame(() => {
|
|
17
|
-
t.current && c() && i(o.contentRect);
|
|
18
|
-
}));
|
|
19
|
-
}), [c]);
|
|
20
|
-
return l(() => (t.current && r?.observe(t.current, n), () => {
|
|
21
|
-
r?.disconnect(), e.current && cancelAnimationFrame(e.current);
|
|
22
|
-
}), [r, n]), [t, u];
|
|
23
|
-
}
|
|
24
|
-
export {
|
|
25
|
-
h as useResizeObserver
|
|
26
|
-
};
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { useState as c, useEffect as U } from "react";
|
|
2
|
-
function w({
|
|
3
|
-
value: e,
|
|
4
|
-
defaultValue: s,
|
|
5
|
-
finalValue: f,
|
|
6
|
-
onChange: o = () => {
|
|
7
|
-
},
|
|
8
|
-
initialControlledDelay: t = 0
|
|
9
|
-
}) {
|
|
10
|
-
const [r, u] = c(!1), [i, d] = c(
|
|
11
|
-
s !== void 0 ? s : f
|
|
12
|
-
), m = (n) => {
|
|
13
|
-
d(n), o?.(n);
|
|
14
|
-
};
|
|
15
|
-
return U(() => {
|
|
16
|
-
(async () => e !== void 0 && !r && t > 0 && (await new Promise(
|
|
17
|
-
(n) => setTimeout(n, t)
|
|
18
|
-
), u(!0)))();
|
|
19
|
-
}, [e, t, r]), e !== void 0 ? !r && t > 0 ? ["", o, !0] : [e, o, !0] : [i, m, !1];
|
|
20
|
-
}
|
|
21
|
-
export {
|
|
22
|
-
w as useUncontrolled
|
|
23
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { useId as u } from "react";
|
|
2
|
-
function i(e) {
|
|
3
|
-
const r = u();
|
|
4
|
-
if (!e)
|
|
5
|
-
return r;
|
|
6
|
-
if (typeof e == "number" || typeof e == "string")
|
|
7
|
-
return `${e}${r}`;
|
|
8
|
-
if (typeof e == "object") {
|
|
9
|
-
const { id: t, prefix: f = "" } = e;
|
|
10
|
-
return typeof t == "number" || typeof t == "string" ? `${f}${t}` : `${f}${r}`;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
export {
|
|
14
|
-
i as useUniqueId
|
|
15
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { useState as n, useCallback as w, useEffect as o } from "react";
|
|
2
|
-
function s() {
|
|
3
|
-
const [e, t] = n({
|
|
4
|
-
width: 0,
|
|
5
|
-
height: 0
|
|
6
|
-
}), i = w(() => {
|
|
7
|
-
t({
|
|
8
|
-
width: window?.visualViewport?.width || window.innerWidth || 0,
|
|
9
|
-
height: window?.visualViewport?.height || window.innerHeight || 0
|
|
10
|
-
});
|
|
11
|
-
}, []);
|
|
12
|
-
return o(() => (i(), window.visualViewport && (window.visualViewport.addEventListener("resize", i, {
|
|
13
|
-
passive: !0
|
|
14
|
-
}), window.visualViewport.addEventListener("orientationchange", i, {
|
|
15
|
-
passive: !0
|
|
16
|
-
})), () => {
|
|
17
|
-
window.visualViewport && (window.visualViewport.removeEventListener("resize", i), window.visualViewport.removeEventListener("orientationchange", i));
|
|
18
|
-
}), [i]), e;
|
|
19
|
-
}
|
|
20
|
-
export {
|
|
21
|
-
s as useVisualViewportSize
|
|
22
|
-
};
|