-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added custom hooks for scroll issues in all pages, implemented scroll…
… to-top and restoration
- Loading branch information
Showing
5 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters