File size: 944 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Button } from '@automattic/components';
import clsx from 'clsx';
import { forwardRef } from 'react';

interface ClipboardButtonProps {
	className?: string;
	compact?: boolean;
	disabled?: boolean;
	primary?: boolean;
	scary?: boolean;
	busy?: boolean;
	borderless?: boolean;
	plain?: boolean;
	transparent?: boolean;
	text: string | null;
	onCopy?: () => void;
	onMouseLeave?: () => void;
}

// eslint-disable-next-line @typescript-eslint/no-empty-function
const noop = () => {};

const ClipboardButton = forwardRef<
	HTMLButtonElement,
	React.PropsWithChildren< ClipboardButtonProps >
>( ( { className, text, onCopy = noop, ...rest }, ref ) => {
	function onCopyHandler() {
		if ( text ) {
			navigator.clipboard.writeText( text );
			onCopy();
		}
	}

	return (
		<Button
			{ ...rest }
			onClick={ onCopyHandler }
			ref={ ref }
			className={ clsx( 'clipboard-button', className ) }
		/>
	);
} );

export default ClipboardButton;