blob: e8346d860e5cef6777399aad34836f5850348ffd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import React from 'react';
const { useState, useRef, useCallback, useLayoutEffect } = React;
/**
* cosnt [ref, remainingHeight] = useRemainingViewPortHeight();
*
* return a reference, and the remaining height of the referenced dom node
* to the bottom of the view port
*
*/
export default function useRemainingViewPortHeight() {
const ref = useRef(null);
const [containerHeight, setContainerHeight] = useState(200);
const updateContainerHeight = useCallback(() => {
const { top } = ref.current.getBoundingClientRect();
setContainerHeight(window.innerHeight - top);
}, []);
useLayoutEffect(() => {
updateContainerHeight();
window.addEventListener('resize', updateContainerHeight);
return () => {
window.removeEventListener('resize', updateContainerHeight);
};
}, [updateContainerHeight]);
return [ref, containerHeight];
}
|