@@ -15,6 +15,7 @@ import {
15
15
Host ,
16
16
Method ,
17
17
Prop ,
18
+ State ,
18
19
Watch ,
19
20
} from '@stencil/core' ;
20
21
import anime from 'animejs' ;
@@ -71,6 +72,8 @@ export class Drawer {
71
72
private callback = this . clickedOutside . bind ( this ) ;
72
73
private divElement ?: HTMLElement ;
73
74
75
+ @State ( ) showContent = true ;
76
+
74
77
@Watch ( 'show' )
75
78
onShowChanged ( newValue : boolean , oldValue ?: boolean ) {
76
79
if ( newValue === oldValue ) {
@@ -96,14 +99,12 @@ export class Drawer {
96
99
}
97
100
98
101
this . show = true ;
99
-
100
- if ( ! this . toggle ) {
102
+ if ( ! this . toggle && this . divElement ) {
101
103
this . slideInRight ( this . divElement ) ;
104
+ setTimeout ( ( ) => {
105
+ window . addEventListener ( 'mousedown' , this . callback ) ;
106
+ } , Drawer . duration ) ;
102
107
}
103
-
104
- setTimeout ( ( ) => {
105
- window . addEventListener ( 'mousedown' , this . callback ) ;
106
- } , 300 ) ;
107
108
} else {
108
109
const { defaultPrevented } = this . drawerClose . emit ( ) ;
109
110
@@ -113,7 +114,7 @@ export class Drawer {
113
114
114
115
this . show = false ;
115
116
116
- if ( this . toggle ) {
117
+ if ( this . toggle && this . divElement ) {
117
118
this . slideOutRight ( this . divElement ) ;
118
119
}
119
120
@@ -147,34 +148,45 @@ export class Drawer {
147
148
}
148
149
}
149
150
150
- private slideOutRight ( el ?: HTMLElement ) {
151
- if ( el ) {
152
- anime ( {
153
- targets : el ,
154
- duration : Drawer . duration ,
155
- translateX : [ 0 , '16rem' ] ,
156
- opacity : [ 1 , 0 ] ,
157
- easing : 'easeInSine' ,
158
- complete : ( ) => {
159
- el . classList . add ( 'd-none' ) ;
160
- } ,
161
- } ) ;
162
- }
151
+ private getConstrainedWidth ( width : number ) {
152
+ return Math . min ( Math . max ( width , this . minWidth ) , this . maxWidth ) ;
163
153
}
164
154
165
- private slideInRight ( el ?: HTMLElement ) {
166
- if ( el ) {
167
- anime ( {
168
- targets : el ,
169
- duration : Drawer . duration ,
170
- translateX : [ '16rem' , 0 ] ,
171
- opacity : [ 0 , 1 ] ,
172
- easing : 'easeOutSine' ,
173
- begin : ( ) => {
174
- el . classList . remove ( 'd-none' ) ;
175
- } ,
176
- } ) ;
177
- }
155
+ private slideOutRight ( el : HTMLElement ) {
156
+ const initialWidth = `${ this . getConstrainedWidth (
157
+ this . width === 'auto' ? this . minWidth : this . width
158
+ ) } rem`;
159
+
160
+ anime ( {
161
+ targets : el ,
162
+ duration : Drawer . duration ,
163
+ width : [ initialWidth , 0 ] ,
164
+ opacity : [ 1 , 0 ] ,
165
+ easing : 'easeInSine' ,
166
+ complete : ( ) => {
167
+ el . classList . add ( 'display-none' ) ;
168
+ } ,
169
+ } ) ;
170
+ }
171
+
172
+ private slideInRight ( el : HTMLElement ) {
173
+ const targetWidth = `${ this . getConstrainedWidth (
174
+ this . width === 'auto' ? this . minWidth : this . width
175
+ ) } rem`;
176
+
177
+ anime ( {
178
+ targets : el ,
179
+ duration : Drawer . duration ,
180
+ width : [ 0 , targetWidth ] ,
181
+ opacity : [ 0 , 1 ] ,
182
+ easing : 'easeOutSine' ,
183
+ begin : ( ) => {
184
+ el . classList . remove ( 'display-none' ) ;
185
+ } ,
186
+ complete : ( ) => {
187
+ this . showContent = true ;
188
+ } ,
189
+ } ) ;
178
190
}
179
191
180
192
componentDidLoad ( ) {
@@ -186,33 +198,50 @@ export class Drawer {
186
198
< Host
187
199
class = { {
188
200
'drawer-container' : true ,
189
- 'd -none' : true ,
201
+ 'display -none' : true ,
190
202
} }
191
203
style = { {
192
- width : this . width === 'auto' ? this . width : `${ this . width } rem` ,
193
- 'min-width' : `${ this . minWidth } rem` ,
204
+ width : '0' ,
194
205
'max-width' : `${ this . maxWidth } rem` ,
195
206
height : this . fullHeight ? '100%' : 'auto' ,
207
+ overflow : 'hidden' ,
196
208
} }
197
209
ref = { ( el ) => ( this . divElement = el as HTMLElement ) }
198
210
data-testid = "container"
199
211
id = "div-container"
200
212
>
201
- < div class = "header" >
202
- < div class = "header-content" >
203
- < slot name = "header" > </ slot >
213
+ < div
214
+ style = { {
215
+ width :
216
+ this . width === 'auto'
217
+ ? 'auto'
218
+ : `${ this . getConstrainedWidth ( this . width ) } rem` ,
219
+ } }
220
+ >
221
+ < div class = "header" >
222
+ < div class = "header-content" >
223
+ < slot name = "header" > </ slot >
224
+ </ div >
225
+ < ix-icon-button
226
+ class = "close-button"
227
+ style = { {
228
+ display : this . showContent ? 'block' : 'none' ,
229
+ } }
230
+ icon = { 'close' }
231
+ size = "24"
232
+ ghost
233
+ onClick = { ( ) => this . onCloseClicked ( ) }
234
+ data-testid = "close-button"
235
+ > </ ix-icon-button >
236
+ </ div >
237
+ < div
238
+ class = "content"
239
+ style = { {
240
+ display : this . showContent ? 'block' : 'none' ,
241
+ } }
242
+ >
243
+ < slot > </ slot >
204
244
</ div >
205
- < ix-icon-button
206
- class = "close-button"
207
- icon = { 'close' }
208
- size = "24"
209
- ghost
210
- onClick = { ( ) => this . onCloseClicked ( ) }
211
- data-testid = "close-button"
212
- > </ ix-icon-button >
213
- </ div >
214
- < div class = "content" >
215
- < slot > </ slot >
216
245
</ div >
217
246
</ Host >
218
247
) ;
0 commit comments