Skip to content

Commit

Permalink
feat: scroll to top on route change and scroll behavior to smooth
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoEscaleira committed Mar 10, 2024
1 parent 1f03002 commit 41529c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import Modal from "react-modal";
import { Outlet, useLocation } from "react-router-dom";
import { Footer } from "@components/Footer/Footer.tsx";
import { Header } from "@components/Header/Header.tsx";
import { useScrollToTop } from "@utils/hooks/useScrollToTop";

export function Layout() {
const location = useLocation();
useEffect(() => {
Modal.setAppElement("#root");
}, []);

useScrollToTop();

return (
<>
<Header />
<main className="relative flex flex-grow w-full flex-col items-center">
<main className="relative flex w-full flex-grow flex-col items-center">
<Outlet />
</main>
{location && location.pathname !== "/" && <Footer />}
Expand Down
4 changes: 4 additions & 0 deletions src/global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
box-sizing: border-box;
}

html {
scroll-behavior: smooth;
}

body {
width: 100%;
height: 100vh;
Expand Down
10 changes: 10 additions & 0 deletions src/utils/hooks/useScrollToTop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useEffect } from "react";
import { useLocation } from "react-router-dom";

export const useScrollToTop = () => {
const { pathname } = useLocation();

useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
};

0 comments on commit 41529c5

Please sign in to comment.