-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathCopyURLButton.tsx
33 lines (27 loc) · 881 Bytes
/
CopyURLButton.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
import { useState } from "react";
import { useLocation } from "react-router-dom";
import Button from "./Button";
import { ShareIcon } from "./Icons";
type Props = {} & React.ButtonHTMLAttributes<HTMLButtonElement>;
const CopyURLButton = ({ ...props }: Props) => {
const location = useLocation();
const [isCopied, setIsCopied] = useState(false);
const copyText = () => {
const fullURL =
window.location.origin + location.pathname + location.search;
navigator.clipboard
.writeText(fullURL)
.then(() => {
setIsCopied(true);
setTimeout(() => setIsCopied(false), 2000);
})
.catch((err) => alert("Error occurred: " + err));
};
return (
<Button isIcon={true} onClick={copyText} {...props}>
<ShareIcon />
<span>{isCopied ? "Shared!" : "Share"}</span>
</Button>
);
};
export default CopyURLButton;