frontend/components/CopyLink/index.tsx (view raw)
1import Button, { ButtonProps } from '@material-ui/core/Button';
2import Icon from '@material-ui/core/Icon';
3
4interface Props {
5 buttonText: string;
6 title: string;
7 url: string;
8 onShare: () => void;
9}
10
11const CopyLink = ({buttonText, title, url, onShare, ...buttonProps}: ButtonProps & Props) => {
12 const share = async () => {
13 if (!url || !title) return null;
14 // If navigator share capability
15 if (!!navigator.share)
16 return await navigator.share({
17 title,
18 url,
19 });
20 // Else copy URL in clipboard
21 else if (!!navigator.clipboard) {
22 await navigator.clipboard.writeText(url);
23 onShare();
24 return true;
25 }
26 };
27
28 return (
29 <Button
30 variant="outlined"
31 startIcon={<Icon>share</Icon>}
32 onClick={share}
33 {...buttonProps}
34 >
35 {buttonText}
36 </Button>
37 );
38};
39
40export default CopyLink;