mirror of
https://github.com/Radiquum/AniX.git
synced 2025-04-05 15:54:39 +00:00
46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
// hooks/useCopyToClipboard.js
|
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
function useCopyToClipboard() {
|
|
const [isCopied, setIsCopied] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (isCopied) {
|
|
setTimeout(() => {
|
|
setIsCopied(false);
|
|
}, 2500);
|
|
}
|
|
}, [isCopied]);
|
|
|
|
async function copyToClipboard(text) {
|
|
if (!navigator.clipboard) {
|
|
// Clipboard API not supported
|
|
try {
|
|
// Fallback for older browsers (like Firefox before v49)
|
|
const input = document.createElement("input");
|
|
document.body.appendChild(input);
|
|
input.setAttribute("value", window.location.href);
|
|
input.select();
|
|
document.execCommand("copy");
|
|
document.body.removeChild(input);
|
|
setIsCopied(true);
|
|
} catch (err) {
|
|
console.error("Failed to copy text: ", err);
|
|
setIsCopied(false);
|
|
}
|
|
} else {
|
|
try {
|
|
await navigator.clipboard.writeText(window.location.href);
|
|
setIsCopied(true);
|
|
} catch (err) {
|
|
console.error("Failed to copy text: ", err);
|
|
setIsCopied(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
return [isCopied, copyToClipboard];
|
|
}
|
|
|
|
export default useCopyToClipboard;
|