This repository was archived by the owner on Feb 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathindex.jsx
369 lines (344 loc) · 11.6 KB
/
index.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import 'core-js/es6';
import 'core-js/es7';
import React from "react";
import { render } from "react-dom";
import AddToCalendarHOC, { SHARE_SITES } from "../../lib";
import Button from './Button';
import CalendarModal from './Modal';
import CodeSnippet from './CodeSnippet';
import Dropdown from './Dropdown';
import { DateTime } from 'luxon';
import moment from 'moment-timezone';
import { css } from 'emotion';
import "./styles.css";
const pageStyles = css`
width: 100%;
padding: 0 20px;
margin: 0 auto;
@media (min-width: 768px) {
width: 75%;
padding: 0;
}
`;
const componentStyles = css`
width: 100%;
margin: 0 auto;
text-align: center;
padding: 0 0 30px;
@media (min-width: 768px) {
width: 50%;
}
`;
const linkStyles = css`
text-decoration: none;
display: block;
color: #E42D2D;
font-size: 18px;
text-align: center;
padding: 6px;
`;
const titleStyles = css`
margin: 75px 0;
text-align: center;
`;
const highlightText = css`
font-family: Courier;
font-style: italic;
color: rgb(218, 49, 80);
background-color: #FFF;
padding: 1px 4px;
`;
const subTitleStyles = css`
margin: 50px 0;
text-align: center;
`;
const paragraphStyles = css`
margin: 30px auto;
width: 80%;
`;
const startDatetime = moment().utc().add(2, 'days');
const endDatetime = startDatetime.clone().add(2, 'hours');
const duration = endDatetime.diff(startDatetime, 'hours');
const event = {
description: 'Description of event. Going to have a lot of fun doing things that we scheduled ahead of time.',
duration,
endDatetime: endDatetime.format('YYYYMMDDTHHmmssZ'),
location: 'NYC',
startDatetime: startDatetime.format('YYYYMMDDTHHmmssZ'),
title: 'Super Fun Event',
}
const eventInDifferentTimezone = {
...event,
location: 'London',
endDatetime: moment().tz('Europe/London').add(2, 'days').add(2, 'hours').format('YYYYMMDDTHHmmss'),
startDatetime: moment().tz('Europe/London').add(2, 'days').format('YYYYMMDDTHHmmss'),
timezone: 'Europe/London',
}
const luxonStart = DateTime.fromObject({ year: 2018, month: 10, day: 24, hour: 12, minute: 15, zone: 'America/New_York' });
const luxonEnd = DateTime.fromObject({ year: 2018, month: 10, day: 24, hour: 14, minute: 15, zone: 'America/New_York' });
const luxonEvent = {
...event,
startDatetime: `${luxonStart.toFormat('yyyyLLdd')}T${luxonStart.toFormat('HHmmss')}`,
endDatetime: `${luxonEnd.toFormat('yyyyLLdd')}T${luxonEnd.toFormat('HHmmss')}`,
location: 'NYC',
timezone: 'America/New_York',
}
const AddToCalendarDropdown = AddToCalendarHOC(Button, Dropdown);
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
const isiOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
function Demo() {
return (
<div className={pageStyles}>
<h1 className={titleStyles}>
Examples of React Add to Calendar HOC
</h1>
<div>
<h2 className={subTitleStyles}>Event passed into examples</h2>
<CodeSnippet>
{`
const startDatetime = moment().utc().add(2, 'days');
const endDatetime = startDatetime.clone().add(2, 'hours');
const duration = moment.duration(endDatetime.diff(startDatetime)).asHours();
const event = {
description: 'Description of event. Going to have a lot of fun doing things that we scheduled ahead of time.',
duration,
endDatetime: endDatetime.format('YYYYMMDDTHHmmssZ'),
location: 'NYC',
startDatetime: startDatetime.format('YYYYMMDDTHHmmssZ'),
title: 'Super Fun Event',
}
`}
</CodeSnippet>
</div>
<h2 className={subTitleStyles}>Dropdown Example</h2>
<AddToCalendarDropdown
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
/>
<CodeSnippet>
{`
const AddToCalendarDropdown = AddToCalendarHOC(Button, Dropdown);
...
<AddToCalendarDropdown
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Dropdown Example - Handle Newlines in Description</h2>
<AddToCalendarDropdown
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={{
...event,
description: 'Description of event. <br>Going to have a lot of fun doing things that we scheduled ahead of time.'
}}
/>
<CodeSnippet>
{`
const AddToCalendarDropdown = AddToCalendarHOC(Button, Dropdown);
...
<AddToCalendarDropdown
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Modal Example</h2>
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
/>
<CodeSnippet>
{`
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
...
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Custom Button Text</h2>
<AddToCalendarModal
className={componentStyles}
buttonText="Custom Button Text"
linkProps={{
className: linkStyles,
}}
event={event}
/>
<CodeSnippet>
{`
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
...
<AddToCalendarModal
className={componentStyles}
buttonText="Custom Button Text"
linkProps={{
className: linkStyles,
}}
event={event}
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Customized Item List Example</h2>
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
items={[SHARE_SITES.GOOGLE, SHARE_SITES.ICAL]}
/>
<CodeSnippet>
{`
import AddToCalendarHOC, { SHARE_SITES } from 'react-add-to-calendar-hoc';
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
...
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
items={[SHARE_SITES.GOOGLE, SHARE_SITES.ICAL]}
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Custom Download Filename</h2>
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
filename="SuperFunEvent"
/>
<CodeSnippet>
{`
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
...
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
filename="SuperFunEvent"
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Handle iPhone Options</h2>
<p className={paragraphStyles}>iPhones don't allow users to select which app they want to open ics files with, so there is no reason to offer both iCal and Outlook options for users on iOS devices. This example shows how to conditionally change which items to display based on the user's device.</p>
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
items={isiOS ? [SHARE_SITES.GOOGLE, SHARE_SITES.ICAL, SHARE_SITES.YAHOO] : undefined}
/>
<CodeSnippet>
{`
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
const isiOS = /iPad|iPhone|iPod/.test(navigator.userAgent);
...
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={event}
items={isiOS ? [SHARE_SITES.GOOGLE, SHARE_SITES.ICAL, SHARE_SITES.YAHOO] : null}
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Handle Timezones</h2>
<p className={paragraphStyles}>To support events in a specific timezone you have to do a couple of things. First, pass in an additional property, <span className={highlightText}>timezone</span>. The value of this should be a valid TZ environment variable (<a href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" target="_blank" rel="noopener noreferrer">See here</a> for a list).</p>
<p className={paragraphStyles}>You should also pass the value of <span className={highlightText}>startDatetime</span> and <span className={highlightText}>endDatetime</span> as local values and not UTC. In other words, if you want to create an event at 11am EST you should pass in a time value of 11am, not 7am UTC (EST has -04:00 offset). You can do this by formatting the date as <span className={highlightText}>YYYYMMDDTHHmmss</span> - without the Z property.</p>
<p className={paragraphStyles}>Doing this will result in two things -- regardless of the timezone of your users, the event will always be created at the correct time for the timezone set. Secondly, the event will now include timezone information, i.e. it will say Eastern Time.</p>
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={eventInDifferentTimezone}
/>
<CodeSnippet>
{`
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
const eventInDifferentTimezone = {
...event,
location: 'London',
endDatetime: moment().tz('Europe/London').add(2, 'days').add(2, 'hours').format('YYYYMMDDTHHmmss'),
startDatetime: moment().tz('Europe/London').add(2, 'days').format('YYYYMMDDTHHmmss'),
timezone: 'Europe/London',
}
...
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={eventInDifferentTimezone}
/>
`}
</CodeSnippet>
<h2 className={subTitleStyles}>Use Moment Alternative</h2>
<p className={paragraphStyles}>Moment is known to be a MASSIVE library. v2.22.2 is 64.2kb minified + gzipped and moment-timezone v0.5.21 is 89.8kb minified + gzipped. There are plenty of other date time libraries for JS that are way smaller. Using one of these helps you avoid overly bloating your application and sending too many vendor files to the client. One great option is Luxon. Luxon v.1.4.4 is 16.9kb minified + gzipped.</p>
<p className={paragraphStyles}>This example shows how to use the Luxon library (instead of Moment) to construct <span className={highlightText}>startDatetime</span> and <span className={highlightText}>endDatetime</span></p>
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={luxonEvent}
/>
<CodeSnippet>
{`
const AddToCalendarModal = AddToCalendarHOC(Button, CalendarModal);
const startTime = DateTime.fromObject({ year: 2018, month: 10, day: 25, hour: 12 });
const endTime = startTime.plus({ hours: 2 });
const duration = endDatetime.diff(startDatetime).as('hours');
const eventInDifferentTimezone = {
...event,
description: 'Description of event. Going to have a lot of fun doing things that we scheduled ahead of time.',
duration,
endDatetime: endTime.toFormat('YYYYMMDDTHHmmss'),
location: 'London',
startDatetime: startTime.toFormat('YYYYMMDDTHHmmss'),
timezone: 'Europe/London',
title: 'Super Fun Event',
}
...
<AddToCalendarModal
className={componentStyles}
linkProps={{
className: linkStyles,
}}
event={eventInDifferentTimezone}
/>
`}
</CodeSnippet>
</div>
);
}
render(<Demo />, document.getElementById("app"));