"use client"; import type { RefObject } from "react"; import { useGSAP } from "@gsap/react"; import { gsap } from "gsap"; import { MotionPathPlugin } from "gsap/dist/MotionPathPlugin"; import { ScrollTrigger } from "gsap/dist/ScrollTrigger"; import type { LabVariant } from "./content"; gsap.registerPlugin(useGSAP, ScrollTrigger, MotionPathPlugin); function revealOffset(direction: string | null) { if (direction === "left") return { x: -56, y: 0 }; if (direction === "right") return { x: 56, y: 0 }; if (direction === "scale") return { x: 0, y: 18, scale: 0.94 }; return { x: 0, y: 42 }; } export function useLabMotion(root: RefObject, variant: LabVariant) { useGSAP( () => { const select = gsap.utils.selector(root); const media = gsap.matchMedia(); media.add("(prefers-reduced-motion: no-preference)", () => { const heroTimeline = gsap.timeline({ defaults: { ease: "power3.out" } }); heroTimeline .from(select("[data-hero-item]"), { autoAlpha: 0, y: 34, duration: 0.72, stagger: 0.09, }) .from( select("[data-hero-visual]"), { autoAlpha: 0, y: 28, scale: 0.96, duration: 0.9 }, "-=0.5" ); select("[data-reveal]").forEach((element) => { const htmlElement = element as HTMLElement; const offset = revealOffset(htmlElement.getAttribute("data-reveal")); gsap.from(htmlElement, { autoAlpha: 0, ...offset, duration: 0.85, ease: "power3.out", clearProps: "transform,opacity,visibility", scrollTrigger: { trigger: htmlElement, start: "clamp(top 86%)", toggleActions: "play none none reverse", }, }); }); select("[data-stagger]").forEach((container) => { const children = Array.from(container.children); gsap.from(children, { autoAlpha: 0, y: 28, duration: 0.65, ease: "power3.out", stagger: { each: 0.07, from: "start" }, clearProps: "transform,opacity,visibility", scrollTrigger: { trigger: container, start: "clamp(top 84%)", toggleActions: "play none none reverse", }, }); }); select("[data-route-line]").forEach((line) => { const path = line as unknown as SVGPathElement; const length = path.getTotalLength(); gsap.set(path, { strokeDasharray: length, strokeDashoffset: length }); heroTimeline.to( path, { strokeDashoffset: 0, duration: 0.9, ease: "power2.inOut" }, "-=0.75" ); }); if (variant === "systemkarte") { heroTimeline.from( select("[data-map-node]"), { autoAlpha: 0, x: (index) => (index % 2 === 0 ? -36 : 36), y: (index) => (index % 3 === 0 ? -24 : 24), scale: 0.86, duration: 0.62, stagger: 0.055, }, "-=0.82" ); gsap.to(select("[data-signal-pulse]"), { scale: 1.5, autoAlpha: 0, duration: 2.4, ease: "sine.out", repeat: -1, stagger: 0.8, }); select("[data-system-lane]").forEach((lane, index) => { gsap.fromTo( lane, { xPercent: index % 2 === 0 ? -7 : 7 }, { xPercent: index % 2 === 0 ? 3 : -3, ease: "none", scrollTrigger: { trigger: lane, start: "top bottom", end: "bottom top", scrub: 1, }, } ); }); } if (variant === "werkstatt") { heroTimeline.from( select("[data-sheet]"), { autoAlpha: 0, x: (index) => (index % 2 === 0 ? -70 : 70), y: (index) => 36 + index * 8, rotation: (index) => (index - 1) * 5, duration: 0.82, stagger: 0.1, ease: "power3.out", }, "-=0.75" ); select("[data-editorial-track]").forEach((track, index) => { gsap.fromTo( track, { xPercent: index % 2 === 0 ? -5 : 5 }, { xPercent: index % 2 === 0 ? 5 : -5, ease: "none", scrollTrigger: { trigger: track, start: "top bottom", end: "bottom top", scrub: 1.1, }, } ); }); } if (variant === "cockpit") { heroTimeline.from( select("[data-cockpit-panel]"), { autoAlpha: 0, y: (index) => (index % 2 === 0 ? 34 : -26), x: (index) => (index % 3 === 0 ? -24 : 24), scale: 0.95, duration: 0.68, stagger: { each: 0.06, from: "center" }, }, "-=0.8" ); gsap.to(select("[data-live-dot]"), { autoAlpha: 0.25, scale: 1.65, duration: 1.25, ease: "sine.inOut", repeat: -1, yoyo: true, }); gsap.to(select("[data-timeline-marker]"), { xPercent: 620, ease: "none", scrollTrigger: { trigger: select("[data-cockpit-scroll]")[0], start: "top 80%", end: "bottom 30%", scrub: 0.8, }, }); } if (variant === "prozessfluss") { heroTimeline.from( select("[data-process-node]"), { autoAlpha: 0, scale: 0.72, y: 22, duration: 0.58, stagger: 0.1, }, "-=0.78" ); const heroPath = select("[data-journey-path]")[0] as unknown as SVGPathElement | undefined; const journeyDot = select("[data-journey-dot]")[0] as unknown as SVGElement | undefined; if (heroPath && journeyDot) { gsap.to(journeyDot, { motionPath: { path: heroPath, align: heroPath, alignOrigin: [0.5, 0.5], }, duration: 8, ease: "none", repeat: -1, }); } select("[data-flow-line]").forEach((line) => { const path = line as unknown as SVGPathElement; const length = path.getTotalLength(); gsap.set(path, { strokeDasharray: length, strokeDashoffset: length }); gsap.to(path, { strokeDashoffset: 0, ease: "none", scrollTrigger: { trigger: path.closest("section") ?? path, start: "top 72%", end: "bottom 58%", scrub: 0.8, }, }); }); } }); return () => media.revert(); }, { scope: root, dependencies: [variant], revertOnUpdate: true } ); }