Files
Christian Svensson c22c20e21f docs: avoid showing "Copied" if copying fails (#3397)
I noticed that in denoland/fresh#3393 it looks like it's possible that
the "Copied"/Check icon will be shown even if copying to clipboard
fails.

This PR changes it so that the Check icon only shows if copy succeeds.
2025-09-11 21:37:31 +02:00

28 lines
656 B
TypeScript

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;
try {
await navigator.clipboard.writeText(code);
el.dataset.copied = "true";
setTimeout(() => {
delete el.dataset.copied;
}, 1000);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
// deno-lint-ignore no-console
console.error(message || "Copy failed");
}
});