-
Notifications
You must be signed in to change notification settings - Fork 5.2k
/
Copy pathShareButton.tsx
91 lines (84 loc) · 3.19 KB
/
ShareButton.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
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
import { DropdownSelector } from 'components/DropdownSelector'
import { Share as ShareIcon } from 'components/Icons/Share'
import { TwitterXLogo } from 'components/Icons/TwitterX'
import { ActionButtonStyle, ActionMenuFlyoutStyle } from 'components/Tokens/TokenDetails/shared'
import useCopyClipboard from 'hooks/useCopyClipboard'
import styled from 'lib/styled-components'
import { useState } from 'react'
import { Link } from 'react-feather'
import { useTranslation } from 'react-i18next'
import { useSearchParams } from 'react-router-dom'
import { colors } from 'theme/colors'
import { opacify } from 'theme/utils'
import { Text, useSporeColors } from 'ui/src'
import { Check } from 'ui/src/components/icons/Check'
import { isMobileWeb } from 'utilities/src/platform'
const TWITTER_WIDTH = 560
const TWITTER_HEIGHT = 480
const ShareAction = styled.div`
display: flex;
align-items: center;
padding: 8px;
border-radius: 8px;
font-size: 16px;
font-weight: 485;
gap: 12px;
height: 40px;
color: ${({ theme }) => theme.neutral1};
cursor: pointer;
:hover {
background-color: ${({ theme }) => opacify(10, theme.darkMode ? colors.gray200 : colors.gray300)};
}
`
export function openShareTweetWindow(name: string) {
const currentLocation = window.location.href
const positionX = (window.screen.width - TWITTER_WIDTH) / 2
const positionY = (window.screen.height - TWITTER_HEIGHT) / 2
window.open(
`https://x.com/intent/tweet?text=Check%20out%20${name}%20${currentLocation}%20via%20@Uniswap`,
'newwindow',
`left=${positionX}, top=${positionY}, width=${TWITTER_WIDTH}, height=${TWITTER_HEIGHT}`,
)
}
export default function ShareButton({ name, utmSource }: { name: string; utmSource: string }) {
const colors = useSporeColors()
const { t } = useTranslation()
const [isOpen, setIsOpen] = useState(false)
const [searchParams] = useSearchParams()
const utmTag = `${searchParams.size > 0 ? '&' : '?'}utm_source=${utmSource}&utm_medium=${isMobileWeb ? 'mobile' : 'web'}`
const currentLocation = window.location.href + utmTag
const [isCopied, setCopied] = useCopyClipboard()
return (
<DropdownSelector
isOpen={isOpen}
toggleOpen={setIsOpen}
menuLabel={<ShareIcon fill={colors.neutral1.val} width={18} height={18} />}
tooltipText={t('common.share')}
internalMenuItems={
<>
<ShareAction onClick={() => setCopied(currentLocation)}>
{isCopied ? (
<Check size={16} p={1} color={colors.statusSuccess.val} />
) : (
<Link width="18px" height="18px" color={colors.neutral1.val} />
)}
<Text variant="body2">{isCopied ? t('common.copied') : t('common.copyLink.button')}</Text>
</ShareAction>
<ShareAction
onClick={() => {
setIsOpen(false)
openShareTweetWindow(name)
}}
>
<TwitterXLogo width="18px" height="18px" fill={colors.neutral1.val} />
<Text variant="body2">{t('common.share.shareToTwitter')}</Text>
</ShareAction>
</>
}
hideChevron
buttonStyle={ActionButtonStyle}
dropdownStyle={ActionMenuFlyoutStyle}
adaptToSheet={false}
/>
)
}