Splash Screen on Gatsby JS

If you embark on the problem to add splash screen to you gatsby website you are in the right place. I myself was trying to add the splash screen to the website and had to go a lot forums and couldn’t but finally done that.

You let me tell you how did…


This content originally appeared on DEV Community and was authored by Syed Faran Mustafa

If you embark on the problem to add splash screen to you gatsby website you are in the right place. I myself was trying to add the splash screen to the website and had to go a lot forums and couldn't but finally done that.

You let me tell you how did I do it

Image description

So first find a cool GIF you want to add as an splash screen then add loader code

src/loader.js

import React from "react";
import styled from "styled-components";
import BCTLoader from "../../assets/gif/BCTloader.gif";

const Loader = () => {
  return (
    <LoaderWrapper id="loader-wrapper">
      <img src={BCTLoader} alt="GIF Image" />
    </LoaderWrapper>
  );
};
export default Loader;

and add some css to it, I like working with styled components feel free to it with css

const LoaderWrapper = styled.div`
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #ffff;
  z-index: 9999;
  display: none;
  justify-content: center;
  align-items: center;

  img {
    width: 20vw;
  }
`;

Add that to your layout file if you have any if you don't have that please make one so we can have a wrapper for all the pages

src/layout.js

import React from "react";
import Header from "./Navbar";
import Footer from "./Footer";
import Loader from "./Loader";

const Layout = ({ children }) => {
  return (
    <>
      <Header />
      <main>{children}</main>
      <Footer />
      <Loader />
    </>
  );
};

export default Layout;

Now when you are done with it we can work with gatsby brower so we can access initial rendering

gatsby-browser.js

export const onInitialClientRender = () => {
  setTimeout(() => {
    if (document.getElementById("loader-wrapper")) {
      document.getElementById("loader-wrapper").style.display = "flex";
    }
  }, 0);
};

export const onRouteUpdate = () => {
  setTimeout(() => {
    if (document.getElementById("loader-wrapper")) {
      document.getElementById("loader-wrapper").style.display = "none";
    }
  }, 3500);
};


This content originally appeared on DEV Community and was authored by Syed Faran Mustafa


Print Share Comment Cite Upload Translate Updates
APA

Syed Faran Mustafa | Sciencx (2022-03-29T11:47:26+00:00) Splash Screen on Gatsby JS. Retrieved from https://www.scien.cx/2022/03/29/splash-screen-on-gatsby-js/

MLA
" » Splash Screen on Gatsby JS." Syed Faran Mustafa | Sciencx - Tuesday March 29, 2022, https://www.scien.cx/2022/03/29/splash-screen-on-gatsby-js/
HARVARD
Syed Faran Mustafa | Sciencx Tuesday March 29, 2022 » Splash Screen on Gatsby JS., viewed ,<https://www.scien.cx/2022/03/29/splash-screen-on-gatsby-js/>
VANCOUVER
Syed Faran Mustafa | Sciencx - » Splash Screen on Gatsby JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/29/splash-screen-on-gatsby-js/
CHICAGO
" » Splash Screen on Gatsby JS." Syed Faran Mustafa | Sciencx - Accessed . https://www.scien.cx/2022/03/29/splash-screen-on-gatsby-js/
IEEE
" » Splash Screen on Gatsby JS." Syed Faran Mustafa | Sciencx [Online]. Available: https://www.scien.cx/2022/03/29/splash-screen-on-gatsby-js/. [Accessed: ]
rf:citation
» Splash Screen on Gatsby JS | Syed Faran Mustafa | Sciencx | https://www.scien.cx/2022/03/29/splash-screen-on-gatsby-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.