Skip to content

Commit

Permalink
Added custom hooks for scroll issues in all pages, implemented scroll…
Browse files Browse the repository at this point in the history
… to-top and restoration
  • Loading branch information
fwedwicc committed Aug 6, 2024
1 parent ff96afe commit 264bac0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/hooks/useScrollRestoration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { useEffect } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';

const useScrollRestoration = () => {
const location = useLocation();
const navigate = useNavigate();

useEffect(() => {
const storedPosition = sessionStorage.getItem('scrollPosition');
if (storedPosition && location.pathname === '/') {
window.scrollTo(0, parseInt(storedPosition, 10));
}

return () => {
if (location.pathname === '/') {
sessionStorage.setItem('scrollPosition', window.scrollY);
}
};
}, [location, navigate]);
};

export default useScrollRestoration;
16 changes: 16 additions & 0 deletions src/hooks/useScrollToTop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';

const useScrollToTop = () => {
const location = useLocation();

useEffect(() => {
const timer = setTimeout(() => {
window.scrollTo(0, 0);
}, 0);

return () => clearTimeout(timer);
}, [location]);
};

export default useScrollToTop;
2 changes: 2 additions & 0 deletions src/pages/ComponentOverview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import React from 'react';
import { useParams } from 'react-router-dom';
import { Tab, Navbar, Footer } from '../components';
import { elementsData, templatesData } from '../constants';
import useScrollToTop from '../hooks/useScrollToTop';
import { motion } from 'framer-motion';

const ComponentOverview = () => {
useScrollToTop();
const { type } = useParams();

const elementData = elementsData[type];
Expand Down
3 changes: 3 additions & 0 deletions src/pages/Components.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { motion } from 'framer-motion';
import React from 'react';
import useScrollRestoration from '../hooks/useScrollRestoration';
import { Navbar, Card, Footer } from '../components';
import { elementsData, templatesData } from '../constants';

const Components = () => {
useScrollRestoration();

return (
<motion.div
initial={{ opacity: 0 }}
Expand Down
5 changes: 4 additions & 1 deletion src/pages/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import React from 'react';
import { motion } from 'framer-motion';
import { Navbar, Hero, SampleCard, SampleCart, SampleChat, SampleFooter, SampleLogin, SampleModal, Footer } from '../components';
import useScrollToTop from '../hooks/useScrollToTop';
import { Navbar, Hero, SampleCard, SampleCart, SampleChat, SampleLogin, SampleModal, Footer } from '../components';

const Home = () => {
useScrollToTop();

return (
<motion.div
initial={{ opacity: 0 }}
Expand Down

0 comments on commit 264bac0

Please sign in to comment.