-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.ts
More file actions
168 lines (147 loc) · 4.94 KB
/
index.ts
File metadata and controls
168 lines (147 loc) · 4.94 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
useEffect,
useState,
useRef,
useMemo,
RefObject,
RefCallback,
useCallback,
} from "react";
import useResolvedElement from "./utils/useResolvedElement";
import extractSize from "./utils/extractSize";
export type ObservedSize = {
width: number | undefined;
height: number | undefined;
};
export type ResizeHandler = (size: ObservedSize) => void;
type HookResponse<T extends Element> = {
ref: RefCallback<T>;
} & ObservedSize;
// Declaring my own type here instead of using the one provided by TS (available since 4.2.2), because this way I'm not
// forcing consumers to use a specific TS version.
export type ResizeObserverBoxOptions =
| "border-box"
| "content-box"
| "device-pixel-content-box";
declare global {
interface ResizeObserverEntry {
readonly devicePixelContentBoxSize: ReadonlyArray<ResizeObserverSize>;
}
}
export type RoundingFunction = (n: number) => number;
function useResizeObserver<T extends Element>(
opts: {
ref?: RefObject<T> | T | null | undefined;
onResize?: ResizeHandler;
box?: ResizeObserverBoxOptions;
round?: RoundingFunction;
} = {}
): HookResponse<T> {
// Saving the callback as a ref. With this, I don't need to put onResize in the
// effect dep array, and just passing in an anonymous function without memoising
// will not reinstantiate the hook's ResizeObserver.
const onResize = opts.onResize;
const onResizeRef = useRef<ResizeHandler | undefined>(undefined);
onResizeRef.current = onResize;
const round = opts.round || Math.round;
// Using a single instance throughout the hook's lifetime
const resizeObserverRef = useRef<{
box?: ResizeObserverBoxOptions;
round?: RoundingFunction;
instance: ResizeObserver;
}>();
const [size, setSize] = useState<{
width?: number;
height?: number;
}>({
width: undefined,
height: undefined,
});
// In certain edge cases the RO might want to report a size change just after
// the component unmounted.
const didUnmount = useRef(false);
useEffect(() => {
didUnmount.current = false;
return () => {
didUnmount.current = true;
};
}, []);
// Using a ref to track the previous width / height to avoid unnecessary renders.
const previous: {
current: {
width?: number;
height?: number;
};
} = useRef({
width: undefined,
height: undefined,
});
// This block is kinda like a useEffect, only it's called whenever a new
// element could be resolved based on the ref option. It also has a cleanup
// function.
const refCallback = useResolvedElement<T>(
useCallback(
(element) => {
// We only use a single Resize Observer instance, and we're instantiating it on demand, only once there's something to observe.
// This instance is also recreated when the `box` option changes, so that a new observation is fired if there was a previously observed element with a different box option.
if (
!resizeObserverRef.current ||
resizeObserverRef.current.box !== opts.box ||
resizeObserverRef.current.round !== round
) {
resizeObserverRef.current = {
box: opts.box,
round,
instance: new ResizeObserver((entries) => {
const entry = entries[0];
const boxProp =
opts.box === "border-box"
? "borderBoxSize"
: opts.box === "device-pixel-content-box"
? "devicePixelContentBoxSize"
: "contentBoxSize";
const reportedWidth = extractSize(entry, boxProp, "inlineSize");
const reportedHeight = extractSize(entry, boxProp, "blockSize");
const newWidth = reportedWidth != null ? round(reportedWidth) : undefined;
const newHeight = reportedHeight != null
? round(reportedHeight)
: undefined;
if (
previous.current.width !== newWidth ||
previous.current.height !== newHeight
) {
const newSize = { width: newWidth, height: newHeight };
previous.current.width = newWidth;
previous.current.height = newHeight;
if (onResizeRef.current) {
onResizeRef.current(newSize);
} else {
if (!didUnmount.current) {
setSize(newSize);
}
}
}
}),
};
}
resizeObserverRef.current.instance.observe(element, { box: opts.box });
return () => {
if (resizeObserverRef.current) {
resizeObserverRef.current.instance.unobserve(element);
}
};
},
[opts.box, round]
),
opts.ref
);
return useMemo(
() => ({
ref: refCallback,
width: size.width,
height: size.height,
}),
[refCallback, size.width, size.height]
);
}
export default useResizeObserver;