This repository has been archived by the owner on Jun 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
/
sidebar.js
executable file
·470 lines (411 loc) · 12.3 KB
/
sidebar.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import React, { Component } from "react";
import PropTypes from "prop-types";
const CANCEL_DISTANCE_ON_SCROLL = 20;
const defaultStyles = {
root: {
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
overflow: "hidden"
},
sidebar: {
zIndex: 2,
position: "absolute",
top: 0,
bottom: 0,
transition: "transform .3s ease-out",
WebkitTransition: "-webkit-transform .3s ease-out",
willChange: "transform",
overflowY: "auto"
},
content: {
position: "absolute",
top: 0,
left: 0,
right: 0,
bottom: 0,
overflowY: "auto",
WebkitOverflowScrolling: "touch",
transition: "left .3s ease-out, right .3s ease-out"
},
overlay: {
zIndex: 1,
position: "fixed",
top: 0,
left: 0,
right: 0,
bottom: 0,
opacity: 0,
visibility: "hidden",
transition: "opacity .3s ease-out, visibility .3s ease-out",
backgroundColor: "rgba(0,0,0,.3)"
},
dragHandle: {
zIndex: 1,
position: "fixed",
top: 0,
bottom: 0
}
};
class Sidebar extends Component {
constructor(props) {
super(props);
this.state = {
// the detected width of the sidebar in pixels
sidebarWidth: props.defaultSidebarWidth,
// keep track of touching params
touchIdentifier: null,
touchStartX: null,
touchCurrentX: null,
// if touch is supported by the browser
dragSupported: false
};
this.overlayClicked = this.overlayClicked.bind(this);
this.onTouchStart = this.onTouchStart.bind(this);
this.onTouchMove = this.onTouchMove.bind(this);
this.onTouchEnd = this.onTouchEnd.bind(this);
this.onScroll = this.onScroll.bind(this);
this.saveSidebarRef = this.saveSidebarRef.bind(this);
}
componentDidMount() {
const isIos = /iPad|iPhone|iPod/.test(navigator ? navigator.userAgent : "");
this.setState({
dragSupported:
typeof window === "object" && "ontouchstart" in window && !isIos
});
this.saveSidebarWidth();
}
componentDidUpdate() {
// filter out the updates when we're touching
if (!this.isTouching()) {
this.saveSidebarWidth();
}
}
onTouchStart(ev) {
// filter out if a user starts swiping with a second finger
if (!this.isTouching()) {
const touch = ev.targetTouches[0];
this.setState({
touchIdentifier: touch.identifier,
touchStartX: touch.clientX,
touchCurrentX: touch.clientX
});
}
}
onTouchMove(ev) {
if (this.isTouching()) {
for (let ind = 0; ind < ev.targetTouches.length; ind++) {
// we only care about the finger that we are tracking
if (ev.targetTouches[ind].identifier === this.state.touchIdentifier) {
this.setState({
touchCurrentX: ev.targetTouches[ind].clientX
});
break;
}
}
}
}
onTouchEnd() {
if (this.isTouching()) {
// trigger a change to open if sidebar has been dragged beyond dragToggleDistance
const touchWidth = this.touchSidebarWidth();
if (
(this.props.open &&
touchWidth <
this.state.sidebarWidth - this.props.dragToggleDistance) ||
(!this.props.open && touchWidth > this.props.dragToggleDistance)
) {
this.props.onSetOpen(!this.props.open);
}
this.setState({
touchIdentifier: null,
touchStartX: null,
touchCurrentX: null
});
}
}
// This logic helps us prevents the user from sliding the sidebar horizontally
// while scrolling the sidebar vertically. When a scroll event comes in, we're
// cancelling the ongoing gesture if it did not move horizontally much.
onScroll() {
if (this.isTouching() && this.inCancelDistanceOnScroll()) {
this.setState({
touchIdentifier: null,
touchStartX: null,
touchCurrentX: null
});
}
}
// True if the on going gesture X distance is less than the cancel distance
inCancelDistanceOnScroll() {
let cancelDistanceOnScroll;
if (this.props.pullRight) {
cancelDistanceOnScroll =
Math.abs(this.state.touchCurrentX - this.state.touchStartX) <
CANCEL_DISTANCE_ON_SCROLL;
} else {
cancelDistanceOnScroll =
Math.abs(this.state.touchStartX - this.state.touchCurrentX) <
CANCEL_DISTANCE_ON_SCROLL;
}
return cancelDistanceOnScroll;
}
isTouching() {
return this.state.touchIdentifier !== null;
}
overlayClicked() {
if (this.props.open) {
this.props.onSetOpen(false);
}
}
saveSidebarWidth() {
const width = this.sidebar.offsetWidth;
if (width !== this.state.sidebarWidth) {
this.setState({ sidebarWidth: width });
}
}
saveSidebarRef(node) {
this.sidebar = node;
}
// calculate the sidebarWidth based on current touch info
touchSidebarWidth() {
// if the sidebar is open and start point of drag is inside the sidebar
// we will only drag the distance they moved their finger
// otherwise we will move the sidebar to be below the finger.
if (this.props.pullRight) {
if (
this.props.open &&
window.innerWidth - this.state.touchStartX < this.state.sidebarWidth
) {
if (this.state.touchCurrentX > this.state.touchStartX) {
return (
this.state.sidebarWidth +
this.state.touchStartX -
this.state.touchCurrentX
);
}
return this.state.sidebarWidth;
}
return Math.min(
window.innerWidth - this.state.touchCurrentX,
this.state.sidebarWidth
);
}
if (this.props.open && this.state.touchStartX < this.state.sidebarWidth) {
if (this.state.touchCurrentX > this.state.touchStartX) {
return this.state.sidebarWidth;
}
return (
this.state.sidebarWidth -
this.state.touchStartX +
this.state.touchCurrentX
);
}
return Math.min(this.state.touchCurrentX, this.state.sidebarWidth);
}
render() {
const sidebarStyle = {
...defaultStyles.sidebar,
...this.props.styles.sidebar
};
const contentStyle = {
...defaultStyles.content,
...this.props.styles.content
};
const overlayStyle = {
...defaultStyles.overlay,
...this.props.styles.overlay
};
const useTouch = this.state.dragSupported && this.props.touch;
const isTouching = this.isTouching();
const rootProps = {
className: this.props.rootClassName,
style: { ...defaultStyles.root, ...this.props.styles.root },
role: "navigation",
id: this.props.rootId
};
let dragHandle;
const hasBoxShadow =
this.props.shadow && (isTouching || this.props.open || this.props.docked);
// sidebarStyle right/left
if (this.props.pullRight) {
sidebarStyle.right = 0;
sidebarStyle.transform = "translateX(100%)";
sidebarStyle.WebkitTransform = "translateX(100%)";
if (hasBoxShadow) {
sidebarStyle.boxShadow = "-2px 2px 4px rgba(0, 0, 0, 0.15)";
}
} else {
sidebarStyle.left = 0;
sidebarStyle.transform = "translateX(-100%)";
sidebarStyle.WebkitTransform = "translateX(-100%)";
if (hasBoxShadow) {
sidebarStyle.boxShadow = "2px 2px 4px rgba(0, 0, 0, 0.15)";
}
}
if (isTouching) {
const percentage = this.touchSidebarWidth() / this.state.sidebarWidth;
// slide open to what we dragged
if (this.props.pullRight) {
sidebarStyle.transform = `translateX(${(1 - percentage) * 100}%)`;
sidebarStyle.WebkitTransform = `translateX(${(1 - percentage) * 100}%)`;
} else {
sidebarStyle.transform = `translateX(-${(1 - percentage) * 100}%)`;
sidebarStyle.WebkitTransform = `translateX(-${(1 - percentage) *
100}%)`;
}
// fade overlay to match distance of drag
overlayStyle.opacity = percentage;
overlayStyle.visibility = "visible";
} else if (this.props.docked) {
// show sidebar
if (this.state.sidebarWidth !== 0) {
sidebarStyle.transform = `translateX(0%)`;
sidebarStyle.WebkitTransform = `translateX(0%)`;
}
// make space on the left/right side of the content for the sidebar
if (this.props.pullRight) {
contentStyle.right = `${this.state.sidebarWidth}px`;
} else {
contentStyle.left = `${this.state.sidebarWidth}px`;
}
} else if (this.props.open) {
// slide open sidebar
sidebarStyle.transform = `translateX(0%)`;
sidebarStyle.WebkitTransform = `translateX(0%)`;
// show overlay
overlayStyle.opacity = 1;
overlayStyle.visibility = "visible";
}
if (isTouching || !this.props.transitions) {
sidebarStyle.transition = "none";
sidebarStyle.WebkitTransition = "none";
contentStyle.transition = "none";
overlayStyle.transition = "none";
}
if (useTouch) {
if (this.props.open) {
rootProps.onTouchStart = this.onTouchStart;
rootProps.onTouchMove = this.onTouchMove;
rootProps.onTouchEnd = this.onTouchEnd;
rootProps.onTouchCancel = this.onTouchEnd;
rootProps.onScroll = this.onScroll;
} else {
const dragHandleStyle = {
...defaultStyles.dragHandle,
...this.props.styles.dragHandle
};
dragHandleStyle.width = this.props.touchHandleWidth;
// dragHandleStyle right/left
if (this.props.pullRight) {
dragHandleStyle.right = 0;
} else {
dragHandleStyle.left = 0;
}
dragHandle = (
<div
style={dragHandleStyle}
onTouchStart={this.onTouchStart}
onTouchMove={this.onTouchMove}
onTouchEnd={this.onTouchEnd}
onTouchCancel={this.onTouchEnd}
/>
);
}
}
return (
<div {...rootProps}>
<div
className={this.props.sidebarClassName}
style={sidebarStyle}
ref={this.saveSidebarRef}
id={this.props.sidebarId}
>
{this.props.sidebar}
</div>
{/* eslint-disable */}
<div
className={this.props.overlayClassName}
style={overlayStyle}
onClick={this.overlayClicked}
id={this.props.overlayId}
/>
{/* eslint-enable */}
<div
className={this.props.contentClassName}
style={contentStyle}
id={this.props.contentId}
>
{dragHandle}
{this.props.children}
</div>
</div>
);
}
}
Sidebar.propTypes = {
// main content to render
children: PropTypes.node.isRequired,
// styles
styles: PropTypes.shape({
root: PropTypes.object,
sidebar: PropTypes.object,
content: PropTypes.object,
overlay: PropTypes.object,
dragHandle: PropTypes.object
}),
// root component optional class
rootClassName: PropTypes.string,
// sidebar optional class
sidebarClassName: PropTypes.string,
// content optional class
contentClassName: PropTypes.string,
// overlay optional class
overlayClassName: PropTypes.string,
// sidebar content to render
sidebar: PropTypes.node.isRequired,
// boolean if sidebar should be docked
docked: PropTypes.bool,
// boolean if sidebar should slide open
open: PropTypes.bool,
// boolean if transitions should be disabled
transitions: PropTypes.bool,
// boolean if touch gestures are enabled
touch: PropTypes.bool,
// max distance from the edge we can start touching
touchHandleWidth: PropTypes.number,
// Place the sidebar on the right
pullRight: PropTypes.bool,
// Enable/Disable sidebar shadow
shadow: PropTypes.bool,
// distance we have to drag the sidebar to toggle open state
dragToggleDistance: PropTypes.number,
// callback called when the overlay is clicked
onSetOpen: PropTypes.func,
// Initial sidebar width when page loads
defaultSidebarWidth: PropTypes.number,
// root component optional id
rootId: PropTypes.string,
// sidebar optional id
sidebarId: PropTypes.string,
// content optional id
contentId: PropTypes.string,
// overlay optional id
overlayId: PropTypes.string
};
Sidebar.defaultProps = {
docked: false,
open: false,
transitions: true,
touch: true,
touchHandleWidth: 20,
pullRight: false,
shadow: true,
dragToggleDistance: 30,
onSetOpen: () => {},
styles: {},
defaultSidebarWidth: 0
};
export default Sidebar;