-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathcopy-page.ts
More file actions
46 lines (43 loc) · 1.57 KB
/
copy-page.ts
File metadata and controls
46 lines (43 loc) · 1.57 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Primary "Copy page" button — directly copies URL
document.querySelectorAll<HTMLButtonElement>(".copy-page-main-btn").forEach(
(btn) => {
btn.addEventListener("click", () => {
navigator?.clipboard?.writeText(window.location.href).then(() => {
const label = btn.querySelector<HTMLElement>(".copy-page-main-label");
if (label) {
const original = label.textContent;
label.textContent = "Copied!";
setTimeout(() => {
label.textContent = original;
}, 2000);
}
}).catch(() => {
const label = btn.querySelector<HTMLElement>(".copy-page-main-label");
if (label) {
const original = label.textContent;
label.textContent = "Copy failed";
setTimeout(() => {
label.textContent = original;
}, 2000);
}
});
});
},
);
// Popover panel — position below the chevron button + rotate chevron
const panel = document.getElementById("copy-page-menu") as HTMLElement | null;
const toggleBtn = document.querySelector<HTMLButtonElement>(
".copy-page-toggle-btn",
);
panel?.addEventListener("toggle", (event) => {
const e = event as ToggleEvent;
const chevron = toggleBtn?.querySelector<SVGElement>(".copy-page-chevron");
if (e.newState === "open" && toggleBtn) {
const rect = toggleBtn.getBoundingClientRect();
panel.style.top = `${rect.bottom + 4}px`;
panel.style.right = `${window.innerWidth - rect.right}px`;
}
if (chevron) {
chevron.style.transform = e.newState === "open" ? "rotate(180deg)" : "";
}
});