@wordpress/compose 5.16.0 → 5.17.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 +2 -0
- package/README.md +36 -3
- package/build/higher-order/with-global-events/listener.js +7 -7
- package/build/higher-order/with-global-events/listener.js.map +1 -1
- package/build/higher-order/with-safe-timeout/index.js +1 -7
- package/build/higher-order/with-safe-timeout/index.js.map +1 -1
- package/build/hooks/use-disabled/index.js +1 -7
- package/build/hooks/use-disabled/index.js.map +1 -1
- package/build/hooks/use-drop-zone/index.js +2 -10
- package/build/hooks/use-drop-zone/index.js.map +1 -1
- package/build/hooks/use-focus-outside/index.js +2 -8
- package/build/hooks/use-focus-outside/index.js.map +1 -1
- package/build/hooks/use-focus-outside/index.native.js +2 -8
- package/build/hooks/use-focus-outside/index.native.js.map +1 -1
- package/build/hooks/use-keyboard-shortcut/index.js +2 -3
- package/build/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build/hooks/use-merge-refs/index.js +4 -2
- package/build/hooks/use-merge-refs/index.js.map +1 -1
- package/build/hooks/use-throttle/index.js +12 -8
- package/build/hooks/use-throttle/index.js.map +1 -1
- package/build/index.js +14 -0
- package/build/index.js.map +1 -1
- package/build/index.native.js +14 -0
- package/build/index.native.js.map +1 -1
- package/build/utils/throttle/index.js +98 -0
- package/build/utils/throttle/index.js.map +1 -0
- package/build-module/higher-order/with-global-events/listener.js +7 -6
- package/build-module/higher-order/with-global-events/listener.js.map +1 -1
- package/build-module/higher-order/with-safe-timeout/index.js +1 -6
- package/build-module/higher-order/with-safe-timeout/index.js.map +1 -1
- package/build-module/hooks/use-disabled/index.js +1 -6
- package/build-module/hooks/use-disabled/index.js.map +1 -1
- package/build-module/hooks/use-drop-zone/index.js +2 -10
- package/build-module/hooks/use-drop-zone/index.js.map +1 -1
- package/build-module/hooks/use-focus-outside/index.js +2 -7
- package/build-module/hooks/use-focus-outside/index.js.map +1 -1
- package/build-module/hooks/use-focus-outside/index.native.js +2 -7
- package/build-module/hooks/use-focus-outside/index.native.js.map +1 -1
- package/build-module/hooks/use-keyboard-shortcut/index.js +2 -2
- package/build-module/hooks/use-keyboard-shortcut/index.js.map +1 -1
- package/build-module/hooks/use-merge-refs/index.js +4 -2
- package/build-module/hooks/use-merge-refs/index.js.map +1 -1
- package/build-module/hooks/use-throttle/index.js +11 -7
- package/build-module/hooks/use-throttle/index.js.map +1 -1
- package/build-module/index.js +3 -1
- package/build-module/index.js.map +1 -1
- package/build-module/index.native.js +3 -1
- package/build-module/index.native.js.map +1 -1
- package/build-module/utils/throttle/index.js +88 -0
- package/build-module/utils/throttle/index.js.map +1 -0
- package/build-types/higher-order/with-global-events/listener.d.ts.map +1 -1
- package/build-types/higher-order/with-safe-timeout/index.d.ts.map +1 -1
- package/build-types/hooks/use-disabled/index.d.ts.map +1 -1
- package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
- package/build-types/hooks/use-focus-outside/index.d.ts.map +1 -1
- package/build-types/hooks/use-keyboard-shortcut/index.d.ts.map +1 -1
- package/build-types/hooks/use-merge-refs/index.d.ts.map +1 -1
- package/build-types/hooks/use-throttle/index.d.ts +6 -6
- package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -0
- package/build-types/utils/throttle/index.d.ts +77 -0
- package/build-types/utils/throttle/index.d.ts.map +1 -0
- package/package.json +8 -10
- package/src/higher-order/with-global-events/listener.js +6 -8
- package/src/higher-order/with-safe-timeout/index.tsx +1 -6
- package/src/hooks/use-disabled/index.js +1 -7
- package/src/hooks/use-drop-zone/index.js +4 -13
- package/src/hooks/use-focus-outside/index.js +2 -8
- package/src/hooks/use-focus-outside/index.native.js +2 -8
- package/src/hooks/use-keyboard-shortcut/index.js +4 -2
- package/src/hooks/use-merge-refs/index.js +6 -1
- package/src/hooks/use-merge-refs/test/index.js +44 -0
- package/src/hooks/use-throttle/index.js +11 -7
- package/src/index.js +2 -0
- package/src/index.native.js +2 -0
- package/src/utils/throttle/index.ts +95 -0
- package/src/utils/throttle/test/index.ts +256 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parts of this source were derived and modified from lodash,
|
|
3
|
+
* released under the MIT license.
|
|
4
|
+
*
|
|
5
|
+
* https://github.com/lodash/lodash
|
|
6
|
+
*
|
|
7
|
+
* Copyright JS Foundation and other contributors <https://js.foundation/>
|
|
8
|
+
*
|
|
9
|
+
* Based on Underscore.js, copyright Jeremy Ashkenas,
|
|
10
|
+
* DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>
|
|
11
|
+
*
|
|
12
|
+
* This software consists of voluntary contributions made by many
|
|
13
|
+
* individuals. For exact contribution history, see the revision history
|
|
14
|
+
* available at https://github.com/lodash/lodash
|
|
15
|
+
*
|
|
16
|
+
* The following license applies to all parts of this software except as
|
|
17
|
+
* documented below:
|
|
18
|
+
*
|
|
19
|
+
* ====
|
|
20
|
+
*
|
|
21
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
22
|
+
* a copy of this software and associated documentation files (the
|
|
23
|
+
* "Software"), to deal in the Software without restriction, including
|
|
24
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
25
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
26
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
27
|
+
* the following conditions:
|
|
28
|
+
*
|
|
29
|
+
* The above copyright notice and this permission notice shall be
|
|
30
|
+
* included in all copies or substantial portions of the Software.
|
|
31
|
+
*
|
|
32
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
33
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
34
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
35
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
36
|
+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
37
|
+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
38
|
+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
39
|
+
*/
|
|
40
|
+
export interface ThrottleOptions {
|
|
41
|
+
leading?: boolean;
|
|
42
|
+
trailing?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* A simplified and properly typed version of lodash's `throttle`, that
|
|
46
|
+
* always uses timers instead of sometimes using rAF.
|
|
47
|
+
*
|
|
48
|
+
* Creates a throttled function that only invokes `func` at most once per
|
|
49
|
+
* every `wait` milliseconds. The throttled function comes with a `cancel`
|
|
50
|
+
* method to cancel delayed `func` invocations and a `flush` method to
|
|
51
|
+
* immediately invoke them. Provide `options` to indicate whether `func`
|
|
52
|
+
* should be invoked on the leading and/or trailing edge of the `wait`
|
|
53
|
+
* timeout. The `func` is invoked with the last arguments provided to the
|
|
54
|
+
* throttled function. Subsequent calls to the throttled function return
|
|
55
|
+
* the result of the last `func` invocation.
|
|
56
|
+
*
|
|
57
|
+
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
58
|
+
* invoked on the trailing edge of the timeout only if the throttled function
|
|
59
|
+
* is invoked more than once during the `wait` timeout.
|
|
60
|
+
*
|
|
61
|
+
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
62
|
+
* until the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
63
|
+
*
|
|
64
|
+
* @param {Function} func The function to throttle.
|
|
65
|
+
* @param {number} wait The number of milliseconds to throttle invocations to.
|
|
66
|
+
* @param {Partial< ThrottleOptions >} options The options object.
|
|
67
|
+
* @param {boolean} options.leading Specify invoking on the leading edge of the timeout.
|
|
68
|
+
* @param {boolean} options.trailing Specify invoking on the trailing edge of the timeout.
|
|
69
|
+
* @return Returns the new throttled function.
|
|
70
|
+
*/
|
|
71
|
+
export declare const throttle: <FunctionT extends (...args: unknown[]) => unknown>(func: FunctionT, wait: number, options?: ThrottleOptions | undefined) => {
|
|
72
|
+
(this: unknown, ...args: Parameters<FunctionT>): ReturnType<FunctionT>;
|
|
73
|
+
cancel: () => void;
|
|
74
|
+
flush: () => ReturnType<FunctionT>;
|
|
75
|
+
pending: () => boolean;
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/throttle/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AAOH,MAAM,WAAW,eAAe;IAC/B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,eAAO,MAAM,QAAQ,+BAAkC,OAAO,EAAE,KAAM,OAAO,yBAEtE,MAAM;;;;;CAeZ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/compose",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.17.0",
|
|
4
4
|
"description": "WordPress higher-order components (HOCs).",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
@@ -30,17 +30,15 @@
|
|
|
30
30
|
"sideEffects": false,
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"@babel/runtime": "^7.16.0",
|
|
33
|
-
"@types/lodash": "^4.14.172",
|
|
34
33
|
"@types/mousetrap": "^1.6.8",
|
|
35
|
-
"@wordpress/deprecated": "^3.
|
|
36
|
-
"@wordpress/dom": "^3.
|
|
37
|
-
"@wordpress/element": "^4.
|
|
38
|
-
"@wordpress/is-shallow-equal": "^4.
|
|
39
|
-
"@wordpress/keycodes": "^3.
|
|
40
|
-
"@wordpress/priority-queue": "^2.
|
|
34
|
+
"@wordpress/deprecated": "^3.19.0",
|
|
35
|
+
"@wordpress/dom": "^3.19.0",
|
|
36
|
+
"@wordpress/element": "^4.17.0",
|
|
37
|
+
"@wordpress/is-shallow-equal": "^4.19.0",
|
|
38
|
+
"@wordpress/keycodes": "^3.19.0",
|
|
39
|
+
"@wordpress/priority-queue": "^2.19.0",
|
|
41
40
|
"change-case": "^4.1.2",
|
|
42
41
|
"clipboard": "^2.0.8",
|
|
43
|
-
"lodash": "^4.17.21",
|
|
44
42
|
"mousetrap": "^1.6.5",
|
|
45
43
|
"use-memo-one": "^1.1.1"
|
|
46
44
|
},
|
|
@@ -50,5 +48,5 @@
|
|
|
50
48
|
"publishConfig": {
|
|
51
49
|
"access": "public"
|
|
52
50
|
},
|
|
53
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "8d42d2febb7d0ba8372a33e560a62f5a5f6a9112"
|
|
54
52
|
}
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { without } from 'lodash';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* Class responsible for orchestrating event handling on the global window,
|
|
8
3
|
* binding a single event to be shared across all handling instances, and
|
|
@@ -27,9 +22,12 @@ class Listener {
|
|
|
27
22
|
}
|
|
28
23
|
|
|
29
24
|
remove( /** @type {any} */ eventType, /** @type {any} */ instance ) {
|
|
30
|
-
this.listeners[ eventType ]
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
if ( ! this.listeners[ eventType ] ) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
this.listeners[ eventType ] = this.listeners[ eventType ].filter(
|
|
30
|
+
( /** @type {any} */ listener ) => listener !== instance
|
|
33
31
|
);
|
|
34
32
|
|
|
35
33
|
if ( ! this.listeners[ eventType ].length ) {
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { without } from 'lodash';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* WordPress dependencies
|
|
8
3
|
*/
|
|
@@ -64,7 +59,7 @@ const withSafeTimeout = createHigherOrderComponent(
|
|
|
64
59
|
|
|
65
60
|
clearTimeout( id: number ) {
|
|
66
61
|
clearTimeout( id );
|
|
67
|
-
this.timeouts
|
|
62
|
+
this.timeouts.filter( ( timeoutId ) => timeoutId !== id );
|
|
68
63
|
}
|
|
69
64
|
|
|
70
65
|
render() {
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { includes } from 'lodash';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* WordPress dependencies
|
|
8
3
|
*/
|
|
@@ -89,8 +84,7 @@ export default function useDisabled( {
|
|
|
89
84
|
|
|
90
85
|
focus.focusable.find( node ).forEach( ( focusable ) => {
|
|
91
86
|
if (
|
|
92
|
-
includes(
|
|
93
|
-
DISABLED_ELIGIBLE_NODE_NAMES,
|
|
87
|
+
DISABLED_ELIGIBLE_NODE_NAMES.includes(
|
|
94
88
|
focusable.nodeName
|
|
95
89
|
) &&
|
|
96
90
|
// @ts-ignore
|
|
@@ -107,11 +107,6 @@ export default function useDropZone( {
|
|
|
107
107
|
|
|
108
108
|
isDragging = true;
|
|
109
109
|
|
|
110
|
-
ownerDocument.removeEventListener(
|
|
111
|
-
'dragenter',
|
|
112
|
-
maybeDragStart
|
|
113
|
-
);
|
|
114
|
-
|
|
115
110
|
// Note that `dragend` doesn't fire consistently for file and
|
|
116
111
|
// HTML drag events where the drag origin is outside the browser
|
|
117
112
|
// window. In Firefox it may also not fire if the originating
|
|
@@ -202,7 +197,6 @@ export default function useDropZone( {
|
|
|
202
197
|
|
|
203
198
|
isDragging = false;
|
|
204
199
|
|
|
205
|
-
ownerDocument.addEventListener( 'dragenter', maybeDragStart );
|
|
206
200
|
ownerDocument.removeEventListener( 'dragend', maybeDragEnd );
|
|
207
201
|
ownerDocument.removeEventListener( 'mousemove', maybeDragEnd );
|
|
208
202
|
|
|
@@ -221,12 +215,6 @@ export default function useDropZone( {
|
|
|
221
215
|
ownerDocument.addEventListener( 'dragenter', maybeDragStart );
|
|
222
216
|
|
|
223
217
|
return () => {
|
|
224
|
-
onDropRef.current = null;
|
|
225
|
-
onDragStartRef.current = null;
|
|
226
|
-
onDragEnterRef.current = null;
|
|
227
|
-
onDragLeaveRef.current = null;
|
|
228
|
-
onDragEndRef.current = null;
|
|
229
|
-
onDragOverRef.current = null;
|
|
230
218
|
delete element.dataset.isDropZone;
|
|
231
219
|
element.removeEventListener( 'drop', onDrop );
|
|
232
220
|
element.removeEventListener( 'dragenter', onDragEnter );
|
|
@@ -234,7 +222,10 @@ export default function useDropZone( {
|
|
|
234
222
|
element.removeEventListener( 'dragleave', onDragLeave );
|
|
235
223
|
ownerDocument.removeEventListener( 'dragend', maybeDragEnd );
|
|
236
224
|
ownerDocument.removeEventListener( 'mousemove', maybeDragEnd );
|
|
237
|
-
ownerDocument.
|
|
225
|
+
ownerDocument.removeEventListener(
|
|
226
|
+
'dragenter',
|
|
227
|
+
maybeDragStart
|
|
228
|
+
);
|
|
238
229
|
};
|
|
239
230
|
},
|
|
240
231
|
[ isDisabled ]
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { includes } from 'lodash';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* WordPress dependencies
|
|
8
3
|
*/
|
|
@@ -42,8 +37,7 @@ function isFocusNormalizedButton( eventTarget ) {
|
|
|
42
37
|
return true;
|
|
43
38
|
|
|
44
39
|
case 'INPUT':
|
|
45
|
-
return includes(
|
|
46
|
-
INPUT_BUTTON_TYPES,
|
|
40
|
+
return INPUT_BUTTON_TYPES.includes(
|
|
47
41
|
/** @type {HTMLInputElement} */ ( eventTarget ).type
|
|
48
42
|
);
|
|
49
43
|
}
|
|
@@ -136,7 +130,7 @@ export default function useFocusOutside( onFocusOutside ) {
|
|
|
136
130
|
*/
|
|
137
131
|
const normalizeButtonFocus = useCallback( ( event ) => {
|
|
138
132
|
const { type, target } = event;
|
|
139
|
-
const isInteractionEnd =
|
|
133
|
+
const isInteractionEnd = [ 'mouseup', 'touchend' ].includes( type );
|
|
140
134
|
|
|
141
135
|
if ( isInteractionEnd ) {
|
|
142
136
|
preventBlurCheck.current = false;
|
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import { includes } from 'lodash';
|
|
5
|
-
|
|
6
1
|
/**
|
|
7
2
|
* WordPress dependencies
|
|
8
3
|
*/
|
|
@@ -39,8 +34,7 @@ function isFocusNormalizedButton( eventTarget ) {
|
|
|
39
34
|
return true;
|
|
40
35
|
|
|
41
36
|
case 'INPUT':
|
|
42
|
-
return includes(
|
|
43
|
-
INPUT_BUTTON_TYPES,
|
|
37
|
+
return INPUT_BUTTON_TYPES.includes(
|
|
44
38
|
/** @type {HTMLInputElement} */ ( eventTarget ).type
|
|
45
39
|
);
|
|
46
40
|
}
|
|
@@ -133,7 +127,7 @@ export default function useFocusOutside( onFocusOutside ) {
|
|
|
133
127
|
*/
|
|
134
128
|
const normalizeButtonFocus = useCallback( ( event ) => {
|
|
135
129
|
const { type, target } = event;
|
|
136
|
-
const isInteractionEnd =
|
|
130
|
+
const isInteractionEnd = [ 'mouseup', 'touchend' ].includes( type );
|
|
137
131
|
|
|
138
132
|
if ( isInteractionEnd ) {
|
|
139
133
|
preventBlurCheck.current = false;
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
*/
|
|
4
4
|
import Mousetrap from 'mousetrap';
|
|
5
5
|
import 'mousetrap/plugins/global-bind/mousetrap-global-bind';
|
|
6
|
-
import { castArray } from 'lodash';
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* WordPress dependencies
|
|
@@ -60,7 +59,10 @@ function useKeyboardShortcut(
|
|
|
60
59
|
// necessary to maintain the existing behavior.
|
|
61
60
|
/** @type {Element} */ ( /** @type {unknown} */ ( document ) )
|
|
62
61
|
);
|
|
63
|
-
|
|
62
|
+
const shortcutsArray = Array.isArray( shortcuts )
|
|
63
|
+
? shortcuts
|
|
64
|
+
: [ shortcuts ];
|
|
65
|
+
shortcutsArray.forEach( ( shortcut ) => {
|
|
64
66
|
const keys = shortcut.split( '+' );
|
|
65
67
|
// Determines whether a key is a modifier by the length of the string.
|
|
66
68
|
// E.g. if I add a pass a shortcut Shift+Cmd+M, it'll determine that
|
|
@@ -71,6 +71,7 @@ function assignRef( ref, value ) {
|
|
|
71
71
|
*/
|
|
72
72
|
export default function useMergeRefs( refs ) {
|
|
73
73
|
const element = useRef();
|
|
74
|
+
const isAttached = useRef( false );
|
|
74
75
|
const didElementChange = useRef( false );
|
|
75
76
|
/* eslint-disable jsdoc/no-undefined-types */
|
|
76
77
|
/** @type {import('react').MutableRefObject<TRef[]>} */
|
|
@@ -86,7 +87,10 @@ export default function useMergeRefs( refs ) {
|
|
|
86
87
|
// ref with the node, except when the element changes in the same cycle, in
|
|
87
88
|
// which case the ref callbacks will already have been called.
|
|
88
89
|
useLayoutEffect( () => {
|
|
89
|
-
if (
|
|
90
|
+
if (
|
|
91
|
+
didElementChange.current === false &&
|
|
92
|
+
isAttached.current === true
|
|
93
|
+
) {
|
|
90
94
|
refs.forEach( ( ref, index ) => {
|
|
91
95
|
const previousRef = previousRefs.current[ index ];
|
|
92
96
|
if ( ref !== previousRef ) {
|
|
@@ -113,6 +117,7 @@ export default function useMergeRefs( refs ) {
|
|
|
113
117
|
assignRef( element, value );
|
|
114
118
|
|
|
115
119
|
didElementChange.current = true;
|
|
120
|
+
isAttached.current = value !== null;
|
|
116
121
|
|
|
117
122
|
// When an element changes, the current ref callback should be called
|
|
118
123
|
// with the new element and the previous one with `null`.
|
|
@@ -41,6 +41,7 @@ describe( 'useMergeRefs', () => {
|
|
|
41
41
|
tagName: TagName = 'div',
|
|
42
42
|
disable1,
|
|
43
43
|
disable2,
|
|
44
|
+
unused,
|
|
44
45
|
} ) {
|
|
45
46
|
function refCallback1( value ) {
|
|
46
47
|
refCallback1.history.push( value );
|
|
@@ -63,6 +64,10 @@ describe( 'useMergeRefs', () => {
|
|
|
63
64
|
! disable2 && ref2,
|
|
64
65
|
] );
|
|
65
66
|
|
|
67
|
+
if ( unused ) {
|
|
68
|
+
return <TagName ref={ ref1 } />;
|
|
69
|
+
}
|
|
70
|
+
|
|
66
71
|
return <TagName ref={ mergedRefs } />;
|
|
67
72
|
}
|
|
68
73
|
|
|
@@ -327,4 +332,43 @@ describe( 'useMergeRefs', () => {
|
|
|
327
332
|
[ [], [ originalElement, null ] ],
|
|
328
333
|
] );
|
|
329
334
|
} );
|
|
335
|
+
|
|
336
|
+
it( 'should allow the hook being unused', () => {
|
|
337
|
+
const rootElement = document.getElementById( 'root' );
|
|
338
|
+
|
|
339
|
+
ReactDOM.render( <MergedRefs unused />, rootElement );
|
|
340
|
+
|
|
341
|
+
const originalElement = rootElement.firstElementChild;
|
|
342
|
+
|
|
343
|
+
// Render 1: ref 1 should updated, ref 2 should not.
|
|
344
|
+
expect( renderCallback.history ).toEqual( [
|
|
345
|
+
[ [ originalElement ], [] ],
|
|
346
|
+
] );
|
|
347
|
+
|
|
348
|
+
ReactDOM.render( <MergedRefs />, rootElement );
|
|
349
|
+
|
|
350
|
+
// Render 2: ref 2 should be updated as well.
|
|
351
|
+
expect( renderCallback.history ).toEqual( [
|
|
352
|
+
[ [ originalElement, null, originalElement ], [ originalElement ] ],
|
|
353
|
+
[ [], [] ],
|
|
354
|
+
] );
|
|
355
|
+
|
|
356
|
+
ReactDOM.render( <MergedRefs unused />, rootElement );
|
|
357
|
+
|
|
358
|
+
// Render 3: ref 2 should be updated with null
|
|
359
|
+
expect( renderCallback.history ).toEqual( [
|
|
360
|
+
[
|
|
361
|
+
[
|
|
362
|
+
originalElement,
|
|
363
|
+
null,
|
|
364
|
+
originalElement,
|
|
365
|
+
null,
|
|
366
|
+
originalElement,
|
|
367
|
+
],
|
|
368
|
+
[ originalElement, null ],
|
|
369
|
+
],
|
|
370
|
+
[ [], [] ],
|
|
371
|
+
[ [], [] ],
|
|
372
|
+
] );
|
|
373
|
+
} );
|
|
330
374
|
} );
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { throttle } from 'lodash';
|
|
5
4
|
import { useMemoOne } from 'use-memo-one';
|
|
6
5
|
|
|
7
6
|
/**
|
|
@@ -10,7 +9,12 @@ import { useMemoOne } from 'use-memo-one';
|
|
|
10
9
|
import { useEffect } from '@wordpress/element';
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
12
|
+
* Internal dependencies
|
|
13
|
+
*/
|
|
14
|
+
import { throttle } from '../../utils/throttle';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Throttles a function similar to Lodash's `throttle`. A new throttled function will
|
|
14
18
|
* be returned and any scheduled calls cancelled if any of the arguments change,
|
|
15
19
|
* including the function to throttle, so please wrap functions created on
|
|
16
20
|
* render in components in `useCallback`.
|
|
@@ -19,14 +23,14 @@ import { useEffect } from '@wordpress/element';
|
|
|
19
23
|
*
|
|
20
24
|
* @template {(...args: any[]) => void} TFunc
|
|
21
25
|
*
|
|
22
|
-
* @param {TFunc}
|
|
23
|
-
* @param {number}
|
|
24
|
-
* @param {import('
|
|
25
|
-
* @return {import('
|
|
26
|
+
* @param {TFunc} fn The function to throttle.
|
|
27
|
+
* @param {number} [wait] The number of milliseconds to throttle invocations to.
|
|
28
|
+
* @param {import('../../utils/throttle').ThrottleOptions} [options] The options object. See linked documentation for details.
|
|
29
|
+
* @return {import('../../utils/debounce').DebouncedFunc<TFunc>} Throttled function.
|
|
26
30
|
*/
|
|
27
31
|
export default function useThrottle( fn, wait, options ) {
|
|
28
32
|
const throttled = useMemoOne(
|
|
29
|
-
() => throttle( fn, wait, options ),
|
|
33
|
+
() => throttle( fn, wait ?? 0, options ),
|
|
30
34
|
[ fn, wait, options ]
|
|
31
35
|
);
|
|
32
36
|
useEffect( () => () => throttled.cancel(), [ throttled ] );
|
package/src/index.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
export * from './utils/create-higher-order-component';
|
|
3
3
|
// The `debounce` helper and its types.
|
|
4
4
|
export * from './utils/debounce';
|
|
5
|
+
// The `throttle` helper and its types.
|
|
6
|
+
export * from './utils/throttle';
|
|
5
7
|
|
|
6
8
|
// The `compose` and `pipe` helpers (inspired by `flowRight` and `flow` from Lodash).
|
|
7
9
|
export { default as compose } from './higher-order/compose';
|
package/src/index.native.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
export * from './utils/create-higher-order-component';
|
|
3
3
|
// The `debounce` helper and its types.
|
|
4
4
|
export * from './utils/debounce';
|
|
5
|
+
// The `throttle` helper and its types.
|
|
6
|
+
export * from './utils/throttle';
|
|
5
7
|
|
|
6
8
|
// The `compose` and `pipe` helpers (inspired by `flowRight` and `flow` from Lodash).
|
|
7
9
|
export { default as compose } from './higher-order/compose';
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parts of this source were derived and modified from lodash,
|
|
3
|
+
* released under the MIT license.
|
|
4
|
+
*
|
|
5
|
+
* https://github.com/lodash/lodash
|
|
6
|
+
*
|
|
7
|
+
* Copyright JS Foundation and other contributors <https://js.foundation/>
|
|
8
|
+
*
|
|
9
|
+
* Based on Underscore.js, copyright Jeremy Ashkenas,
|
|
10
|
+
* DocumentCloud and Investigative Reporters & Editors <http://underscorejs.org/>
|
|
11
|
+
*
|
|
12
|
+
* This software consists of voluntary contributions made by many
|
|
13
|
+
* individuals. For exact contribution history, see the revision history
|
|
14
|
+
* available at https://github.com/lodash/lodash
|
|
15
|
+
*
|
|
16
|
+
* The following license applies to all parts of this software except as
|
|
17
|
+
* documented below:
|
|
18
|
+
*
|
|
19
|
+
* ====
|
|
20
|
+
*
|
|
21
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
|
22
|
+
* a copy of this software and associated documentation files (the
|
|
23
|
+
* "Software"), to deal in the Software without restriction, including
|
|
24
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
|
25
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
26
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
|
27
|
+
* the following conditions:
|
|
28
|
+
*
|
|
29
|
+
* The above copyright notice and this permission notice shall be
|
|
30
|
+
* included in all copies or substantial portions of the Software.
|
|
31
|
+
*
|
|
32
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
33
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
34
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
35
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
36
|
+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
37
|
+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
38
|
+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Internal dependencies
|
|
43
|
+
*/
|
|
44
|
+
import { debounce } from '../debounce';
|
|
45
|
+
|
|
46
|
+
export interface ThrottleOptions {
|
|
47
|
+
leading?: boolean;
|
|
48
|
+
trailing?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A simplified and properly typed version of lodash's `throttle`, that
|
|
53
|
+
* always uses timers instead of sometimes using rAF.
|
|
54
|
+
*
|
|
55
|
+
* Creates a throttled function that only invokes `func` at most once per
|
|
56
|
+
* every `wait` milliseconds. The throttled function comes with a `cancel`
|
|
57
|
+
* method to cancel delayed `func` invocations and a `flush` method to
|
|
58
|
+
* immediately invoke them. Provide `options` to indicate whether `func`
|
|
59
|
+
* should be invoked on the leading and/or trailing edge of the `wait`
|
|
60
|
+
* timeout. The `func` is invoked with the last arguments provided to the
|
|
61
|
+
* throttled function. Subsequent calls to the throttled function return
|
|
62
|
+
* the result of the last `func` invocation.
|
|
63
|
+
*
|
|
64
|
+
* **Note:** If `leading` and `trailing` options are `true`, `func` is
|
|
65
|
+
* invoked on the trailing edge of the timeout only if the throttled function
|
|
66
|
+
* is invoked more than once during the `wait` timeout.
|
|
67
|
+
*
|
|
68
|
+
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
|
|
69
|
+
* until the next tick, similar to `setTimeout` with a timeout of `0`.
|
|
70
|
+
*
|
|
71
|
+
* @param {Function} func The function to throttle.
|
|
72
|
+
* @param {number} wait The number of milliseconds to throttle invocations to.
|
|
73
|
+
* @param {Partial< ThrottleOptions >} options The options object.
|
|
74
|
+
* @param {boolean} options.leading Specify invoking on the leading edge of the timeout.
|
|
75
|
+
* @param {boolean} options.trailing Specify invoking on the trailing edge of the timeout.
|
|
76
|
+
* @return Returns the new throttled function.
|
|
77
|
+
*/
|
|
78
|
+
export const throttle = < FunctionT extends ( ...args: unknown[] ) => unknown >(
|
|
79
|
+
func: FunctionT,
|
|
80
|
+
wait: number,
|
|
81
|
+
options?: ThrottleOptions
|
|
82
|
+
) => {
|
|
83
|
+
let leading = true;
|
|
84
|
+
let trailing = true;
|
|
85
|
+
|
|
86
|
+
if ( options ) {
|
|
87
|
+
leading = 'leading' in options ? !! options.leading : leading;
|
|
88
|
+
trailing = 'trailing' in options ? !! options.trailing : trailing;
|
|
89
|
+
}
|
|
90
|
+
return debounce( func, wait, {
|
|
91
|
+
leading,
|
|
92
|
+
trailing,
|
|
93
|
+
maxWait: wait,
|
|
94
|
+
} );
|
|
95
|
+
};
|