@stryke/hooks 0.4.8 → 0.4.9
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/use-debounce.cjs +9 -9
- package/dist/use-debounce.d.ts +2 -2
- package/dist/use-debounce.mjs +1 -1
- package/dist/use-did-finish-ssr.cjs +2 -2
- package/dist/use-did-finish-ssr.mjs +1 -1
- package/dist/use-event.cjs +3 -3
- package/dist/use-event.mjs +1 -1
- package/dist/use-idle.cjs +8 -8
- package/dist/use-idle.mjs +1 -1
- package/dist/use-memo-stable.cjs +3 -3
- package/dist/use-memo-stable.mjs +1 -1
- package/dist/use-network-state.cjs +3 -3
- package/dist/use-network-state.mjs +1 -1
- package/package.json +7 -6
package/dist/use-debounce.cjs
CHANGED
|
@@ -7,20 +7,20 @@ exports.useDebounce = useDebounce;
|
|
|
7
7
|
exports.useDebounceValue = useDebounceValue;
|
|
8
8
|
var _debounce = require("@stryke/helpers/debounce");
|
|
9
9
|
var _react = require("react");
|
|
10
|
-
function useDebounce(e, n,
|
|
11
|
-
const
|
|
10
|
+
function useDebounce(e, n, t = {}) {
|
|
11
|
+
const u = (0, _react.useRef)(null);
|
|
12
12
|
return (0, _react.useEffect)(() => () => {
|
|
13
|
-
|
|
14
|
-
}, []), (0, _react.useMemo)(() => (
|
|
13
|
+
u.current?.cancel();
|
|
14
|
+
}, []), (0, _react.useMemo)(() => (u.current = (0, _debounce.debounce)(e, n, t), u.current), [e, t, n]);
|
|
15
15
|
}
|
|
16
16
|
function useDebounceValue(e, n = 0) {
|
|
17
|
-
const [
|
|
17
|
+
const [t, u] = (0, _react.useState)(e);
|
|
18
18
|
return (0, _react.useEffect)(() => {
|
|
19
|
-
const
|
|
20
|
-
r
|
|
19
|
+
const c = setTimeout(() => {
|
|
20
|
+
u(r => r === e ? r : e);
|
|
21
21
|
}, n);
|
|
22
22
|
return () => {
|
|
23
|
-
clearTimeout(
|
|
23
|
+
clearTimeout(c);
|
|
24
24
|
};
|
|
25
|
-
}, [n, e]),
|
|
25
|
+
}, [n, e]), t;
|
|
26
26
|
}
|
package/dist/use-debounce.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { DebounceOptions } from "@stryke/helpers/debounce";
|
|
1
|
+
import type { DebounceOptions } from "@stryke/helpers/debounce";
|
|
2
2
|
export declare function useDebounce<A extends (...args: any) => any | undefined | null, DebouncedFn extends A & {
|
|
3
3
|
cancel: () => void;
|
|
4
|
-
}>(fn: A, wait: number, options?: DebounceOptions
|
|
4
|
+
}>(fn: A, wait: number, options?: DebounceOptions): DebouncedFn;
|
|
5
5
|
/**
|
|
6
6
|
* Returns a value once it stops changing after "amt" time.
|
|
7
7
|
* Note: you may need to memo or this will keep re-rendering
|
package/dist/use-debounce.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{debounce as s}from"@stryke/helpers/debounce";import{useEffect as o,useMemo as b,useRef as d,useState as f}from"react";export function useDebounce(e,n,
|
|
1
|
+
import{debounce as s}from"@stryke/helpers/debounce";import{useEffect as o,useMemo as b,useRef as d,useState as f}from"react";export function useDebounce(e,n,t={}){const u=d(null);return o(()=>()=>{u.current?.cancel()},[]),b(()=>(u.current=s(e,n,t),u.current),[e,t,n])}export function useDebounceValue(e,n=0){const[t,u]=f(e);return o(()=>{const c=setTimeout(()=>{u(r=>r===e?r:e)},n);return()=>{clearTimeout(c)}},[n,e]),t}
|
|
@@ -13,9 +13,9 @@ const a = () => {},
|
|
|
13
13
|
function useDidFinishSSR(e, n) {
|
|
14
14
|
if (process.env.TAMAGUI_TARGET === "native") return e ?? !0;
|
|
15
15
|
if (n?.sync) return (0, _react.useSyncExternalStore)(c, () => e ?? !0, () => !1);
|
|
16
|
-
const [t,
|
|
16
|
+
const [t, u] = (0, _react.useState)(e);
|
|
17
17
|
return (0, _react.useEffect)(() => {
|
|
18
|
-
|
|
18
|
+
u(e ?? !0);
|
|
19
19
|
}, [e]), t ?? !1;
|
|
20
20
|
}
|
|
21
21
|
function useDidFinishSSRSync(e) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isFunction as
|
|
1
|
+
import{isFunction as r}from"@stryke/type-checks/is-function";import{useEffect as o,useState as s,useSyncExternalStore as i}from"react";const a=()=>{},c=()=>a;export function useDidFinishSSR(e,n){if(process.env.TAMAGUI_TARGET==="native")return e??!0;if(n?.sync)return i(c,()=>e??!0,()=>!1);const[t,u]=s(e);return o(()=>{u(e??!0)},[e]),t??!1}export function useDidFinishSSRSync(e){return useDidFinishSSR(e,{sync:!0})}export function useClientValue(e){return useDidFinishSSR()?r(e)?e():e:void 0}
|
package/dist/use-event.cjs
CHANGED
|
@@ -6,12 +6,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.useEvent = useEvent;
|
|
7
7
|
var _react = require("react");
|
|
8
8
|
var _useIsomorphicLayoutEffect = require("./use-isomorphic-layout-effect.cjs");
|
|
9
|
-
function useEvent(n) {
|
|
10
|
-
return s(n, l, !0);
|
|
11
|
-
}
|
|
12
9
|
const l = () => {
|
|
13
10
|
throw new Error("Cannot call an event handler while rendering.");
|
|
14
11
|
};
|
|
12
|
+
function useEvent(n) {
|
|
13
|
+
return s(n, l, !0);
|
|
14
|
+
}
|
|
15
15
|
function s(n, r, t) {
|
|
16
16
|
const e = (0, _react.useRef)(r ?? n);
|
|
17
17
|
return (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
package/dist/use-event.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useCallback as u,useRef as a}from"react";import{useIsomorphicLayoutEffect as c}from"./use-isomorphic-layout-effect";
|
|
1
|
+
import{useCallback as u,useRef as a}from"react";import{useIsomorphicLayoutEffect as c}from"./use-isomorphic-layout-effect";const l=()=>{throw new Error("Cannot call an event handler while rendering.")};export function useEvent(n){return s(n,l,!0)}function s(n,r,t){const e=a(r??n);return c(()=>{e.current=n}),u(t?(...o)=>e.current?.apply(null,o):()=>e.current,[])}
|
package/dist/use-idle.cjs
CHANGED
|
@@ -6,22 +6,22 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
exports.useIdle = void 0;
|
|
7
7
|
var _throttle = require("@stryke/helpers/throttle");
|
|
8
8
|
var _react = require("react");
|
|
9
|
-
const useIdle = (
|
|
10
|
-
const [
|
|
9
|
+
const useIdle = (n = 1e3 * 60) => {
|
|
10
|
+
const [s, o] = (0, _react.useState)(!1);
|
|
11
11
|
return (0, _react.useEffect)(() => {
|
|
12
12
|
let t = -1;
|
|
13
13
|
const i = () => {
|
|
14
|
-
|
|
14
|
+
o(!0);
|
|
15
15
|
},
|
|
16
16
|
e = (0, _throttle.throttle)(() => {
|
|
17
|
-
|
|
17
|
+
o(!1), clearTimeout(t), t = setTimeout(i, n);
|
|
18
18
|
}, 500),
|
|
19
|
-
|
|
19
|
+
r = () => {
|
|
20
20
|
document.hidden || e();
|
|
21
21
|
};
|
|
22
|
-
return t =
|
|
23
|
-
|
|
22
|
+
return t = setTimeout(i, n), addEventListener("mousemove", e), addEventListener("mousedown", e), window.addEventListener("resize", e), addEventListener("keydown", e), addEventListener("touchstart", e), window.addEventListener("wheel", e), document.addEventListener("visibilitychange", r), () => {
|
|
23
|
+
removeEventListener("mousemove", e), removeEventListener("mousedown", e), window.removeEventListener("resize", e), removeEventListener("keydown", e), removeEventListener("touchstart", e), window.removeEventListener("wheel", e), document.removeEventListener("visibilitychange", r), clearTimeout(t);
|
|
24
24
|
};
|
|
25
|
-
}, [
|
|
25
|
+
}, [n]), s;
|
|
26
26
|
};
|
|
27
27
|
exports.useIdle = useIdle;
|
package/dist/use-idle.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{throttle as
|
|
1
|
+
import{throttle as d}from"@stryke/helpers/throttle";import{useEffect as v,useState as m}from"react";export const useIdle=(n=1e3*60)=>{const[s,o]=m(!1);return v(()=>{let t=-1;const i=()=>{o(!0)},e=d(()=>{o(!1),clearTimeout(t),t=setTimeout(i,n)},500),r=()=>{document.hidden||e()};return t=setTimeout(i,n),addEventListener("mousemove",e),addEventListener("mousedown",e),window.addEventListener("resize",e),addEventListener("keydown",e),addEventListener("touchstart",e),window.addEventListener("wheel",e),document.addEventListener("visibilitychange",r),()=>{removeEventListener("mousemove",e),removeEventListener("mousedown",e),window.removeEventListener("resize",e),removeEventListener("keydown",e),removeEventListener("touchstart",e),window.removeEventListener("wheel",e),document.removeEventListener("visibilitychange",r),clearTimeout(t)}},[n]),s};
|
package/dist/use-memo-stable.cjs
CHANGED
|
@@ -17,11 +17,11 @@ function useMemoStable(t, e) {
|
|
|
17
17
|
}))[0],
|
|
18
18
|
r = (0, _react.useRef)(!0),
|
|
19
19
|
n = (0, _react.useRef)(u),
|
|
20
|
-
|
|
21
|
-
a = (0, _react.useMemo)(() =>
|
|
20
|
+
c = r.current || !!(e && n.current.inputs && i(e, n.current.inputs)),
|
|
21
|
+
a = (0, _react.useMemo)(() => c ? n.current : {
|
|
22
22
|
inputs: e,
|
|
23
23
|
result: t()
|
|
24
|
-
}, [e, t,
|
|
24
|
+
}, [e, t, c]);
|
|
25
25
|
return (0, _react.useEffect)(() => {
|
|
26
26
|
r.current = !1, n.current = a;
|
|
27
27
|
}, [a]), a.result;
|
package/dist/use-memo-stable.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{useEffect as o,useMemo as l,useRef as
|
|
1
|
+
import{useEffect as o,useMemo as l,useRef as s,useState as f}from"react";const i=(t,e)=>{if(t.length!==e.length)return!1;for(const[u,r]of t.entries())if(r!==e[u])return!1;return!0};export function useMemoStable(t,e){const u=f(()=>({inputs:e,result:t()}))[0],r=s(!0),n=s(u),c=r.current||!!(e&&n.current.inputs&&i(e,n.current.inputs)),a=l(()=>c?n.current:{inputs:e,result:t()},[e,t,c]);return o(()=>{r.current=!1,n.current=a},[a]),a.result}
|
|
@@ -9,16 +9,16 @@ var _isEqual = require("@stryke/type-checks/is-equal");
|
|
|
9
9
|
var _react = require("react");
|
|
10
10
|
const i = () => navigator["connection"] || navigator["mozConnection"] || navigator["webkitConnection"];
|
|
11
11
|
const useNetworkStateSubscribe = e => {
|
|
12
|
-
|
|
12
|
+
addEventListener("online", e, {
|
|
13
13
|
passive: !0
|
|
14
|
-
}),
|
|
14
|
+
}), addEventListener("offline", e, {
|
|
15
15
|
passive: !0
|
|
16
16
|
});
|
|
17
17
|
const t = i();
|
|
18
18
|
return t && t.addEventListener("change", e, {
|
|
19
19
|
passive: !0
|
|
20
20
|
}), () => {
|
|
21
|
-
|
|
21
|
+
removeEventListener("online", e), removeEventListener("offline", e), t && t.removeEventListener("change", e);
|
|
22
22
|
};
|
|
23
23
|
};
|
|
24
24
|
exports.useNetworkStateSubscribe = useNetworkStateSubscribe;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{isEqual as a}from"@stryke/type-checks/is-equal";import{useRef as s,useSyncExternalStore as c}from"react";const i=()=>navigator["connection"]||navigator["mozConnection"]||navigator["webkitConnection"];export const useNetworkStateSubscribe=e=>{
|
|
1
|
+
import{isEqual as a}from"@stryke/type-checks/is-equal";import{useRef as s,useSyncExternalStore as c}from"react";const i=()=>navigator["connection"]||navigator["mozConnection"]||navigator["webkitConnection"];export const useNetworkStateSubscribe=e=>{addEventListener("online",e,{passive:!0}),addEventListener("offline",e,{passive:!0});const t=i();return t&&t.addEventListener("change",e,{passive:!0}),()=>{removeEventListener("online",e),removeEventListener("offline",e),t&&t.removeEventListener("change",e)}};const v=()=>{throw new Error("useNetworkState is a client-only hook")};export function useNetworkState(){const e=s({});return c(useNetworkStateSubscribe,()=>{const r=navigator.onLine,n=i(),o={online:r,downlink:n?.downlink,downlinkMax:n?.downlinkMax,effectiveType:n?.effectiveType,rtt:n?.rtt,saveData:n?.saveData,type:n?.type};return a(e.current,o)?e.current:(e.current=o,o)},v)}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stryke/hooks",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.9",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A package containing shared hooks that can be used in any React UI project",
|
|
6
6
|
"repository": {
|
|
@@ -23,10 +23,10 @@
|
|
|
23
23
|
"react": "^19.1.0",
|
|
24
24
|
"react-dom": "^19.1.0",
|
|
25
25
|
"react-native": "^0.77.0",
|
|
26
|
-
"@stryke/env": "^0.15.
|
|
27
|
-
"@stryke/helpers": "^0.
|
|
28
|
-
"@stryke/type-checks": "^0.3.
|
|
29
|
-
"@stryke/types": "^0.8.
|
|
26
|
+
"@stryke/env": "^0.15.10",
|
|
27
|
+
"@stryke/helpers": "^0.7.1",
|
|
28
|
+
"@stryke/type-checks": "^0.3.6",
|
|
29
|
+
"@stryke/types": "^0.8.6"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@types/react": "^19.0.12",
|
|
@@ -335,5 +335,6 @@
|
|
|
335
335
|
},
|
|
336
336
|
"main": "./dist/index.cjs",
|
|
337
337
|
"module": "./dist/index.mjs",
|
|
338
|
-
"types": "./dist/index.d.ts"
|
|
338
|
+
"types": "./dist/index.d.ts",
|
|
339
|
+
"gitHead": "57da58e3434302b35f798674d9f18f92ca7f7ac9"
|
|
339
340
|
}
|