-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.tsx
91 lines (82 loc) · 2.32 KB
/
index.tsx
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { IconButton, IconButtonProps, TooltipProps, styled, useTheme } from "@mui/material";
import React, { useEffect, useState } from "react";
import { BiCheckCircle } from "react-icons/bi";
import { useCopyToClipboard } from "react-use";
import { CopyIconSquare } from "src/commons/resources";
import CustomIcon from "../CustomIcon";
import CustomTooltip from "../CustomTooltip";
const Button = styled(IconButton)`
color: ${(props) => props.theme.palette.text.primary};
width: 23px;
height: 23px;
font-size: var(--font-size-text-large);
pointer-events: auto;
`;
interface CopyButtonProps extends IconButtonProps {
text?: string;
placement?: TooltipProps["placement"];
className?: string;
children?: React.ReactNode;
customIcon?: React.FunctionComponent<React.SVGAttributes<SVGElement>>;
height?: number;
fill?: string;
onClick?: (e: React.MouseEvent) => void;
}
const CopyButton: React.FC<CopyButtonProps> = ({
text = "",
height,
onClick,
fill,
children,
placement,
customIcon,
...props
}) => {
const [, copyToClipboard] = useCopyToClipboard();
const [copied, setCopied] = useState<boolean>();
const [open, setOpen] = useState<boolean>(false);
const theme = useTheme();
useEffect(() => {
if (copied) {
const timeout = setTimeout(() => {
setCopied(false);
}, 2000);
return () => clearTimeout(timeout);
}
}, [copied]);
const onCopy = (e: React.MouseEvent) => {
e.preventDefault();
if (!copied) {
copyToClipboard(text);
setCopied(true);
onClick?.(e);
}
};
return (
<CustomTooltip
open={open}
onClose={() => setOpen(false)}
onOpen={() => setOpen(true)}
placement={placement || "top"}
title={copied ? "Copied" : "Copy"}
enterTouchDelay={0}
>
<Button {...props} onClick={onCopy}>
{children ||
(copied ? (
<BiCheckCircle
color={theme.palette.secondary.light}
style={{ verticalAlign: "text-bottom", scale: "2", height: 16 }}
/>
) : (
<CustomIcon
fill={fill ? fill : theme.palette.secondary.light}
icon={customIcon || CopyIconSquare}
height={height ? height : 16}
/>
))}
</Button>
</CustomTooltip>
);
};
export default CopyButton;