@siberiacancode/reactuse 0.2.34 → 0.2.35
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/cjs/hooks/useDeviceMotion/useDeviceMotion.cjs +1 -1
- package/dist/cjs/hooks/useDeviceMotion/useDeviceMotion.cjs.map +1 -1
- package/dist/cjs/hooks/useHash/useHash.cjs +1 -1
- package/dist/cjs/hooks/useHash/useHash.cjs.map +1 -1
- package/dist/esm/hooks/useDeviceMotion/useDeviceMotion.mjs +21 -21
- package/dist/esm/hooks/useDeviceMotion/useDeviceMotion.mjs.map +1 -1
- package/dist/esm/hooks/useHash/useHash.mjs +17 -17
- package/dist/esm/hooks/useHash/useHash.mjs.map +1 -1
- package/dist/types/hooks/useDeviceMotion/useDeviceMotion.d.ts +25 -4
- package/dist/types/hooks/useHash/useHash.d.ts +3 -1
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const l=require("react"),d=require("../../utils/helpers/throttle.cjs"),s=(...e)=>{const o=typeof e[0]=="number"?e[0]:e[0]?.delay??1e3,i=typeof e[0]=="function"?e[0]:e[0]?.onChange,c=e[0]?.enabled??!0,[n,u]=l.useState({interval:0,rotationRate:{alpha:null,beta:null,gamma:null},acceleration:{x:null,y:null,z:null},accelerationIncludingGravity:{x:null,y:null,z:null}}),a=l.useRef(i);return a.current=i,l.useEffect(()=>{if(!c)return;const r=d.throttle(t=>{a.current?.(t),u({interval:t.interval,rotationRate:{...n.rotationRate,...t.rotationRate},acceleration:{...n.acceleration,...t.acceleration},accelerationIncludingGravity:{...n.accelerationIncludingGravity,...t.accelerationIncludingGravity}})},o);return window.addEventListener("devicemotion",r),()=>{window.removeEventListener("devicemotion",r)}},[o,c]),n};exports.useDeviceMotion=s;
|
|
2
2
|
//# sourceMappingURL=useDeviceMotion.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeviceMotion.cjs","sources":["../../../../src/hooks/useDeviceMotion/useDeviceMotion.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { throttle } from '@/utils/helpers';\n\nexport interface UseDeviceMotionReturn {\n acceleration: DeviceMotionEventAcceleration;\n accelerationIncludingGravity: DeviceMotionEventAcceleration;\n interval: DeviceMotionEvent['interval'];\n rotationRate: DeviceMotionEventRotationRate;\n}\n\nexport interface
|
|
1
|
+
{"version":3,"file":"useDeviceMotion.cjs","sources":["../../../../src/hooks/useDeviceMotion/useDeviceMotion.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { throttle } from '@/utils/helpers';\n\nexport interface UseDeviceMotionReturn {\n acceleration: DeviceMotionEventAcceleration;\n accelerationIncludingGravity: DeviceMotionEventAcceleration;\n interval: DeviceMotionEvent['interval'];\n rotationRate: DeviceMotionEventRotationRate;\n}\n\nexport interface UseDeviceMotionOptions {\n /** The delay in milliseconds */\n delay?: number;\n /** Whether to enable the hook */\n enabled?: boolean;\n /** The callback function to be invoked */\n onChange?: (event: DeviceMotionEvent) => void;\n}\n\nexport interface UseDeviceMotion {\n (callback?: (event: DeviceMotionEvent) => void, delay?: number): UseDeviceMotionReturn;\n\n (options?: UseDeviceMotionOptions): UseDeviceMotionReturn;\n}\n\n/**\n * @name useDeviceMotion\n * @description - Hook that work with device motion\n * @category Sensors\n * @usage low\n *\n * @browserapi DeviceMotionEvent https://developer.mozilla.org/en-US/docs/Web/API/Window/DeviceMotionEvent\n *\n * @overload\n * @param {number} [delay=1000] The delay in milliseconds\n * @param {(event: DeviceMotionEvent) => void} [callback] The callback function to be invoked\n * @returns {UseDeviceMotionReturn} The device motion data and interval\n *\n * @example\n * const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion(500, (event) => console.log(event));\n *\n * @overload\n * @param {(event: DeviceMotionEvent) => void} [callback] The callback function to be invoked\n * @returns {UseDeviceMotionReturn} The device motion data and interval\n *\n * @example\n * const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion((event) => console.log(event));\n *\n * @overload\n * @param {UseDeviceMotionOptions} [options] Configuration options\n * @param {number} [options.delay] The delay in milliseconds\n * @param {boolean} [options.enabled] Whether to enable the hook\n * @param {(event: DeviceMotionEvent) => void} [options.onChange] The callback function to be invoked\n * @returns {UseDeviceMotionReturn} The device motion data and interval\n *\n * @example\n * const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion();\n */\nexport const useDeviceMotion = ((...params: any[]) => {\n const delay = typeof params[0] === 'number' ? params[0] : (params[0]?.delay ?? 1000);\n const callback = typeof params[0] === 'function' ? params[0] : params[0]?.onChange;\n const enabled = params[0]?.enabled ?? true;\n\n const [value, setValue] = useState<UseDeviceMotionReturn>({\n interval: 0,\n rotationRate: { alpha: null, beta: null, gamma: null },\n acceleration: { x: null, y: null, z: null },\n accelerationIncludingGravity: { x: null, y: null, z: null }\n });\n const internalCallbackRef = useRef(callback);\n internalCallbackRef.current = callback;\n\n useEffect(() => {\n if (!enabled) return;\n\n const onDeviceMotion = throttle<[DeviceMotionEvent]>((event) => {\n internalCallbackRef.current?.(event);\n\n setValue({\n interval: event.interval,\n rotationRate: {\n ...value.rotationRate,\n ...event.rotationRate\n },\n acceleration: {\n ...value.acceleration,\n ...event.acceleration\n },\n accelerationIncludingGravity: {\n ...value.accelerationIncludingGravity,\n ...event.accelerationIncludingGravity\n }\n });\n }, delay);\n\n window.addEventListener('devicemotion', onDeviceMotion);\n\n return () => {\n window.removeEventListener('devicemotion', onDeviceMotion);\n };\n }, [delay, enabled]);\n\n return value;\n}) as UseDeviceMotion;\n"],"names":["useDeviceMotion","params","delay","callback","enabled","value","setValue","useState","internalCallbackRef","useRef","useEffect","onDeviceMotion","throttle","event"],"mappings":"uJA2DaA,EAAmB,IAAIC,IAAkB,CACpD,MAAMC,EAAQ,OAAOD,EAAO,CAAC,GAAM,SAAWA,EAAO,CAAC,EAAKA,EAAO,CAAC,GAAG,OAAS,IACzEE,EAAW,OAAOF,EAAO,CAAC,GAAM,WAAaA,EAAO,CAAC,EAAIA,EAAO,CAAC,GAAG,SACpEG,EAAUH,EAAO,CAAC,GAAG,SAAW,GAEhC,CAACI,EAAOC,CAAQ,EAAIC,WAAgC,CACxD,SAAU,EACV,aAAc,CAAE,MAAO,KAAM,KAAM,KAAM,MAAO,IAAA,EAChD,aAAc,CAAE,EAAG,KAAM,EAAG,KAAM,EAAG,IAAA,EACrC,6BAA8B,CAAE,EAAG,KAAM,EAAG,KAAM,EAAG,IAAA,CAAK,CAC3D,EACKC,EAAsBC,EAAAA,OAAON,CAAQ,EAC3C,OAAAK,EAAoB,QAAUL,EAE9BO,EAAAA,UAAU,IAAM,CACd,GAAI,CAACN,EAAS,OAEd,MAAMO,EAAiBC,WAA+BC,GAAU,CAC9DL,EAAoB,UAAUK,CAAK,EAEnCP,EAAS,CACP,SAAUO,EAAM,SAChB,aAAc,CACZ,GAAGR,EAAM,aACT,GAAGQ,EAAM,YAAA,EAEX,aAAc,CACZ,GAAGR,EAAM,aACT,GAAGQ,EAAM,YAAA,EAEX,6BAA8B,CAC5B,GAAGR,EAAM,6BACT,GAAGQ,EAAM,4BAAA,CACX,CACD,CAAA,EACAX,CAAK,EAER,cAAO,iBAAiB,eAAgBS,CAAc,EAE/C,IAAM,CACX,OAAO,oBAAoB,eAAgBA,CAAc,CAAA,CAC3D,EACC,CAACT,EAAOE,CAAO,CAAC,EAEZC,CACT"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("react"),c=()=>decodeURIComponent(window.location.hash.replace("#","")),w=(...e)=>{const i=typeof e[0]=="string"?e[0]:"",t=typeof e[1]=="object"?e[1]:typeof e[1]=="function"?{onChange:e[1]}:typeof e[0]=="object"?e[0]:{},h=t?.enabled??!0,r=t?.mode??"replace",[u,a]=s.useState(()=>typeof window>"u"?i:c()||i),o=s.useRef(t);o.current=t;const f=n=>{window.location.hash=n,a(n),o.current?.onChange?.(n)};return s.useEffect(()=>{if(!h)return;r==="replace"&&(window.location.hash=u);const n=()=>{const d=c();a(d),o.current?.onChange?.(d)};return window.addEventListener("hashchange",n),()=>{window.removeEventListener("hashchange",n)}},[h,r]),[u,f]};exports.getHash=c;exports.useHash=w;
|
|
2
2
|
//# sourceMappingURL=useHash.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHash.cjs","sources":["../../../../src/hooks/useHash/useHash.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nexport const getHash = () => decodeURIComponent(window.location.hash.replace('#', ''));\n\n/** The use hash options type */\nexport interface UseHashOptions {\n /** The enabled state of the hook */\n enabled?: boolean;\n /** The mode of hash setting */\n mode?: 'initial' | 'replace';\n /** Callback function called when hash changes */\n onChange?: (hash: string) => void;\n}\n\n/** The use hash return type */\ntype UseHashReturn = [string, (value: string) => void];\n\nexport interface UseHash {\n (initialValue?: string, options?: UseHashOptions): UseHashReturn;\n\n (initialValue?: string, callback?: (hash: string) => void): UseHashReturn;\n}\n\n/**\n * @name useHash\n * @description - Hook that manages the hash value\n * @category State\n * @usage low\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {UseHashOptions} [options] Configuration options\n * @param {boolean} [options.enabled] The enabled state of the hook\n * @param {'initial' | 'replace'} [options.mode] The mode of hash setting\n * @param {(hash: string) => void} [options.onChange] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", {\n * enabled: true,\n * mode: \"replace\",\n * onChange: (newHash) => console.log('Hash changed:', newHash)\n * });\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {(hash: string) => void} [callback] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", (newHash) => console.log('
|
|
1
|
+
{"version":3,"file":"useHash.cjs","sources":["../../../../src/hooks/useHash/useHash.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nexport const getHash = () => decodeURIComponent(window.location.hash.replace('#', ''));\n\n/** The use hash options type */\nexport interface UseHashOptions {\n /** The enabled state of the hook */\n enabled?: boolean;\n /** The mode of hash setting */\n mode?: 'initial' | 'replace';\n /** Callback function called when hash changes */\n onChange?: (hash: string) => void;\n}\n\n/** The use hash return type */\ntype UseHashReturn = [string, (value: string) => void];\n\nexport interface UseHash {\n (initialValue?: string, options?: UseHashOptions): UseHashReturn;\n\n (options?: UseHashOptions): UseHashReturn;\n\n (initialValue?: string, callback?: (hash: string) => void): UseHashReturn;\n\n (callback?: (hash: string) => void): UseHashReturn;\n}\n\n/**\n * @name useHash\n * @description - Hook that manages the hash value\n * @category State\n * @usage low\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {UseHashOptions} [options] Configuration options\n * @param {boolean} [options.enabled] The enabled state of the hook\n * @param {'initial' | 'replace'} [options.mode] The mode of hash setting\n * @param {(hash: string) => void} [options.onChange] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", {\n * enabled: true,\n * mode: \"replace\",\n * onChange: (newHash) => console.log('Hash changed:', newHash)\n * });\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {(hash: string) => void} [callback] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", (newHash) => console.log('callback'));\n */\nexport const useHash = ((...params: any[]) => {\n const initialValue = typeof params[0] === 'string' ? params[0] : '';\n const options =\n typeof params[1] === 'object'\n ? params[1]\n : typeof params[1] === 'function'\n ? { onChange: params[1] }\n : typeof params[0] === 'object'\n ? params[0]\n : {};\n\n const enabled = options?.enabled ?? true;\n const mode = options?.mode ?? 'replace';\n\n const [hash, setHash] = useState(() => {\n if (typeof window === 'undefined') return initialValue;\n return getHash() || initialValue;\n });\n\n const optionsRef = useRef(options);\n optionsRef.current = options;\n\n const set = (value: string) => {\n window.location.hash = value;\n setHash(value);\n optionsRef.current?.onChange?.(value);\n };\n\n useEffect(() => {\n if (!enabled) return;\n\n if (mode === 'replace') window.location.hash = hash;\n\n const onHashChange = () => {\n const newHash = getHash();\n setHash(newHash);\n optionsRef.current?.onChange?.(newHash);\n };\n\n window.addEventListener('hashchange', onHashChange);\n return () => {\n window.removeEventListener('hashchange', onHashChange);\n };\n }, [enabled, mode]);\n\n return [hash, set] as const;\n}) as UseHash;\n"],"names":["getHash","useHash","params","initialValue","options","enabled","mode","hash","setHash","useState","optionsRef","useRef","set","value","useEffect","onHashChange","newHash"],"mappings":"yGAEaA,EAAU,IAAM,mBAAmB,OAAO,SAAS,KAAK,QAAQ,IAAK,EAAE,CAAC,EAsDxEC,EAAW,IAAIC,IAAkB,CAC5C,MAAMC,EAAe,OAAOD,EAAO,CAAC,GAAM,SAAWA,EAAO,CAAC,EAAI,GAC3DE,EACJ,OAAOF,EAAO,CAAC,GAAM,SACjBA,EAAO,CAAC,EACR,OAAOA,EAAO,CAAC,GAAM,WACnB,CAAE,SAAUA,EAAO,CAAC,CAAA,EACpB,OAAOA,EAAO,CAAC,GAAM,SACnBA,EAAO,CAAC,EACR,CAAA,EAEJG,EAAUD,GAAS,SAAW,GAC9BE,EAAOF,GAAS,MAAQ,UAExB,CAACG,EAAMC,CAAO,EAAIC,EAAAA,SAAS,IAC3B,OAAO,OAAW,IAAoBN,EACnCH,KAAaG,CACrB,EAEKO,EAAaC,EAAAA,OAAOP,CAAO,EACjCM,EAAW,QAAUN,EAErB,MAAMQ,EAAOC,GAAkB,CAC7B,OAAO,SAAS,KAAOA,EACvBL,EAAQK,CAAK,EACbH,EAAW,SAAS,WAAWG,CAAK,CAAA,EAGtCC,OAAAA,EAAAA,UAAU,IAAM,CACd,GAAI,CAACT,EAAS,OAEVC,IAAS,YAAW,OAAO,SAAS,KAAOC,GAE/C,MAAMQ,EAAe,IAAM,CACzB,MAAMC,EAAUhB,EAAA,EAChBQ,EAAQQ,CAAO,EACfN,EAAW,SAAS,WAAWM,CAAO,CAAA,EAGxC,cAAO,iBAAiB,aAAcD,CAAY,EAC3C,IAAM,CACX,OAAO,oBAAoB,aAAcA,CAAY,CAAA,CACvD,EACC,CAACV,EAASC,CAAI,CAAC,EAEX,CAACC,EAAMK,CAAG,CACnB"}
|
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
import { throttle as
|
|
3
|
-
const
|
|
4
|
-
const l =
|
|
1
|
+
import { useState as u, useRef as d, useEffect as v } from "react";
|
|
2
|
+
import { throttle as f } from "../../utils/helpers/throttle.mjs";
|
|
3
|
+
const b = (...e) => {
|
|
4
|
+
const l = typeof e[0] == "number" ? e[0] : e[0]?.delay ?? 1e3, o = typeof e[0] == "function" ? e[0] : e[0]?.onChange, i = e[0]?.enabled ?? !0, [t, r] = u({
|
|
5
5
|
interval: 0,
|
|
6
6
|
rotationRate: { alpha: null, beta: null, gamma: null },
|
|
7
7
|
acceleration: { x: null, y: null, z: null },
|
|
8
8
|
accelerationIncludingGravity: { x: null, y: null, z: null }
|
|
9
|
-
}),
|
|
10
|
-
return
|
|
11
|
-
if (!
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
interval:
|
|
9
|
+
}), a = d(o);
|
|
10
|
+
return a.current = o, v(() => {
|
|
11
|
+
if (!i) return;
|
|
12
|
+
const c = f((n) => {
|
|
13
|
+
a.current?.(n), r({
|
|
14
|
+
interval: n.interval,
|
|
15
15
|
rotationRate: {
|
|
16
|
-
...
|
|
17
|
-
...
|
|
16
|
+
...t.rotationRate,
|
|
17
|
+
...n.rotationRate
|
|
18
18
|
},
|
|
19
19
|
acceleration: {
|
|
20
|
-
...
|
|
21
|
-
...
|
|
20
|
+
...t.acceleration,
|
|
21
|
+
...n.acceleration
|
|
22
22
|
},
|
|
23
23
|
accelerationIncludingGravity: {
|
|
24
|
-
...
|
|
25
|
-
...
|
|
24
|
+
...t.accelerationIncludingGravity,
|
|
25
|
+
...n.accelerationIncludingGravity
|
|
26
26
|
}
|
|
27
27
|
});
|
|
28
|
-
},
|
|
29
|
-
return window.addEventListener("devicemotion",
|
|
30
|
-
window.removeEventListener("devicemotion",
|
|
28
|
+
}, l);
|
|
29
|
+
return window.addEventListener("devicemotion", c), () => {
|
|
30
|
+
window.removeEventListener("devicemotion", c);
|
|
31
31
|
};
|
|
32
|
-
}, [
|
|
32
|
+
}, [l, i]), t;
|
|
33
33
|
};
|
|
34
34
|
export {
|
|
35
|
-
|
|
35
|
+
b as useDeviceMotion
|
|
36
36
|
};
|
|
37
37
|
//# sourceMappingURL=useDeviceMotion.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDeviceMotion.mjs","sources":["../../../../src/hooks/useDeviceMotion/useDeviceMotion.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { throttle } from '@/utils/helpers';\n\nexport interface UseDeviceMotionReturn {\n acceleration: DeviceMotionEventAcceleration;\n accelerationIncludingGravity: DeviceMotionEventAcceleration;\n interval: DeviceMotionEvent['interval'];\n rotationRate: DeviceMotionEventRotationRate;\n}\n\nexport interface
|
|
1
|
+
{"version":3,"file":"useDeviceMotion.mjs","sources":["../../../../src/hooks/useDeviceMotion/useDeviceMotion.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nimport { throttle } from '@/utils/helpers';\n\nexport interface UseDeviceMotionReturn {\n acceleration: DeviceMotionEventAcceleration;\n accelerationIncludingGravity: DeviceMotionEventAcceleration;\n interval: DeviceMotionEvent['interval'];\n rotationRate: DeviceMotionEventRotationRate;\n}\n\nexport interface UseDeviceMotionOptions {\n /** The delay in milliseconds */\n delay?: number;\n /** Whether to enable the hook */\n enabled?: boolean;\n /** The callback function to be invoked */\n onChange?: (event: DeviceMotionEvent) => void;\n}\n\nexport interface UseDeviceMotion {\n (callback?: (event: DeviceMotionEvent) => void, delay?: number): UseDeviceMotionReturn;\n\n (options?: UseDeviceMotionOptions): UseDeviceMotionReturn;\n}\n\n/**\n * @name useDeviceMotion\n * @description - Hook that work with device motion\n * @category Sensors\n * @usage low\n *\n * @browserapi DeviceMotionEvent https://developer.mozilla.org/en-US/docs/Web/API/Window/DeviceMotionEvent\n *\n * @overload\n * @param {number} [delay=1000] The delay in milliseconds\n * @param {(event: DeviceMotionEvent) => void} [callback] The callback function to be invoked\n * @returns {UseDeviceMotionReturn} The device motion data and interval\n *\n * @example\n * const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion(500, (event) => console.log(event));\n *\n * @overload\n * @param {(event: DeviceMotionEvent) => void} [callback] The callback function to be invoked\n * @returns {UseDeviceMotionReturn} The device motion data and interval\n *\n * @example\n * const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion((event) => console.log(event));\n *\n * @overload\n * @param {UseDeviceMotionOptions} [options] Configuration options\n * @param {number} [options.delay] The delay in milliseconds\n * @param {boolean} [options.enabled] Whether to enable the hook\n * @param {(event: DeviceMotionEvent) => void} [options.onChange] The callback function to be invoked\n * @returns {UseDeviceMotionReturn} The device motion data and interval\n *\n * @example\n * const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion();\n */\nexport const useDeviceMotion = ((...params: any[]) => {\n const delay = typeof params[0] === 'number' ? params[0] : (params[0]?.delay ?? 1000);\n const callback = typeof params[0] === 'function' ? params[0] : params[0]?.onChange;\n const enabled = params[0]?.enabled ?? true;\n\n const [value, setValue] = useState<UseDeviceMotionReturn>({\n interval: 0,\n rotationRate: { alpha: null, beta: null, gamma: null },\n acceleration: { x: null, y: null, z: null },\n accelerationIncludingGravity: { x: null, y: null, z: null }\n });\n const internalCallbackRef = useRef(callback);\n internalCallbackRef.current = callback;\n\n useEffect(() => {\n if (!enabled) return;\n\n const onDeviceMotion = throttle<[DeviceMotionEvent]>((event) => {\n internalCallbackRef.current?.(event);\n\n setValue({\n interval: event.interval,\n rotationRate: {\n ...value.rotationRate,\n ...event.rotationRate\n },\n acceleration: {\n ...value.acceleration,\n ...event.acceleration\n },\n accelerationIncludingGravity: {\n ...value.accelerationIncludingGravity,\n ...event.accelerationIncludingGravity\n }\n });\n }, delay);\n\n window.addEventListener('devicemotion', onDeviceMotion);\n\n return () => {\n window.removeEventListener('devicemotion', onDeviceMotion);\n };\n }, [delay, enabled]);\n\n return value;\n}) as UseDeviceMotion;\n"],"names":["useDeviceMotion","params","delay","callback","enabled","value","setValue","useState","internalCallbackRef","useRef","useEffect","onDeviceMotion","throttle","event"],"mappings":";;AA2DO,MAAMA,IAAmB,IAAIC,MAAkB;AACpD,QAAMC,IAAQ,OAAOD,EAAO,CAAC,KAAM,WAAWA,EAAO,CAAC,IAAKA,EAAO,CAAC,GAAG,SAAS,KACzEE,IAAW,OAAOF,EAAO,CAAC,KAAM,aAAaA,EAAO,CAAC,IAAIA,EAAO,CAAC,GAAG,UACpEG,IAAUH,EAAO,CAAC,GAAG,WAAW,IAEhC,CAACI,GAAOC,CAAQ,IAAIC,EAAgC;AAAA,IACxD,UAAU;AAAA,IACV,cAAc,EAAE,OAAO,MAAM,MAAM,MAAM,OAAO,KAAA;AAAA,IAChD,cAAc,EAAE,GAAG,MAAM,GAAG,MAAM,GAAG,KAAA;AAAA,IACrC,8BAA8B,EAAE,GAAG,MAAM,GAAG,MAAM,GAAG,KAAA;AAAA,EAAK,CAC3D,GACKC,IAAsBC,EAAON,CAAQ;AAC3C,SAAAK,EAAoB,UAAUL,GAE9BO,EAAU,MAAM;AACd,QAAI,CAACN,EAAS;AAEd,UAAMO,IAAiBC,EAA8B,CAACC,MAAU;AAC9D,MAAAL,EAAoB,UAAUK,CAAK,GAEnCP,EAAS;AAAA,QACP,UAAUO,EAAM;AAAA,QAChB,cAAc;AAAA,UACZ,GAAGR,EAAM;AAAA,UACT,GAAGQ,EAAM;AAAA,QAAA;AAAA,QAEX,cAAc;AAAA,UACZ,GAAGR,EAAM;AAAA,UACT,GAAGQ,EAAM;AAAA,QAAA;AAAA,QAEX,8BAA8B;AAAA,UAC5B,GAAGR,EAAM;AAAA,UACT,GAAGQ,EAAM;AAAA,QAAA;AAAA,MACX,CACD;AAAA,IAAA,GACAX,CAAK;AAER,kBAAO,iBAAiB,gBAAgBS,CAAc,GAE/C,MAAM;AACX,aAAO,oBAAoB,gBAAgBA,CAAc;AAAA,IAAA;AAAA,EAC3D,GACC,CAACT,GAAOE,CAAO,CAAC,GAEZC;AACT;"}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
import { useState as
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
t.current =
|
|
5
|
-
const
|
|
6
|
-
window.location.hash =
|
|
1
|
+
import { useState as a, useRef as w, useEffect as g } from "react";
|
|
2
|
+
const f = () => decodeURIComponent(window.location.hash.replace("#", "")), p = (...e) => {
|
|
3
|
+
const c = typeof e[0] == "string" ? e[0] : "", o = typeof e[1] == "object" ? e[1] : typeof e[1] == "function" ? { onChange: e[1] } : typeof e[0] == "object" ? e[0] : {}, s = o?.enabled ?? !0, h = o?.mode ?? "replace", [i, r] = a(() => typeof window > "u" ? c : f() || c), t = w(o);
|
|
4
|
+
t.current = o;
|
|
5
|
+
const u = (n) => {
|
|
6
|
+
window.location.hash = n, r(n), t.current?.onChange?.(n);
|
|
7
7
|
};
|
|
8
|
-
return
|
|
9
|
-
if (!
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
|
|
8
|
+
return g(() => {
|
|
9
|
+
if (!s) return;
|
|
10
|
+
h === "replace" && (window.location.hash = i);
|
|
11
|
+
const n = () => {
|
|
12
|
+
const d = f();
|
|
13
|
+
r(d), t.current?.onChange?.(d);
|
|
14
14
|
};
|
|
15
|
-
return window.addEventListener("hashchange",
|
|
16
|
-
window.removeEventListener("hashchange",
|
|
15
|
+
return window.addEventListener("hashchange", n), () => {
|
|
16
|
+
window.removeEventListener("hashchange", n);
|
|
17
17
|
};
|
|
18
|
-
}, [
|
|
18
|
+
}, [s, h]), [i, u];
|
|
19
19
|
};
|
|
20
20
|
export {
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
f as getHash,
|
|
22
|
+
p as useHash
|
|
23
23
|
};
|
|
24
24
|
//# sourceMappingURL=useHash.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useHash.mjs","sources":["../../../../src/hooks/useHash/useHash.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nexport const getHash = () => decodeURIComponent(window.location.hash.replace('#', ''));\n\n/** The use hash options type */\nexport interface UseHashOptions {\n /** The enabled state of the hook */\n enabled?: boolean;\n /** The mode of hash setting */\n mode?: 'initial' | 'replace';\n /** Callback function called when hash changes */\n onChange?: (hash: string) => void;\n}\n\n/** The use hash return type */\ntype UseHashReturn = [string, (value: string) => void];\n\nexport interface UseHash {\n (initialValue?: string, options?: UseHashOptions): UseHashReturn;\n\n (initialValue?: string, callback?: (hash: string) => void): UseHashReturn;\n}\n\n/**\n * @name useHash\n * @description - Hook that manages the hash value\n * @category State\n * @usage low\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {UseHashOptions} [options] Configuration options\n * @param {boolean} [options.enabled] The enabled state of the hook\n * @param {'initial' | 'replace'} [options.mode] The mode of hash setting\n * @param {(hash: string) => void} [options.onChange] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", {\n * enabled: true,\n * mode: \"replace\",\n * onChange: (newHash) => console.log('Hash changed:', newHash)\n * });\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {(hash: string) => void} [callback] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", (newHash) => console.log('
|
|
1
|
+
{"version":3,"file":"useHash.mjs","sources":["../../../../src/hooks/useHash/useHash.ts"],"sourcesContent":["import { useEffect, useRef, useState } from 'react';\n\nexport const getHash = () => decodeURIComponent(window.location.hash.replace('#', ''));\n\n/** The use hash options type */\nexport interface UseHashOptions {\n /** The enabled state of the hook */\n enabled?: boolean;\n /** The mode of hash setting */\n mode?: 'initial' | 'replace';\n /** Callback function called when hash changes */\n onChange?: (hash: string) => void;\n}\n\n/** The use hash return type */\ntype UseHashReturn = [string, (value: string) => void];\n\nexport interface UseHash {\n (initialValue?: string, options?: UseHashOptions): UseHashReturn;\n\n (options?: UseHashOptions): UseHashReturn;\n\n (initialValue?: string, callback?: (hash: string) => void): UseHashReturn;\n\n (callback?: (hash: string) => void): UseHashReturn;\n}\n\n/**\n * @name useHash\n * @description - Hook that manages the hash value\n * @category State\n * @usage low\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {UseHashOptions} [options] Configuration options\n * @param {boolean} [options.enabled] The enabled state of the hook\n * @param {'initial' | 'replace'} [options.mode] The mode of hash setting\n * @param {(hash: string) => void} [options.onChange] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", {\n * enabled: true,\n * mode: \"replace\",\n * onChange: (newHash) => console.log('Hash changed:', newHash)\n * });\n *\n * @overload\n * @param {string} [initialValue] The initial hash value if no hash exists\n * @param {(hash: string) => void} [callback] Callback function called when hash changes\n * @returns {UseHashReturn} An array containing the hash value and a function to set the hash value\n *\n * @example\n * const [hash, setHash] = useHash(\"initial\", (newHash) => console.log('callback'));\n */\nexport const useHash = ((...params: any[]) => {\n const initialValue = typeof params[0] === 'string' ? params[0] : '';\n const options =\n typeof params[1] === 'object'\n ? params[1]\n : typeof params[1] === 'function'\n ? { onChange: params[1] }\n : typeof params[0] === 'object'\n ? params[0]\n : {};\n\n const enabled = options?.enabled ?? true;\n const mode = options?.mode ?? 'replace';\n\n const [hash, setHash] = useState(() => {\n if (typeof window === 'undefined') return initialValue;\n return getHash() || initialValue;\n });\n\n const optionsRef = useRef(options);\n optionsRef.current = options;\n\n const set = (value: string) => {\n window.location.hash = value;\n setHash(value);\n optionsRef.current?.onChange?.(value);\n };\n\n useEffect(() => {\n if (!enabled) return;\n\n if (mode === 'replace') window.location.hash = hash;\n\n const onHashChange = () => {\n const newHash = getHash();\n setHash(newHash);\n optionsRef.current?.onChange?.(newHash);\n };\n\n window.addEventListener('hashchange', onHashChange);\n return () => {\n window.removeEventListener('hashchange', onHashChange);\n };\n }, [enabled, mode]);\n\n return [hash, set] as const;\n}) as UseHash;\n"],"names":["getHash","useHash","params","initialValue","options","enabled","mode","hash","setHash","useState","optionsRef","useRef","set","value","useEffect","onHashChange","newHash"],"mappings":";AAEO,MAAMA,IAAU,MAAM,mBAAmB,OAAO,SAAS,KAAK,QAAQ,KAAK,EAAE,CAAC,GAsDxEC,IAAW,IAAIC,MAAkB;AAC5C,QAAMC,IAAe,OAAOD,EAAO,CAAC,KAAM,WAAWA,EAAO,CAAC,IAAI,IAC3DE,IACJ,OAAOF,EAAO,CAAC,KAAM,WACjBA,EAAO,CAAC,IACR,OAAOA,EAAO,CAAC,KAAM,aACnB,EAAE,UAAUA,EAAO,CAAC,EAAA,IACpB,OAAOA,EAAO,CAAC,KAAM,WACnBA,EAAO,CAAC,IACR,CAAA,GAEJG,IAAUD,GAAS,WAAW,IAC9BE,IAAOF,GAAS,QAAQ,WAExB,CAACG,GAAMC,CAAO,IAAIC,EAAS,MAC3B,OAAO,SAAW,MAAoBN,IACnCH,OAAaG,CACrB,GAEKO,IAAaC,EAAOP,CAAO;AACjC,EAAAM,EAAW,UAAUN;AAErB,QAAMQ,IAAM,CAACC,MAAkB;AAC7B,WAAO,SAAS,OAAOA,GACvBL,EAAQK,CAAK,GACbH,EAAW,SAAS,WAAWG,CAAK;AAAA,EAAA;AAGtC,SAAAC,EAAU,MAAM;AACd,QAAI,CAACT,EAAS;AAEd,IAAIC,MAAS,cAAW,OAAO,SAAS,OAAOC;AAE/C,UAAMQ,IAAe,MAAM;AACzB,YAAMC,IAAUhB,EAAA;AAChB,MAAAQ,EAAQQ,CAAO,GACfN,EAAW,SAAS,WAAWM,CAAO;AAAA,IAAA;AAGxC,kBAAO,iBAAiB,cAAcD,CAAY,GAC3C,MAAM;AACX,aAAO,oBAAoB,cAAcA,CAAY;AAAA,IAAA;AAAA,EACvD,GACC,CAACV,GAASC,CAAI,CAAC,GAEX,CAACC,GAAMK,CAAG;AACnB;"}
|
|
@@ -4,13 +4,17 @@ export interface UseDeviceMotionReturn {
|
|
|
4
4
|
interval: DeviceMotionEvent['interval'];
|
|
5
5
|
rotationRate: DeviceMotionEventRotationRate;
|
|
6
6
|
}
|
|
7
|
-
export interface
|
|
7
|
+
export interface UseDeviceMotionOptions {
|
|
8
8
|
/** The delay in milliseconds */
|
|
9
9
|
delay?: number;
|
|
10
10
|
/** Whether to enable the hook */
|
|
11
11
|
enabled?: boolean;
|
|
12
12
|
/** The callback function to be invoked */
|
|
13
|
-
|
|
13
|
+
onChange?: (event: DeviceMotionEvent) => void;
|
|
14
|
+
}
|
|
15
|
+
export interface UseDeviceMotion {
|
|
16
|
+
(callback?: (event: DeviceMotionEvent) => void, delay?: number): UseDeviceMotionReturn;
|
|
17
|
+
(options?: UseDeviceMotionOptions): UseDeviceMotionReturn;
|
|
14
18
|
}
|
|
15
19
|
/**
|
|
16
20
|
* @name useDeviceMotion
|
|
@@ -20,12 +24,29 @@ export interface UseDeviceMotionParams {
|
|
|
20
24
|
*
|
|
21
25
|
* @browserapi DeviceMotionEvent https://developer.mozilla.org/en-US/docs/Web/API/Window/DeviceMotionEvent
|
|
22
26
|
*
|
|
27
|
+
* @overload
|
|
23
28
|
* @param {number} [delay=1000] The delay in milliseconds
|
|
24
29
|
* @param {(event: DeviceMotionEvent) => void} [callback] The callback function to be invoked
|
|
25
|
-
* @
|
|
30
|
+
* @returns {UseDeviceMotionReturn} The device motion data and interval
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion(500, (event) => console.log(event));
|
|
34
|
+
*
|
|
35
|
+
* @overload
|
|
36
|
+
* @param {(event: DeviceMotionEvent) => void} [callback] The callback function to be invoked
|
|
37
|
+
* @returns {UseDeviceMotionReturn} The device motion data and interval
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion((event) => console.log(event));
|
|
41
|
+
*
|
|
42
|
+
* @overload
|
|
43
|
+
* @param {UseDeviceMotionOptions} [options] Configuration options
|
|
44
|
+
* @param {number} [options.delay] The delay in milliseconds
|
|
45
|
+
* @param {boolean} [options.enabled] Whether to enable the hook
|
|
46
|
+
* @param {(event: DeviceMotionEvent) => void} [options.onChange] The callback function to be invoked
|
|
26
47
|
* @returns {UseDeviceMotionReturn} The device motion data and interval
|
|
27
48
|
*
|
|
28
49
|
* @example
|
|
29
50
|
* const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion();
|
|
30
51
|
*/
|
|
31
|
-
export declare const useDeviceMotion:
|
|
52
|
+
export declare const useDeviceMotion: UseDeviceMotion;
|
|
@@ -12,7 +12,9 @@ export interface UseHashOptions {
|
|
|
12
12
|
type UseHashReturn = [string, (value: string) => void];
|
|
13
13
|
export interface UseHash {
|
|
14
14
|
(initialValue?: string, options?: UseHashOptions): UseHashReturn;
|
|
15
|
+
(options?: UseHashOptions): UseHashReturn;
|
|
15
16
|
(initialValue?: string, callback?: (hash: string) => void): UseHashReturn;
|
|
17
|
+
(callback?: (hash: string) => void): UseHashReturn;
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
20
|
* @name useHash
|
|
@@ -41,7 +43,7 @@ export interface UseHash {
|
|
|
41
43
|
* @returns {UseHashReturn} An array containing the hash value and a function to set the hash value
|
|
42
44
|
*
|
|
43
45
|
* @example
|
|
44
|
-
* const [hash, setHash] = useHash("initial", (newHash) => console.log('
|
|
46
|
+
* const [hash, setHash] = useHash("initial", (newHash) => console.log('callback'));
|
|
45
47
|
*/
|
|
46
48
|
export declare const useHash: UseHash;
|
|
47
49
|
export {};
|