-
-
Notifications
You must be signed in to change notification settings - Fork 443
Expand file tree
/
Copy pathcreateShareButton.jsx
More file actions
225 lines (195 loc) · 5.06 KB
/
createShareButton.jsx
File metadata and controls
225 lines (195 loc) · 5.06 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
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
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
const isPromise = obj => !!obj
&& (typeof obj === 'object' || typeof obj === 'function')
&& typeof obj.then === 'function';
const getBoxPositionOnWindowCenter = (width, height) => ({
left: (window.outerWidth / 2)
+ (window.screenX || window.screenLeft || 0) - (width / 2),
top: (window.outerHeight / 2)
+ (window.screenY || window.screenTop || 0) - (height / 2),
});
const getBoxPositionOnScreenCenter = (width, height) => ({
top: (window.screen.height - height) / 2,
left: (window.screen.width - width) / 2,
});
function windowOpen(url, { height = 400, width = 550, ...configRest }, onClose) {
const config = {
height,
width,
location: 'no',
toolbar: 'no',
status: 'no',
directories: 'no',
menubar: 'no',
scrollbars: 'yes',
resizable: 'no',
centerscreen: 'yes',
chrome: 'yes',
...configRest,
};
const shareDialog = window.open(
url,
'',
Object.keys(config).map(key => `${key}=${config[key]}`).join(', '),
);
if (onClose) {
const interval = window.setInterval(() => {
try {
if (shareDialog === null || shareDialog.closed) {
window.clearInterval(interval);
onClose(shareDialog);
}
} catch (e) {
/* eslint-disable no-console */
console.error(e);
/* eslint-enable no-console */
}
}, 1000);
}
return shareDialog;
}
class ShareButton extends PureComponent {
static propTypes = {
additionalProps: PropTypes.object,
children: PropTypes.node,
className: PropTypes.string,
disabled: PropTypes.bool,
disabledStyle: PropTypes.object,
network: PropTypes.string.isRequired,
networkLink: PropTypes.func.isRequired,
onClick: PropTypes.func,
opts: PropTypes.object,
openWindow: PropTypes.bool,
url: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
]).isRequired,
role: PropTypes.string,
style: PropTypes.object,
windowWidth: PropTypes.number,
windowHeight: PropTypes.number,
windowPosition: PropTypes.oneOf(['windowCenter', 'screenCenter']),
beforeOnClick: PropTypes.func,
onShareWindowClose: PropTypes.func,
tabIndex: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
};
static defaultProps = {
disabledStyle: {
opacity: 0.6,
},
openWindow: true,
role: 'button',
windowPosition: 'windowCenter',
tabIndex: '0',
}
onClick = async (e) => {
const {
disabled,
onClick,
openWindow,
beforeOnClick,
} = this.props;
if (disabled) {
return;
}
e.preventDefault();
const link = await this.link();
const clickHandler = openWindow ? () => this.openWindow(link) : () => onClick(link);
if (beforeOnClick) {
const returnVal = beforeOnClick();
if (isPromise(returnVal)) {
returnVal.then(clickHandler);
} else {
clickHandler();
}
} else {
clickHandler();
}
}
onKeyPress = (e) => {
if (e.key === 'Enter' || e.key === 13 || e.key === ' ' || e.key === 32) {
this.onClick(e);
}
}
openWindow = (link) => {
const {
windowPosition,
onShareWindowClose,
windowWidth,
windowHeight,
} = this.props;
const windowConfig = {
height: windowHeight,
width: windowWidth,
...(windowPosition === 'windowCenter'
? getBoxPositionOnWindowCenter(windowWidth, windowHeight)
: getBoxPositionOnScreenCenter(windowWidth, windowHeight)
),
};
windowOpen(link, windowConfig, onShareWindowClose);
}
async link() {
let { url } = this.props;
const { opts, networkLink } = this.props;
if (typeof url === 'function') {
url = await Promise.resolve(url());
}
return networkLink(url, opts);
}
render() {
const {
additionalProps,
children,
className,
disabled,
disabledStyle,
network,
role,
style,
tabIndex,
} = this.props;
const classes = cx(
'SocialMediaShareButton',
`SocialMediaShareButton--${network}`,
{
'SocialMediaShareButton--disabled': !!disabled,
disabled: !!disabled,
},
className,
);
return (
<div
aria-label={network}
{...additionalProps}
role={role}
tabIndex={tabIndex}
onClick={this.onClick}
onKeyPress={this.onKeyPress}
className={classes}
style={{
...style,
...(disabled ? disabledStyle : {}),
}}>
{children}
</div>
);
}
}
function createShareButton(network, link, optsMap = () => ({}), propTypes, defaultProps = {}) {
const CreatedButton = React.forwardRef((props, ref) => (
<ShareButton {...props}
ref={ref}
network={network}
networkLink={link}
opts={optsMap(props)} />
));
CreatedButton.propTypes = propTypes;
CreatedButton.defaultProps = defaultProps;
return CreatedButton;
}
export default createShareButton;