element-call/src/button/CopyButton.jsx

33 lines
924 B
React
Raw Normal View History

2021-12-10 14:01:55 -08:00
import React, { useCallback } from "react";
2021-12-07 11:59:57 -08:00
import useClipboard from "react-use-clipboard";
import { ReactComponent as CheckIcon } from "../icons/Check.svg";
import { ReactComponent as CopyIcon } from "../icons/Copy.svg";
import { Button } from "./Button";
2021-12-13 12:39:29 -08:00
export function CopyButton({ value, children, onClassName, variant, ...rest }) {
2021-12-07 11:59:57 -08:00
const [isCopied, setCopied] = useClipboard(value, { successDuration: 3000 });
return (
2021-12-07 17:59:55 -08:00
<Button
{...rest}
2021-12-13 12:39:29 -08:00
variant={variant === "icon" ? "iconCopy" : "copy"}
2021-12-07 17:59:55 -08:00
on={isCopied}
2021-12-13 12:39:29 -08:00
onClassName={onClassName}
2021-12-07 17:59:55 -08:00
onPress={setCopied}
iconStyle={isCopied ? "stroke" : "fill"}
>
2021-12-07 11:59:57 -08:00
{isCopied ? (
<>
2021-12-07 17:59:55 -08:00
{variant !== "icon" && <span>Copied!</span>}
2021-12-07 11:59:57 -08:00
<CheckIcon />
</>
) : (
<>
2021-12-07 17:59:55 -08:00
{variant !== "icon" && <span>{children || value}</span>}
2021-12-07 11:59:57 -08:00
<CopyIcon />
</>
)}
</Button>
);
}