@vezham/hooks 26.3.1 → 26.3.2
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/README.md
CHANGED
|
@@ -45,4 +45,7 @@ const CopyButton = ({ text }: { text: string }) => {
|
|
|
45
45
|
|
|
46
46
|
Uses the Clipboard API when available and falls back to a temporary selection for
|
|
47
47
|
restricted origins. The hook exposes the last copy result as `status`, `isCopied`,
|
|
48
|
-
and `isUnavailable`.
|
|
48
|
+
and `isUnavailable`. Successful feedback resets from `copied` to `idle` after
|
|
49
|
+
two seconds; another successful copy restarts that delay. Failure feedback stays
|
|
50
|
+
`unavailable` until a later copy succeeds. Pending results from older attempts
|
|
51
|
+
cannot overwrite a newer result, and feedback timers are cleared on unmount.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/use-copy-to-clipboard/index.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,QAAQ,GAAG,aAAa,CAAA;AAErE,eAAO,MAAM,kBAAkB;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/use-copy-to-clipboard/index.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,qBAAqB,GAAG,MAAM,GAAG,QAAQ,GAAG,aAAa,CAAA;AAErE,eAAO,MAAM,kBAAkB;iBAiBS,MAAM;;;;CA6B7C,CAAA"}
|
|
@@ -1,18 +1,37 @@
|
|
|
1
1
|
import { writeClipboardText } from "./write-clipboard-text.js";
|
|
2
|
-
import { useCallback, useState } from "react";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
3
|
//#region src/lib/use-copy-to-clipboard/index.ts
|
|
4
4
|
var useCopyToClipboard = () => {
|
|
5
5
|
const [status, setStatus] = useState("idle");
|
|
6
|
+
const resetTimer = useRef(void 0);
|
|
7
|
+
const latestCopy = useRef(0);
|
|
8
|
+
const mounted = useRef(false);
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
mounted.current = true;
|
|
11
|
+
return () => {
|
|
12
|
+
mounted.current = false;
|
|
13
|
+
latestCopy.current += 1;
|
|
14
|
+
clearTimeout(resetTimer.current);
|
|
15
|
+
};
|
|
16
|
+
}, []);
|
|
6
17
|
return {
|
|
7
18
|
copy: useCallback(async (text) => {
|
|
19
|
+
const attempt = ++latestCopy.current;
|
|
20
|
+
let copied;
|
|
8
21
|
try {
|
|
9
22
|
await writeClipboardText(text);
|
|
10
|
-
|
|
11
|
-
return true;
|
|
23
|
+
copied = true;
|
|
12
24
|
} catch {
|
|
13
|
-
|
|
14
|
-
return false;
|
|
25
|
+
copied = false;
|
|
15
26
|
}
|
|
27
|
+
if (!mounted.current || attempt !== latestCopy.current) return copied;
|
|
28
|
+
clearTimeout(resetTimer.current);
|
|
29
|
+
setStatus(copied ? "copied" : "unavailable");
|
|
30
|
+
if (copied) resetTimer.current = setTimeout(() => {
|
|
31
|
+
resetTimer.current = void 0;
|
|
32
|
+
setStatus("idle");
|
|
33
|
+
}, 2e3);
|
|
34
|
+
return copied;
|
|
16
35
|
}, []),
|
|
17
36
|
status,
|
|
18
37
|
isCopied: status === "copied",
|