Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Loader #578

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion src/layout/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as React from 'react'
import Head from 'next/head'
import { useRouter } from 'next/router'
import styled from 'styled-components'
import { transparentize } from 'polished'
import loaderImg from '~/public/defillama-press-kit/nft/PNG/defillama_211230_brand_logo_defillama-nft-icon.png'
import ThemeProvider, { GlobalStyle, ThemedBackground } from '~/Theme'
import SEO from '~/components/SEO'
import Nav from '~/components/Nav'
Expand Down Expand Up @@ -35,6 +37,44 @@ const PageWrapper = styled.div`
}
`

const LoaderBody = styled.div`
margin: 0 auto;
margin-top: 35vh;
width: fit-content;
`

const LoaderText = styled.div`
margin-top: 8px;
font-size: 20px;
font-weight: 500;
text-align: center;
padding-left: 8px;
`

const Loader = styled.img`
width: 120px;
height: 120px;
-webkit-animation: spin 3s linear infinite;
-moz-animation: spin 3s linear infinite;
animation: spin 3s linear infinite;
@-moz-keyframes spin {
100% {
-moz-transform: rotate(360deg);
}
}
@-webkit-keyframes spin {
100% {
-webkit-transform: rotate(360deg);
}
}
@keyframes spin {
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
`

interface ILayoutProps {
title: string
children: React.ReactNode
Expand All @@ -50,6 +90,21 @@ interface IBackground {
const Background = styled(ThemedBackground)<IBackground>``

export default function Layout({ title, children, defaultSEO = false, backgroundColor, ...props }: ILayoutProps) {
const router = useRouter()
const [pageLoading, setPageLoading] = React.useState<boolean>(false)
React.useEffect(() => {
const handleStart = () => {
setPageLoading(true)
}
const handleComplete = () => {
setPageLoading(false)
}

router.events.on('routeChangeStart', handleStart)
router.events.on('routeChangeComplete', handleComplete)
router.events.on('routeChangeError', handleComplete)
}, [router])

return (
<>
<Head>
Expand All @@ -63,7 +118,14 @@ export default function Layout({ title, children, defaultSEO = false, background
<Nav />
<PageWrapper>
<Background backgroundColor={backgroundColor || transparentize(0.8, '#445ed0')} />
<Center {...props}>{children}</Center>
{pageLoading ? (
<LoaderBody>
<Loader src={loaderImg.src} />
<LoaderText>Loading...</LoaderText>
</LoaderBody>
) : (
<Center {...props}>{children}</Center>
)}
</PageWrapper>
</ThemeProvider>
</>
Expand Down