forked from Talenttrust/Talenttrust-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCopyToClipboard.ts
More file actions
81 lines (70 loc) · 2.98 KB
/
Copy pathuseCopyToClipboard.ts
File metadata and controls
81 lines (70 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { useState, useEffect, useRef, useCallback } from 'react';
export interface UseCopyToClipboardOptions {
/** Delay in milliseconds to reset the `copied` state. Defaults to 2000ms. */
delay?: number;
/** Callback triggered when the copy operation succeeds. */
onSuccess?: () => void;
/** Callback triggered when the copy operation fails. Passed the error object or reason. */
onError?: (error: unknown) => void;
}
/**
* A custom React hook that manages copying text to the system clipboard.
*
* It provides:
* - A stateful indicator (`copied`) showing whether the copy succeeded.
* - An automatic reset timer that clears the `copied` state after a configurable delay.
* - Proper cleanup of timers on component unmount or subsequent copy triggers.
* - Safety guards for SSR environments and browsers without clipboard support.
* - Success and failure callbacks for custom event notifications (e.g., toasts).
*
* @param options - Configuration options for the hook.
* @returns An object containing:
* - `copied`: boolean indicating if the text has been successfully copied within the delay window.
* - `copy`: a function accepting a string to copy to the clipboard. Returns a promise resolving to `true` on success and `false` on failure.
*/
export function useCopyToClipboard(options: UseCopyToClipboardOptions = {}) {
const { delay = 2000, onSuccess, onError } = options;
const [copied, setCopied] = useState(false);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const cleanUp = useCallback(() => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
}, []);
const copy = useCallback(async (text: string): Promise<boolean> => {
// 1. Guard against Server-Side Rendering (SSR) environments
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
const error = new Error('Clipboard API is not available in SSR environments');
onError?.(error);
return false;
}
// 2. Guard against missing or restricted navigator.clipboard API
if (!navigator.clipboard || !navigator.clipboard.writeText) {
const error = new Error('Clipboard API is not supported in this environment');
onError?.(error);
return false;
}
// 3. Clear any existing reset timer (handles rapid consecutive copy triggers)
cleanUp();
try {
await navigator.clipboard.writeText(text);
setCopied(true);
// 4. Schedule the copied state to reset back to false
timeoutRef.current = setTimeout(() => {
setCopied(false);
}, delay);
onSuccess?.();
return true;
} catch (err) {
// 5. Treat the copy error gracefully and do not leak sensitive parameters to logs
onError?.(err);
return false;
}
}, [delay, onSuccess, onError, cleanUp]);
// 6. Clean up timer on unmount to prevent state updates on unmounted components
useEffect(() => {
return cleanUp;
}, [cleanUp]);
return { copied, copy };
}