|
| 1 | +import { useEffect, useState, type CSSProperties } from 'react'; |
| 2 | +import { usePrefersReducedMotion } from '@/hooks/usePrefersReducedMotion'; |
| 3 | +import { cn } from '@/lib/utils'; |
| 4 | + |
| 5 | +type Photo = { jpg: string; webp: string }; |
| 6 | +type PanVector = { fromX: number; fromY: number; toX: number; toY: number }; |
| 7 | + |
| 8 | +const VISIBLE_MS = 8000; |
| 9 | +const CROSSFADE_MS = 1500; |
| 10 | +const LAYER_LIFETIME_MS = CROSSFADE_MS + VISIBLE_MS + CROSSFADE_MS; |
| 11 | +const PRELOAD_TIMEOUT_MS = 3000; |
| 12 | +const PAN_RANGE_PCT = 2; |
| 13 | + |
| 14 | +function shuffle<T>(arr: readonly T[]): T[] { |
| 15 | + const out = arr.slice(); |
| 16 | + for (let i = out.length - 1; i > 0; i--) { |
| 17 | + const j = Math.floor(Math.random() * (i + 1)); |
| 18 | + const tmp = out[i] as T; |
| 19 | + out[i] = out[j] as T; |
| 20 | + out[j] = tmp; |
| 21 | + } |
| 22 | + return out; |
| 23 | +} |
| 24 | + |
| 25 | +function randomVector(): PanVector { |
| 26 | + const r = () => (Math.random() * 2 - 1) * PAN_RANGE_PCT; |
| 27 | + return { fromX: r(), fromY: r(), toX: r(), toY: r() }; |
| 28 | +} |
| 29 | + |
| 30 | +function preload(url: string): Promise<void> { |
| 31 | + return new Promise((resolve) => { |
| 32 | + const img = new Image(); |
| 33 | + let settled = false; |
| 34 | + const done = () => { |
| 35 | + if (settled) return; |
| 36 | + settled = true; |
| 37 | + resolve(); |
| 38 | + }; |
| 39 | + img.onload = done; |
| 40 | + img.onerror = done; |
| 41 | + img.src = url; |
| 42 | + setTimeout(done, PRELOAD_TIMEOUT_MS); |
| 43 | + }); |
| 44 | +} |
| 45 | + |
| 46 | +interface PanLayerProps { |
| 47 | + photo: Photo; |
| 48 | + vector: PanVector; |
| 49 | + fadingIn: boolean; |
| 50 | + fadingOut: boolean; |
| 51 | + reducedMotion: boolean; |
| 52 | +} |
| 53 | + |
| 54 | +function PanLayer({ photo, vector, fadingIn, fadingOut, reducedMotion }: PanLayerProps) { |
| 55 | + const [appeared, setAppeared] = useState(!fadingIn); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + if (!fadingIn) return; |
| 59 | + const id = requestAnimationFrame(() => { |
| 60 | + requestAnimationFrame(() => setAppeared(true)); |
| 61 | + }); |
| 62 | + return () => cancelAnimationFrame(id); |
| 63 | + }, [fadingIn]); |
| 64 | + |
| 65 | + const opacity = fadingOut ? 0 : appeared ? 1 : 0; |
| 66 | + |
| 67 | + const style = { |
| 68 | + opacity, |
| 69 | + transition: `opacity ${CROSSFADE_MS}ms ease-in-out`, |
| 70 | + animation: reducedMotion ? 'none' : `hero-ken-burns ${LAYER_LIFETIME_MS}ms linear forwards`, |
| 71 | + transformOrigin: 'center center', |
| 72 | + willChange: 'transform, opacity', |
| 73 | + '--kb-from-x': `${vector.fromX}%`, |
| 74 | + '--kb-from-y': `${vector.fromY}%`, |
| 75 | + '--kb-to-x': `${vector.toX}%`, |
| 76 | + '--kb-to-y': `${vector.toY}%`, |
| 77 | + } as CSSProperties; |
| 78 | + |
| 79 | + return ( |
| 80 | + <picture className="absolute inset-0 block" style={style}> |
| 81 | + <source srcSet={photo.webp} type="image/webp" /> |
| 82 | + <img src={photo.jpg} alt="" className="w-full h-full object-cover" loading="eager" /> |
| 83 | + </picture> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +interface HeroSlideshowProps { |
| 88 | + className?: string; |
| 89 | +} |
| 90 | + |
| 91 | +export function HeroSlideshow({ className }: HeroSlideshowProps) { |
| 92 | + const reducedMotion = usePrefersReducedMotion(); |
| 93 | + const [photos, setPhotos] = useState<Photo[]>([]); |
| 94 | + const [tick, setTick] = useState(0); |
| 95 | + const [currentVector, setCurrentVector] = useState<PanVector>(randomVector); |
| 96 | + const [nextVector, setNextVector] = useState<PanVector | null>(null); |
| 97 | + |
| 98 | + useEffect(() => { |
| 99 | + let cancelled = false; |
| 100 | + fetch('/hero/manifest.json') |
| 101 | + .then((r) => (r.ok ? (r.json() as Promise<Photo[]>) : [])) |
| 102 | + .catch(() => [] as Photo[]) |
| 103 | + .then((data) => { |
| 104 | + if (cancelled) return; |
| 105 | + if (Array.isArray(data) && data.length > 0) { |
| 106 | + setPhotos(shuffle(data)); |
| 107 | + } |
| 108 | + }); |
| 109 | + return () => { |
| 110 | + cancelled = true; |
| 111 | + }; |
| 112 | + }, []); |
| 113 | + |
| 114 | + useEffect(() => { |
| 115 | + if (photos.length === 0) return; |
| 116 | + let cancelled = false; |
| 117 | + const timers: ReturnType<typeof setTimeout>[] = []; |
| 118 | + |
| 119 | + const visibleTimer = setTimeout(() => { |
| 120 | + if (cancelled) return; |
| 121 | + const nextIdx = (tick + 1) % photos.length; |
| 122 | + const nextPhoto = photos[nextIdx]; |
| 123 | + if (!nextPhoto) return; |
| 124 | + preload(nextPhoto.jpg).then(() => { |
| 125 | + if (cancelled) return; |
| 126 | + const v = randomVector(); |
| 127 | + setNextVector(v); |
| 128 | + const settleTimer = setTimeout(() => { |
| 129 | + if (cancelled) return; |
| 130 | + setCurrentVector(v); |
| 131 | + setNextVector(null); |
| 132 | + setTick((prev) => prev + 1); |
| 133 | + }, CROSSFADE_MS); |
| 134 | + timers.push(settleTimer); |
| 135 | + }); |
| 136 | + }, VISIBLE_MS); |
| 137 | + timers.push(visibleTimer); |
| 138 | + |
| 139 | + return () => { |
| 140 | + cancelled = true; |
| 141 | + timers.forEach(clearTimeout); |
| 142 | + }; |
| 143 | + }, [photos, tick]); |
| 144 | + |
| 145 | + if (photos.length === 0) { |
| 146 | + return <div className={cn('absolute inset-0 bg-neutral-900', className)} aria-hidden="true" />; |
| 147 | + } |
| 148 | + |
| 149 | + const currentPhoto = photos[tick % photos.length]; |
| 150 | + const nextPhoto = photos[(tick + 1) % photos.length]; |
| 151 | + if (!currentPhoto || !nextPhoto) return null; |
| 152 | + |
| 153 | + const transitioning = nextVector !== null; |
| 154 | + |
| 155 | + return ( |
| 156 | + <div className={cn('relative overflow-hidden', className)} aria-hidden="true"> |
| 157 | + <PanLayer |
| 158 | + key={tick} |
| 159 | + photo={currentPhoto} |
| 160 | + vector={currentVector} |
| 161 | + fadingIn={false} |
| 162 | + fadingOut={transitioning} |
| 163 | + reducedMotion={reducedMotion} |
| 164 | + /> |
| 165 | + {nextVector !== null && ( |
| 166 | + <PanLayer |
| 167 | + key={tick + 1} |
| 168 | + photo={nextPhoto} |
| 169 | + vector={nextVector} |
| 170 | + fadingIn |
| 171 | + fadingOut={false} |
| 172 | + reducedMotion={reducedMotion} |
| 173 | + /> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
0 commit comments