/* row.js — horizontal carousel row. row({ title, moreHref?, variant: 'poster'|'wide'|'logo'|'cast', items?: array, load?: () => Promise, render: (item, i) => Node, lazy = true, skeletons = 8, className? }) →
with .setItems(items) for live refreshes. - Loads when it nears the viewport (IntersectionObserver) and shows skeletons meanwhile. - Removes itself (hidden) when it ends up empty or the load fails. - Arrow buttons on pointer devices; scroll-snap + swipe on touch. - Keyboard: ←/→ move focus between cards, Home/End jump to the ends. */ import { h } from '../util.js'; import { skeletonCard } from './card.js'; import { icon } from './icons.js'; const io = 'IntersectionObserver' in window ? new IntersectionObserver((entries) => { for (const e of entries) { if (e.isIntersecting && e.target._load) { io.unobserve(e.target); e.target._load(); } } }, { rootMargin: '600px 0px' }) : null; export function row(opts) { const variant = opts.variant || 'poster'; const track = h('div.row__track', { role: 'list', 'aria-label': opts.title || '' }); const prev = h('button.row__arrow.row__arrow--prev', { type: 'button', 'aria-label': 'Scroll left', tabindex: '-1' }, icon('left', { size: 22 })); const next = h('button.row__arrow.row__arrow--next', { type: 'button', 'aria-label': 'Scroll right', tabindex: '-1' }, icon('right', { size: 22 })); const titleEl = opts.title ? h('h2.row__title', opts.title) : null; const header = opts.title || opts.moreHref ? h('div.row__head', titleEl, opts.moreHref ? h('a.row__more', { href: opts.moreHref }, 'See all', icon('right', { size: 16 })) : null) : null; const section = h('section.row', { class: [`row--${variant}`, opts.className] }, header, h('div.row__viewport', prev, track, next)); for (let i = 0; i < (opts.skeletons || (variant === 'wide' ? 5 : 8)); i++) track.appendChild(skeletonCard(variant === 'cast' ? 'cast' : variant)); function updateArrows() { const max = track.scrollWidth - track.clientWidth - 2; prev.classList.toggle('is-hidden', track.scrollLeft <= 2); next.classList.toggle('is-hidden', track.scrollLeft >= max); } function scrollByPage(dir) { const amount = Math.max(track.clientWidth * 0.85, 200); track.scrollBy({ left: dir * amount, behavior: 'smooth' }); } prev.addEventListener('click', () => scrollByPage(-1)); next.addEventListener('click', () => scrollByPage(1)); track.addEventListener('scroll', updateArrows, { passive: true }); if ('ResizeObserver' in window) new ResizeObserver(updateArrows).observe(track); track.addEventListener('keydown', (e) => { const links = [...track.querySelectorAll(':scope > .card > .card__link, :scope > .card > [data-focus]')]; const i = links.indexOf(document.activeElement); if (i < 0) return; let j = -1; if (e.key === 'ArrowRight') j = Math.min(links.length - 1, i + 1); else if (e.key === 'ArrowLeft') j = Math.max(0, i - 1); else if (e.key === 'Home') j = 0; else if (e.key === 'End') j = links.length - 1; if (j < 0) return; e.preventDefault(); links[j].focus({ preventScroll: true }); links[j].scrollIntoView({ inline: 'nearest', block: 'nearest', behavior: 'smooth' }); }); section.setItems = (items) => { const list = (items || []).filter(Boolean); if (!list.length) { section.hidden = true; track.replaceChildren(); return; } section.hidden = false; track.replaceChildren(...list.map((it, i) => opts.render(it, i)).filter(Boolean)); section.classList.add('is-loaded'); requestAnimationFrame(updateArrows); }; const load = async () => { try { const items = opts.items || (opts.load ? await opts.load() : []); section.setItems(items); } catch (e) { console.warn(`row "${opts.title}" failed:`, e); section.hidden = true; } }; if (opts.items && opts.lazy !== true) { section.setItems(opts.items); } else if (io && opts.lazy !== false) { section._load = load; io.observe(section); } else { load(); } return section; }