clipboard.tsx

 1import { Button, Tooltip } from '@material-ui/core';
 2import copy from 'clipboard-copy';
 3import * as React from 'react';
 4
 5export interface ClipboardButtonProps {
 6    buttonText: string;
 7    toCopy: string;
 8    icon: React.ReactNode;
 9}
10
11export const ClipboardButton = (props: ClipboardButtonProps) => {
12    const [showTooltip, setShowTooltip] = React.useState(false);
13
14    return (
15        <Tooltip
16            open={showTooltip}
17            title="Copied to clipboard."
18            leaveDelay={2000}
19            onClose={() => setShowTooltip(false)}
20        >
21            <Button
22                type="button"
23                onClick={() => {
24                    copy(props.toCopy);
25                    setShowTooltip(true);
26                }}
27                startIcon={props.icon}
28            >
29                {props.buttonText}
30            </Button>
31        </Tooltip>
32    );
33};