"use client"; import { RichText } from "@payloadcms/richtext-lexical/react"; import { motion, useScroll, useTransform } from "motion/react"; import { useRef, useEffect, useState, useCallback } from "react"; import * as d3 from "d3"; import { HomeHero } from "@payload-types"; export default function HeroIntro({ title, desc, }: { title: string; desc: HomeHero["heroGroup"]["heroDescription"]; }) { const containerRef = useRef(null); const { scrollYProgress } = useScroll({ target: containerRef, offset: ["start end", "start start"], }); const titleY = useTransform(scrollYProgress, [0, 1], [-100, 0]); const paraOpacity = useTransform(scrollYProgress, [0.8, 1], [0, 1]); const paraY = useTransform(scrollYProgress, [0.8, 1], [20, 0]); const [windowSize, setWindowSize] = useState<[number, number]>(); useEffect(() => { window.addEventListener("resize", () => { setWindowSize([window.innerWidth, window.innerHeight]); }); setWindowSize([window.innerWidth, window.innerHeight]); }, []); const [beamSize, setBeamSize] = useState(0); scrollYProgress.on("change", (e) => setBeamSize(e.valueOf())); const drawBeam = useCallback(() => { if (!windowSize) return; const isSmall = windowSize[0] < 1124; const beam: [number, number][] = [ [0, 1 - beamSize / 2], // modify over scroll [0.9, 0.1], [isSmall ? 0.7 : beamSize / 2, 1], // modify over scroll [0, 1], ]; const xScale = d3.scaleLinear().domain([0, 1]).range([0, windowSize[0]]); const yScale = d3.scaleLinear().domain([0, 1]).range([0, windowSize[1]]); const drawArea = d3 .area() .x((p) => xScale(p[0])) .y0(() => 0) .y1((p) => yScale(p[1])); return drawArea(beam); }, [windowSize, beamSize]); return (
{drawBeam && ( )}
{title}
); }