interface Options {
frame: HTMLElement; grid: HTMLElement; previousLabel: string; nextLabel: string;
onScroll?: () => void; onResize?: () => void;
}
/** Shared navigation for catalog rails. Cards and pagination stay with their owner. */
export const createCatalogCarousel = ({ frame, grid, previousLabel, nextLabel, onScroll, onResize }: Options) => {
const navigation = document.createElement('div'); navigation.className = 'fotogenic-ai-template-navigation';
const controls = [previousLabel, nextLabel].map((label, index) => {
const button = document.createElement('button'); button.type = 'button'; button.setAttribute('aria-label', label);
const icon = document.createElement('span'); icon.className = `fotogenic-ai-icon fotogenic-ai-icon--arrow-${index ? 'right' : 'left'} fotogenic-ai-icon--regular fotogenic-ai-icon--size-20`;
icon.setAttribute('aria-hidden', 'true'); button.append(icon);
button.addEventListener('click', () => grid.scrollTo({ left: grid.scrollLeft + (index ? 1 : -1) * grid.clientWidth,
behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'instant' : 'smooth' }));
navigation.append(button); return button;
});
const sync = (): void => { controls[0]!.disabled = grid.scrollLeft <= 1; controls[1]!.disabled = grid.scrollLeft + grid.clientWidth >= grid.scrollWidth - 1; };
const scroll = (): void => { sync(); onScroll?.(); };
grid.addEventListener('scroll', scroll, { passive: true });
const resize = new ResizeObserver(() => { onResize?.(); sync(); }); resize.observe(grid);
frame.append(navigation);
const scheduled = window.requestAnimationFrame(sync);
return { sync, destroy() { window.cancelAnimationFrame(scheduled); resize.disconnect(); grid.removeEventListener('scroll', scroll); navigation.remove(); } };
};