forked from denoland/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.client.ts
More file actions
31 lines (24 loc) · 807 Bytes
/
copy.client.ts
File metadata and controls
31 lines (24 loc) · 807 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
document.addEventListener("click", (event) => {
const btn = (event.target as HTMLElement).closest("button[data-copy]");
if (!btn) {
return;
}
let textToCopy = btn.getAttribute("data-copy") as string;
// CLEAN COMMANDS: Remove leading spaces, $, and > from each line
textToCopy = textToCopy.replace(/^[\$>\s]+/, "");
navigator?.clipboard?.writeText(textToCopy).then(() => {
if (!btn) {
return;
}
const copyIcon = btn.querySelector(".copy-icon");
const checkIcon = btn.querySelector(".check-icon");
if (copyIcon && checkIcon) {
copyIcon.classList.add("hidden");
checkIcon.classList.remove("hidden");
setTimeout(() => {
copyIcon.classList.remove("hidden");
checkIcon.classList.add("hidden");
}, 2000);
}
});
});