docs: add copy buttons (#3393)
This commit is contained in:
@@ -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");
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user