/* SAB program-card share buttons — v1.0 (2026-10-03) * Injects a small share button into every .sab-card on the page. * Uses the Web Share API when available (mobile), otherwise copies the * card's "Learn more" link to the clipboard with a toast confirmation. * Loaded as an external file via wp_enqueue_script (see AGENTS.md: WordPress * entity-encodes inline shortcode JS, so this must stay external). */ (function () { "use strict"; function toast(msg) { var t = document.querySelector(".sab-toast"); if (!t) { t = document.createElement("div"); t.className = "sab-toast"; t.setAttribute("role", "status"); document.body.appendChild(t); } t.textContent = msg; t.classList.add("show"); clearTimeout(t._hideTimer); t._hideTimer = setTimeout(function () { t.classList.remove("show"); }, 2200); } function copyText(txt, cb) { function done() { if (cb) cb(); } if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(txt).then(done, function () { legacyCopy(txt); done(); }); } else { legacyCopy(txt); done(); } } function legacyCopy(txt) { var ta = document.createElement("textarea"); ta.value = txt; ta.style.position = "fixed"; ta.style.opacity = "0"; document.body.appendChild(ta); ta.select(); try { document.execCommand("copy"); } catch (e) { /* noop */ } document.body.removeChild(ta); } var SHARE_SVG = '"; function bind() { var cards = document.querySelectorAll(".sab-card"); for (var i = 0; i < cards.length; i++) { (function (card) { if (card.querySelector(".sab-share")) return; // already bound var h = card.querySelector("h3"); var title = h ? h.textContent.trim() : "San Antonio Benefits program"; var a = card.querySelector(".sab-learn"); var url = a && a.href ? a.href : window.location.href.split("#")[0]; var btn = document.createElement("button"); btn.type = "button"; btn.className = "sab-share"; btn.setAttribute("aria-label", "Share " + title); btn.title = "Share this program"; btn.innerHTML = SHARE_SVG; btn.addEventListener("click", function (ev) { ev.preventDefault(); ev.stopPropagation(); var text = title + " \u2014 free help in San Antonio"; if (navigator.share) { navigator.share({ title: title, text: text, url: url }).catch(function () { /* dismissed */ }); } else { copyText(url, function () { toast("Link copied \u2014 paste it anywhere to share"); }); } }); card.appendChild(btn); })(cards[i]); } } if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", bind); } else { bind(); } })();