-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.tsx
362 lines (330 loc) · 11.3 KB
/
index.tsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* External dependencies
*/
import type { ReactElement, RefCallback, RefObject } from 'react';
/**
* WordPress dependencies
*/
import {
useMemo,
useRef,
useCallback,
useEffect,
useState,
} from '@wordpress/element';
type SubscriberCleanup = () => void;
type SubscriberResponse = SubscriberCleanup | void;
// This of course could've been more streamlined with internal state instead of
// refs, but then host hooks / components could not opt out of renders.
// This could've been exported to its own module, but the current build doesn't
// seem to work with module imports and I had no more time to spend on this...
function useResolvedElement< T extends HTMLElement >(
subscriber: ( element: T ) => SubscriberResponse,
refOrElement?: T | RefObject< T > | null
): RefCallback< T > {
const callbackRefElement = useRef< T | null >( null );
const lastReportRef = useRef< {
reporter: () => void;
element: T | null;
} | null >( null );
const cleanupRef = useRef< SubscriberResponse | null >();
const callSubscriber = useCallback( () => {
let element = null;
if ( callbackRefElement.current ) {
element = callbackRefElement.current;
} else if ( refOrElement ) {
if ( refOrElement instanceof HTMLElement ) {
element = refOrElement;
} else {
element = refOrElement.current;
}
}
if (
lastReportRef.current &&
lastReportRef.current.element === element &&
lastReportRef.current.reporter === callSubscriber
) {
return;
}
if ( cleanupRef.current ) {
cleanupRef.current();
// Making sure the cleanup is not called accidentally multiple times.
cleanupRef.current = null;
}
lastReportRef.current = {
reporter: callSubscriber,
element,
};
// Only calling the subscriber, if there's an actual element to report.
if ( element ) {
cleanupRef.current = subscriber( element );
}
}, [ refOrElement, subscriber ] );
// On each render, we check whether a ref changed, or if we got a new raw
// element.
useEffect( () => {
// With this we're *technically* supporting cases where ref objects' current value changes, but only if there's a
// render accompanying that change as well.
// To guarantee we always have the right element, one must use the ref callback provided instead, but we support
// RefObjects to make the hook API more convenient in certain cases.
callSubscriber();
}, [ callSubscriber ] );
return useCallback< RefCallback< T > >(
( element ) => {
callbackRefElement.current = element;
callSubscriber();
},
[ callSubscriber ]
);
}
type ObservedSize = {
width: number | undefined;
height: number | undefined;
};
type ResizeHandler = ( size: ObservedSize ) => void;
type HookResponse< T extends HTMLElement > = {
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.
type ResizeObserverBoxOptions =
| 'border-box'
| 'content-box'
| 'device-pixel-content-box';
declare global {
interface ResizeObserverEntry {
readonly devicePixelContentBoxSize: ReadonlyArray< ResizeObserverSize >;
}
}
// We're only using the first element of the size sequences, until future versions of the spec solidify on how
// exactly it'll be used for fragments in multi-column scenarios:
// From the spec:
// > The box size properties are exposed as FrozenArray in order to support elements that have multiple fragments,
// > which occur in multi-column scenarios. However the current definitions of content rect and border box do not
// > mention how those boxes are affected by multi-column layout. In this spec, there will only be a single
// > ResizeObserverSize returned in the FrozenArray, which will correspond to the dimensions of the first column.
// > A future version of this spec will extend the returned FrozenArray to contain the per-fragment size information.
// (https://drafts.csswg.org/resize-observer/#resize-observer-entry-interface)
//
// Also, testing these new box options revealed that in both Chrome and FF everything is returned in the callback,
// regardless of the "box" option.
// The spec states the following on this:
// > This does not have any impact on which box dimensions are returned to the defined callback when the event
// > is fired, it solely defines which box the author wishes to observe layout changes on.
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
// I'm not exactly clear on what this means, especially when you consider a later section stating the following:
// > This section is non-normative. An author may desire to observe more than one CSS box.
// > In this case, author will need to use multiple ResizeObservers.
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
// Which is clearly not how current browser implementations behave, and seems to contradict the previous quote.
// For this reason I decided to only return the requested size,
// even though it seems we have access to results for all box types.
// This also means that we get to keep the current api, being able to return a simple { width, height } pair,
// regardless of box option.
const extractSize = (
entry: ResizeObserverEntry,
boxProp: 'borderBoxSize' | 'contentBoxSize' | 'devicePixelContentBoxSize',
sizeType: keyof ResizeObserverSize
): number | undefined => {
if ( ! entry[ boxProp ] ) {
if ( boxProp === 'contentBoxSize' ) {
// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.
// See the 6th step in the description for the RO algorithm:
// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h
// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of "content-box".
// In real browser implementations of course these objects differ, but the width/height values should be equivalent.
return entry.contentRect[
sizeType === 'inlineSize' ? 'width' : 'height'
];
}
return undefined;
}
// A couple bytes smaller than calling Array.isArray() and just as effective here.
return entry[ boxProp ][ 0 ]
? entry[ boxProp ][ 0 ][ sizeType ]
: // TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's current
// behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.
// @ts-ignore
entry[ boxProp ][ sizeType ];
};
type RoundingFunction = ( n: number ) => number;
function useResizeObserver< T extends HTMLElement >(
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 ];
let boxProp:
| 'borderBoxSize'
| 'contentBoxSize'
| 'devicePixelContentBoxSize' = 'borderBoxSize';
if ( opts.box === 'border-box' ) {
boxProp = 'borderBoxSize';
} else {
boxProp =
opts.box === 'device-pixel-content-box'
? 'devicePixelContentBoxSize'
: 'contentBoxSize';
}
const reportedWidth = extractSize(
entry,
boxProp,
'inlineSize'
);
const reportedHeight = extractSize(
entry,
boxProp,
'blockSize'
);
const newWidth = reportedWidth
? round( reportedWidth )
: undefined;
const newHeight = reportedHeight
? 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 ? size.width : null, size ? size.height : null ]
);
}
/**
* Hook which allows to listen the resize event of any target element when it changes sizes.
* _Note: `useResizeObserver` will report `null` until after first render.
*
* @example
*
* ```js
* const App = () => {
* const [ resizeListener, sizes ] = useResizeObserver();
*
* return (
* <div>
* { resizeListener }
* Your content here
* </div>
* );
* };
* ```
*/
export default function useResizeAware(): [
ReactElement,
{ width: number | null; height: number | null },
] {
const { ref, width, height } = useResizeObserver();
const sizes = useMemo( () => {
return { width: width ?? null, height: height ?? null };
}, [ width, height ] );
const resizeListener = (
<div
style={ {
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
pointerEvents: 'none',
opacity: 0,
overflow: 'hidden',
zIndex: -1,
} }
aria-hidden="true"
ref={ ref }
/>
);
return [ resizeListener, sizes ];
}