"use client"; import { animate } from "motion"; import { useEffect, useRef, useState } from "react"; import CurvyRect from "@/components/shared/layout/curvy-rect"; import { sleep } from "@/utils/sleep"; import BrowserMobile from "./_svg/BrowserMobile"; import BrowserTab from "./_svg/BrowserTab"; import HeroScrapingCode from "./Code/Code"; import HeroScrapingTag from "./Tag/Tag"; import "./HeroScraping.css"; export default function HeroScraping() { const [step, setStep] = useState(-1); const navigationRef = useRef(null); const buttonRef = useRef(null); const h1Ref = useRef(null); const descriptionRef = useRef(null); const ctaRef = useRef(null); const highlightRef = useRef(null); const containerRef = useRef(null); useEffect(() => { const wrapElement = async ( element: HTMLElement, { borderRadius }: { borderRadius?: number } = {}, ) => { if (!containerRef.current) return; const containerBnds = containerRef.current.getBoundingClientRect(); const elementBnds = element.getBoundingClientRect(); if (!highlightRef.current) return; try { if (highlightRef.current) { await animate(highlightRef.current, { opacity: 0 }, { duration: 0.3 }); } } catch (error) { console.error("Error animating highlight:", error); } if (!highlightRef.current) return; Object.assign(highlightRef.current.style, { left: elementBnds.left - containerBnds.left - 4 + "px", top: elementBnds.top - containerBnds.top - 4 + "px", width: elementBnds.width + 8 + "px", height: elementBnds.height + 8 + "px", borderRadius: borderRadius ? `${borderRadius}px` : undefined, }); try { await animate( highlightRef.current, { opacity: [1, 0.5, 0.3, 0.8, 0.6, 0.9, 0.7, 1] }, { duration: 0.4 }, ); } catch (error) { console.error("Error animating highlight:", error); } }; const start = async () => { setStep(0); if (!highlightRef.current) return; await animate(highlightRef.current, { scale: 1, opacity: 1, }); await sleep(700); setTimeout(() => setStep(1), 300); if (navigationRef.current) { await wrapElement(navigationRef.current); } await sleep(1200); setTimeout(() => setStep(2), 300); if (buttonRef.current) { await wrapElement(buttonRef.current); } await sleep(1200); setTimeout(() => setStep(3), 300); if (h1Ref.current) { await wrapElement(h1Ref.current, { borderRadius: 12 }); } await sleep(1200); setTimeout(() => setStep(4), 300); if (descriptionRef.current) { await wrapElement(descriptionRef.current, { borderRadius: 8 }); } await sleep(1200); setTimeout(() => setStep(5), 300); if (ctaRef.current) { await wrapElement(ctaRef.current, { borderRadius: 24 }); } await sleep(1500); setTimeout(() => setStep(6), 300); if (highlightRef.current) { await animate(highlightRef.current, { opacity: 0 }, { duration: 0.3 }); } }; let started = false; const onScroll = () => { if (started) return; if (window.scrollY > 100) { started = true; start(); window.removeEventListener("scroll", onScroll); } }; setTimeout(() => { if (started) return; started = true; start(); window.removeEventListener("scroll", onScroll); }, 2000); window.addEventListener("scroll", onScroll); return () => window.removeEventListener("scroll", onScroll); }, []); return (
{Array.from({ length: 3 }).map((_, index) => (
))}
{step >= 0 && ( )}
{step >= 1 && ( )} {Array.from({ length: 4 }).map((_, index) => (
))}
{step >= 2 && ( )}
{step >= 3 && ( )}
{step >= 4 && ( )}
{step >= 5 && ( )}
); }