@wordpress/compose 7.41.0 → 7.41.1-next.v.202603161435.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.
@@ -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
- }