import { ChevronLeft, ChevronRight } from 'lucide-react'; import { useEffect, useState } from 'react'; import { useStorefront } from '../context'; import { storefrontText } from '../text'; /** How long each message stays, in milliseconds. */ const ROTATE_EVERY = 4500; /** * The slim bar above the header: free shipping, running automatic discounts and the merchant's own lines. * Several messages take turns. They stop moving while the shopper points at the bar or uses the arrows, * and never move for shoppers whose device asks for less motion. */ export function AnnouncementBar() { const { shop, language } = useStorefront(); const t = storefrontText(language); const messages = shop.announcements; const [index, setIndex] = useState(0); const [paused, setPaused] = useState(false); useEffect(() => { const calm = window.matchMedia( '(prefers-reduced-motion: reduce)', ).matches; if (messages.length < 2 || paused || calm) { return; } const timer = setInterval( () => setIndex((current) => (current + 1) % messages.length), ROTATE_EVERY, ); return () => clearInterval(timer); }, [messages.length, paused]); if (messages.length === 0) { return null; } const current = messages[index % messages.length]; const step = (by: number) => { setPaused(true); setIndex((value) => (value + by + messages.length) % messages.length); }; return (
{current}
{messages.length > 1 ? ( ) : ( )}