UNPKG

@gechiui/components

Version:
70 lines (59 loc) 1.54 kB
/** * External dependencies */ import classnames from 'classnames'; /** * GeChiUI dependencies */ import { useRef, useEffect } from '@gechiui/element'; import { useCopyToClipboard } from '@gechiui/compose'; import deprecated from '@gechiui/deprecated'; /** * Internal dependencies */ import Button from '../button'; const TIMEOUT = 4000; export default function ClipboardButton( { className, children, onCopy, onFinishCopy, text, ...buttonProps } ) { deprecated( 'gc.components.ClipboardButton', { since: '10.3', plugin: 'Gutenberg', alternative: 'gc.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> ); }