UNPKG

@wordpress/components

Version:
70 lines (59 loc) 1.55 kB
/** * External dependencies */ import classnames from 'classnames'; /** * WordPress dependencies */ import { useRef, useEffect } from '@wordpress/element'; import { useCopyToClipboard } from '@wordpress/compose'; import deprecated from '@wordpress/deprecated'; /** * Internal dependencies */ import Button from '../button'; const TIMEOUT = 4000; export default function ClipboardButton( { className, children, onCopy, onFinishCopy, text, ...buttonProps } ) { deprecated( 'wp.components.ClipboardButton', { since: '10.3', plugin: 'Gutenberg', alternative: 'wp.compose.useCopyToClipboard', } ); const timeoutId = useRef(); const ref = useCopyToClipboard( text, () => { onCopy(); clearTimeout( timeoutId.current ); if ( onFinishCopy ) { timeoutId.current = setTimeout( () => onFinishCopy(), TIMEOUT ); } } ); useEffect( () => { clearTimeout( timeoutId.current ); }, [] ); const classes = classnames( 'components-clipboard-button', className ); // Workaround for inconsistent behavior in Safari, where <textarea> is not // the document.activeElement at the moment when the copy event fires. // This causes documentHasSelection() in the copy-handler component to // mistakenly override the ClipboardButton, and copy a serialized string // of the current block instead. const focusOnCopyEventTarget = ( event ) => { event.target.focus(); }; return ( <Button { ...buttonProps } className={ classes } ref={ ref } onCopy={ focusOnCopyEventTarget } > { children } </Button> ); }