|
2 | 2 | document.querySelectorAll<HTMLButtonElement>(".copy-page-main-btn").forEach( |
3 | 3 | (btn) => { |
4 | 4 | btn.addEventListener("click", () => { |
5 | | - navigator?.clipboard?.writeText(window.location.href).then(() => { |
| 5 | + navigator?.clipboard?.writeText(globalThis.location.href).then(() => { |
6 | 6 | const label = btn.querySelector<HTMLElement>(".copy-page-main-label"); |
7 | 7 | if (label) { |
8 | 8 | const original = label.textContent; |
@@ -31,14 +31,43 @@ const toggleBtn = document.querySelector<HTMLButtonElement>( |
31 | 31 | ".copy-page-toggle-btn", |
32 | 32 | ); |
33 | 33 |
|
| 34 | +const supportsAnchor = CSS.supports("anchor-name", "--a"); |
| 35 | + |
| 36 | +if (!supportsAnchor && panel) { |
| 37 | + panel.style.setProperty("position-anchor", "unset"); |
| 38 | + panel.style.setProperty("position-area", "unset"); |
| 39 | + panel.style.setProperty("position-try-fallbacks", "none"); |
| 40 | +} |
| 41 | + |
34 | 42 | panel?.addEventListener("toggle", (event) => { |
35 | 43 | const e = event as ToggleEvent; |
36 | 44 | const chevron = toggleBtn?.querySelector<SVGElement>(".copy-page-chevron"); |
37 | 45 |
|
38 | | - if (e.newState === "open" && toggleBtn) { |
39 | | - const rect = toggleBtn.getBoundingClientRect(); |
40 | | - panel.style.top = `${rect.bottom + 4}px`; |
41 | | - panel.style.right = `${window.innerWidth - rect.right}px`; |
| 46 | + const splitBtn = toggleBtn?.closest<HTMLElement>(".copy-page-split"); |
| 47 | + if (e.newState === "open" && splitBtn && toggleBtn) { |
| 48 | + // Only position via JS when CSS anchor positioning isn't supported |
| 49 | + if (!supportsAnchor) { |
| 50 | + // Use requestAnimationFrame to ensure the panel is rendered before measuring |
| 51 | + requestAnimationFrame(() => { |
| 52 | + // Use toggleBtn for vertical position (splitBtn may include popover in measurement) |
| 53 | + // Use splitBtn for horizontal alignment (left edge of whole button) |
| 54 | + const btnRect = toggleBtn.getBoundingClientRect(); |
| 55 | + const splitRect = splitBtn.getBoundingClientRect(); |
| 56 | + const panelWidth = panel.offsetWidth; |
| 57 | + |
| 58 | + const top = btnRect.bottom + 4; |
| 59 | + let right = globalThis.innerWidth - splitRect.right; |
| 60 | + |
| 61 | + // Clamp so the panel doesn't overflow the left edge |
| 62 | + const left = globalThis.innerWidth - right - panelWidth; |
| 63 | + if (left < 8) { |
| 64 | + right = globalThis.innerWidth - panelWidth - 8; |
| 65 | + } |
| 66 | + |
| 67 | + panel.style.top = `${top}px`; |
| 68 | + panel.style.right = `${right}px`; |
| 69 | + }); |
| 70 | + } |
42 | 71 | } |
43 | 72 | if (chevron) { |
44 | 73 | chevron.style.transform = e.newState === "open" ? "rotate(180deg)" : ""; |
|
0 commit comments