@ttoss/react-hooks 2.0.2 → 2.0.3
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/esm/index.js +64 -0
- package/dist/index.d.ts +14 -0
- package/package.json +3 -3
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
|
|
2
|
+
|
|
3
|
+
// src/useScript.ts
|
|
4
|
+
import * as React from "react";
|
|
5
|
+
var useScript = src => {
|
|
6
|
+
const [status, setStatus] = React.useState(src ? "loading" : "idle");
|
|
7
|
+
React.useEffect(() => {
|
|
8
|
+
if (!src) {
|
|
9
|
+
setStatus("idle");
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
let script = document.querySelector(`script[src="${src}"]`);
|
|
13
|
+
if (!script) {
|
|
14
|
+
script = document.createElement("script");
|
|
15
|
+
script.src = src;
|
|
16
|
+
script.async = true;
|
|
17
|
+
script.setAttribute("data-status", "loading");
|
|
18
|
+
document.body.appendChild(script);
|
|
19
|
+
const setAttributeFromEvent = event => {
|
|
20
|
+
if (script) {
|
|
21
|
+
script.setAttribute("data-status", event.type === "load" ? "ready" : "error");
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
script.addEventListener("load", setAttributeFromEvent);
|
|
25
|
+
script.addEventListener("error", setAttributeFromEvent);
|
|
26
|
+
} else {
|
|
27
|
+
setStatus(script.getAttribute("data-status") || "error");
|
|
28
|
+
}
|
|
29
|
+
const setStateFromEvent = event => {
|
|
30
|
+
setStatus(event.type === "load" ? "ready" : "error");
|
|
31
|
+
};
|
|
32
|
+
script.addEventListener("load", setStateFromEvent);
|
|
33
|
+
script.addEventListener("error", setStateFromEvent);
|
|
34
|
+
return () => {
|
|
35
|
+
if (script) {
|
|
36
|
+
script.removeEventListener("load", setStateFromEvent);
|
|
37
|
+
script.removeEventListener("error", setStateFromEvent);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
/**
|
|
42
|
+
* Only re-run effect if script src changes.
|
|
43
|
+
*/
|
|
44
|
+
[src]);
|
|
45
|
+
return {
|
|
46
|
+
status
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/useDebounce.ts
|
|
51
|
+
import * as React2 from "react";
|
|
52
|
+
var useDebounce = (value, delay) => {
|
|
53
|
+
const [debouncedValue, setDebouncedValue] = React2.useState(value);
|
|
54
|
+
React2.useEffect(() => {
|
|
55
|
+
const timer = setTimeout(() => {
|
|
56
|
+
return setDebouncedValue(value);
|
|
57
|
+
}, delay || 500);
|
|
58
|
+
return () => {
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
};
|
|
61
|
+
}, [value, delay]);
|
|
62
|
+
return debouncedValue;
|
|
63
|
+
};
|
|
64
|
+
export { useDebounce, useScript };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type ScriptStatus = 'idle' | 'loading' | 'ready' | 'error';
|
|
2
|
+
/**
|
|
3
|
+
* https://usehooks.com/useScript/
|
|
4
|
+
*
|
|
5
|
+
* @param src: string - the url of the script to load.
|
|
6
|
+
* @returns status: Status
|
|
7
|
+
*/
|
|
8
|
+
declare const useScript: (src: string) => {
|
|
9
|
+
status: ScriptStatus;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
declare const useDebounce: <T>(value: T, delay?: number) => T;
|
|
13
|
+
|
|
14
|
+
export { type ScriptStatus, useDebounce, useScript };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/react-hooks",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.3",
|
|
4
4
|
"description": "React hooks.",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"jest": "^29.7.0",
|
|
31
31
|
"react": "^18.3.1",
|
|
32
32
|
"tsup": "^8.3.0",
|
|
33
|
-
"@ttoss/config": "^1.
|
|
34
|
-
"@ttoss/test-utils": "^2.1.
|
|
33
|
+
"@ttoss/config": "^1.34.0",
|
|
34
|
+
"@ttoss/test-utils": "^2.1.16"
|
|
35
35
|
},
|
|
36
36
|
"keywords": [
|
|
37
37
|
"React",
|