@wordpress/compose 7.40.1-next.v.202602271551.0 → 7.40.1
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 +4 -0
- package/README.md +11 -9
- package/build/hooks/use-copy-on-click/index.cjs +43 -18
- package/build/hooks/use-copy-on-click/index.cjs.map +4 -4
- package/build/hooks/use-copy-to-clipboard/index.cjs +58 -12
- package/build/hooks/use-copy-to-clipboard/index.cjs.map +4 -4
- package/build/hooks/use-dialog/index.cjs +2 -2
- package/build/hooks/use-dialog/index.cjs.map +3 -3
- package/build/hooks/use-focus-on-mount/index.cjs +15 -13
- package/build/hooks/use-focus-on-mount/index.cjs.map +3 -3
- package/build/hooks/use-media-query/index.cjs +12 -9
- package/build/hooks/use-media-query/index.cjs.map +3 -3
- package/build/hooks/use-viewport-match/index.cjs +2 -2
- package/build/hooks/use-viewport-match/index.cjs.map +2 -2
- package/build/index.cjs +2 -2
- package/build/index.cjs.map +2 -2
- package/build-module/hooks/use-copy-on-click/index.mjs +44 -19
- package/build-module/hooks/use-copy-on-click/index.mjs.map +3 -3
- package/build-module/hooks/use-copy-to-clipboard/index.mjs +53 -12
- package/build-module/hooks/use-copy-to-clipboard/index.mjs.map +3 -3
- package/build-module/hooks/use-dialog/index.mjs +1 -1
- package/build-module/hooks/use-dialog/index.mjs.map +2 -2
- package/build-module/hooks/use-focus-on-mount/index.mjs +15 -9
- package/build-module/hooks/use-focus-on-mount/index.mjs.map +3 -3
- package/build-module/hooks/use-media-query/index.mjs +12 -9
- package/build-module/hooks/use-media-query/index.mjs.map +3 -3
- package/build-module/hooks/use-viewport-match/index.mjs +2 -2
- package/build-module/hooks/use-viewport-match/index.mjs.map +2 -2
- package/build-module/index.mjs +46 -46
- package/build-module/index.mjs.map +2 -2
- package/build-types/hooks/use-copy-on-click/index.d.ts +8 -9
- package/build-types/hooks/use-copy-on-click/index.d.ts.map +1 -1
- package/build-types/hooks/use-copy-to-clipboard/index.d.ts +22 -6
- package/build-types/hooks/use-copy-to-clipboard/index.d.ts.map +1 -1
- package/build-types/hooks/use-dialog/index.d.ts +6 -9
- package/build-types/hooks/use-dialog/index.d.ts.map +1 -1
- package/build-types/hooks/use-focus-on-mount/index.d.ts +4 -10
- package/build-types/hooks/use-focus-on-mount/index.d.ts.map +1 -1
- package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts +2 -2
- package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts.map +1 -1
- package/build-types/hooks/use-media-query/index.d.ts +4 -3
- package/build-types/hooks/use-media-query/index.d.ts.map +1 -1
- package/build-types/hooks/use-viewport-match/index.d.ts +2 -1
- package/build-types/hooks/use-viewport-match/index.d.ts.map +1 -1
- package/build-types/index.d.ts +1 -1
- package/build-types/lock-unlock.d.ts +2 -0
- package/build-types/lock-unlock.d.ts.map +1 -0
- package/build-types/private-apis.d.ts +5 -0
- package/build-types/private-apis.d.ts.map +1 -0
- package/build-types/utils/subscribe-delegated-listener/index.d.ts +27 -0
- package/build-types/utils/subscribe-delegated-listener/index.d.ts.map +1 -0
- package/package.json +9 -10
- package/src/hooks/use-copy-on-click/index.ts +101 -0
- package/src/hooks/use-copy-on-click/test/index.tsx +162 -0
- package/src/hooks/use-copy-to-clipboard/index.ts +120 -0
- package/src/hooks/use-copy-to-clipboard/test/index.tsx +144 -0
- package/src/hooks/use-dialog/index.ts +6 -9
- package/src/hooks/use-focus-on-mount/{index.ts → index.js} +34 -23
- package/src/hooks/use-media-query/{index.js → index.ts} +27 -16
- package/src/hooks/use-viewport-match/index.js +3 -2
- package/src/hooks/use-viewport-match/test/index.js +32 -10
- package/src/index.js +1 -1
- package/src/hooks/use-copy-on-click/index.js +0 -75
- package/src/hooks/use-copy-to-clipboard/index.js +0 -69
|
@@ -44,15 +44,18 @@ describe( 'useViewportMatch', () => {
|
|
|
44
44
|
expect( useMediaQueryMock ).toHaveBeenCalledTimes( 3 );
|
|
45
45
|
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
46
46
|
1,
|
|
47
|
-
'(max-width: 1280px)'
|
|
47
|
+
'(max-width: 1280px)',
|
|
48
|
+
window
|
|
48
49
|
);
|
|
49
50
|
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
50
51
|
2,
|
|
51
|
-
'(min-width: 782px)'
|
|
52
|
+
'(min-width: 782px)',
|
|
53
|
+
window
|
|
52
54
|
);
|
|
53
55
|
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
54
56
|
3,
|
|
55
|
-
'(min-width: 600px)'
|
|
57
|
+
'(min-width: 600px)',
|
|
58
|
+
window
|
|
56
59
|
);
|
|
57
60
|
} );
|
|
58
61
|
|
|
@@ -76,15 +79,18 @@ describe( 'useViewportMatch', () => {
|
|
|
76
79
|
expect( useMediaQueryMock ).toHaveBeenCalledTimes( 3 );
|
|
77
80
|
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
78
81
|
1,
|
|
79
|
-
'(min-width: 1440px)'
|
|
82
|
+
'(min-width: 1440px)',
|
|
83
|
+
window
|
|
80
84
|
);
|
|
81
85
|
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
82
86
|
2,
|
|
83
|
-
'(max-width: 960px)'
|
|
87
|
+
'(max-width: 960px)',
|
|
88
|
+
window
|
|
84
89
|
);
|
|
85
90
|
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
86
91
|
3,
|
|
87
|
-
'(max-width: 480px)'
|
|
92
|
+
'(max-width: 480px)',
|
|
93
|
+
window
|
|
88
94
|
);
|
|
89
95
|
} );
|
|
90
96
|
|
|
@@ -121,9 +127,25 @@ describe( 'useViewportMatch', () => {
|
|
|
121
127
|
|
|
122
128
|
expect( useMediaQueryMock ).toHaveBeenCalledTimes( 4 );
|
|
123
129
|
// `useMediaQuery` is expected to receive `undefined` when simulating width.
|
|
124
|
-
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
130
|
+
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
131
|
+
1,
|
|
132
|
+
undefined,
|
|
133
|
+
window
|
|
134
|
+
);
|
|
135
|
+
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
136
|
+
2,
|
|
137
|
+
undefined,
|
|
138
|
+
window
|
|
139
|
+
);
|
|
140
|
+
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
141
|
+
3,
|
|
142
|
+
undefined,
|
|
143
|
+
window
|
|
144
|
+
);
|
|
145
|
+
expect( useMediaQueryMock ).toHaveBeenNthCalledWith(
|
|
146
|
+
4,
|
|
147
|
+
undefined,
|
|
148
|
+
window
|
|
149
|
+
);
|
|
128
150
|
} );
|
|
129
151
|
} );
|
package/src/index.js
CHANGED
|
@@ -27,7 +27,7 @@ export { default as __experimentalUseDialog } from './hooks/use-dialog';
|
|
|
27
27
|
export { default as useDisabled } from './hooks/use-disabled';
|
|
28
28
|
export { default as useEvent } from './hooks/use-event';
|
|
29
29
|
export { default as __experimentalUseDragging } from './hooks/use-dragging';
|
|
30
|
-
export { useFocusOnMount } from './hooks/use-focus-on-mount';
|
|
30
|
+
export { default as useFocusOnMount } from './hooks/use-focus-on-mount';
|
|
31
31
|
export { default as __experimentalUseFocusOutside } from './hooks/use-focus-outside';
|
|
32
32
|
export { default as useFocusReturn } from './hooks/use-focus-return';
|
|
33
33
|
export { default as useInstanceId } from './hooks/use-instance-id';
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import Clipboard from 'clipboard';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* WordPress dependencies
|
|
8
|
-
*/
|
|
9
|
-
import { useRef, useEffect, useState } from '@wordpress/element';
|
|
10
|
-
import deprecated from '@wordpress/deprecated';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Copies the text to the clipboard when the element is clicked.
|
|
14
|
-
*
|
|
15
|
-
* @deprecated
|
|
16
|
-
*
|
|
17
|
-
* @param {React.RefObject<string | Element | NodeListOf<Element>>} ref Reference with the element.
|
|
18
|
-
* @param {string|Function} text The text to copy.
|
|
19
|
-
* @param {number} [timeout] Optional timeout to reset the returned
|
|
20
|
-
* state. 4 seconds by default.
|
|
21
|
-
*
|
|
22
|
-
* @return {boolean} Whether or not the text has been copied. Resets after the
|
|
23
|
-
* timeout.
|
|
24
|
-
*/
|
|
25
|
-
export default function useCopyOnClick( ref, text, timeout = 4000 ) {
|
|
26
|
-
deprecated( 'wp.compose.useCopyOnClick', {
|
|
27
|
-
since: '5.8',
|
|
28
|
-
alternative: 'wp.compose.useCopyToClipboard',
|
|
29
|
-
} );
|
|
30
|
-
|
|
31
|
-
/** @type {React.MutableRefObject<Clipboard | undefined>} */
|
|
32
|
-
const clipboardRef = useRef( undefined );
|
|
33
|
-
const [ hasCopied, setHasCopied ] = useState( false );
|
|
34
|
-
|
|
35
|
-
useEffect( () => {
|
|
36
|
-
/** @type {number | undefined} */
|
|
37
|
-
let timeoutId;
|
|
38
|
-
|
|
39
|
-
if ( ! ref.current ) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Clipboard listens to click events.
|
|
44
|
-
clipboardRef.current = new Clipboard( ref.current, {
|
|
45
|
-
text: () => ( typeof text === 'function' ? text() : text ),
|
|
46
|
-
} );
|
|
47
|
-
|
|
48
|
-
clipboardRef.current.on( 'success', ( { clearSelection, trigger } ) => {
|
|
49
|
-
// Clearing selection will move focus back to the triggering button,
|
|
50
|
-
// ensuring that it is not reset to the body, and further that it is
|
|
51
|
-
// kept within the rendered node.
|
|
52
|
-
clearSelection();
|
|
53
|
-
|
|
54
|
-
// Handle ClipboardJS focus bug, see https://github.com/zenorocha/clipboard.js/issues/680
|
|
55
|
-
if ( trigger ) {
|
|
56
|
-
/** @type {HTMLElement} */ ( trigger ).focus();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if ( timeout ) {
|
|
60
|
-
setHasCopied( true );
|
|
61
|
-
clearTimeout( timeoutId );
|
|
62
|
-
timeoutId = setTimeout( () => setHasCopied( false ), timeout );
|
|
63
|
-
}
|
|
64
|
-
} );
|
|
65
|
-
|
|
66
|
-
return () => {
|
|
67
|
-
if ( clipboardRef.current ) {
|
|
68
|
-
clipboardRef.current.destroy();
|
|
69
|
-
}
|
|
70
|
-
clearTimeout( timeoutId );
|
|
71
|
-
};
|
|
72
|
-
}, [ text, timeout, setHasCopied ] );
|
|
73
|
-
|
|
74
|
-
return hasCopied;
|
|
75
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* External dependencies
|
|
3
|
-
*/
|
|
4
|
-
import Clipboard from 'clipboard';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* WordPress dependencies
|
|
8
|
-
*/
|
|
9
|
-
import { useRef, useLayoutEffect } from '@wordpress/element';
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Internal dependencies
|
|
13
|
-
*/
|
|
14
|
-
import useRefEffect from '../use-ref-effect';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @template T
|
|
18
|
-
* @param {T} value
|
|
19
|
-
* @return {React.RefObject<T>} The updated ref
|
|
20
|
-
*/
|
|
21
|
-
function useUpdatedRef( value ) {
|
|
22
|
-
const ref = useRef( value );
|
|
23
|
-
useLayoutEffect( () => {
|
|
24
|
-
ref.current = value;
|
|
25
|
-
}, [ value ] );
|
|
26
|
-
return ref;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Copies the given text to the clipboard when the element is clicked.
|
|
31
|
-
*
|
|
32
|
-
* @template {HTMLElement} TElementType
|
|
33
|
-
* @param {string | (() => string)} text The text to copy. Use a function if not
|
|
34
|
-
* already available and expensive to compute.
|
|
35
|
-
* @param {Function} onSuccess Called when to text is copied.
|
|
36
|
-
*
|
|
37
|
-
* @return {React.Ref<TElementType>} A ref to assign to the target element.
|
|
38
|
-
*/
|
|
39
|
-
export default function useCopyToClipboard( text, onSuccess ) {
|
|
40
|
-
// Store the dependencies as refs and continuously update them so they're
|
|
41
|
-
// fresh when the callback is called.
|
|
42
|
-
const textRef = useUpdatedRef( text );
|
|
43
|
-
const onSuccessRef = useUpdatedRef( onSuccess );
|
|
44
|
-
return useRefEffect( ( node ) => {
|
|
45
|
-
// Clipboard listens to click events.
|
|
46
|
-
const clipboard = new Clipboard( node, {
|
|
47
|
-
text() {
|
|
48
|
-
return typeof textRef.current === 'function'
|
|
49
|
-
? textRef.current()
|
|
50
|
-
: textRef.current || '';
|
|
51
|
-
},
|
|
52
|
-
} );
|
|
53
|
-
|
|
54
|
-
clipboard.on( 'success', ( { clearSelection } ) => {
|
|
55
|
-
// Clearing selection will move focus back to the triggering
|
|
56
|
-
// button, ensuring that it is not reset to the body, and
|
|
57
|
-
// further that it is kept within the rendered node.
|
|
58
|
-
clearSelection();
|
|
59
|
-
|
|
60
|
-
if ( onSuccessRef.current ) {
|
|
61
|
-
onSuccessRef.current();
|
|
62
|
-
}
|
|
63
|
-
} );
|
|
64
|
-
|
|
65
|
-
return () => {
|
|
66
|
-
clipboard.destroy();
|
|
67
|
-
};
|
|
68
|
-
}, [] );
|
|
69
|
-
}
|