-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compose: Add new useCopyToClipboard
hook
#29643
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ import { | |
ToolbarGroup, | ||
ToolbarButton, | ||
} from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { | ||
BlockControls, | ||
BlockIcon, | ||
|
@@ -23,28 +23,34 @@ import { | |
useBlockProps, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { useEffect, useState, useRef } from '@wordpress/element'; | ||
import { useCopyOnClick } from '@wordpress/compose'; | ||
import { useEffect, useState } from '@wordpress/element'; | ||
import { useCopyToClipboard } from '@wordpress/compose'; | ||
import { __, _x } from '@wordpress/i18n'; | ||
import { file as icon } from '@wordpress/icons'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import FileBlockInspector from './inspector'; | ||
|
||
function ClipboardToolbarButton( { text, disabled } ) { | ||
const ref = useRef(); | ||
const hasCopied = useCopyOnClick( ref, text ); | ||
const { createNotice } = useDispatch( noticesStore ); | ||
const ref = useCopyToClipboard( text, () => { | ||
createNotice( 'info', __( 'Copied URL to clipboard.' ), { | ||
isDismissible: true, | ||
type: 'snackbar', | ||
} ); | ||
} ); | ||
|
||
return ( | ||
<ToolbarButton | ||
className="components-clipboard-toolbar-button" | ||
ref={ ref } | ||
disabled={ disabled } | ||
> | ||
{ hasCopied ? __( 'Copied!' ) : __( 'Copy URL' ) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes the behavior from the button label change to snacker, I just want to make sure that this is what we want :) |
||
{ __( 'Copy URL' ) } | ||
</ToolbarButton> | ||
); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import Clipboard from 'clipboard'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useRefEffect from '../use-ref-effect'; | ||
|
||
/** @typedef {import('@wordpress/element').RefObject} RefObject */ | ||
|
||
function useUpdatedRef( value ) { | ||
const ref = useRef( value ); | ||
ref.current = value; | ||
return ref; | ||
} | ||
|
||
/** | ||
* Copies the given text to the clipboard when the element is clicked. | ||
* | ||
* @param {text|Function} text The text to copy. Use a function if not | ||
* already available and expensive to compute. | ||
* @param {Function} onSuccess Called when to text is copied. | ||
* | ||
* @return {RefObject} A ref to assign to the target element. | ||
*/ | ||
export default function useCopyToClipboard( text, onSuccess ) { | ||
// Store the dependencies as refs and continuesly update them so they're | ||
// fresh when the callback is called. | ||
const textRef = useUpdatedRef( text ); | ||
const onSuccesRef = useUpdatedRef( onSuccess ); | ||
return useRefEffect( ( node ) => { | ||
// Clipboard listens to click events. | ||
const clipboard = new Clipboard( node, { | ||
text() { | ||
return typeof textRef.current === 'function' | ||
? textRef.current() | ||
: textRef.current; | ||
}, | ||
} ); | ||
|
||
clipboard.on( 'success', ( { clearSelection } ) => { | ||
// Clearing selection will move focus back to the triggering | ||
// button, ensuring that it is not reset to the body, and | ||
// further that it is kept within the rendered node. | ||
clearSelection(); | ||
// Handle ClipboardJS focus bug, see | ||
// https://github.com/zenorocha/clipboard.js/issues/680 | ||
node.focus(); | ||
|
||
if ( onSuccesRef.current ) { | ||
onSuccesRef.current(); | ||
} | ||
} ); | ||
|
||
return () => { | ||
clipboard.destroy(); | ||
}; | ||
}, [] ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notice how we can use the
MenuItem
here instead of emulating it.