mirror of
https://github.com/Motion-Games/MotionWebStudio.git
synced 2026-04-21 09:00:53 +02:00
22 lines
576 B
TypeScript
22 lines
576 B
TypeScript
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;
|
|
} |