// The home hero controller — the collection ledger lights each collection in // turn on a loop. Point at a deck (or tab to it) and it takes the loop over; // leave and the loop resumes. The stat counters are rolled up separately by // initCounters() in app.ts (shared with the platform stats below the fold). // // Everything the hero shows is in the server-rendered markup, so with JS off it // is a static, fully-legible ledger. This only adds the cycling, which collapses // to a single lit deck under prefers-reduced-motion. Bound once per element and // torn down on view-transition navigations. export function initHero(): void { const hero = document.querySelector('[data-hero]'); if (!hero || hero.dataset.heroBound) return; hero.dataset.heroBound = '1'; const reduced = matchMedia('(prefers-reduced-motion: reduce)').matches || document.documentElement.dataset.motion === 'reduce'; const rows = Array.from(hero.querySelectorAll('[data-hero-row]')); const blurbEl = hero.querySelector('[data-hero-blurb]'); if (!rows.length) return; let active = 0; let paused = false; let timer = 0; const light = (i: number): void => { active = ((i % rows.length) + rows.length) % rows.length; rows.forEach((row, j) => { const on = j === active; row.classList.toggle('is-lit', on); if (on) row.setAttribute('aria-current', 'true'); else row.removeAttribute('aria-current'); }); const blurb = rows[active].dataset.blurb; if (blurbEl && blurb) blurbEl.textContent = blurb; }; const stop = (): void => { if (timer) { clearInterval(timer); timer = 0; } }; const start = (): void => { if (reduced) return; stop(); timer = window.setInterval(() => { if (!paused) light(active + 1); }, 3000); }; rows.forEach((row, i) => { const take = (): void => { paused = true; light(i); }; const release = (): void => { paused = false; }; row.addEventListener('mouseenter', take); row.addEventListener('focusin', take); row.addEventListener('mouseleave', release); row.addEventListener('focusout', release); }); // Pause the loop while the tab is hidden so it does not silently advance // through several decks in the background and jump on return. const onVis = (): void => { if (document.hidden) stop(); else start(); }; document.addEventListener('visibilitychange', onVis); light(0); start(); document.addEventListener('astro:before-swap', () => { stop(); document.removeEventListener('visibilitychange', onVis); }, { once: true }); }