mirror of
https://github.com/Radiquum/AniX.git
synced 2025-06-06 03:59:38 +05:00
feat(navigation): add a copy current url button to navbar for ease of sharing
This commit is contained in:
parent
6bd46e8437
commit
65b26613c5
2 changed files with 56 additions and 0 deletions
46
frontend/app/hooks/useCopyToClipboard.js
Normal file
46
frontend/app/hooks/useCopyToClipboard.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
// 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;
|
Loading…
Add table
Add a link
Reference in a new issue