2026-02-14 10:30:27 +01:00
|
|
|
/**
|
|
|
|
|
* HeroCarousel — crossfade between hero images on a timer.
|
|
|
|
|
*
|
|
|
|
|
* If only one image is provided, renders a static image with no timer.
|
|
|
|
|
* When multiple images are provided, crossfades between them every 6 seconds.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
|
|
|
import Image from 'next/image';
|
|
|
|
|
|
|
|
|
|
interface HeroImage {
|
2026-02-14 11:47:26 +01:00
|
|
|
src: string;
|
|
|
|
|
blurDataURL?: string;
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface HeroCarouselProps {
|
2026-02-14 11:47:26 +01:00
|
|
|
images: HeroImage[];
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const INTERVAL_MS = 6000;
|
|
|
|
|
|
|
|
|
|
export function HeroCarousel({ images }: HeroCarouselProps) {
|
2026-02-14 11:47:26 +01:00
|
|
|
const [currentIndex, setCurrentIndex] = useState(0);
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
const advance = useCallback(() => {
|
|
|
|
|
setCurrentIndex((prev) => (prev + 1) % images.length);
|
|
|
|
|
}, [images.length]);
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (images.length <= 1) return;
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
const timer = setInterval(advance, INTERVAL_MS);
|
|
|
|
|
return () => clearInterval(timer);
|
|
|
|
|
}, [images.length, advance]);
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
if (images.length === 0) {
|
|
|
|
|
return <div className="hero__image-placeholder" />;
|
|
|
|
|
}
|
2026-02-14 10:30:27 +01:00
|
|
|
|
2026-02-14 11:47:26 +01:00
|
|
|
if (images.length === 1) {
|
|
|
|
|
const img = images[0];
|
2026-02-14 10:30:27 +01:00
|
|
|
return (
|
2026-02-14 11:47:26 +01:00
|
|
|
<Image
|
|
|
|
|
src={img.src}
|
|
|
|
|
alt=""
|
|
|
|
|
className="hero__image"
|
|
|
|
|
fill
|
|
|
|
|
sizes="50vw"
|
|
|
|
|
priority
|
|
|
|
|
{...(img.blurDataURL ? { placeholder: 'blur' as const, blurDataURL: img.blurDataURL } : {})}
|
|
|
|
|
/>
|
2026-02-14 10:30:27 +01:00
|
|
|
);
|
2026-02-14 11:47:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{images.map((img, i) => (
|
|
|
|
|
<Image
|
|
|
|
|
key={i}
|
|
|
|
|
src={img.src}
|
|
|
|
|
alt=""
|
|
|
|
|
className={`hero__carousel-image${i === currentIndex ? ' hero__carousel-image--active' : ''}`}
|
|
|
|
|
fill
|
|
|
|
|
sizes="50vw"
|
|
|
|
|
priority={i === 0}
|
|
|
|
|
{...(img.blurDataURL
|
|
|
|
|
? { placeholder: 'blur' as const, blurDataURL: img.blurDataURL }
|
|
|
|
|
: {})}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</>
|
|
|
|
|
);
|
2026-02-14 10:30:27 +01:00
|
|
|
}
|