Skip to content
The Chat Archive
gpt-4-1106-preview· OpenAI WildChat-4.8M (AI2)

How do i set the width and height to 500 for mobile and 1100 for desktop screen sizes:…

  1. User

    How do i set the width and height to 500 for mobile and 1100 for desktop screen sizes: 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 { setGlobeReady } = useGlobe();
    const router = useRouter(); const globeRef = useRef();

    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 = { 3}
            hexPolygonMargin = {0.75}
            hexPolygonAltitude = {0.01}
            hexPolygonUseDots = {false}
            enablePointerInteraction ={false}
            onGlobeReady={() => setGlobeReady(true)}
            showAtmosphere = { true}
            atmosphereColor="#ffffff"
            atmosphereAltitude = { 0.1}
            backgroundColor="rgba(0, 0, 0, 0)" 
            globeImageUrl="//unpkg.com/three-globe/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={1100}
            height={1100}
            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; My breakpoints are defined in the tailwinds config as: screens: { sm: "640px", md: "768px", lg: "960px", xl: "1200px", },

  2. Assistant

    To conditionally set different width and height for the Globe component based on the screen size, you can make use of window matchMedia inside a useEffect hook and a state to store the dimensions.

    You can reuse the breakpoints defined in your Tailwind config, but make sure you comply with the min-max logic of media queries accordingly. In your case, since you want to set the size to 500 for mobile and 1100 for desktop screen sizes, you can consider everything below lg as mobile (or another breakpoint that suits your design). Here's how you would modify the GlobeComponent to include responsive sizing:

    import React, { useEffect, useState, useRef } from 'react';
    import Globe from 'react-globe.gl';
    // ...other imports
    
    const GlobeComponent = () => {
        const [dimensions, setDimensions] = useState({ width: 500, height: 500 });
        const globeRef = useRef();
        // ...other useState, useRouter, useGlobe, etc.
    
        useEffect(() => {
            // Function to update the dimensions state based on the window size
            const handleResize = () => {
                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: 1100, height: 1100 });
                } else {
                    // If smaller, set to mobile size
                    setDimensions({ width: 500, height: 500 });
                }
            };
    
            // 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);
        }, []);
    
        // ...other useEffect and functions
    
        return (
            <Globe
                ref={globeRef}
                // ...other properties
                width={dimensions.width}
                height={dimensions.height}
                // ...rest of the Globe props
            />
        );
    };
    
    export default GlobeComponent;
    

    In the example above, on initial render and upon each resize event, handleResize will check the window width and update the state with appropriate dimensions for the Globe component. The Globe component will then re-render with the updated width and height.

    Remember that direct window manipulation and event listeners can cause issues with server-side rendering (SSR) in Next.js. Since window is not available during SSR, you must ensure that you are either using the useEffect hook (which only runs client-side after mount) or checking if typeof window !== 'undefined' before accessing window. In the example provided, the useEffect method is employed, which is safe for Next.js.

  3. User

    How do i show the GlobeComponent in its current location for desktop and show the globecomponent above the <motion.h1> which has the the TrackVisibility for mobile devices: 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;

    useEffect(() => { let ticker = setInterval(() => { tick(); }, delta);

    return () => { clearInterval(ticker) };
    

    }, [text])

    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="h1 my-text" > {({ isVisible }) => <div className={isVisible ? "animate__animated animate__fadeIn" : "text-center flex flex-col justify-center"}> Transforming Your Ideas Into{" "} {text} } </motion.h1>

          {/* subtitle */}
          <motion.p
            variants={fadeIn("down", 0.3)}
            initial="hidden"
            animate="show"
            exit="hidden"
            className="max-w-sm xl:max-w-xl mx-auto xl:mx-0 mb-10 xl:mb-16 text-lg font-monda"
          >
            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="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"
        >
            <div className="w-full h-full max-w-[100%] max-h-[100%] xl:pb-50 xl:mr-50 sm:m-auto">
              <GlobeComponent
                globeWidth={1100}
                globeHeight={1100}
              />
          </div>
          
        </motion.div> 
        </div>
       </div>
       <Bulb /> 
    </div>
    

    ); };

    export default Home;

  4. Assistant

    To conditionally render the GlobeComponent based on screen size (above or below md breakpoint in your Tailwind configuration), you can use CSS classes with media queries that Tailwind provides, or you could dynamically apply styles based on window.innerWidth if JavaScript-based responsiveness is preferred.

    For the CSS approach, you'd have one instance of the GlobeComponent that only appears on desktop using hidden md:block and another that appears only on mobile using block md:hidden, placed respectively at the desired locations.

    Using your current code structure, here's how to modify the Home component to achieve this:

    // ...other imports
    
    const Home = () => {
      // ... existing useState and useEffect logic for typing effect
    
      return (
        <div className="bg-primary/30 h-full overflow-x-auto">
          {/* ... existing code for text */}
    
          {/* Conditionally render globe for mobile */}
          <div className="block md:hidden w-full h-full pt-10">
            <GlobeComponent />
          </div>
    
          {/* rest of code */}
          
          <motion.h1
            // ... existing props
            className="h1 my-text"
          >
            <TrackVisibility>
              {({ isVisible }) => /* ... existing code */}
            </TrackVisibility>
          </motion.h1>
          {/* ... more of your existing code */}
          
          {/* Conditionally render globe for desktop */}
          <div className="hidden md:block xl:w-[50%] md:w-[100%] h-full pt-10 xl:mr-10 absolute xl:right-0">
            {/* bg img */}
            <motion.div
              // ... existing props
              className="w-full h-full max-w-[100%] max-h-[100%] absolute"
            >
              <GlobeComponent />
            </motion.div>
          </div>
        </div>
        {/* ... rest of existing code */}
      );
    };
    
    export default Home;
    

    In this modified code:

    • The first div with the class block md:hidden will only show the globe on screens smaller than md breakpoint (768px).
    • The second div with the class hidden md:block will only show the globe on screens larger than or equal to md breakpoint (768px), placed to the right of the text content as you originally had it.

    This approach relies on CSS to toggle visibility based on the viewport size, which is more performant than handling resize events in JavaScript, and maintains separation of concerns between styling and logic.

    Remember, since you are using SSR with Next.js, you cannot use window in the code that is executed server-side. The above solution works well because it relies on CSS for responsiveness, which is compatible with SSR.

  5. User

    How do i add a 50px padding above the globe on mobile: 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;

    useEffect(() => { let ticker = setInterval(() => { tick(); }, delta);

    return () => { clearInterval(ticker) };
    

    }, [text])

    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="h1 my-text" > {({ isVisible }) => <div className={isVisible ? "animate__animated animate__fadeIn" : "text-center flex flex-col justify-center"}> Transforming Your Ideas Into{" "} {text} } </motion.h1>

          {/* subtitle */}
          <motion.p
            variants={fadeIn("down", 0.3)}
            initial="hidden"
            animate="show"
            exit="hidden"
            className="max-w-sm xl:max-w-xl mx-auto xl:mx-0 mb-10 xl:mb-16 text-lg font-monda"
          >
            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 pb-15">
            <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"
        >
            <div className="w-full h-full max-w-[100%] max-h-[100%] xl:pb-50 xl:mr-50 sm:m-auto">
              <GlobeComponent
                globeWidth={1100}
                globeHeight={1100}
              />
          </div>
          
        </motion.div> 
        </div>
       </div>
       <Bulb /> 
    </div>
    

    ); };

    export default Home;

  6. Assistant

    To add a 50px padding above the GlobeComponent on mobile devices, you need to modify the div that wraps the GlobeComponent when it is displayed for mobile. Using Tailwind's utility classes, you can simply add pt-12 (since 1 utility unit in Tailwind by default equals 4px, thus 12 * 4px = 48px which is close enough to 50px) for mobile devices and ensure it doesn't apply to larger screens.

    Here's how to adjust your code:

    // ...other imports
    
    const Home = () => {
      // ... existing useState and useEffect logic for typing effect
    
      return (
        <div className="bg-primary/30 h-full overflow-x-auto">
          {/* text */}
          <div className="w-full h-full pt-20">
            <div className="text-center flex flex-col justify-center xl:pt-40 xl:text-left h-full container mx-auto">
              {/* globe for mobile with padding */}
              <div className="pt-12 md:pt-0 md:hidden w-full">
                <GlobeComponent />
              </div>
              {/* ... other content ... */}
            </div>
          </div>
          
          {/* globe for desktop */}
          <div className="hidden md:block xl:w-[50%] md:w-[100%] h-full pt-10 xl:mr-10 absolute xl:right-0 bottom-0">
            {/* bg img */}
            <motion.div
              className="w-full h-full max-w-[100%] max-h-[100%] absolute"
            >
              <GlobeComponent />
            </motion.div>
          </div>
          {/* ... more content such as the Bulb component ... */}
        </div>
      );
    };
    
    export default Home;
    

    In this code snippet, the GlobeComponent for mobile devices has an additional pt-12 class, giving it the desired padding on top.

    This solution preserves the padding on mobile devices without affecting the desktop layout. Note that I removed the sm:m-auto class since it's not present in the Tailwind configuration you provided, and you might not need centered margin for mobile if you only want the padding on top.

    In case you want to add a new custom size that isn't available by default in Tailwind, you can extend the spacing scale in your tailwind.config.js file, but for this example, the pre-existing pt-12 should suffice.

  7. User

    The only issue now is that the globe is malfunctioning due to being declared twice on the page but i still want to show the globe component above the Transforming Your...text on the webpage. How can i use something like an invert property on a flex for md and sm to rearrange the globecomponent to the top or something similar using declaration of just one globe component on the page: 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;

    useEffect(() => { let ticker = setInterval(() => { tick(); }, delta);

    return () => { clearInterval(ticker) };
    

    }, [text])

    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="h1 my-text" > {({ isVisible }) => <div className={isVisible ? "animate__animated animate__fadeIn" : "text-center flex flex-col justify-center"}> Transforming Your Ideas Into{" "} {text} } </motion.h1>

          {/* subtitle */}
          <motion.p
            variants={fadeIn("down", 0.3)}
            initial="hidden"
            animate="show"
            exit="hidden"
            className="max-w-sm xl:max-w-xl mx-auto xl:mx-0 mb-10 xl:mb-16 text-lg font-monda"
          >
            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 pb-15">
            <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"
        >
            <div className="w-full h-full max-w-[100%] max-h-[100%] xl:pb-50 xl:mr-50 sm:m-auto">
              <GlobeComponent
                globeWidth={1100}
                globeHeight={1100}
              />
          </div>
          
        </motion.div> 
        </div>
       </div>
       <Bulb /> 
    </div>
    

    ); };

    export default Home;

  8. Assistant

    If you want to reorder elements in a responsive manner without declaring them twice in your markup, you can utilize the CSS Flexbox property order combined with Tailwind classes. The order property in Flexbox allows you to change the visual order of flex items without changing the DOM.

    Here's an example of how you could structure your Home component using Flexbox and the order property to move GlobeComponent above the text on mobile screens, while preserving its location on desktop screens:

    // ...other imports
    
    const Home = () => {
      // ... existing useState and useEffect logic for typing effect
    
      return (
        <div className="bg-primary/30 h-full overflow-x-auto">
          <div className="w-full h-full pt-20 flex flex-col md:flex-row-reverse justify-center md:justify-between container mx-auto">
            {/* GlobeComponent should be the first flex item in the DOM 
                so that it can be displayed above the text on mobile,
                but we use order classes to visually position it as the second item on desktop */}
            <div className="w-full md:w-[50%] pt-12 md:pt-10 order-2 md:order-1">
              <GlobeComponent />
            </div>
            {/* Text content */}
            <div className="text-center md:text-left w-full md:w-[50%] order-1 md:order-2">
              {/* title and other content*/}
              <motion.h1 /* existing props */ className="h1 my-text">
                <TrackVisibility>
                  {({ isVisible }) =>
                    <div className={isVisible ? "animate__animated animate__fadeIn" : ""}>
                      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>
              <motion.p /* existing props */>
                {/* subtitle text */}
              </motion.p>
              {/* btn */}
              <ProjectsBtn />
            </div>
          </div>
    
          {/* Rest of your content such as the Bulb component */}
          <Bulb />
        </div>
      );
    };
    
    export default Home;
    

    With this approach, you only need to declare GlobeComponent once. On mobile screens (small to medium), GlobeComponent will naturally appear above the text because it's the first flex item in the document flow. On desktop screens (medium and larger), the order classes (order-2 md:order-1) rearrange the flex items visually, moving GlobeComponent to the right side of the text content.

    The Tailwind classes used are:

    • flex-col stacks items vertically on mobile.
    • md:flex-row-reverse places items side-by-side on desktop, with GlobeComponent placed on the right due to the reverse row direction.
    • order-2 places the GlobeComponent second visually on mobile.
    • md:order-1 places the GlobeComponent first visually on desktop since we reversed the row direction.

    Make sure that any additional styling adjustments are made so that the layout behaves as expected at all screen sizes.