Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 1x 1x 1x 13x 13x 10x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 4x 4x 7x 5x 5x 9x 9x 9x 9x 9x 9x 9x 9x 4x 4x 9x 9x 9x 9x 4x 4x 4x 3x 6x 3x 6x 4x 4x 1x 1x 3x 3x 1x | // /find-us-badge copy buttons and the light/dark badge toggle.
//
// One delegated click listener on document, bound once at module load, so it
// survives ClientRouter view-transition navigations without per-page wiring
// (same pattern as pro-signup-cta.ts). Every copy click fires a GA4
// `badge_copy` event via GTM — see apps/marketplace/CLAUDE.md §Analytics.
import { pushDataLayerEvent } from "../lib/gtm";
const ROOT = "[data-find-us-badge]";
const FLASH_MS = 2000;
const flashTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>();
/** Clipboard API first; a hidden-textarea execCommand copy as the fallback. */
export async function copyText(text: string): Promise<boolean> {
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text);
return true;
}
} catch {
// Denied or unavailable (insecure context, older WebViews) — fall back.
}
return legacyCopy(text);
}
function legacyCopy(text: string): boolean {
const textarea = document.createElement("textarea");
textarea.value = text;
// readonly keeps the iOS keyboard closed; 16px stops iOS zooming in.
textarea.setAttribute("readonly", "");
textarea.style.cssText =
"position:fixed;top:0;left:-9999px;opacity:0;font-size:16px";
document.body.appendChild(textarea);
textarea.select();
textarea.setSelectionRange(0, text.length);
let ok = false;
try {
ok = document.execCommand("copy");
} catch {
ok = false;
}
textarea.remove();
return ok;
}
function closestInRoot<T extends HTMLElement>(
target: EventTarget | null,
selector: string,
): T | null {
if (!(target instanceof Element)) return null;
const el = target.closest<T>(selector);
return el?.closest(ROOT) ? el : null;
}
function flash(button: HTMLElement, message: string): void {
const label =
button.querySelector<HTMLElement>("[data-copy-label]") ?? button;
// Remember the resting label once, so rapid repeat clicks restore it.
label.dataset.label ??= label.textContent ?? "";
const original = label.dataset.label;
label.textContent = message;
const status = document.querySelector<HTMLElement>(
`${ROOT} [data-copy-status]`,
);
if (status) status.textContent = message;
clearTimeout(flashTimers.get(button));
flashTimers.set(
button,
setTimeout(() => {
label.textContent = original;
if (status) status.textContent = "";
}, FLASH_MS),
);
}
export async function handleCopy(button: HTMLElement): Promise<void> {
const root = button.closest<HTMLElement>(ROOT);
pushDataLayerEvent("badge_copy", {
snippet_type: button.dataset.snippetType,
pro_slug: root?.dataset.proSlug || "none",
page_path: window.location.pathname,
});
const ok = await copyText(button.dataset.copyText ?? "");
flash(button, ok ? "Copied" : "Couldn't copy");
}
export function selectBadgeTheme(button: HTMLElement): void {
const theme = button.dataset.badgeTheme;
const section = button.closest<HTMLElement>("[data-badge-section]");
if (!section) return;
for (const b of section.querySelectorAll("[data-badge-theme]")) {
b.setAttribute("aria-pressed", String(b === button));
}
for (const panel of section.querySelectorAll<HTMLElement>(
"[data-badge-panel]",
)) {
panel.hidden = panel.dataset.badgePanel !== theme;
}
}
export function handleClick(e: MouseEvent): void {
const copyButton = closestInRoot(e.target, "button[data-copy-text]");
if (copyButton) {
void handleCopy(copyButton);
return;
}
const themeButton = closestInRoot(e.target, "button[data-badge-theme]");
if (themeButton) selectBadgeTheme(themeButton);
}
document.addEventListener("click", handleClick);
|