Исправлен скролл к обьектам

This commit is contained in:
2026-08-01 03:12:07 -07:00
parent 354c8e5489
commit bff945e701

View File

@@ -5,15 +5,42 @@ export function ScrollToTop() {
const { pathname, hash } = useLocation();
useEffect(() => {
const frame = window.requestAnimationFrame(() => {
if (hash) {
const target = document.getElementById(decodeURIComponent(hash.slice(1)));
if (target) {
target.scrollIntoView();
return;
}
let targetId: string;
try {
targetId = decodeURIComponent(hash.slice(1));
} catch {
targetId = hash.slice(1);
}
const scrollToTarget = () => {
const target = document.getElementById(targetId);
if (!target) {
return false;
}
target.scrollIntoView({ block: 'start' });
return true;
};
if (scrollToTarget()) {
return;
}
const observer = new MutationObserver(() => {
if (scrollToTarget()) {
observer.disconnect();
}
});
observer.observe(document.body, { childList: true, subtree: true });
return () => observer.disconnect();
}
const frame = window.requestAnimationFrame(() => {
window.scrollTo({ top: 0, left: 0, behavior: 'instant' });
});