@@ -32,18 +32,14 @@ export default class SidebarContainer extends FlexContainer {
32
32
}
33
33
34
34
#onDragStart( e ) {
35
- if ( ! this . backdropEl ) {
36
- this . backdropEl = document . getElementById ( "mobile-sidebar-container" ) ;
37
- this . sidebarEl = document . getElementById ( "mobile-sidebar-wrapper" ) ;
38
- }
39
-
40
35
const x = e . touches ? e . touches [ 0 ] . clientX : e . clientX ;
41
36
42
37
if ( x > 30 && this . currentTranslate === - 100 ) {
43
38
return ;
44
39
}
45
40
46
41
this . startX = x ;
42
+ this . #setInitialState( ) ;
47
43
this . dragState = DRAG_STATE_INITIAL_DRAG ;
48
44
this . translatePercentage = 0 ;
49
45
}
@@ -58,8 +54,6 @@ export default class SidebarContainer extends FlexContainer {
58
54
if ( this . dragState === DRAG_STATE_INITIAL_DRAG ) {
59
55
if ( Math . abs ( deltaX ) > 5 ) {
60
56
/* Disable the transitions since they affect performance, they are going to reenabled once drag ends. */
61
- this . originalSidebarTransition = this . sidebarEl . style . transition ;
62
- this . originalBackdropTransition = this . backdropEl . style . transition ;
63
57
this . sidebarEl . style . transition = "none" ;
64
58
this . backdropEl . style . transition = "none" ;
65
59
@@ -87,25 +81,41 @@ export default class SidebarContainer extends FlexContainer {
87
81
// If the sidebar is closed, snap the sidebar open only if the user swiped over a threshold.
88
82
// When the sidebar is open, always close for a smooth experience.
89
83
const isOpen = ( this . currentTranslate === - 100 && this . translatePercentage > - ( 100 - DRAG_THRESHOLD ) ) ;
84
+ this . #setSidebarOpen( isOpen ) ;
85
+ }
86
+
87
+ #setInitialState( ) {
88
+ if ( this . sidebarEl ) {
89
+ // Already initialized.
90
+ return ;
91
+ }
92
+
93
+ this . sidebarEl = document . getElementById ( "mobile-sidebar-wrapper" ) ;
94
+ this . backdropEl = document . getElementById ( "mobile-sidebar-container" ) ;
95
+ this . originalSidebarTransition = this . sidebarEl . style . transition ;
96
+ this . originalBackdropTransition = this . backdropEl . style . transition ;
97
+ }
98
+
99
+ #setSidebarOpen( isOpen ) {
100
+ if ( ! this . sidebarEl ) {
101
+ return ;
102
+ }
103
+
90
104
this . sidebarEl . classList . toggle ( "show" , isOpen ) ;
91
105
this . sidebarEl . style . transform = isOpen ? 'translateX(0)' : 'translateX(-100%)' ;
92
106
this . sidebarEl . style . transition = this . originalSidebarTransition ;
93
- this . backdropEl . style . transition = this . originalBackdropTransition ;
94
- this . currentTranslate = isOpen ? 0 : - 100 ;
95
107
96
- if ( ! isOpen ) {
97
- this . backdropEl . classList . remove ( "show" ) ;
98
- }
108
+ this . backdropEl . classList . toggle ( "show" , isOpen ) ;
109
+ this . backdropEl . style . transition = this . originalBackdropTransition ;
110
+ this . backdropEl . style . opacity = isOpen ? 1 : 0 ;
99
111
112
+ this . currentTranslate = isOpen ? 0 : - 100 ;
100
113
this . dragState = DRAG_STATE_NONE ;
101
114
}
102
115
103
116
activeScreenChangedEvent ( { activeScreen} ) {
104
- if ( activeScreen === this . screenName ) {
105
- this . $widget . addClass ( 'show' ) ;
106
- } else {
107
- this . $widget . removeClass ( 'show' ) ;
108
- }
117
+ this . #setInitialState( ) ;
118
+ this . #setSidebarOpen( activeScreen === this . screenName ) ;
109
119
}
110
120
111
121
}
0 commit comments