feat: implement clipboard utility for copying text and update components to use it

This commit is contained in:
Anders Böttcher
2026-05-13 17:19:43 +02:00
parent f7c52d0b1c
commit ffbbf1b6eb
3 changed files with 41 additions and 16 deletions
+3 -12
View File
@@ -1,6 +1,7 @@
"use client";
import { useState } from "react";
import { copyText } from "@/lib/clipboard";
export default function CopyLinkButton({ token }: { token: string }) {
const [copied, setCopied] = useState(false);
@@ -8,21 +9,11 @@ export default function CopyLinkButton({ token }: { token: string }) {
async function copy() {
const url = `${window.location.origin}/files/${token}`;
try {
await navigator.clipboard.writeText(url);
await copyText(url);
setCopied(true);
setTimeout(() => setCopied(false), 2500);
} catch {
// Fallback for browsers that deny clipboard access
const el = document.createElement("textarea");
el.value = url;
el.style.position = "fixed";
el.style.opacity = "0";
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
setCopied(true);
setTimeout(() => setCopied(false), 2500);
// copy completely unavailable (e.g. sandboxed iframe) — do nothing
}
}