-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.tsx
More file actions
129 lines (104 loc) Β· 4.19 KB
/
index.tsx
File metadata and controls
129 lines (104 loc) Β· 4.19 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import findDOMNode from 'rc-util/lib/Dom/findDOMNode';
import { supportRef, useComposeRef, getNodeRef } from 'rc-util/lib/ref';
import * as React from 'react';
import type { ResizeObserverProps } from '..';
import { CollectionContext } from '../Collection';
import { observe, unobserve } from '../utils/observerUtil';
import DomRef from './Ref';
export interface SingleObserverProps extends ResizeObserverProps {
children: React.ReactElement | ((ref: React.RefObject<Element>) => React.ReactElement);
}
function SingleObserver(props: SingleObserverProps, ref: React.Ref<HTMLElement>) {
const { children, disabled } = props;
const elementRef = React.useRef<Element>(null);
const wrapperRef = React.useRef<Element|Text|null>(null);
const onCollectionResize = React.useContext(CollectionContext);
// =========================== Children ===========================
const isRenderProps = typeof children === 'function';
const mergedChildren = isRenderProps ? children(elementRef) : children;
// ============================= Size =============================
const sizeRef = React.useRef({
width: -1,
height: -1,
offsetWidth: -1,
offsetHeight: -1,
});
// ============================= Ref ==============================
const canRef =
!isRenderProps && React.isValidElement(mergedChildren) && supportRef(mergedChildren);
const originRef: React.Ref<Element> = canRef ? getNodeRef(mergedChildren) : null;
const mergedRef = useComposeRef(originRef, elementRef);
const getDom = () =>
findDOMNode<HTMLElement>(elementRef.current) ||
// Support `nativeElement` format
(elementRef.current && typeof elementRef.current === 'object'
? findDOMNode<HTMLElement>((elementRef.current as any)?.nativeElement)
: null) ||
wrapperRef.current;
React.useImperativeHandle(ref, () => getDom());
// =========================== Observe ============================
const propsRef = React.useRef<SingleObserverProps>(props);
propsRef.current = props;
// Handler
const onInternalResize = React.useCallback((target: HTMLElement) => {
const { onResize, data } = propsRef.current;
const { width, height } = target.getBoundingClientRect();
const { offsetWidth, offsetHeight } = target;
/**
* Resize observer trigger when content size changed.
* In most case we just care about element size,
* let's use `boundary` instead of `contentRect` here to avoid shaking.
*/
const fixedWidth = Math.floor(width);
const fixedHeight = Math.floor(height);
if (
sizeRef.current.width !== fixedWidth ||
sizeRef.current.height !== fixedHeight ||
sizeRef.current.offsetWidth !== offsetWidth ||
sizeRef.current.offsetHeight !== offsetHeight
) {
const size = { width: fixedWidth, height: fixedHeight, offsetWidth, offsetHeight };
sizeRef.current = size;
// IE is strange, right?
const mergedOffsetWidth = offsetWidth === Math.round(width) ? width : offsetWidth;
const mergedOffsetHeight = offsetHeight === Math.round(height) ? height : offsetHeight;
const sizeInfo = {
...size,
offsetWidth: mergedOffsetWidth,
offsetHeight: mergedOffsetHeight,
};
// Let collection know what happened
onCollectionResize?.(sizeInfo, target, data);
if (onResize) {
// defer the callback but not defer to next frame
Promise.resolve().then(() => {
onResize(sizeInfo, target);
});
}
}
}, []);
// Dynamic observe
React.useEffect(() => {
const currentElement: HTMLElement = getDom();
if (currentElement && !disabled) {
observe(currentElement, onInternalResize);
}
return () => unobserve(currentElement, onInternalResize);
}, [elementRef.current, disabled]);
// ============================ Render ============================
if(canRef){
return React.cloneElement(mergedChildren as any, {
ref: mergedRef,
})
}
return (
<DomRef ref={wrapperRef}>
{ mergedChildren}
</DomRef>
);
}
const RefSingleObserver = React.forwardRef(SingleObserver);
if (process.env.NODE_ENV !== 'production') {
RefSingleObserver.displayName = 'SingleObserver';
}
export default RefSingleObserver;