-
Notifications
You must be signed in to change notification settings - Fork 74
/
ArtworkSharePanel.tsx
233 lines (207 loc) · 5.74 KB
/
ArtworkSharePanel.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { color, Flex, media, Sans, Separator } from "@artsy/palette"
import { ArtworkSharePanel_artwork } from "__generated__/ArtworkSharePanel_artwork.graphql"
import Icon from "Components/Icon"
import React from "react"
import { createFragmentContainer, graphql } from "react-relay"
import { data as sd } from "sharify"
import styled from "styled-components"
import { ArtworkPopoutPanel } from "./ArtworkPopoutPanel"
interface ArtworkSharePanelProps {
artwork: ArtworkSharePanel_artwork
onClose: () => void
}
interface ArtworkSharePanelState {
copyLabelText: string
copyLocked: boolean
}
const MODAL = {
width: 750,
height: 400,
}
const COPY_LABELS = {
copy: "Copy",
copied: "Copied",
}
export class ArtworkSharePanel extends React.Component<
ArtworkSharePanelProps,
ArtworkSharePanelState
> {
private input: HTMLInputElement
state = {
copyLabelText: COPY_LABELS.copy,
copyLocked: false,
}
handleCopy = () => {
if (!this.state.copyLocked) {
this.input.focus()
this.input.setSelectionRange(0, this.input.value.length)
document.execCommand("copy")
this.setState(
{
copyLabelText: COPY_LABELS.copied,
copyLocked: true,
},
() => {
setTimeout(() => {
this.setState({
copyLabelText: COPY_LABELS.copy,
copyLocked: false,
})
}, 500)
}
)
}
}
openShareModal = ({ service, url }) => event => {
event.preventDefault()
// Extracted from https://github.com/artsy/force/blob/master/src/desktop/components/share/view.coffee#L19
const wLeft = window.screenLeft || window.screenX
const wTop = window.screenTop || window.screenY
const width = MODAL.width
const height = MODAL.height
const left = wLeft + window.innerWidth / 2 - width / 2 || 0
const top = wTop + window.innerHeight / 2 - height / 2 || 0
const options = Object.entries({
status: 1,
width,
height,
top,
left,
})
.map(([key, value]) => `${key}=${value}`)
.join(",")
window.open(url, service, options)
}
renderShareButton({ service, label, message, url }) {
return (
<Flex
flexDirection="row"
flexBasis="50%"
mt={2}
onClick={this.openShareModal({
service,
url,
})}
>
<Icon name={service} color="black" />
<Sans size="3" color="black60">
<a>{label}</a>
</Sans>
</Flex>
)
}
render() {
const {
artwork: {
href,
artworkMeta: { share },
images,
},
} = this.props
const shareImageUrl = images && images[0].url
const url = sd.APP_URL + href
return (
<ArtworkPopoutPanel title="Share" onClose={this.props.onClose}>
<Flex flexDirection="row" mb={1}>
<SansGrow size={["3", "2"]} color="black60" mr={4}>
<URLInput
type="text"
readOnly
value={url}
ref={input => (this.input = input)}
onClick={this.handleCopy}
/>
</SansGrow>
<Sans size={["3", "2"]} weight="medium" color="black60">
<a onClick={this.handleCopy}>{this.state.copyLabelText}</a>
</Sans>
</Flex>
<Separator />
<Flex flexDirection="row" flexWrap="wrap">
{this.renderShareButton({
service: "facebook",
label: "Facebook",
message: "Post to Facebook",
url: `https://www.facebook.com/sharer/sharer.php?u=${url}`,
})}
{this.renderShareButton({
service: "twitter",
label: "Twitter",
message: "Share on Twitter",
url: `https://twitter.com/intent/tweet?original_referer=${url}&text=${share}&url=${url}&via=artsy`,
})}
{/*
NOTE: Safari requires direct user interaction.
See: https://developer.apple.com/safari/technology-preview/release-notes/#r15
*/}
<Flex flexDirection="row" flexBasis="50%" mt={2}>
<Icon name="mail" color="black" />
<Sans size="3" color="black60">
<UnstyledLink
href={`mailto:?subject=${share}&body=${share} on Artsy: ${url}`}
>
Mail
</UnstyledLink>
</Sans>
</Flex>
{this.renderShareButton({
service: "pinterest",
label: "Pinterest",
message: "Pin It on Pinterest",
url: `https://pinterest.com/pin/create/button/?url=${url}&media=${shareImageUrl}&description=${share}`,
})}
{this.renderShareButton({
service: "tumblr",
label: "Tumblr",
message: "",
url: `https://www.tumblr.com/share/photo?source=${shareImageUrl}&caption=${share}&clickthru=${url}`,
})}
</Flex>
</ArtworkPopoutPanel>
)
}
}
export const ArtworkSharePanelFragmentContainer = createFragmentContainer(
ArtworkSharePanel,
{
artwork: graphql`
fragment ArtworkSharePanel_artwork on Artwork {
href
images {
url
}
artworkMeta: meta {
share
}
}
`,
}
)
const SansGrow = styled(Sans)`
display: flex;
flex-grow: 1;
`
const URLInput = styled.input`
border: 0;
text-overflow: ellipsis;
display: flex;
flex-grow: 1;
color: inherit;
font-family: Arial, Helvetica, sans-serif;
${media.xs`
font-size: 16px;
`};
&:hover {
color: ${color("black100")};
}
&::selection {
color: ${color("white100")};
background: ${color("purple100")};
}
`
const UnstyledLink = styled.a`
text-decoration: none;
&:hover {
text-decoration: none;
}
`