CopyButton.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import ContentCopyIcon from "@mui/icons-material/ContentCopy";
  2. import DoneIcon from "@mui/icons-material/Done";
  3. import {
  4. IconButton,
  5. Tooltip,
  6. type IconButtonProps,
  7. type SvgIconProps,
  8. } from "@mui/material";
  9. import { t } from "i18next";
  10. import { useState } from "react";
  11. export default function CopyButton({
  12. code,
  13. color,
  14. size,
  15. }: {
  16. code: string;
  17. color?: IconButtonProps["color"];
  18. size?: SvgIconProps["fontSize"];
  19. }) {
  20. const [copied, setCopied] = useState<boolean>(false);
  21. const copyToClipboardHelper = (text: string) => () => {
  22. navigator.clipboard.writeText(text);
  23. setCopied(true);
  24. setTimeout(() => setCopied(false), 1000);
  25. };
  26. return (
  27. <Tooltip
  28. arrow
  29. open={copied}
  30. title={t("COPIED")}
  31. PopperProps={{ sx: { zIndex: 2000 } }}
  32. >
  33. <IconButton onClick={copyToClipboardHelper(code)} color={color}>
  34. {copied ? (
  35. <DoneIcon fontSize={size ?? "small"} />
  36. ) : (
  37. <ContentCopyIcon fontSize={size ?? "small"} />
  38. )}
  39. </IconButton>
  40. </Tooltip>
  41. );
  42. }