All files / src/components/common CopyButton.tsx

56.25% Statements 9/16
50% Branches 6/12
50% Functions 2/4
53.33% Lines 8/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117                                        89x                 56x   56x                     56x                     56x                                               56x                                           56x     89x                            
import { CopyIcon } from "@assets/icons/RebrandedIcons";
import { Box, SxProps, styled } from "@mui/material";
import { palette } from "@palette";
import { useEffect, useState } from "react";
import { Text } from "./Text";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { Stack } from "@mui/material";
import { CheckIcon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
 
type Props = {
  text: string;
  hidden?: boolean;
  sx?: SxProps;
  label?: string;
  customCopySx?: SxProps;
  testId?: string;
  withTooltip?: boolean;
};
 
const CopyButton = ({
  text,
  hidden,
  sx,
  label,
  customCopySx,
  testId,
  withTooltip,
}: Props) => {
  const [isCopied, setIsCopied] = useState(false);
 
  const handleCopy = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
    e.stopPropagation();
    navigator?.clipboard?.writeText(text);
    setIsCopied(true);
 
    setTimeout(() => {
      setIsCopied(false);
    }, 2000);
  };
 
  const button = (
    <Button
      sx={{ ...sx }}
      type="button"
      data-testid={testId}
      hidden={hidden || !text}
      onClick={handleCopy}
    >
      <CopyIcon />
    </Button>
  );
 
  Iif (withTooltip) {
    return (
      <GiveTooltip
        color="default"
        title={
          isCopied ? (
            <Stack direction="row" gap="12px" alignItems="center">
              <GiveText variant="bodyS" color="default">
                Link Copied{" "}
              </GiveText>
              <CheckIcon size={18} color="white" />
            </Stack>
          ) : (
            "Copy Link"
          )
        }
        placement="top-start"
        sx={{ width: "fit-content" }}
      >
        {button}
      </GiveTooltip>
    );
  }
 
  Iif (isCopied)
    return (
      <Box
        sx={{
          backgroundColor: "#fff",
          right: 0,
          paddingRight: "8px",
          paddingLeft: "8px",
          paddingY: "12px",
          ...customCopySx,
        }}
      >
        <Text
          color={palette.neutral[80]}
          fontWeight="book"
          sx={{ userSelect: "none" }}
        >
          {label || "Link copied!"}
        </Text>
      </Box>
    );
 
  return button;
};
 
const Button = styled("button")<{ hidden: boolean }>(({ hidden }) => ({
  cursor: "pointer",
  background: "transparent",
  outline: "none",
  border: "none",
  "&:hover rect": {
    fill: "#ECECE9",
  },
  ...(hidden && {
    display: "none",
  }),
}));
 
export default CopyButton;