am experiencing a layout shift during the transitionDelay where the page index.jsx appears at a with less than 100% and the motion.div with the transitionDelay gets a with greater than the screen at the exact time when the globe component just becomes ready and the transition ends. How do i fix this: index.jsx: import { motion } from "framer-motion";
import dynamic from 'next/dynamic';
import { useState, useEffect, useRef } from "react";
import Bulb from "../components/Bulb";
import ProjectsBtn from "../components/ProjectsBtn";
import TrackVisibility from 'react-on-screen';
import { fadeIn } from "../variants";
const GlobeComponent = dynamic(() => import("../components/GlobeComponent"), { ssr: false });
const Home = () => {
const [loopNum, setLoopNum] = useState(0);
const [isDeleting, setIsDeleting] = useState(false);
const [text, setText] = useState('');
const [delta, setDelta] = useState(300 - Math.random() * 100);
const [index, setIndex] = useState(1);
const toRotate = ["Digital Reality", "Modern Websites", "Global Brands"];
const period = 750;
const [isMobileView, setIsMobileView] = useState(false);
useEffect(() => {
let ticker = setInterval(() => {
tick();
}, delta);
return () => { clearInterval(ticker) };
}, [text]);
useEffect(() => {
function handleResize() {
// Define the width breakpoint for mobile, for example 768px
if (window.innerWidth < 1200) {
setIsMobileView(true);
} else {
setIsMobileView(false);
}
}
// Call the handler right away so state gets updated with initial window size
handleResize();
// Set up event listener
window.addEventListener('resize', handleResize);
// Remove event listener on cleanup
return () => window.removeEventListener('resize', handleResize);
}, []);
const tick = () => {
let i = loopNum % toRotate.length;
let fullText = toRotate[i];
let updatedText = isDeleting ? fullText.substring(0, text.length - 1) : fullText.substring(0, text.length + 1);
setText(updatedText);
if (isDeleting) {
setDelta(prevDelta => prevDelta / 2);
}
if (!isDeleting && updatedText === fullText) {
setIsDeleting(true);
setIndex(prevIndex => prevIndex - 1);
setDelta(period);
} else if (isDeleting && updatedText === '') {
setIsDeleting(false);
setLoopNum(loopNum + 1);
setIndex(1);
setDelta(250);
} else {
setIndex(prevIndex => prevIndex + 1);
}
}
return (
{/* text /}
{/ title */}
<motion.h1
variants={fadeIn("down", 0.2)}
initial="hidden"
animate="show"
exit="hidden"
className="h2 xl:h1 font-sans my-text w-full"
>
{({ isVisible }) =>
<div className={isVisible ? "animate__animated animate__fadeIn" : "text-center flex flex-col justify-center"}>
<div className="globe-container relative xl:hidden w-[100%] h-full flex justify-center mx-auto">
{isMobileView && (
<GlobeComponent />
)}
</div>
Transforming Your <br /> Ideas Into{" "}<br />
<span className="text-accent typewriter" data-rotate='[ "Digital Reality", "Modern Websites", "Global Brands"]'>
{text}
</span>
</div>}
</TrackVisibility>
</motion.h1>
{/* subtitle */}
<motion.p
variants={fadeIn("down", 0.3)}
initial="hidden"
animate="show"
exit="hidden"
className="max-w-sm px-5 xl:px-0 xl:max-w-xl mx-auto xl:mx-0 mb-5 xl:mb-16 text-sm xl:text-lg"
>
With a keen eye for design and a passion for code, I bring visions to life with precision and polish. Let's collaborate to create a stunning online presence that showcases your unique brand and leaves a lasting impression. Elevate your digital game with a developer who is committed to quality and innovation.
</motion.p>
{/* btn */}
<div className="flex justify-center xl:hidden relative">
<ProjectsBtn />
</div>
<motion.div
variants={fadeIn("down", 0.4)}
initial="hidden"
animate="show"
exit="hidden"
className="hidden xl:flex"
>
<ProjectsBtn />
</motion.div>
</div>
</div>
{/* image */}
<div className="hidden md:block w-[100%] xl:w-[50%] md:w-[100%] h-full pt-10 xl:mr-10 absolute xl:right-0 bottom-0">
{/* bg img */}
<div
role="img"
className="bg-none xl:bg-right xl:bg-no-repeat w-full h-full bg-cover bg-center relative"
aria-hidden
>
<motion.div
variants={fadeIn("up", 0.5)}
initial="hidden"
animate="show"
exit="hidden"
transition={{ duration: 1, ease: "easeInOut" }}
className="w-full h-full max-w-[100%] max-h-[100%] absolute"
>
{!isMobileView && (
<div className="w-full h-full max-w-[100%] max-h-[100%] xl:pb-50 xl:mr-50">
<GlobeComponent />
</div>
)}
</motion.div>
</div>
</div>
<Bulb />
</div>
);
};
export default Home;
Transition.jsx: import { motion } from "framer-motion";
import React, { useState, useEffect } from "react";
import { useRouter } from "next/router";
import ParticlesContainer from "./ParticlesContainer";
import { useGlobe } from '../components/globe/globeContext';
const Transition = () => {
const [isMobileView, setIsMobileView] = useState(false);
const [isLoaded, setIsLoaded] = useState(false);
const router = useRouter();
const { globeReady } = useGlobe();
let transitionDelay;
const baseDelay = router.pathname === '/' ? 0.2 : 0.2;
const transitionVariants = {
initial: {
x: "100%",
width: "100%",
},
animate: {
x: "0%",
width: "0%",
},
exit: {
x: ["0%", "100%"],
width: ["0%", "100%"],
},
};
useEffect(() => {
function handleResize() {
// Safeguarding window access
if (typeof window !== 'undefined') {
if (window.innerWidth < 768) {
setIsMobileView(true);
} else {
setIsMobileView(false);
}
}
}
// Since we're using window, we safeguard the entire effect
if (typeof window !== 'undefined') {
handleResize();
window.addEventListener('resize', handleResize);
}
// Cleanup
return () => {
if (typeof window !== 'undefined') {
window.removeEventListener('resize', handleResize);
}
};
}, []);
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]);
if (router.pathname === '/' && !globeReady) {
transitionDelay = globeReady ? baseDelay : Infinity;
} else if (router.pathname !== '/' && !globeReady) {
transitionDelay = baseDelay;
}
return (
<>
{!isLoaded && (
<motion.div
role="status"
className="fixed top-0 bottom-0 right-full w-full h-full z-30 bg-gradient-to-tl from-violet-900 to-black"
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"
className="w-[300px] h-[300px] xl:w-[500px] xl:h-[500px]"
style={{
position: 'absolute',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
}}
/>
<motion.img
src="/hand.gif"
alt="Descriptive text"
className="w-[300px] h-[300px] xl:w-[500px] xl:h-[500px]"
style={{
position: 'absolute',
top: '70%',
left: '50%',
transform: 'translate(-50%, -50%)',
}}
/>
<motion.div
className="pt-5 text-center sm:px-6 lg:px-8 font-bold w-[300px] h-[300px] xl:w-[500px] xl:h-[500px]"
style={{
position: 'absolute',
textAlign: 'center',
top: '100%',
left: '50%',
transform: 'translate(-50%, -50%)',
fontSize: '1.2rem',
}}>
loading ...
</motion.div>
{!isMobileView && (
<ParticlesContainer />
)}
</motion.div>
)}
<motion.div
role="status"
className="fixed top-0 bottom-0 right-full w-full h-full z-20 bg-gradient-to-tl from-violet-900 to-blue-600"
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-full h-full z-10 bg-gradient-to-tl from-black to-violet-900"
variants={transitionVariants}
initial="initial"
animate="animate"
exit="exit"
transition={{ delay: 0.6, duration: 0.6, ease: "easeInOut" }}
aria-hidden
/>
</>
);
};
export default Transition;
GlobeComponent.jsx: import React, { useEffect, useState, useRef } from 'react';
import Globe from 'react-globe.gl';
import * as THREE from 'three';
import { useRouter } from 'next/router';
import { useGlobe } from '../components/globe/globeContext';
import countries from './files/globe-data-min.json';
import travelHistory from './files/my-flights.json';
import airportHistory from './files/my-airports.json';
const GlobeComponent = () => {
const [arcsData, setArcsData] = useState([]);
const [hexPolygonResolution, setHexPolygonResolution] = useState(3);
const { setGlobeReady } = useGlobe();
const router = useRouter();
const globeRef = useRef();
const [dimensions, setDimensions] = useState({ width: 500, height: 500 });
useEffect(() => {
// Function to update the dimensions state based on the window size
const handleResize = () => {
const isDesktop = window.matchMedia("(min-width: 768px)").matches;
if (window.matchMedia("(min-width: 1200px)").matches) {
// If the screen width is larger than or equal to ‘lg’ breakpoint, set to desktop size
setDimensions({ width: 1000, height: 1000 });
} else if (window.matchMedia("(min-width: 960px)").matches) {
// If the screen width is larger than or equal to ‘lg’ breakpoint, set to desktop size
setDimensions({ width: 800, height: 800 });
} else if (window.matchMedia("(min-width: 768px)").matches) {
// If the screen width is larger than or equal to ‘lg’ breakpoint, set to desktop size
setDimensions({ width: 700, height: 700 });
} else if (window.matchMedia("(min-width: 425px)").matches) {
// If the screen width is larger than or equal to ‘lg’ breakpoint, set to desktop size
setDimensions({ width: 400, height: 400 });
} else if (window.matchMedia("(min-width: 375px)").matches) {
// If the screen width is larger than or equal to ‘lg’ breakpoint, set to desktop size
setDimensions({ width: 350, height: 350 });
} else {
// If smaller, set to mobile size
setDimensions({ width: 320, height: 320 });
}
setHexPolygonResolution(isDesktop ? 3 : 2);
};
// Set the initial globe size on component mount
handleResize();
// Add event listener for window resize
window.addEventListener('resize', handleResize);
// Remove event listener on cleanup
return () => window.removeEventListener('resize', handleResize);
}, []);
useEffect(() => {
// Check if the globe instance is loaded
if (globeRef && globeRef.current) {
const kenyaCoordinates = {
lat: 2.286389, // latitude for Nairobi, Kenya
lng: 18.82023 // longitude for Nairobi, Kenya
};
globeRef.current.pointOfView(kenyaCoordinates, 3000); // Animate the camera movement over 3000ms (3 seconds)
}
}, []);
useEffect(() => {
const handleRouteChange = () => {
// You could update state here to ensure fresh data is fetched when a route changes
setArcsData(travelHistory.flights);
};
router.events.on('routeChangeComplete', handleRouteChange);
// Fetch the initial data
setArcsData(travelHistory.flights);
return () => {
router.events.off('routeChangeComplete', handleRouteChange);
};
}, [router.events]);
return (
<Globe
ref={globeRef}
hexPolygonsData = { countries.features }
hexPolygonResolution={hexPolygonResolution}
hexPolygonMargin = {0.75}
hexPolygonAltitude = {0.01}
hexPolygonUseDots = {false}
enablePointerInteraction ={false}
onGlobeReady={() => {
setGlobeReady(true);
}}
showAtmosphere = { true}
atmosphereColor="#ffffff"
atmosphereAltitude = { 0.12}
backgroundColor="rgba(0, 0, 0, 0)"
globeImageUrl="//unpkg.com/three-globe@2.31.0/example/img/earth-dark.jpg"
hexPolygonColor = {(e) => {
return ["KEN", "CHN", "FRA", "ZAF", "JPN", "USA", "AUS", "CAN"].includes(e.properties.ISO_A3)
? "#9cff00"
: "rgba(255, 255, 255, 0.5)";
}}
width={dimensions.width}
height={dimensions.height}
arcsData={arcsData}
arcColor = {(e) => {
return e.status ? "#9cff00" : "#f13024";
}}
arcAltitude = {(e) => {
return e.arcAlt;
}}
arcStroke = {(e) => {
return e.status ? 0.5 : 0.3;
}}
arcDashLength = { 0.9}
arcDashGap = { 4}
arcDashAnimateTime = { 1000}
arcsTransitionDuration = { 1000}
arcDashInitialGap = {(e) => e.order * 1}
labelsData = { airportHistory.airports }
labelColor={() => "#ffffff"}
labelDotOrientation = {(e) => {
return e.text === "NGA" ? "top" : "right";
}}
labelDotRadius = { 0.35}
labelSize = {1.1}
labelText = {"city" }
labelResolution = { 6}
labelAltitude = { 0.07}
pointsData = { airportHistory.airports }
pointColor={() => "#ffffff"}
pointsMerge = { true}
pointAltitude = { 0.07}
pointRadius = { 0.10}
/>
);
};
export default GlobeComponent;