@wordpress/compose 7.6.0 → 7.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -2
- package/README.md +41 -11
- package/build/hooks/use-copy-on-click/index.js +5 -5
- package/build/hooks/use-copy-on-click/index.js.map +1 -1
- package/build/hooks/use-drop-zone/index.js +1 -0
- package/build/hooks/use-drop-zone/index.js.map +1 -1
- package/build/hooks/use-event/index.js +51 -0
- package/build/hooks/use-event/index.js.map +1 -0
- package/build/hooks/use-focus-on-mount/index.js +11 -11
- package/build/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build/hooks/use-focus-outside/index.js +11 -11
- package/build/hooks/use-focus-outside/index.js.map +1 -1
- package/build/hooks/use-keyboard-shortcut/index.js +3 -3
- package/build/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build/hooks/use-merge-refs/index.js +12 -12
- package/build/hooks/use-merge-refs/index.js.map +1 -1
- package/build/hooks/use-ref-effect/index.js +4 -4
- package/build/hooks/use-ref-effect/index.js.map +1 -1
- package/build/hooks/use-resize-observer/_legacy/index.js +139 -0
- package/build/hooks/use-resize-observer/_legacy/index.js.map +1 -0
- package/build/hooks/use-resize-observer/_legacy/index.native.js.map +1 -0
- package/build/hooks/use-resize-observer/index.js +70 -212
- package/build/hooks/use-resize-observer/index.js.map +1 -1
- package/build/index.js +8 -0
- package/build/index.js.map +1 -1
- package/build-module/hooks/use-copy-on-click/index.js +5 -5
- package/build-module/hooks/use-copy-on-click/index.js.map +1 -1
- package/build-module/hooks/use-drop-zone/index.js +1 -0
- package/build-module/hooks/use-drop-zone/index.js.map +1 -1
- package/build-module/hooks/use-event/index.js +45 -0
- package/build-module/hooks/use-event/index.js.map +1 -0
- package/build-module/hooks/use-focus-on-mount/index.js +11 -11
- package/build-module/hooks/use-focus-on-mount/index.js.map +1 -1
- package/build-module/hooks/use-focus-outside/index.js +11 -11
- package/build-module/hooks/use-focus-outside/index.js.map +1 -1
- package/build-module/hooks/use-keyboard-shortcut/index.js +3 -3
- package/build-module/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build-module/hooks/use-merge-refs/index.js +12 -12
- package/build-module/hooks/use-merge-refs/index.js.map +1 -1
- package/build-module/hooks/use-ref-effect/index.js +4 -4
- package/build-module/hooks/use-ref-effect/index.js.map +1 -1
- package/build-module/hooks/use-resize-observer/_legacy/index.js +130 -0
- package/build-module/hooks/use-resize-observer/_legacy/index.js.map +1 -0
- package/build-module/hooks/use-resize-observer/_legacy/index.native.js.map +1 -0
- package/build-module/hooks/use-resize-observer/index.js +69 -213
- package/build-module/hooks/use-resize-observer/index.js.map +1 -1
- package/build-module/index.js +1 -0
- package/build-module/index.js.map +1 -1
- package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
- package/build-types/hooks/use-event/index.d.ts +32 -0
- package/build-types/hooks/use-event/index.d.ts.map +1 -0
- package/build-types/hooks/use-focus-on-mount/index.d.ts.map +1 -1
- package/build-types/hooks/use-merge-refs/index.d.ts.map +1 -1
- package/build-types/hooks/use-resize-observer/_legacy/index.d.ts +32 -0
- package/build-types/hooks/use-resize-observer/_legacy/index.d.ts.map +1 -0
- package/build-types/hooks/use-resize-observer/index.d.ts +42 -14
- package/build-types/hooks/use-resize-observer/index.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -0
- package/package.json +9 -9
- package/src/hooks/use-copy-on-click/index.js +5 -5
- package/src/hooks/use-drop-zone/index.js +2 -0
- package/src/hooks/use-event/index.ts +51 -0
- package/src/hooks/use-focus-on-mount/index.js +11 -13
- package/src/hooks/use-focus-outside/index.ts +11 -11
- package/src/hooks/use-keyboard-shortcut/index.js +3 -3
- package/src/hooks/use-merge-refs/index.js +15 -13
- package/src/hooks/use-ref-effect/index.ts +4 -4
- package/src/hooks/use-resize-observer/_legacy/index.tsx +143 -0
- package/src/hooks/use-resize-observer/{test → _legacy/test}/index.native.js +1 -1
- package/src/hooks/use-resize-observer/index.ts +119 -0
- package/src/index.js +1 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/build/hooks/use-resize-observer/index.native.js.map +0 -1
- package/build-module/hooks/use-resize-observer/index.native.js.map +0 -1
- package/src/hooks/use-resize-observer/index.tsx +0 -362
- /package/build/hooks/use-resize-observer/{index.native.js → _legacy/index.native.js} +0 -0
- /package/build-module/hooks/use-resize-observer/{index.native.js → _legacy/index.native.js} +0 -0
- /package/src/hooks/use-resize-observer/{index.native.js → _legacy/index.native.js} +0 -0
|
@@ -1,362 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import type { ReactElement, RefCallback, RefObject } from 'react';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* WordPress dependencies
|
|
8
|
-
*/
|
|
9
|
-
import {
|
|
10
|
-
useMemo,
|
|
11
|
-
useRef,
|
|
12
|
-
useCallback,
|
|
13
|
-
useEffect,
|
|
14
|
-
useState,
|
|
15
|
-
} from '@wordpress/element';
|
|
16
|
-
|
|
17
|
-
type SubscriberCleanup = () => void;
|
|
18
|
-
type SubscriberResponse = SubscriberCleanup | void;
|
|
19
|
-
|
|
20
|
-
// This of course could've been more streamlined with internal state instead of
|
|
21
|
-
// refs, but then host hooks / components could not opt out of renders.
|
|
22
|
-
// This could've been exported to its own module, but the current build doesn't
|
|
23
|
-
// seem to work with module imports and I had no more time to spend on this...
|
|
24
|
-
function useResolvedElement< T extends HTMLElement >(
|
|
25
|
-
subscriber: ( element: T ) => SubscriberResponse,
|
|
26
|
-
refOrElement?: T | RefObject< T > | null
|
|
27
|
-
): RefCallback< T > {
|
|
28
|
-
const callbackRefElement = useRef< T | null >( null );
|
|
29
|
-
const lastReportRef = useRef< {
|
|
30
|
-
reporter: () => void;
|
|
31
|
-
element: T | null;
|
|
32
|
-
} | null >( null );
|
|
33
|
-
const cleanupRef = useRef< SubscriberResponse | null >();
|
|
34
|
-
|
|
35
|
-
const callSubscriber = useCallback( () => {
|
|
36
|
-
let element = null;
|
|
37
|
-
if ( callbackRefElement.current ) {
|
|
38
|
-
element = callbackRefElement.current;
|
|
39
|
-
} else if ( refOrElement ) {
|
|
40
|
-
if ( refOrElement instanceof HTMLElement ) {
|
|
41
|
-
element = refOrElement;
|
|
42
|
-
} else {
|
|
43
|
-
element = refOrElement.current;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (
|
|
48
|
-
lastReportRef.current &&
|
|
49
|
-
lastReportRef.current.element === element &&
|
|
50
|
-
lastReportRef.current.reporter === callSubscriber
|
|
51
|
-
) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if ( cleanupRef.current ) {
|
|
56
|
-
cleanupRef.current();
|
|
57
|
-
// Making sure the cleanup is not called accidentally multiple times.
|
|
58
|
-
cleanupRef.current = null;
|
|
59
|
-
}
|
|
60
|
-
lastReportRef.current = {
|
|
61
|
-
reporter: callSubscriber,
|
|
62
|
-
element,
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// Only calling the subscriber, if there's an actual element to report.
|
|
66
|
-
if ( element ) {
|
|
67
|
-
cleanupRef.current = subscriber( element );
|
|
68
|
-
}
|
|
69
|
-
}, [ refOrElement, subscriber ] );
|
|
70
|
-
|
|
71
|
-
// On each render, we check whether a ref changed, or if we got a new raw
|
|
72
|
-
// element.
|
|
73
|
-
useEffect( () => {
|
|
74
|
-
// With this we're *technically* supporting cases where ref objects' current value changes, but only if there's a
|
|
75
|
-
// render accompanying that change as well.
|
|
76
|
-
// To guarantee we always have the right element, one must use the ref callback provided instead, but we support
|
|
77
|
-
// RefObjects to make the hook API more convenient in certain cases.
|
|
78
|
-
callSubscriber();
|
|
79
|
-
}, [ callSubscriber ] );
|
|
80
|
-
|
|
81
|
-
return useCallback< RefCallback< T > >(
|
|
82
|
-
( element ) => {
|
|
83
|
-
callbackRefElement.current = element;
|
|
84
|
-
callSubscriber();
|
|
85
|
-
},
|
|
86
|
-
[ callSubscriber ]
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
type ObservedSize = {
|
|
91
|
-
width: number | undefined;
|
|
92
|
-
height: number | undefined;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
type ResizeHandler = ( size: ObservedSize ) => void;
|
|
96
|
-
|
|
97
|
-
type HookResponse< T extends HTMLElement > = {
|
|
98
|
-
ref: RefCallback< T >;
|
|
99
|
-
} & ObservedSize;
|
|
100
|
-
|
|
101
|
-
// Declaring my own type here instead of using the one provided by TS (available since 4.2.2), because this way I'm not
|
|
102
|
-
// forcing consumers to use a specific TS version.
|
|
103
|
-
type ResizeObserverBoxOptions =
|
|
104
|
-
| 'border-box'
|
|
105
|
-
| 'content-box'
|
|
106
|
-
| 'device-pixel-content-box';
|
|
107
|
-
|
|
108
|
-
declare global {
|
|
109
|
-
interface ResizeObserverEntry {
|
|
110
|
-
readonly devicePixelContentBoxSize: ReadonlyArray< ResizeObserverSize >;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// We're only using the first element of the size sequences, until future versions of the spec solidify on how
|
|
115
|
-
// exactly it'll be used for fragments in multi-column scenarios:
|
|
116
|
-
// From the spec:
|
|
117
|
-
// > The box size properties are exposed as FrozenArray in order to support elements that have multiple fragments,
|
|
118
|
-
// > which occur in multi-column scenarios. However the current definitions of content rect and border box do not
|
|
119
|
-
// > mention how those boxes are affected by multi-column layout. In this spec, there will only be a single
|
|
120
|
-
// > ResizeObserverSize returned in the FrozenArray, which will correspond to the dimensions of the first column.
|
|
121
|
-
// > A future version of this spec will extend the returned FrozenArray to contain the per-fragment size information.
|
|
122
|
-
// (https://drafts.csswg.org/resize-observer/#resize-observer-entry-interface)
|
|
123
|
-
//
|
|
124
|
-
// Also, testing these new box options revealed that in both Chrome and FF everything is returned in the callback,
|
|
125
|
-
// regardless of the "box" option.
|
|
126
|
-
// The spec states the following on this:
|
|
127
|
-
// > This does not have any impact on which box dimensions are returned to the defined callback when the event
|
|
128
|
-
// > is fired, it solely defines which box the author wishes to observe layout changes on.
|
|
129
|
-
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
|
|
130
|
-
// I'm not exactly clear on what this means, especially when you consider a later section stating the following:
|
|
131
|
-
// > This section is non-normative. An author may desire to observe more than one CSS box.
|
|
132
|
-
// > In this case, author will need to use multiple ResizeObservers.
|
|
133
|
-
// (https://drafts.csswg.org/resize-observer/#resize-observer-interface)
|
|
134
|
-
// Which is clearly not how current browser implementations behave, and seems to contradict the previous quote.
|
|
135
|
-
// For this reason I decided to only return the requested size,
|
|
136
|
-
// even though it seems we have access to results for all box types.
|
|
137
|
-
// This also means that we get to keep the current api, being able to return a simple { width, height } pair,
|
|
138
|
-
// regardless of box option.
|
|
139
|
-
const extractSize = (
|
|
140
|
-
entry: ResizeObserverEntry,
|
|
141
|
-
boxProp: 'borderBoxSize' | 'contentBoxSize' | 'devicePixelContentBoxSize',
|
|
142
|
-
sizeType: keyof ResizeObserverSize
|
|
143
|
-
): number | undefined => {
|
|
144
|
-
if ( ! entry[ boxProp ] ) {
|
|
145
|
-
if ( boxProp === 'contentBoxSize' ) {
|
|
146
|
-
// The dimensions in `contentBoxSize` and `contentRect` are equivalent according to the spec.
|
|
147
|
-
// See the 6th step in the description for the RO algorithm:
|
|
148
|
-
// https://drafts.csswg.org/resize-observer/#create-and-populate-resizeobserverentry-h
|
|
149
|
-
// > Set this.contentRect to logical this.contentBoxSize given target and observedBox of "content-box".
|
|
150
|
-
// In real browser implementations of course these objects differ, but the width/height values should be equivalent.
|
|
151
|
-
return entry.contentRect[
|
|
152
|
-
sizeType === 'inlineSize' ? 'width' : 'height'
|
|
153
|
-
];
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return undefined;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// A couple bytes smaller than calling Array.isArray() and just as effective here.
|
|
160
|
-
return entry[ boxProp ][ 0 ]
|
|
161
|
-
? entry[ boxProp ][ 0 ][ sizeType ]
|
|
162
|
-
: // TS complains about this, because the RO entry type follows the spec and does not reflect Firefox's current
|
|
163
|
-
// behaviour of returning objects instead of arrays for `borderBoxSize` and `contentBoxSize`.
|
|
164
|
-
// @ts-ignore
|
|
165
|
-
entry[ boxProp ][ sizeType ];
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
type RoundingFunction = ( n: number ) => number;
|
|
169
|
-
|
|
170
|
-
function useResizeObserver< T extends HTMLElement >(
|
|
171
|
-
opts: {
|
|
172
|
-
ref?: RefObject< T > | T | null | undefined;
|
|
173
|
-
onResize?: ResizeHandler;
|
|
174
|
-
box?: ResizeObserverBoxOptions;
|
|
175
|
-
round?: RoundingFunction;
|
|
176
|
-
} = {}
|
|
177
|
-
): HookResponse< T > {
|
|
178
|
-
// Saving the callback as a ref. With this, I don't need to put onResize in the
|
|
179
|
-
// effect dep array, and just passing in an anonymous function without memoising
|
|
180
|
-
// will not reinstantiate the hook's ResizeObserver.
|
|
181
|
-
const onResize = opts.onResize;
|
|
182
|
-
const onResizeRef = useRef< ResizeHandler | undefined >( undefined );
|
|
183
|
-
onResizeRef.current = onResize;
|
|
184
|
-
const round = opts.round || Math.round;
|
|
185
|
-
|
|
186
|
-
// Using a single instance throughout the hook's lifetime
|
|
187
|
-
const resizeObserverRef = useRef< {
|
|
188
|
-
box?: ResizeObserverBoxOptions;
|
|
189
|
-
round?: RoundingFunction;
|
|
190
|
-
instance: ResizeObserver;
|
|
191
|
-
} >();
|
|
192
|
-
|
|
193
|
-
const [ size, setSize ] = useState< {
|
|
194
|
-
width?: number;
|
|
195
|
-
height?: number;
|
|
196
|
-
} >( {
|
|
197
|
-
width: undefined,
|
|
198
|
-
height: undefined,
|
|
199
|
-
} );
|
|
200
|
-
|
|
201
|
-
// In certain edge cases the RO might want to report a size change just after
|
|
202
|
-
// the component unmounted.
|
|
203
|
-
const didUnmount = useRef( false );
|
|
204
|
-
useEffect( () => {
|
|
205
|
-
didUnmount.current = false;
|
|
206
|
-
return () => {
|
|
207
|
-
didUnmount.current = true;
|
|
208
|
-
};
|
|
209
|
-
}, [] );
|
|
210
|
-
|
|
211
|
-
// Using a ref to track the previous width / height to avoid unnecessary renders.
|
|
212
|
-
const previous: {
|
|
213
|
-
current: {
|
|
214
|
-
width?: number;
|
|
215
|
-
height?: number;
|
|
216
|
-
};
|
|
217
|
-
} = useRef( {
|
|
218
|
-
width: undefined,
|
|
219
|
-
height: undefined,
|
|
220
|
-
} );
|
|
221
|
-
|
|
222
|
-
// This block is kinda like a useEffect, only it's called whenever a new
|
|
223
|
-
// element could be resolved based on the ref option. It also has a cleanup
|
|
224
|
-
// function.
|
|
225
|
-
const refCallback = useResolvedElement< T >(
|
|
226
|
-
useCallback(
|
|
227
|
-
( element ) => {
|
|
228
|
-
// We only use a single Resize Observer instance, and we're instantiating it on demand, only once there's something to observe.
|
|
229
|
-
// 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.
|
|
230
|
-
if (
|
|
231
|
-
! resizeObserverRef.current ||
|
|
232
|
-
resizeObserverRef.current.box !== opts.box ||
|
|
233
|
-
resizeObserverRef.current.round !== round
|
|
234
|
-
) {
|
|
235
|
-
resizeObserverRef.current = {
|
|
236
|
-
box: opts.box,
|
|
237
|
-
round,
|
|
238
|
-
instance: new ResizeObserver( ( entries ) => {
|
|
239
|
-
const entry = entries[ 0 ];
|
|
240
|
-
|
|
241
|
-
let boxProp:
|
|
242
|
-
| 'borderBoxSize'
|
|
243
|
-
| 'contentBoxSize'
|
|
244
|
-
| 'devicePixelContentBoxSize' = 'borderBoxSize';
|
|
245
|
-
if ( opts.box === 'border-box' ) {
|
|
246
|
-
boxProp = 'borderBoxSize';
|
|
247
|
-
} else {
|
|
248
|
-
boxProp =
|
|
249
|
-
opts.box === 'device-pixel-content-box'
|
|
250
|
-
? 'devicePixelContentBoxSize'
|
|
251
|
-
: 'contentBoxSize';
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const reportedWidth = extractSize(
|
|
255
|
-
entry,
|
|
256
|
-
boxProp,
|
|
257
|
-
'inlineSize'
|
|
258
|
-
);
|
|
259
|
-
const reportedHeight = extractSize(
|
|
260
|
-
entry,
|
|
261
|
-
boxProp,
|
|
262
|
-
'blockSize'
|
|
263
|
-
);
|
|
264
|
-
|
|
265
|
-
const newWidth = reportedWidth
|
|
266
|
-
? round( reportedWidth )
|
|
267
|
-
: undefined;
|
|
268
|
-
const newHeight = reportedHeight
|
|
269
|
-
? round( reportedHeight )
|
|
270
|
-
: undefined;
|
|
271
|
-
|
|
272
|
-
if (
|
|
273
|
-
previous.current.width !== newWidth ||
|
|
274
|
-
previous.current.height !== newHeight
|
|
275
|
-
) {
|
|
276
|
-
const newSize = {
|
|
277
|
-
width: newWidth,
|
|
278
|
-
height: newHeight,
|
|
279
|
-
};
|
|
280
|
-
previous.current.width = newWidth;
|
|
281
|
-
previous.current.height = newHeight;
|
|
282
|
-
if ( onResizeRef.current ) {
|
|
283
|
-
onResizeRef.current( newSize );
|
|
284
|
-
} else if ( ! didUnmount.current ) {
|
|
285
|
-
setSize( newSize );
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
} ),
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
resizeObserverRef.current.instance.observe( element, {
|
|
293
|
-
box: opts.box,
|
|
294
|
-
} );
|
|
295
|
-
|
|
296
|
-
return () => {
|
|
297
|
-
if ( resizeObserverRef.current ) {
|
|
298
|
-
resizeObserverRef.current.instance.unobserve( element );
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
},
|
|
302
|
-
[ opts.box, round ]
|
|
303
|
-
),
|
|
304
|
-
opts.ref
|
|
305
|
-
);
|
|
306
|
-
|
|
307
|
-
return useMemo(
|
|
308
|
-
() => ( {
|
|
309
|
-
ref: refCallback,
|
|
310
|
-
width: size.width,
|
|
311
|
-
height: size.height,
|
|
312
|
-
} ),
|
|
313
|
-
[ refCallback, size ? size.width : null, size ? size.height : null ]
|
|
314
|
-
);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Hook which allows to listen the resize event of any target element when it changes sizes.
|
|
319
|
-
* _Note: `useResizeObserver` will report `null` until after first render.
|
|
320
|
-
*
|
|
321
|
-
* @example
|
|
322
|
-
*
|
|
323
|
-
* ```js
|
|
324
|
-
* const App = () => {
|
|
325
|
-
* const [ resizeListener, sizes ] = useResizeObserver();
|
|
326
|
-
*
|
|
327
|
-
* return (
|
|
328
|
-
* <div>
|
|
329
|
-
* { resizeListener }
|
|
330
|
-
* Your content here
|
|
331
|
-
* </div>
|
|
332
|
-
* );
|
|
333
|
-
* };
|
|
334
|
-
* ```
|
|
335
|
-
*/
|
|
336
|
-
export default function useResizeAware(): [
|
|
337
|
-
ReactElement,
|
|
338
|
-
{ width: number | null; height: number | null },
|
|
339
|
-
] {
|
|
340
|
-
const { ref, width, height } = useResizeObserver();
|
|
341
|
-
const sizes = useMemo( () => {
|
|
342
|
-
return { width: width ?? null, height: height ?? null };
|
|
343
|
-
}, [ width, height ] );
|
|
344
|
-
const resizeListener = (
|
|
345
|
-
<div
|
|
346
|
-
style={ {
|
|
347
|
-
position: 'absolute',
|
|
348
|
-
top: 0,
|
|
349
|
-
left: 0,
|
|
350
|
-
right: 0,
|
|
351
|
-
bottom: 0,
|
|
352
|
-
pointerEvents: 'none',
|
|
353
|
-
opacity: 0,
|
|
354
|
-
overflow: 'hidden',
|
|
355
|
-
zIndex: -1,
|
|
356
|
-
} }
|
|
357
|
-
aria-hidden="true"
|
|
358
|
-
ref={ ref }
|
|
359
|
-
/>
|
|
360
|
-
);
|
|
361
|
-
return [ resizeListener, sizes ];
|
|
362
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|