/
useIndexOfLastVisibleChild.jsx
57 lines (48 loc) 路 2.22 KB
/
useIndexOfLastVisibleChild.jsx
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { useLayoutEffect, useState } from 'react';
/**
* This hook will find the index of the last child of a containing element
* that fits within its bounding rectangle. This is done by summing the widths
* of the children until they exceed the width of the container.
*
* @param {Element} containerElementRef - container element
* @param {Element} overflowElementRef - overflow element
*
* The hook returns the index of the last visible child.
*/
const useIndexOfLastVisibleChild = (containerElementRef, overflowElementRef) => {
const [indexOfLastVisibleChild, setIndexOfLastVisibleChild] = useState(-1);
useLayoutEffect(() => {
function updateLastVisibleChildIndex() {
// Get array of child nodes from NodeList form
const childNodesArr = Array.prototype.slice.call(containerElementRef.children);
const { nextIndexOfLastVisibleChild } = childNodesArr
// filter out the overflow element
.filter(childNode => childNode !== overflowElementRef)
// sum the widths to find the last visible element's index
.reduce((acc, childNode, index) => {
acc.sumWidth += childNode.getBoundingClientRect().width;
if (acc.sumWidth <= containerElementRef.getBoundingClientRect().width) {
acc.nextIndexOfLastVisibleChild = index;
}
return acc;
}, {
// Include the overflow element's width to begin with. Doing this means
// sometimes we'll show a dropdown with one item in it when it would fit,
// but allowing this case dramatically simplifies the calculations we need
// to do above.
sumWidth: overflowElementRef ? overflowElementRef.getBoundingClientRect().width : 0,
nextIndexOfLastVisibleChild: -1,
});
setIndexOfLastVisibleChild(nextIndexOfLastVisibleChild);
}
if (containerElementRef) {
updateLastVisibleChildIndex();
const resizeObserver = new ResizeObserver(() => updateLastVisibleChildIndex());
resizeObserver.observe(containerElementRef);
return () => resizeObserver.disconnect();
}
return undefined;
}, [containerElementRef, overflowElementRef]);
return indexOfLastVisibleChild;
};
export default useIndexOfLastVisibleChild;