mirror of
https://github.com/Motion-Games/MotionWebStudio.git
synced 2026-04-21 09:00:53 +02:00
init
This commit is contained in:
22
components/ScrollToTop.tsx
Normal file
22
components/ScrollToTop.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
import { useEffect } from "react";
|
||||
import { useLocation } from "react-router-dom";
|
||||
|
||||
export default function ScrollToTop() {
|
||||
const { pathname, hash } = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
if (hash) {
|
||||
// Use setTimeout to allow DOM to render if navigating from another page
|
||||
setTimeout(() => {
|
||||
const element = document.getElementById(hash.replace('#', ''));
|
||||
if (element) {
|
||||
element.scrollIntoView({ behavior: 'smooth' });
|
||||
}
|
||||
}, 100);
|
||||
} else {
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
}, [pathname, hash]);
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user