Files
MotionWebStudio/components/ScrollToTop.tsx

22 lines
576 B
TypeScript
Raw Normal View History

2025-12-21 20:40:32 +01:00
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;
}