mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-29 06:36:41 +07:00
added experimental scroll helper
This commit is contained in:
parent
c2154e930c
commit
8d1e6989c2
8 changed files with 105 additions and 4 deletions
35
src/hooks/dom/useScrollHeight.ts
Normal file
35
src/hooks/dom/useScrollHeight.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { useEffect, useState } from 'react';
|
||||
|
||||
const getHeight = () => {
|
||||
if (typeof document === 'undefined') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const body = document.body;
|
||||
const html = document.documentElement;
|
||||
|
||||
return Math.max(
|
||||
body.scrollHeight,
|
||||
body.offsetHeight,
|
||||
html.clientHeight,
|
||||
html.scrollHeight,
|
||||
html.offsetHeight
|
||||
);
|
||||
};
|
||||
export const useScrollHeight = () => {
|
||||
const [scrollHeight, setScrollHeight] = useState(getHeight());
|
||||
|
||||
useEffect(() => {
|
||||
const measure = () => setScrollHeight(getHeight());
|
||||
|
||||
window.addEventListener('scroll', measure);
|
||||
window.addEventListener('resize', measure);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('scroll', measure);
|
||||
window.removeEventListener('resize', measure);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return scrollHeight;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue