-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathsidebar-mobile.jsx
193 lines (165 loc) · 5.04 KB
/
sidebar-mobile.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
import React from 'react';
import Link from '../link/link';
let initialTouchPosition = {};
let lastTouchPosition = {};
export default class SidebarMobile extends React.Component {
constructor(props) {
super(props);
this._handleBodyClick = this._handleBodyClick.bind(this);
}
render() {
return (
<nav
className="sidebar-mobile"
ref={ ref => this.container = ref }
onTouchStart={this._handleTouchStart.bind(this)}
onTouchMove={this._handleTouchMove.bind(this)}
onTouchEnd={this._handleTouchEnd.bind(this)}>
<div
className="sidebar-mobile__toggle"
onTouchStart={this._handleTouchStart.bind(this)}
onTouchMove={this._handleOpenerTouchMove.bind(this)}
onTouchEnd={this._handleTouchEnd.bind(this)} />
<div className="sidebar-mobile__content">
<i
className="sidebar-mobile__close icon-cross"
onClick={ this._close.bind(this) } />
{ this._getSections() }
</div>
</nav>
);
}
componentDidMount() {
if (typeof window !== 'undefined') {
window.addEventListener('click', this._handleBodyClick);
window.addEventListener('touchstart', this._handleBodyClick);
}
}
componentWillUnmount() {
if (typeof window !== 'undefined') {
window.removeEventListener('click', this._handleBodyClick);
window.removeEventListener('touchstart', this._handleBodyClick);
}
}
/**
* Get markup for each section
*
* @return {array} - Markup containing sections and links
*/
_getSections() {
let pathname = '';
if (typeof window !== 'undefined') {
pathname = window.location.pathname;
}
return this.props.sections.map(section => {
let active = pathname === section.url || pathname.includes(`/${section.url}`),
absoluteUrl = `/${section.url}`;
return (
<div
className={ `sidebar-mobile__section ${active ? 'sidebar-mobile__section--active' : ''}` }
key={ absoluteUrl }>
<Link
className="sidebar-mobile__section-header"
key={ absoluteUrl }
to={ absoluteUrl }
onClick={ this._close.bind(this) }>
<h3>{ section.title }</h3>
</Link>
{ this._getPages(section.pages) }
</div>
);
});
}
/**
* Retrieve markup for page links
*
* @param {array} pages - A list of page objects
* @return {array} - Markup containing the page links
*/
_getPages(pages) {
let pathname = '';
if (typeof window !== 'undefined') {
pathname = window.location.pathname;
}
return pages.map(page => {
let url = `/${page.url}`,
active = pathname === url || pathname.includes(`${url}/`);
return (
<Link
key={ url }
className={ `sidebar-mobile__page ${active ? 'sidebar-mobile__page--active' : ''}` }
to={ url }
onClick={ this._close.bind(this) }>
{ page.title }
</Link>
);
});
}
/**
* Handle clicks on content
*
* @param {object} e - Native click event
*/
_handleBodyClick(e) {
if (
!e.target.classList.contains('icon-menu') &&
!this.container.contains(e.target)
) {
this._close();
}
}
/**
* Hide the sidebar
*
*/
_close() {
this.container.classList.remove(
'sidebar-mobile--visible'
);
}
_open() {
this.container.classList.add(
'sidebar-mobile--visible'
);
}
_handleTouchStart(e){
initialTouchPosition.x = e.touches[0].pageX;
initialTouchPosition.y = e.touches[0].pageY;
// For instant transform along with the touch
this.container.classList.add('no-delay');
}
_handleTouchMove(e){
let xDiff = initialTouchPosition.x - e.touches[0].pageX;
let yDiff = initialTouchPosition.y - e.touches[0].pageY;
let factor = Math.abs(yDiff / xDiff);
// Factor makes sure horizontal and vertical scroll dont take place together
if (xDiff>0 && factor < 0.8) {
e.preventDefault();
this.container.style.transform = `translateX(-${xDiff}px)`;
lastTouchPosition.x = e.touches[0].pageX;
lastTouchPosition.y = e.touches[0].pageY;
}
}
_handleOpenerTouchMove(e){
let xDiff = e.touches[0].pageX - initialTouchPosition.x;
let yDiff = initialTouchPosition.y - e.touches[0].pageY;
let factor = Math.abs(yDiff / xDiff);
// Factor makes sure horizontal and vertical scroll dont take place together
if (xDiff > 0 && xDiff < 295 && factor < 0.8) {
e.preventDefault();
this.container.style.transform = `translateX(calc(-100% + ${xDiff}px))`;
lastTouchPosition.x = e.touches[0].pageX;
lastTouchPosition.y = e.touches[0].pageY;
}
}
_handleTouchEnd(e){
// Free up all the inline styling
this.container.classList.remove('no-delay');
this.container.style.transform = '';
if (initialTouchPosition.x - lastTouchPosition.x > 100) {
this._close();
} else if (lastTouchPosition.x - initialTouchPosition.x > 100) {
this._open();
}
}
}