@@ -2,6 +2,9 @@ import React from 'react';
2
2
import Link from '../link/link' ;
3
3
import './sidebar-mobile-style' ;
4
4
5
+ let initialTouchPosition = { } ;
6
+ let lastTouchPosition = { } ;
7
+
5
8
export default class SidebarMobile extends React . Component {
6
9
constructor ( props ) {
7
10
super ( props ) ;
@@ -11,11 +14,21 @@ export default class SidebarMobile extends React.Component {
11
14
12
15
render ( ) {
13
16
return (
14
- < nav className = "sidebar-mobile" ref = { ref => this . container = ref } >
15
- < i className = "sidebar-mobile__close icon-cross"
17
+ < nav className = "sidebar-mobile" ref = { ref => this . container = ref }
18
+ onTouchStart = { this . _handleTouchStart . bind ( this ) }
19
+ onTouchMove = { this . _handleTouchMove . bind ( this ) }
20
+ onTouchEnd = { this . _handleTouchEnd . bind ( this ) } >
21
+ < div className = "opener"
22
+ onTouchStart = { this . _handleTouchStart . bind ( this ) }
23
+ onTouchMove = { this . _handleOpenerTouchMove . bind ( this ) }
24
+ onTouchEnd = { this . _handleTouchEnd . bind ( this ) } >
25
+ </ div >
26
+ < div className = "sidebar-content" >
27
+ < i className = "sidebar-mobile__close icon-cross"
16
28
onClick = { this . _close . bind ( this ) } />
17
-
18
- { this . _getSections ( ) }
29
+
30
+ { this . _getSections ( ) }
31
+ </ div >
19
32
</ nav >
20
33
) ;
21
34
}
@@ -101,4 +114,55 @@ export default class SidebarMobile extends React.Component {
101
114
'sidebar-mobile--visible'
102
115
) ;
103
116
}
117
+
118
+ _open ( ) {
119
+ this . container . classList . add (
120
+ 'sidebar-mobile--visible'
121
+ ) ;
122
+ }
123
+
124
+ _handleTouchStart ( e ) {
125
+ initialTouchPosition . x = e . touches [ 0 ] . pageX ;
126
+ initialTouchPosition . y = e . touches [ 0 ] . clientY ;
127
+ //for instant transform along with the touch
128
+ this . container . classList . add ( "no-delay" ) ;
129
+ }
130
+
131
+ _handleTouchMove ( e ) {
132
+ let xDiff = initialTouchPosition . x - e . touches [ 0 ] . pageX ;
133
+ let yDiff = initialTouchPosition . y - e . touches [ 0 ] . pageY ;
134
+ let factor = Math . abs ( yDiff / xDiff ) ;
135
+ //factor makes sure horizontal and vertical scroll dont take place together
136
+ if ( xDiff > 0 && factor < 0.8 ) {
137
+ e . preventDefault ( ) ;
138
+ this . container . style . transform = "translateX(-" + xDiff + "px)" ;
139
+ lastTouchPosition . x = e . touches [ 0 ] . pageX ;
140
+ lastTouchPosition . y = e . touches [ 0 ] . clientY ;
141
+ }
142
+ }
143
+
144
+ _handleOpenerTouchMove ( e ) {
145
+ let xDiff = e . touches [ 0 ] . pageX - initialTouchPosition . x ;
146
+ let yDiff = initialTouchPosition . y - e . touches [ 0 ] . pageY ;
147
+ let factor = Math . abs ( yDiff / xDiff ) ;
148
+ //factor makes sure horizontal and vertical scroll dont take place together
149
+ if ( xDiff > 0 && xDiff < 295 && factor < 0.8 ) {
150
+ e . preventDefault ( ) ;
151
+ this . container . style . transform = "translateX(calc(-100% + " + xDiff + "px))" ;
152
+ lastTouchPosition . x = e . touches [ 0 ] . pageX ;
153
+ lastTouchPosition . y = e . touches [ 0 ] . clientY ;
154
+ }
155
+ }
156
+
157
+ _handleTouchEnd ( e ) {
158
+ //free up all the inline styling
159
+ this . container . classList . remove ( "no-delay" ) ;
160
+ this . container . style = "" ;
161
+ if ( initialTouchPosition . x - lastTouchPosition . x > 100 ) {
162
+ this . _close ( ) ;
163
+ } else if ( lastTouchPosition . x - initialTouchPosition . x > 100 ) {
164
+ this . _open ( ) ;
165
+ }
166
+
167
+ }
104
168
}
0 commit comments