import './style-carousel.css'; interface Choice { value: string; imageUrl?: string; description?: string } interface Options { select: HTMLSelectElement; label: string; choices?: Choice[]; previousLabel?: string; nextLabel?: string } /** A visual presentation of the same native select value and change contract. */ export const createStyleCarousel = ({ select, label, choices = [], previousLabel = 'Previous styles', nextLabel = 'Next styles' }: Options) => { const element = document.createElement('div'); element.className = 'fotogenic-ai-style-carousel'; const rail = document.createElement('div'); rail.className = 'fotogenic-ai-style-carousel__rail'; rail.setAttribute('role', 'group'); rail.setAttribute('aria-label', label); const navigation = document.createElement('div'); navigation.className = 'fotogenic-ai-style-carousel__navigation'; const arrow = (name: string, direction: number) => { const button = document.createElement('button'); button.type = 'button'; button.setAttribute('aria-label', name); const icon = document.createElement('span'); icon.className = `fotogenic-ai-icon fotogenic-ai-icon--arrow-${direction < 0 ? 'left' : 'right'} fotogenic-ai-icon--regular fotogenic-ai-icon--size-20`; icon.setAttribute('aria-hidden', 'true'); button.append(icon); button.addEventListener('click', () => rail.scrollBy({ left: direction * rail.clientWidth, behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'instant' : 'smooth' })); return button; }; const previous = arrow(previousLabel, -1); const next = arrow(nextLabel, 1); navigation.append(previous, next); select.hidden = true; select.tabIndex = -1; const buttons: HTMLButtonElement[] = []; const arrows = () => { previous.disabled = rail.scrollLeft <= 1; next.disabled = rail.scrollLeft + rail.clientWidth >= rail.scrollWidth - 1; }; const sync = () => { buttons.forEach((button, index) => { button.setAttribute('aria-pressed', String(index === select.selectedIndex)); button.disabled = select.disabled || Boolean(select.options[index]?.disabled); }); arrows(); }; const reveal = (event: FocusEvent) => { const button = event.currentTarget as HTMLButtonElement; const card = button.getBoundingClientRect(); const viewport = rail.getBoundingClientRect(); if (card.left < viewport.left || card.right > viewport.right) button.scrollIntoView({ block: 'nearest', inline: 'start', behavior: 'instant' }); }; [...select.options].forEach((option, index) => { const button = document.createElement('button'); button.type = 'button'; button.className = 'fotogenic-ai-style-carousel__card'; button.hidden = option.hidden; const choice = choices.find((item) => item.value === option.value); const preview = document.createElement('span'); preview.className = 'fotogenic-ai-style-carousel__preview'; if (!option.value) { preview.classList.add('is-automatic'); const icon = document.createElement('span'); icon.className = 'fotogenic-ai-icon fotogenic-ai-icon--image-square fotogenic-ai-icon--regular'; icon.setAttribute('aria-hidden', 'true'); preview.append(icon); } if (choice?.imageUrl && URL.canParse(choice.imageUrl, window.location.href)) { const url = new URL(choice.imageUrl, window.location.href); if ((url.protocol === 'https:' || url.origin === window.location.origin) && !url.username && !url.password) { const image = document.createElement('img'); image.src = url.href; image.alt = ''; image.loading = 'lazy'; image.decoding = 'async'; image.addEventListener('error', () => image.remove(), { once: true }); preview.append(image); } } const caption = document.createElement('span'); caption.textContent = option.textContent; button.title = choice?.description ?? ''; button.append(preview, caption); button.addEventListener('focus', reveal); button.addEventListener('click', () => { if (button.disabled || select.selectedIndex === index) return; select.selectedIndex = index; sync(); select.dispatchEvent(new Event('change', { bubbles: true })); }); buttons.push(button); rail.append(button); }); element.append(select, rail, navigation); select.addEventListener('change', sync); rail.addEventListener('scroll', arrows, { passive: true }); const resize = new ResizeObserver(arrows); resize.observe(rail); const mutation = new MutationObserver(sync); mutation.observe(select, { attributes: true, attributeFilter: ['disabled'] }); sync(); return { element, trigger: buttons[select.selectedIndex] ?? previous, sync, destroy: () => { resize.disconnect(); mutation.disconnect(); select.removeEventListener('change', sync); buttons.forEach(button => button.removeEventListener('focus', reveal)); } }; }; const runtime = window as Window & { FotogenicAI?: { Shared?: { ui?: { select?: { carousel?: typeof createStyleCarousel } } } } }; const selectApi = runtime.FotogenicAI?.Shared?.ui?.select; if (selectApi) selectApi.carousel = createStyleCarousel;