(feedbackDuration = 2000)
| 32 | * React hook for managing clipboard copy state with feedback |
| 33 | */ |
| 34 | export const useCopyToClipboard = (feedbackDuration = 2000) => { |
| 35 | const [showCopyFeedback, setShowCopyFeedback] = useState(false) |
| 36 | const timeoutRef = useRef<NodeJS.Timeout | null>(null) |
| 37 | |
| 38 | const copyWithFeedback = useCallback( |
| 39 | async (text: string, e?: React.MouseEvent) => { |
| 40 | e?.stopPropagation() |
| 41 | |
| 42 | // Clear any existing timeout |
| 43 | if (timeoutRef.current) { |
| 44 | clearTimeout(timeoutRef.current) |
| 45 | } |
| 46 | |
| 47 | const success = await copyToClipboard(text, { |
| 48 | onSuccess: () => { |
| 49 | setShowCopyFeedback(true) |
| 50 | timeoutRef.current = setTimeout(() => { |
| 51 | setShowCopyFeedback(false) |
| 52 | timeoutRef.current = null |
| 53 | }, feedbackDuration) |
| 54 | }, |
| 55 | }) |
| 56 | |
| 57 | return success |
| 58 | }, |
| 59 | [feedbackDuration], |
| 60 | ) |
| 61 | |
| 62 | // Cleanup timeout on unmount |
| 63 | useEffect(() => { |
| 64 | return () => { |
| 65 | if (timeoutRef.current) { |
| 66 | clearTimeout(timeoutRef.current) |
| 67 | } |
| 68 | } |
| 69 | }, []) |
| 70 | |
| 71 | return { |
| 72 | showCopyFeedback, |
| 73 | copyWithFeedback, |
| 74 | } |
| 75 | } |
no test coverage detected