Skip to content

Commit

Permalink
fix: scroll to top of page
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Sep 19, 2024
1 parent 65d2459 commit d60626a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { CssBaseline, ThemeProvider } from "@mui/material"
import { type ThemeProviderProps } from "@mui/material/styles/ThemeProvider"
import { useCallback, type FC, type ReactNode } from "react"
import { Provider, type ProviderProps } from "react-redux"
import { BrowserRouter, Routes as RouterRoutes } from "react-router-dom"
import { BrowserRouter } from "react-router-dom"
import { type Action } from "redux"

import { ScrollRoutes } from "./router"
import { InactiveDialog, ScreenTimeDialog } from "../features"
import { useCountdown, useEventListener, useLocation } from "../hooks"
// import "../scripts"
Expand Down Expand Up @@ -42,7 +43,7 @@ const Routes: FC<
return (
<>
{!headerExcludePaths.includes(pathname) && header}
<RouterRoutes>{routes}</RouterRoutes>
<ScrollRoutes>{routes}</ScrollRoutes>
{!footerExcludePaths.includes(pathname) && footer}
</>
)
Expand Down
23 changes: 0 additions & 23 deletions src/components/ScrollRoutes.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export * from "./ItemizedList"
export { default as ItemizedList } from "./ItemizedList"
export * from "./OrderedGrid"
export { default as OrderedGrid } from "./OrderedGrid"
export * from "./ScrollRoutes"
export { default as ScrollRoutes } from "./ScrollRoutes"
export * from "./SyncError"
export { default as SyncError } from "./SyncError"
export * from "./TablePagination"
Expand Down
25 changes: 25 additions & 0 deletions src/components/router/ScrollRoutes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { type FC, useEffect } from "react"
import { Routes, type RoutesProps } from "react-router-dom"

import { useLocation } from "../../hooks/router"

export interface ScrollRoutesState {
scroll: { x: number; y: number }
}

export interface ScrollRoutesProps extends RoutesProps {}

const ScrollRoutes: FC<ScrollRoutesProps> = props => {
const { pathname, state } = useLocation<ScrollRoutesState>()

let { scroll } = state || {}
scroll = scroll || { x: 0, y: 0 }

useEffect(() => {
window.scroll(scroll.x, scroll.y)
}, [pathname, scroll.x, scroll.y])

return <Routes {...props} />
}

export default ScrollRoutes
2 changes: 2 additions & 0 deletions src/components/router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ export * from "./LinkTab"
export { default as LinkTab } from "./LinkTab"
export * from "./Navigate"
export { default as Navigate } from "./Navigate"
export * from "./ScrollRoutes"
export { default as ScrollRoutes } from "./ScrollRoutes"

0 comments on commit d60626a

Please sign in to comment.