import { useEffect, useRef, FC } from "react"; import { gsap } from "gsap"; import SpotlightCard from "./SpotlightCard"; import { useNavigate } from "react-router-dom"; export interface ProjectItem { id: string; img: string; link: string; title?: string; description?: string; } interface HorizontalMotionProps { items: ProjectItem[]; gradientColor?: string; } const HorizontalMotion: FC = ({ items = [], gradientColor = "transparent", }) => { const containerRef = useRef(null); const rowRefs = useRef<(HTMLDivElement | null)[]>([]); const mouseXRef = useRef(window.innerWidth / 2); const navigate = useNavigate(); const handleCardClick = (link: string) => { if (!link) return; const cleanLink = link.replace("_blank", "").trim(); if (link.includes("_blank") || cleanLink.startsWith("http")) { window.open(cleanLink, "_blank", "noopener,noreferrer"); } else { navigate(cleanLink); } }; useEffect(() => { gsap.ticker.lagSmoothing(0); const handleMouseMove = (e: MouseEvent): void => { mouseXRef.current = e.clientX; }; const updateMotion = (): void => { // Sur mobile, on désactive ou réduit fortement le mouvement pour éviter les bugs de scroll const isMobile = window.innerWidth < 768; const maxMoveAmount = isMobile ? 30 : 150; const baseDuration = 0.8; const inertiaFactors = [0.6, 0.4, 0.3, 0.2]; rowRefs.current.forEach((row, index) => { if (row) { const direction = index % 2 === 0 ? 1 : -1; const moveAmount = ((mouseXRef.current / window.innerWidth) * maxMoveAmount - maxMoveAmount / 2) * direction; gsap.to(row, { x: moveAmount, duration: baseDuration + inertiaFactors[index % inertiaFactors.length], ease: "power3.out", overwrite: "auto", }); } }); }; const removeAnimationLoop = gsap.ticker.add(updateMotion); window.addEventListener("mousemove", handleMouseMove); return () => { window.removeEventListener("mousemove", handleMouseMove); removeAnimationLoop(); }; }, []); if (items.length === 0) return null; return (
{items.map((item, index) => (
{ if (el) rowRefs.current[index] = el; }} >
handleCardClick(item.link)} >

{item.title}

{item.description}

))}
); }; export default HorizontalMotion;