1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 12:26:40 +07:00
vault-frontend/src/hooks/dom/useScrollToTop.ts
2025-01-24 17:51:59 +07:00

23 lines
584 B
TypeScript

import { useEffect } from 'react';
import { NEW_COMMENT_CLASSNAME } from '~/constants/comment';
export const useScrollToTop = (deps?: any[]) => {
useEffect(
() => {
const targetElement = document.querySelector(`.${NEW_COMMENT_CLASSNAME}`);
if (!targetElement) {
window.scrollTo(0, 0);
return;
}
const bounds = targetElement.getBoundingClientRect();
window.scrollTo({
top: bounds.top - 100,
});
},
// eslint-disable-next-line react-hooks/exhaustive-deps
deps && Array.isArray(deps) ? deps : [],
);
};