Files
AndreaSlopFotografie/web/src/components/Hero/useHeroSlider.ts
T
SluijsensandClaude Opus 5.5 614707bfff Builds the hero slider with arrows, swipe, and social icons
Replaces the placeholder hero with a crossfading slider (slow zoom, 6s per
slide) driven by the progress bar animation; it pauses on mouse hover,
keyboard focus, and hidden tabs, and skips autoplay with reduced motion.
Adds prev/next arrows and touch swipe, and Facebook/Instagram icons from a
fixed set of platforms using bundled Iconify data (@iconify/react/offline
and @iconify-icons/ant-design, so no runtime API calls).

Content (tagline, title, CTA, slides, socials) is plain data in
heroContent.ts and passed in as a prop, so it can come from the CMS API
later. The tagline sits inside the h1 together with the name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-13 23:12:42 +02:00

75 lines
3.1 KiB
TypeScript

import { useEffect, useRef, useState } from 'react';
import { useMediaQuery } from '@chakra-ui/react';
// Minimale horizontale afstand in px voordat een veeg als swipe telt
const SWIPE_THRESHOLD = 50;
// Houdt de actieve slide bij en of autoplay mag lopen. Het doorschuiven zelf
// gebeurt via de animatie van de voortgangsbalk (zie Hero.tsx), zodat pauzeren
// simpelweg die animatie stilzet.
export default function useHeroSlider(count: number) {
const [index, setIndex] = useState(0);
const [hovered, setHovered] = useState(false);
const [focused, setFocused] = useState(false);
const [hidden, setHidden] = useState(() => document.hidden);
const [reducedMotion] = useMediaQuery(['(prefers-reduced-motion: reduce)'], {
ssr: false,
});
const touchStart = useRef<{ x: number; y: number } | null>(null);
useEffect(() => {
const onVisibilityChange = () => setHidden(document.hidden);
document.addEventListener('visibilitychange', onVisibilityChange);
return () => document.removeEventListener('visibilitychange', onVisibilityChange);
}, []);
// Als de API minder slides teruggeeft dan de huidige index, begin vooraan.
const active = index < count ? index : 0;
const next = () => setIndex((active + 1) % count);
const prev = () => setIndex((active - 1 + count) % count);
return {
active,
reducedMotion,
autoplay: count > 1 && !reducedMotion,
paused: hovered || focused || hidden,
next,
prev,
handlers: {
// Alleen een echte muis pauzeert; een tik op mobiel niet.
onPointerEnter: (event: React.PointerEvent<HTMLElement>) => {
if (event.pointerType === 'mouse') setHovered(true);
},
onPointerLeave: () => setHovered(false),
// Alleen toetsenbordfocus pauzeert, niet een klik op een pijl.
onFocus: (event: React.FocusEvent<HTMLElement>) => {
if (event.target.matches(':focus-visible')) setFocused(true);
},
onBlur: (event: React.FocusEvent<HTMLElement>) => {
if (!event.currentTarget.contains(event.relatedTarget)) {
setFocused(false);
}
},
onTouchStart: (event: React.TouchEvent<HTMLElement>) => {
const touch = event.touches[0];
touchStart.current = { x: touch.clientX, y: touch.clientY };
},
onTouchEnd: (event: React.TouchEvent<HTMLElement>) => {
const start = touchStart.current;
touchStart.current = null;
if (!start || count < 2) return;
const touch = event.changedTouches[0];
const dx = touch.clientX - start.x;
const dy = touch.clientY - start.y;
// Horizontaal genoeg, en niet bedoeld als verticaal scrollen
if (Math.abs(dx) < SWIPE_THRESHOLD || Math.abs(dx) < Math.abs(dy)) return;
if (dx < 0) next();
else prev();
},
},
};
}