()
| 9 | // One pass through the perks, back to the first, then it rests. Hovering or |
| 10 | // focusing the row pauses it (WCAG 2.2.2). |
| 11 | export const PlusPerkTicker = (): ReactElement => { |
| 12 | const isReducedMotion = usePrefersReducedMotion(); |
| 13 | const [tick, setTick] = useState(0); |
| 14 | const [isPaused, setIsPaused] = useState(false); |
| 15 | const rootRef = useRef<HTMLSpanElement>(null); |
| 16 | const count = plusTickerPerks.length; |
| 17 | const isDone = tick >= count; |
| 18 | |
| 19 | useEffect(() => { |
| 20 | const row = rootRef.current?.closest('a, button'); |
| 21 | if (!row) { |
| 22 | return undefined; |
| 23 | } |
| 24 | |
| 25 | const pause = () => setIsPaused(true); |
| 26 | const resume = () => setIsPaused(false); |
| 27 | row.addEventListener('pointerenter', pause); |
| 28 | row.addEventListener('pointerleave', resume); |
| 29 | row.addEventListener('focusin', pause); |
| 30 | row.addEventListener('focusout', resume); |
| 31 | |
| 32 | return () => { |
| 33 | row.removeEventListener('pointerenter', pause); |
| 34 | row.removeEventListener('pointerleave', resume); |
| 35 | row.removeEventListener('focusin', pause); |
| 36 | row.removeEventListener('focusout', resume); |
| 37 | }; |
| 38 | }, []); |
| 39 | |
| 40 | useEffect(() => { |
| 41 | if (isReducedMotion || isPaused || isDone) { |
| 42 | return undefined; |
| 43 | } |
| 44 | |
| 45 | const interval = window.setInterval(() => { |
| 46 | if (document.visibilityState === 'visible') { |
| 47 | setTick((current) => current + 1); |
| 48 | } |
| 49 | }, TICK_INTERVAL_MS); |
| 50 | |
| 51 | return () => window.clearInterval(interval); |
| 52 | }, [isReducedMotion, isPaused, isDone]); |
| 53 | |
| 54 | const index = tick % count; |
| 55 | const previous = (tick - 1 + count) % count; |
| 56 | |
| 57 | return ( |
| 58 | <span |
| 59 | ref={rootRef} |
| 60 | aria-hidden |
| 61 | className="grid grid-cols-[minmax(0,1fr)] overflow-hidden" |
| 62 | > |
| 63 | {tick > 0 && ( |
| 64 | <span |
| 65 | key={`out-${tick}`} |
| 66 | className="plus-perk-out truncate [grid-area:1/1]" |
| 67 | > |
| 68 | {plusTickerPerks[previous]} |
nothing calls this directly
no test coverage detected