forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShareButton.tsx
More file actions
85 lines (79 loc) · 2.15 KB
/
Copy pathShareButton.tsx
File metadata and controls
85 lines (79 loc) · 2.15 KB
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
"use client";
import { LinkIcon } from "@chakra-ui/icons";
import {
IconButton,
Menu,
MenuButton,
MenuItem,
MenuList,
Tooltip,
} from "@chakra-ui/react";
import { useState } from "react";
function twitterIntentUrl(url: string, text: string): string {
const params = new URLSearchParams({ url, text });
return `https://twitter.com/intent/tweet?${params.toString()}`;
}
/**
* Share menu for a pool or leaderboard entry (#264): copy the current page
* link, or open a prefilled X/Twitter share intent in a new tab.
*/
export default function ShareButton({
url,
shareText,
size = "xs",
}: {
url: string;
shareText: string;
size?: "2xs" | "xs" | "sm";
}) {
const [copied, setCopied] = useState(false);
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(url);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch {
// Clipboard may be unavailable in hardened browser contexts.
}
};
const handleTweet = () => {
window.open(twitterIntentUrl(url, shareText), "_blank", "noopener,noreferrer");
};
return (
<Menu placement="bottom-end">
<Tooltip label={copied ? "Copied!" : "Share"} hasArrow fontSize="xs">
<MenuButton
as={IconButton}
aria-label="Share"
icon={<LinkIcon />}
onClick={(e) => e.stopPropagation()}
size={size}
variant="ghost"
color={copied ? "app.accent" : "app.muted"}
_hover={{ color: "app.accent", bg: "app.surfaceHover" }}
minW="auto"
h="auto"
p={1}
/>
</Tooltip>
<MenuList bg="app.surface" borderColor="app.border" fontSize="sm" minW="160px">
<MenuItem
bg="app.surface"
color="app.text"
_hover={{ bg: "app.surfaceHover" }}
onClick={() => void handleCopy()}
>
{copied ? "Copied!" : "Copy link"}
</MenuItem>
<MenuItem
bg="app.surface"
color="app.text"
_hover={{ bg: "app.surfaceHover" }}
onClick={handleTweet}
>
Share on X
</MenuItem>
</MenuList>
</Menu>
);
}