-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathuseHeights.tsx
More file actions
100 lines (84 loc) · 2.62 KB
/
useHeights.tsx
File metadata and controls
100 lines (84 loc) · 2.62 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import findDOMNode from '@rc-component/util/lib/Dom/findDOMNode';
import * as React from 'react';
import { useEffect, useRef } from 'react';
import type { GetKey } from '../interface';
import CacheMap from '../utils/CacheMap';
function parseNumber(value: string) {
const num = parseFloat(value);
return isNaN(num) ? 0 : num;
}
export default function useHeights<T>(
getKey: GetKey<T>,
onItemAdd?: (item: T) => void,
onItemRemove?: (item: T) => void,
): [
setInstanceRef: (item: T, instance: HTMLElement) => void,
collectHeight: (sync?: boolean) => void,
cacheMap: CacheMap,
updatedMark: number,
] {
const [updatedMark, setUpdatedMark] = React.useState(0);
const instanceRef = useRef(new Map<React.Key, HTMLElement>());
const heightsRef = useRef(new CacheMap());
const promiseIdRef = useRef<number>(0);
function cancelRaf() {
promiseIdRef.current += 1;
}
function collectHeight(sync = false) {
cancelRaf();
const doCollect = () => {
let changed = false;
instanceRef.current.forEach((element, key) => {
if (element && element.offsetParent) {
const htmlElement = findDOMNode<HTMLElement>(element);
const { offsetHeight } = htmlElement;
const { marginTop, marginBottom } = getComputedStyle(htmlElement);
const marginTopNum = parseNumber(marginTop);
const marginBottomNum = parseNumber(marginBottom);
const totalHeight = offsetHeight + marginTopNum + marginBottomNum;
if (heightsRef.current.get(key) !== totalHeight) {
heightsRef.current.set(key, totalHeight);
changed = true;
}
}
});
// Always trigger update mark to tell parent that should re-calculate heights when resized
if (changed) {
setUpdatedMark((c) => c + 1);
}
};
if (sync) {
doCollect();
} else {
promiseIdRef.current += 1;
const id = promiseIdRef.current;
Promise.resolve().then(() => {
if (id === promiseIdRef.current) {
doCollect();
}
});
}
}
function setInstanceRef(item: T, instance: HTMLElement) {
const key = getKey(item);
const origin = instanceRef.current.get(key);
if (instance) {
instanceRef.current.set(key, instance);
collectHeight();
} else {
instanceRef.current.delete(key);
}
// Instance changed
if (!origin !== !instance) {
if (instance) {
onItemAdd?.(item);
} else {
onItemRemove?.(item);
}
}
}
useEffect(() => {
return cancelRaf;
}, []);
return [setInstanceRef, collectHeight, heightsRef.current, updatedMark];
}