docs: add copy buttons (#3393)

This commit is contained in:
Marvin Hagemeister
2025-09-11 15:19:34 +02:00
committed by GitHub
parent a7af5c0c3d
commit 554644b240
7 changed files with 128 additions and 70 deletions
+26
View File
@@ -1 +1,27 @@
import "./assets/styles.css";
document.addEventListener("click", async (ev) => {
let el = ev.target as HTMLElement | null;
if (el === null) return;
if (!(el instanceof HTMLButtonElement)) {
el = el.closest("button");
}
if (el === null) return;
const code = el.dataset.code;
if (!code) return;
el.dataset.copied = "true";
setTimeout(() => {
delete el.dataset.copied;
}, 1000);
try {
await navigator.clipboard.writeText(code);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
// deno-lint-ignore no-console
console.error(message || "Copy failed");
}
});