-
Notifications
You must be signed in to change notification settings - Fork 8
/
HeadwayWidget.js
262 lines (244 loc) · 6.51 KB
/
HeadwayWidget.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import React, { useCallback, useEffect, useRef } from "react";
import PropTypes from "prop-types";
const HeadwayWidgetClassName = "HW_widget_component";
const HeadwayWidgetSelector = "." + HeadwayWidgetClassName;
const HeadwayWidgetTriggerClassName = "HW_trigger";
const HeadwayWidgetTriggerSelector = "." + HeadwayWidgetTriggerClassName;
const HeadwayWidgetTrigger = ({ widgetId, component, children }) => {
const Component = component || "div";
const className = HeadwayWidgetTriggerClassName + `_${widgetId}`;
return <Component className={className}>{children}</Component>;
};
HeadwayWidgetTrigger.defaultProps = {
widgetId: "widget-1",
};
const HeadwayWidget = ({
id,
account,
children,
widgetPosition,
badgePosition,
component,
trigger,
onWidgetReady,
onShowWidget,
onShowDetails,
onReadMore,
onHideWidget,
styles,
options,
translations,
...rest
}) => {
const onWidgetReadyRef = useRef(onWidgetReady);
const onShowWidgetRef = useRef(onShowWidget);
const onShowDetailsRef = useRef(onShowDetails);
const onReadMoreRef = useRef(onReadMore);
const onHideWidgetRef = useRef(onHideWidget);
const optionsRef = useRef(options);
useEffect(() => {
onWidgetReadyRef.current =
onWidgetReady || (options.callbacks && options.callbacks.onWidgetReady);
onShowWidgetRef.current =
onShowWidget || (options.callbacks && options.callbacks.onShowWidget);
onShowDetailsRef.current =
onShowDetails || (options.callbacks && options.callbacks.onShowDetails);
onReadMoreRef.current =
onReadMore || (options.callbacks && options.callbacks.onReadMore);
onHideWidgetRef.current =
onHideWidget || (options.callbacks && options.callbacks.onHideWidget);
optionsRef.current = options;
});
const handleWidgetReady = useCallback(
() => onWidgetReadyRef.current && onWidgetReadyRef.current(),
[]
);
const handleShowWidget = useCallback(
() => onShowWidgetRef.current && onShowWidgetRef.current(),
[]
);
const handleShowDetails = useCallback(
() => onShowDetailsRef.current && onShowDetailsRef.current(),
[]
);
const handleReadMore = useCallback(
() => onReadMoreRef.current && onReadMoreRef.current(),
[]
);
const handleHideWidget = useCallback(
() => onHideWidgetRef.current && onHideWidgetRef.current(),
[]
);
const initHeadway = useCallback(() => {
const hwConfig = {
selector: HeadwayWidgetSelector + `_${id}`,
account: account || options.account,
trigger: trigger
? HeadwayWidgetSelector + `_${id}`
: options.trigger || HeadwayWidgetTriggerSelector + `_${id}`,
callbacks: {
onWidgetReady: handleWidgetReady,
onShowWidget: handleShowWidget,
onShowDetails: handleShowDetails,
onReadMore: handleReadMore,
onHideWidget: handleHideWidget,
},
translations,
widgetPosition,
badgePosition,
__component: true,
styles,
...options,
};
const widget = window.Headway.getNewWidget();
widget.init(hwConfig);
return () => {
widget.destroy();
};
}, [
account,
trigger,
handleWidgetReady,
handleShowWidget,
handleShowDetails,
handleReadMore,
handleHideWidget,
badgePosition,
widgetPosition,
translations,
]);
useEffect(() => {
let destroy;
if (window.Headway) {
destroy = initHeadway();
} else {
const head = document.getElementsByTagName("head")[0];
const script = document.createElement("script");
script.type = "text/javascript";
const p = new Promise((resolve) => {
script.onload = () => {
const d = initHeadway();
resolve(d);
};
});
destroy = () => {
p.then((d) => d());
};
script.src = "https://cdn.headwayapp.co/widget.js"; // "https://cdn.headwaystaging.com/widget.js"; //;
head.appendChild(script);
}
return destroy;
}, [initHeadway]);
const Component = component || "div";
return (
<Component
className={HeadwayWidgetClassName + `_${id}`}
{...rest}
style={{ position: "relative", display: "inline-block" }}
>
{children}
</Component>
);
};
export default HeadwayWidget;
HeadwayWidget.propTypes = {
/**
* account of your Headway.
*/
account: PropTypes.string.isRequired,
/**
* id of the Widget. It is required if you have more than one widget instance on the page.
*/
id: PropTypes.string,
/**
* position of a badge
*/
badgePosition: PropTypes.oneOf([
"top-left",
"top-center",
"top-right",
"center-left",
"center",
"center-right",
"bottom-left",
"bottom-center",
"bottom-right",
]),
/**
* badge relative widget position
*/
widgetPosition: PropTypes.oneOf([
"top-left",
"top-right",
"center-left",
"center-right",
"bottom-left",
"bottom-right",
]),
/**
* callback fired when widget has loaded
*/
onWidgetReady: PropTypes.func,
/**
* callback fired when widget is shown
*/
onShowWidget: PropTypes.func,
/**
* callback fired when an item in the widget is clicked
*/
onShowDetails: PropTypes.func,
/**
* callback fired when user clicks on readMore link
*/
onReadMore: PropTypes.func,
/**
* callback fired when user closes the widget
*/
onHideWidget: PropTypes.func,
/**
* Translating the text inside of the widget
* */
translations: PropTypes.shape({
title: PropTypes.string,
labels: PropTypes.shape({
new: PropTypes.string,
update: PropTypes.string,
improvement: PropTypes.string,
}),
readMore: PropTypes.string,
footer: PropTypes.string,
}),
/**
* options defined in classic object format. Read more at https://docs.headwayapp.co/widget
*/
options: PropTypes.shape({
account: PropTypes.string,
callbacks: PropTypes.shape({
onWidgetReady: PropTypes.func,
onShowWidget: PropTypes.func,
onShowDetails: PropTypes.func,
onReadMore: PropTypes.func,
onHideWidget: PropTypes.func,
}),
translations: PropTypes.shape({
title: PropTypes.string,
labels: PropTypes.shape({
new: PropTypes.string,
update: PropTypes.string,
improvement: PropTypes.string,
}),
readMore: PropTypes.string,
footer: PropTypes.string,
}),
}),
};
HeadwayWidget.defaultProps = {
account: "",
id: "widget-1",
badgePosition: "bottom-right",
widgetPosition: "bottom-right",
styles: {},
translations: {},
options: {},
};
export { HeadwayWidgetTrigger };