pages that arent "/" dont seem to show the motion.div with the transtitionDelay import {…
- User
pages that arent "/" dont seem to show the motion.div with the transtitionDelay import { motion } from "framer-motion"; import { useState, useEffect } from "react"; import { useRouter } from "next/router"; import ParticlesContainer from "./ParticlesContainer"; import { useGlobe } from '../components/globe/globeContext';
const Transition = () => { const [isLoaded, setIsLoaded] = useState(false); const router = useRouter(); const { globeReady } = useGlobe();
const baseDelay = router.pathname === '/' ? 0 : 0.2;
const transitionVariants = { initial: { x: "100%", width: "100%", }, animate: { x: "0%", width: "0%", }, exit: { x: ["0%", "100%"], width: ["0%", "100%"], }, };
useEffect(() => { // If we're on the homepage and globe is ready, set loaded to true if (router.pathname === '/' && globeReady) { setIsLoaded(true); } else if(router.pathname!== '/') { setIsLoaded(true); } }, [globeReady, router.pathname]);
const transitionDelay = globeReady ? baseDelay : Infinity;
return ( <> {!isLoaded && ( <motion.div role="status" className="fixed top-0 bottom-0 right-full w-screen h-screen z-30 bg-[#2e2257]" variants={transitionVariants} initial="initial" animate="animate" exit="exit" transition={{ delay: transitionDelay, duration: 0.6, ease: "easeInOut" }} aria-hidden="true" > <motion.img src="/logo.svg" alt="Descriptive text" style={{ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', width: '500px', height: '500px' }} /> </motion.div> )} <motion.div role="status" className="fixed top-0 bottom-0 right-full w-screen h-screen z-20 bg-[#3b2d71]" variants={transitionVariants} initial="initial" animate="animate" exit="exit" transition={{ delay: 0.4, duration: 0.6, ease: "easeInOut" }} aria-hidden /> <motion.div role="status" className="fixed top-0 bottom-0 right-full w-screen h-screen z-10 bg-[#4b3792]" variants={transitionVariants} initial="initial" animate="animate" exit="exit" transition={{ delay: 0.6, duration: 0.6, ease: "easeInOut" }} aria-hidden />
code</>); };
export default Transition;
- Assistant
- User
show me how to fix the first problem
- Assistant
The first problem relates to how the
transitionDelayis set toInfinitywhenglobeReadyis false. Since essentially disables the animation, this would prevent the transition effect on pages where is not true. To provide a transition on all pages, regardless of the state, you can set a finite delay instead. - User
explain: const baseDelay = router.pathname === '/' ? 0.2 : 0.2;
- Assistant
The line of code you've written, as it currently stands, is a bit redundant:
javascriptconst baseDelay = router.pathname === '/' ? 0.2 : 0.2;This expression is using the ternary conditional operator, which has the general form:
javascriptcondition ? valueIfTrue : valueIfFalse;In your case,
conditionisrouter.pathname === '/',valueIfTrueis0.2, andvalueIfFalseis also0.2. This ternary operator is, therefore, unnecessary because it evaluates to0.2whether the condition is true or false.Essentially, you are assigning a delay of
0.2seconds tobaseDelayregardless of whether is (the homepage) or any other path. If your intent was to have a different delay for the homepage and other pages, then you need to provide different values for and . Here's the corrected version based on common use cases: