@wordpress/compose 7.41.0 → 7.41.1-next.v.202603102151.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/README.md +6 -6
- 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-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-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-isomorphic-layout-effect/index.d.ts +2 -2
- package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts.map +1 -1
- 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-copy-on-click/index.js +0 -75
- package/src/hooks/use-copy-to-clipboard/index.js +0 -69
|
@@ -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
|
-
}
|