1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-24 20:36:40 +07:00
vault-frontend/src/hooks/dom/useScrollToTop.ts
2022-01-19 12:30:04 +07:00

23 lines
583 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 : []
);
};