Skip to content

Commit

Permalink
feat: support offset when nav back to home screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaron Galperin committed Feb 6, 2023
1 parent c7ed078 commit c09dec0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
27 changes: 15 additions & 12 deletions pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useEffect, useState } from 'react'
import { Router, useRouter } from 'next/router'
import { Loading } from '../components/loading'
import { ErrorBoundary } from '../components/ErrorBoundary'
import { ScrollContextProvider } from '../providers/scroll'

export default function App({ Component, pageProps }: AppProps) {
const [isLoading, setIsLoading] = useState<boolean>(false)
Expand All @@ -31,18 +32,20 @@ export default function App({ Component, pageProps }: AppProps) {

return (
<>
<Header
customTitle={!isRouterQueryEmpty ? 'Go Back' : undefined}
activateLink={ isActivatedLink }/>
{
isLoading ?
<Loading/> :
(<WrapperStyled>
<ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
</WrapperStyled>)
}
<ScrollContextProvider>
<Header
customTitle={!isRouterQueryEmpty ? 'Go Back' : undefined}
activateLink={ isActivatedLink }/>
{
isLoading ?
<Loading/> :
(<WrapperStyled>
<ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
</WrapperStyled>)
}
</ScrollContextProvider>
</>
)
}
Expand Down
27 changes: 26 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Head from 'next/head'
import React from 'react'
import { Router } from 'next/router'
import React, { useContext, useEffect } from 'react'
import CardList from '../components/cardList'
import { ScrollContext } from '../providers/scroll'
import { POKEMON_BASE_URL, POKEMON_LIST_URL } from '../utils/constants'

interface IPokemonItem {
Expand All @@ -10,6 +12,29 @@ interface IPokemonItem {
}

const Pokemon = ({pokemon = []}: {pokemon: IPokemonItem[]}) => {
const { offsetY, updateOffsetY } = useContext(ScrollContext)

useEffect(() => {
const handleRouteChange = (url: unknown) => {
updateOffsetY(window.pageYOffset)
}

Router.events.on('routeChangeStart', handleRouteChange)

return () => {
Router.events.off('routeChangeStart', handleRouteChange)
}
}, [updateOffsetY])

useEffect(() => {
if (offsetY > 0) {
window.scrollTo(0, offsetY)
setTimeout(() => {
updateOffsetY(0)
}, 0)
}
}, [offsetY, updateOffsetY])

return (
<>
<Head>
Expand Down
26 changes: 26 additions & 0 deletions providers/scroll.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useState, createContext } from 'react'

interface IScrollContext {
offsetY: number
updateOffsetY: (offset: number) => void
}

const INITIAL_STATE: IScrollContext = {
offsetY: 0,
updateOffsetY: (offset: number) => {offset}
}

export const ScrollContext = createContext(INITIAL_STATE)

export const ScrollContextProvider = ({ children }: {children: React.ReactNode}) => {
const [offsetY, setOffsetY] = useState<number>(0)

const updateOffsetY = (offset: number) => {
setOffsetY(offset)
}

return (
<ScrollContext.Provider value={{ offsetY, updateOffsetY }}>
{children}
</ScrollContext.Provider>)
}

2 comments on commit c09dec0

@vercel
Copy link

@vercel vercel bot commented on c09dec0 Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pokemon – ./

pokemon-git-main-yaronglp.vercel.app
pokemon-yaronglp.vercel.app
pokemon-one-puce.vercel.app

@vercel
Copy link

@vercel vercel bot commented on c09dec0 Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pokemon-kde6 – ./

pokemon-kde6.vercel.app
pokemon-kde6-yaronglp.vercel.app
pokemon-kde6-git-main-yaronglp.vercel.app

Please sign in to comment.