@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.
Files changed (64) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +11 -9
  3. package/build/hooks/use-copy-on-click/index.cjs +43 -18
  4. package/build/hooks/use-copy-on-click/index.cjs.map +4 -4
  5. package/build/hooks/use-copy-to-clipboard/index.cjs +58 -12
  6. package/build/hooks/use-copy-to-clipboard/index.cjs.map +4 -4
  7. package/build/hooks/use-dialog/index.cjs +2 -2
  8. package/build/hooks/use-dialog/index.cjs.map +3 -3
  9. package/build/hooks/use-focus-on-mount/index.cjs +15 -13
  10. package/build/hooks/use-focus-on-mount/index.cjs.map +3 -3
  11. package/build/hooks/use-media-query/index.cjs +12 -9
  12. package/build/hooks/use-media-query/index.cjs.map +3 -3
  13. package/build/hooks/use-viewport-match/index.cjs +2 -2
  14. package/build/hooks/use-viewport-match/index.cjs.map +2 -2
  15. package/build/index.cjs +2 -2
  16. package/build/index.cjs.map +2 -2
  17. package/build-module/hooks/use-copy-on-click/index.mjs +44 -19
  18. package/build-module/hooks/use-copy-on-click/index.mjs.map +3 -3
  19. package/build-module/hooks/use-copy-to-clipboard/index.mjs +53 -12
  20. package/build-module/hooks/use-copy-to-clipboard/index.mjs.map +3 -3
  21. package/build-module/hooks/use-dialog/index.mjs +1 -1
  22. package/build-module/hooks/use-dialog/index.mjs.map +2 -2
  23. package/build-module/hooks/use-focus-on-mount/index.mjs +15 -9
  24. package/build-module/hooks/use-focus-on-mount/index.mjs.map +3 -3
  25. package/build-module/hooks/use-media-query/index.mjs +12 -9
  26. package/build-module/hooks/use-media-query/index.mjs.map +3 -3
  27. package/build-module/hooks/use-viewport-match/index.mjs +2 -2
  28. package/build-module/hooks/use-viewport-match/index.mjs.map +2 -2
  29. package/build-module/index.mjs +46 -46
  30. package/build-module/index.mjs.map +2 -2
  31. package/build-types/hooks/use-copy-on-click/index.d.ts +8 -9
  32. package/build-types/hooks/use-copy-on-click/index.d.ts.map +1 -1
  33. package/build-types/hooks/use-copy-to-clipboard/index.d.ts +22 -6
  34. package/build-types/hooks/use-copy-to-clipboard/index.d.ts.map +1 -1
  35. package/build-types/hooks/use-dialog/index.d.ts +6 -9
  36. package/build-types/hooks/use-dialog/index.d.ts.map +1 -1
  37. package/build-types/hooks/use-focus-on-mount/index.d.ts +4 -10
  38. package/build-types/hooks/use-focus-on-mount/index.d.ts.map +1 -1
  39. package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts +2 -2
  40. package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts.map +1 -1
  41. package/build-types/hooks/use-media-query/index.d.ts +4 -3
  42. package/build-types/hooks/use-media-query/index.d.ts.map +1 -1
  43. package/build-types/hooks/use-viewport-match/index.d.ts +2 -1
  44. package/build-types/hooks/use-viewport-match/index.d.ts.map +1 -1
  45. package/build-types/index.d.ts +1 -1
  46. package/build-types/lock-unlock.d.ts +2 -0
  47. package/build-types/lock-unlock.d.ts.map +1 -0
  48. package/build-types/private-apis.d.ts +5 -0
  49. package/build-types/private-apis.d.ts.map +1 -0
  50. package/build-types/utils/subscribe-delegated-listener/index.d.ts +27 -0
  51. package/build-types/utils/subscribe-delegated-listener/index.d.ts.map +1 -0
  52. package/package.json +9 -10
  53. package/src/hooks/use-copy-on-click/index.ts +101 -0
  54. package/src/hooks/use-copy-on-click/test/index.tsx +162 -0
  55. package/src/hooks/use-copy-to-clipboard/index.ts +120 -0
  56. package/src/hooks/use-copy-to-clipboard/test/index.tsx +144 -0
  57. package/src/hooks/use-dialog/index.ts +6 -9
  58. package/src/hooks/use-focus-on-mount/{index.ts → index.js} +34 -23
  59. package/src/hooks/use-media-query/{index.js → index.ts} +27 -16
  60. package/src/hooks/use-viewport-match/index.js +3 -2
  61. package/src/hooks/use-viewport-match/test/index.js +32 -10
  62. package/src/index.js +1 -1
  63. package/src/hooks/use-copy-on-click/index.js +0 -75
  64. 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( 1, undefined );
125
- expect( useMediaQueryMock ).toHaveBeenNthCalledWith( 2, undefined );
126
- expect( useMediaQueryMock ).toHaveBeenNthCalledWith( 3, undefined );
127
- expect( useMediaQueryMock ).toHaveBeenNthCalledWith( 4, undefined );
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
- }