storebound/site/main.js

186 lines
8.4 KiB
JavaScript

// storebound.dev: copy buttons, AI tool tabs, the hero demo and the How it works stepper. No network, no storage.
(() => {
const copyText = async text => {
try { await navigator.clipboard.writeText(text); return true; } catch {}
const ta = document.createElement('textarea');
ta.value = text; ta.setAttribute('readonly', ''); ta.style.position = 'fixed'; ta.style.opacity = '0';
document.body.appendChild(ta); ta.select();
let ok = false; try { ok = document.execCommand('copy'); } catch {}
ta.remove(); return ok;
};
for (const btn of document.querySelectorAll('.copy')) {
btn.addEventListener('click', async () => {
const target = btn.dataset.copyTarget && document.getElementById(btn.dataset.copyTarget);
// Drop "# comment" tails so the copied commands run as is.
const text = btn.dataset.copy ?? (target ? target.innerText.split('\n').map(l => l.replace(/\s+#.*$/, '')).join('\n').trim() : '');
const ok = await copyText(text);
btn.textContent = ok ? 'Copied' : 'Press Ctrl+C';
btn.classList.toggle('done', ok);
setTimeout(() => { btn.textContent = 'Copy'; btn.classList.remove('done'); }, 1600);
});
}
// Teaser: click to play (nothing loads before that); native controls take over afterwards.
for (const root of document.querySelectorAll('[data-player]')) {
const video = root.querySelector('video');
const btn = root.querySelector('.play');
video.controls = false;
btn.hidden = false;
btn.addEventListener('click', () => {
btn.hidden = true;
video.controls = true;
video.play().catch(() => {});
video.focus();
});
}
for (const root of document.querySelectorAll('[data-tabs]')) {
const tabs = [...root.querySelectorAll('[role="tab"]')];
const select = (tab, focus) => {
for (const t of tabs) {
const on = t === tab;
t.setAttribute('aria-selected', String(on));
t.tabIndex = on ? 0 : -1;
document.getElementById(t.getAttribute('aria-controls')).hidden = !on;
}
if (focus) { tab.focus(); tab.scrollIntoView({ block: 'nearest', inline: 'nearest' }); }
};
tabs.forEach((t, i) => {
t.addEventListener('click', () => select(t, false));
t.addEventListener('keydown', e => {
const k = { ArrowRight: i + 1, ArrowLeft: i - 1, Home: 0, End: tabs.length - 1 }[e.key];
if (k === undefined) return;
e.preventDefault();
select(tabs[(k + tabs.length) % tabs.length], true);
});
});
select(tabs.find(t => t.getAttribute('aria-selected') === 'true') ?? tabs[0], false);
}
const reduce = matchMedia('(prefers-reduced-motion: reduce)');
// Hero demo: types out an example session, loops, pauses off-screen. Reduced motion keeps the static final frame.
const demo = document.querySelector('.demo');
if (demo && !reduce.matches) {
const items = [...demo.querySelectorAll('[data-d]')];
let visible = false;
new IntersectionObserver(es => { visible = es[es.length - 1].isIntersecting; }, { threshold: 0.2 }).observe(demo);
const wait = ms => new Promise(done => {
let left = ms;
const tick = () => {
const step = Math.min(left, 40);
setTimeout(() => { if (visible && !document.hidden) left -= step; if (left <= 0) done(); else tick(); }, step);
};
tick();
});
demo.classList.add('is-anim');
(async () => {
for (;;) {
for (const el of items) el.classList.remove('on', 'typing');
demo.classList.remove('fade');
await wait(600);
for (const el of items) {
const kind = el.dataset.d;
if (kind === 'cmd') {
const n = el.querySelector('span').textContent.length;
el.style.setProperty('--n', '0');
el.classList.add('typing', 'on');
await wait(320);
for (let i = 1; i <= n; i++) { el.style.setProperty('--n', String(i)); await wait(22 + Math.random() * 34); }
await wait(300);
el.classList.remove('typing');
} else {
el.classList.add('on');
await wait(kind === 'say' ? 750 : kind === 'end' ? 0 : 240);
}
}
await wait(5200);
demo.classList.add('fade');
await wait(450);
}
})();
}
// How it works: on wide screens a vertical tab list with one shared preview, auto-advancing while in view
// until the visitor picks a step. On narrow screens a plain timeline with every step's preview inline.
const steps = document.querySelector('[data-steps]');
if (steps) {
const items = [...steps.children];
const heads = items.map(li => li.querySelector('.st-head'));
const panels = items.map(li => li.querySelector('.st-panel'));
const wide = matchMedia('(min-width: 1024px)');
const STEP_MS = 6500;
steps.style.setProperty('--step-ms', STEP_MS + 'ms');
let cur = 0, auto = !reduce.matches, timer = 0, inView = false, held = false;
const stopTimer = () => { clearTimeout(timer); timer = 0; };
const schedule = () => {
stopTimer();
const run = wide.matches && auto && inView && !held && !document.hidden;
steps.classList.toggle('is-auto', auto && wide.matches);
steps.classList.toggle('is-paused', !run);
if (run) timer = setTimeout(() => show((cur + 1) % heads.length, false, true), STEP_MS);
};
const restartBar = () => { steps.classList.remove('is-auto'); void steps.offsetWidth; };
const show = (i, focus, animate) => {
cur = i;
heads.forEach((h, k) => {
const on = k === i;
h.setAttribute('aria-selected', String(on));
h.tabIndex = on ? 0 : -1;
panels[k].hidden = !on;
if (on && animate) { panels[k].classList.remove('enter'); void panels[k].offsetWidth; panels[k].classList.add('enter'); }
});
if (focus) heads[i].focus();
restartBar();
schedule();
};
const resume = () => { if (wide.matches && auto) show(cur, false, false); };
const pick = (i, focus) => { auto = false; show(i, focus, true); };
heads.forEach((h, i) => {
h.addEventListener('click', () => { if (wide.matches) pick(i, false); });
h.addEventListener('keydown', e => {
if (!wide.matches) return;
const k = { ArrowDown: i + 1, ArrowRight: i + 1, ArrowUp: i - 1, ArrowLeft: i - 1, Home: 0, End: heads.length - 1 }[e.key];
if (k === undefined) return;
e.preventDefault();
pick((k + heads.length) % heads.length, true);
});
});
for (const b of steps.querySelectorAll('[data-next]')) b.addEventListener('click', () => pick(Number(b.dataset.next), true));
steps.addEventListener('pointerenter', () => { held = true; schedule(); });
steps.addEventListener('pointerleave', () => { held = false; resume(); });
steps.addEventListener('focusin', () => { held = true; schedule(); });
steps.addEventListener('focusout', e => { if (!steps.contains(e.relatedTarget)) { held = false; resume(); } });
document.addEventListener('visibilitychange', schedule);
new IntersectionObserver(es => { inView = es[es.length - 1].isIntersecting; resume(); }, { threshold: 0.35 }).observe(steps);
const layout = () => {
if (wide.matches) {
steps.setAttribute('role', 'tablist');
steps.setAttribute('aria-orientation', 'vertical');
steps.setAttribute('aria-label', 'Steps');
items.forEach((li, k) => {
li.setAttribute('role', 'presentation');
heads[k].setAttribute('role', 'tab');
heads[k].setAttribute('aria-controls', panels[k].id);
panels[k].setAttribute('role', 'tabpanel');
panels[k].setAttribute('aria-labelledby', heads[k].id);
panels[k].tabIndex = 0;
});
for (const f of steps.querySelectorAll('.st-foot')) f.hidden = false;
show(cur, false, false);
} else {
for (const f of steps.querySelectorAll('.st-foot')) f.hidden = true;
stopTimer();
for (const a of ['role', 'aria-orientation', 'aria-label']) steps.removeAttribute(a);
items.forEach((li, k) => {
li.removeAttribute('role');
for (const a of ['role', 'aria-controls', 'aria-selected', 'tabindex']) heads[k].removeAttribute(a);
for (const a of ['role', 'aria-labelledby', 'tabindex']) panels[k].removeAttribute(a);
panels[k].hidden = false;
});
steps.classList.remove('is-auto', 'is-paused');
}
};
wide.addEventListener('change', layout);
layout();
}
})();