/
share-button-group.jsx
166 lines (146 loc) · 3.68 KB
/
share-button-group.jsx
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
import React from "react";
import classNames from "classnames";
import copyToClipboard from "../../copy-to-clipboard";
export default class ShareButtonGroup extends React.Component {
constructor(props) {
super(props);
this.state = {
linkCopied: false
};
}
componentDidMount() {
if (this.props.whenLoaded) {
this.props.whenLoaded();
}
}
renderFacebookButton() {
let label =
this.props.version === `mini` ? (
<span class="sr-only">Share on Facebook</span>
) : (
`Facebook`
);
let link = this.props.link || ``;
return (
<a
class="btn btn-secondary btn-share facebook-share"
href={`https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(
link
)}`}
>
{label}
</a>
);
}
renderTwitterButton() {
let shareText = this.props.shareText
? encodeURIComponent(this.props.shareText)
: ``;
let link = this.props.link ? ` ${encodeURIComponent(this.props.link)}` : ``;
let label =
this.props.version === `mini` ? (
<span class="sr-only">Share on Twitter</span>
) : (
`Twitter`
);
return (
<a
class="btn btn-secondary btn-share twitter-share"
href={`https://twitter.com/intent/tweet?text=${shareText}${link}`}
>
{label}
</a>
);
}
renderEmailButton() {
let shareText = this.props.shareText
? encodeURIComponent(this.props.shareText)
: ``;
let link = this.props.link ? ` ${encodeURIComponent(this.props.link)}` : ``;
let label =
this.props.version === `mini` ? (
<span class="sr-only">Share by email</span>
) : (
`Email`
);
return (
<a
class="btn btn-secondary btn-share email-share"
href={`mailto:?&body=${shareText}${link}`}
>
{label}
</a>
);
}
handleLinkButtonClick(event) {
event.preventDefault();
copyToClipboard(event.target, window.location.href);
this.setState({
linkCopied: true
});
}
renderLinkButton() {
let tooltip = this.state.linkCopied
? `Copied`
: `Copy page URL to clipboard`;
let label = this.state.linkCopied ? `Copied` : `Copy`;
label =
this.props.version === `mini` ? (
<span class="sr-only">{label} page link</span>
) : (
label
);
let classes = classNames(`btn btn-secondary btn-share link-share`, {
copied: this.state.linkCopied
});
return (
<a
class={classes}
href="#"
onClick={event => this.handleLinkButtonClick(event)}
title={tooltip}
>
{label}
</a>
);
}
renderRectangleButtons() {
let classes = classNames(`share-button-group rectangle`, {
stacked: this.props.layout === `stacked`
});
return (
<div className={classes}>
<div className="subgroup">
{this.renderFacebookButton()}
{this.renderTwitterButton()}
</div>
<div className="subgroup">
{this.renderEmailButton()}
{this.renderLinkButton()}
</div>
</div>
);
}
renderCircleButtons() {
let classes = classNames(`share-button-group circle`, {
stacked: this.props.layout === `stacked`
});
return (
<div className={classes}>
{this.renderFacebookButton()}
{this.renderTwitterButton()}
{this.renderEmailButton()}
{this.renderLinkButton()}
</div>
);
}
render() {
return (
<React.Fragment>
{this.props.version === `mini`
? this.renderCircleButtons()
: this.renderRectangleButtons()}
</React.Fragment>
);
}
}