@@ -30,10 +30,12 @@ interface IState {
30
30
class DrawerChild extends React . Component < IDrawerChildProps , IState > {
31
31
static defaultProps = {
32
32
switchScrollingEffect : ( ) => { } ,
33
- }
33
+ } ;
34
34
35
- public static getDerivedStateFromProps ( props : IDrawerChildProps ,
36
- { prevProps, _self } : { prevProps : IDrawerChildProps , _self : DrawerChild } ) {
35
+ public static getDerivedStateFromProps (
36
+ props : IDrawerChildProps ,
37
+ { prevProps, _self } : { prevProps : IDrawerChildProps ; _self : DrawerChild } ,
38
+ ) {
37
39
const nextState = {
38
40
prevProps : props ,
39
41
} ;
@@ -69,8 +71,8 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
69
71
private passive : { passive : boolean } | boolean ;
70
72
71
73
private startPos : {
72
- x : number ,
73
- y : number ,
74
+ x : number ;
75
+ y : number ;
74
76
} ;
75
77
76
78
constructor ( props : IDrawerChildProps ) {
@@ -83,24 +85,26 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
83
85
public componentDidMount ( ) {
84
86
if ( ! windowIsUndefined ) {
85
87
let passiveSupported = false ;
86
- window . addEventListener (
87
- 'test' ,
88
- ( ) => { } ,
89
- Object . defineProperty ( { } , 'passive' , {
90
- get : ( ) => {
91
- passiveSupported = true ;
92
- return null ;
93
- } ,
94
- } ) ,
95
- ) ;
88
+ try {
89
+ window . addEventListener (
90
+ 'test' ,
91
+ null ,
92
+ Object . defineProperty ( { } , 'passive' , {
93
+ get : ( ) => {
94
+ passiveSupported = true ;
95
+ return null ;
96
+ } ,
97
+ } ) ,
98
+ ) ;
99
+ } catch ( err ) { }
96
100
this . passive = passiveSupported ? { passive : false } : false ;
97
101
}
98
102
const { open } = this . props ;
99
- this . drawerId = `drawer_id_${
100
- Number (
101
- ( Date . now ( ) + Math . random ( ) ) . toString ( ) . replace ( '.' , Math . round ( Math . random ( ) * 9 ) . toString ( ) ) ,
102
- ) . toString ( 16 )
103
- } `;
103
+ this . drawerId = `drawer_id_${ Number (
104
+ ( Date . now ( ) + Math . random ( ) )
105
+ . toString ( )
106
+ . replace ( '.' , Math . round ( Math . random ( ) * 9 ) . toString ( ) ) ,
107
+ ) . toString ( 16 ) } `;
104
108
this . getLevelDom ( this . props ) ;
105
109
if ( open ) {
106
110
currentDrawer [ this . drawerId ] = open ;
@@ -125,7 +129,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
125
129
126
130
public componentWillUnmount ( ) {
127
131
const { getOpenCount, open, switchScrollingEffect } = this . props ;
128
- const openCount = typeof getOpenCount === 'function' && getOpenCount ( )
132
+ const openCount = typeof getOpenCount === 'function' && getOpenCount ( ) ;
129
133
delete currentDrawer [ this . drawerId ] ;
130
134
if ( open ) {
131
135
this . setLevelTransform ( false ) ;
@@ -141,7 +145,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
141
145
if ( this . dom ) {
142
146
this . dom . focus ( ) ;
143
147
}
144
- }
148
+ } ;
145
149
146
150
private removeStartHandler = ( e : React . TouchEvent | TouchEvent ) => {
147
151
if ( e . touches . length > 1 ) {
@@ -151,7 +155,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
151
155
x : e . touches [ 0 ] . clientX ,
152
156
y : e . touches [ 0 ] . clientY ,
153
157
} ;
154
- }
158
+ } ;
155
159
156
160
private removeMoveHandler = ( e : React . TouchEvent | TouchEvent ) => {
157
161
if ( e . changedTouches . length > 1 ) {
@@ -164,17 +168,22 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
164
168
currentTarget === this . maskDom ||
165
169
currentTarget === this . handlerDom ||
166
170
( currentTarget === this . contentDom &&
167
- getTouchParentScroll ( currentTarget , e . target as HTMLElement , differX , differY ) )
171
+ getTouchParentScroll (
172
+ currentTarget ,
173
+ e . target as HTMLElement ,
174
+ differX ,
175
+ differY ,
176
+ ) )
168
177
) {
169
178
e . preventDefault ( ) ;
170
179
}
171
- }
180
+ } ;
172
181
173
182
private transitionEnd = ( e : TransitionEvent ) => {
174
183
const dom : HTMLElement = e . target as HTMLElement ;
175
184
removeEventListener ( dom , transitionEnd , this . transitionEnd ) ;
176
185
dom . style . transition = '' ;
177
- }
186
+ } ;
178
187
179
188
private onKeyDown = ( e : React . KeyboardEvent ) => {
180
189
if ( e . keyCode === KeyCode . ESC ) {
@@ -184,11 +193,14 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
184
193
onClose ( e as any ) ;
185
194
}
186
195
}
187
- }
196
+ } ;
188
197
189
198
private onWrapperTransitionEnd = ( e : React . TransitionEvent ) => {
190
199
const { open, afterVisibleChange } = this . props ;
191
- if ( e . target === this . contentWrapper && e . propertyName . match ( / t r a n s f o r m $ / ) ) {
200
+ if (
201
+ e . target === this . contentWrapper &&
202
+ e . propertyName . match ( / t r a n s f o r m $ / )
203
+ ) {
192
204
this . dom . style . transition = '' ;
193
205
if ( ! open && this . getCurrentDrawerSome ( ) ) {
194
206
document . body . style . overflowX = '' ;
@@ -201,17 +213,22 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
201
213
afterVisibleChange ( ! ! open ) ;
202
214
}
203
215
}
204
- }
216
+ } ;
205
217
206
218
private openLevelTransition = ( ) => {
207
219
const { open, width, height } = this . props ;
208
- const { isHorizontal, placementName } = this . getHorizontalBoolAndPlacementName ( ) ;
220
+ const {
221
+ isHorizontal,
222
+ placementName,
223
+ } = this . getHorizontalBoolAndPlacementName ( ) ;
209
224
const contentValue = this . contentDom
210
- ? this . contentDom . getBoundingClientRect ( ) [ isHorizontal ? 'width' : 'height' ]
225
+ ? this . contentDom . getBoundingClientRect ( ) [
226
+ isHorizontal ? 'width' : 'height'
227
+ ]
211
228
: 0 ;
212
229
const value = ( isHorizontal ? width : height ) || contentValue ;
213
230
this . setLevelAndScrolling ( open , placementName , value ) ;
214
- }
231
+ } ;
215
232
216
233
private setLevelTransform = (
217
234
open ?: boolean ,
@@ -229,13 +246,19 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
229
246
const $levelMove = transformArguments ( levelMove , { target : dom , open } ) ;
230
247
levelValue = open ? $levelMove [ 0 ] : $levelMove [ 1 ] || 0 ;
231
248
}
232
- const $value = typeof levelValue === 'number' ? `${ levelValue } px` : levelValue ;
233
- let placementPos = placement === 'left' || placement === 'top' ? $value : `-${ $value } ` ;
234
- placementPos = showMask && placement === 'right' && right ?
235
- `calc(${ placementPos } + ${ right } px)` : placementPos ;
236
- dom . style . transform = levelValue ? `${ placementName } (${ placementPos } )` : '' ;
249
+ const $value =
250
+ typeof levelValue === 'number' ? `${ levelValue } px` : levelValue ;
251
+ let placementPos =
252
+ placement === 'left' || placement === 'top' ? $value : `-${ $value } ` ;
253
+ placementPos =
254
+ showMask && placement === 'right' && right
255
+ ? `calc(${ placementPos } + ${ right } px)`
256
+ : placementPos ;
257
+ dom . style . transform = levelValue
258
+ ? `${ placementName } (${ placementPos } )`
259
+ : '' ;
237
260
} ) ;
238
- }
261
+ } ;
239
262
240
263
private setLevelAndScrolling = (
241
264
open ?: boolean ,
@@ -244,16 +267,19 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
244
267
) => {
245
268
const { onChange } = this . props ;
246
269
if ( ! windowIsUndefined ) {
247
- const right = document . body . scrollHeight >
248
- ( window . innerHeight || document . documentElement . clientHeight ) &&
249
- window . innerWidth > document . body . offsetWidth ? getScrollBarSize ( true ) : 0 ;
270
+ const right =
271
+ document . body . scrollHeight >
272
+ ( window . innerHeight || document . documentElement . clientHeight ) &&
273
+ window . innerWidth > document . body . offsetWidth
274
+ ? getScrollBarSize ( true )
275
+ : 0 ;
250
276
this . setLevelTransform ( open , placementName , value , right ) ;
251
277
this . toggleScrollingToDrawerAndBody ( right ) ;
252
278
}
253
279
if ( onChange ) {
254
280
onChange ( open ) ;
255
281
}
256
- }
282
+ } ;
257
283
258
284
private toggleScrollingToDrawerAndBody = ( right : number ) => {
259
285
const { getOpenCount, getContainer, showMask, open } = this . props ;
@@ -262,7 +288,12 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
262
288
// 处理 body 滚动
263
289
if ( container && container . parentNode === document . body && showMask ) {
264
290
const eventArray = [ 'touchstart' ] ;
265
- const domArray = [ document . body , this . maskDom , this . handlerDom , this . contentDom ] ;
291
+ const domArray = [
292
+ document . body ,
293
+ this . maskDom ,
294
+ this . handlerDom ,
295
+ this . contentDom ,
296
+ ] ;
266
297
if ( open && document . body . style . overflow !== 'hidden' ) {
267
298
if ( right ) {
268
299
this . addScrollingEffect ( right ) ;
@@ -306,10 +337,16 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
306
337
} ) ;
307
338
}
308
339
}
309
- }
340
+ } ;
310
341
311
342
private addScrollingEffect = ( right : number ) => {
312
- const { placement, duration, ease, getOpenCount, switchScrollingEffect } = this . props ;
343
+ const {
344
+ placement,
345
+ duration,
346
+ ease,
347
+ getOpenCount,
348
+ switchScrollingEffect,
349
+ } = this . props ;
313
350
const openCount = getOpenCount && getOpenCount ( ) ;
314
351
if ( openCount === 1 ) {
315
352
switchScrollingEffect ( ) ;
@@ -337,10 +374,16 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
337
374
this . dom . style . transform = '' ;
338
375
}
339
376
} ) ;
340
- }
377
+ } ;
341
378
342
379
private remScrollingEffect = ( right : number ) => {
343
- const { placement, duration, ease, getOpenCount, switchScrollingEffect } = this . props ;
380
+ const {
381
+ placement,
382
+ duration,
383
+ ease,
384
+ getOpenCount,
385
+ switchScrollingEffect,
386
+ } = this . props ;
344
387
const openCount = getOpenCount && getOpenCount ( ) ;
345
388
if ( ! openCount ) {
346
389
switchScrollingEffect ( true ) ;
@@ -384,25 +427,28 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
384
427
if ( this . dom ) {
385
428
this . dom . style . transition = `${ transformTransition } ,${
386
429
heightTransition ? `${ heightTransition } ,` : ''
387
- } ${ widthTransition } `;
430
+ } ${ widthTransition } `;
388
431
this . dom . style . transform = '' ;
389
432
this . dom . style . width = '' ;
390
433
this . dom . style . height = '' ;
391
434
}
392
435
} ) ;
393
- }
436
+ } ;
394
437
395
- private getCurrentDrawerSome = ( ) => ! Object . keys ( currentDrawer ) . some ( key => currentDrawer [ key ] ) ;
438
+ private getCurrentDrawerSome = ( ) =>
439
+ ! Object . keys ( currentDrawer ) . some ( key => currentDrawer [ key ] ) ;
396
440
397
441
private getLevelDom = ( { level, getContainer } : IDrawerChildProps ) => {
398
442
if ( windowIsUndefined ) {
399
443
return ;
400
444
}
401
445
const container = getContainer && getContainer ( ) ;
402
- const parent = container ? container . parentNode as HTMLElement : null ;
446
+ const parent = container ? ( container . parentNode as HTMLElement ) : null ;
403
447
this . levelDom = [ ] ;
404
448
if ( level === 'all' ) {
405
- const children : HTMLElement [ ] = parent ? Array . prototype . slice . call ( parent . children ) : [ ] ;
449
+ const children : HTMLElement [ ] = parent
450
+ ? Array . prototype . slice . call ( parent . children )
451
+ : [ ] ;
406
452
children . forEach ( ( child : HTMLElement ) => {
407
453
if (
408
454
child . nodeName !== 'SCRIPT' &&
@@ -420,7 +466,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
420
466
} ) ;
421
467
} ) ;
422
468
}
423
- }
469
+ } ;
424
470
425
471
private getHorizontalBoolAndPlacementName = ( ) => {
426
472
const { placement } = this . props ;
@@ -430,7 +476,7 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
430
476
isHorizontal,
431
477
placementName,
432
478
} ;
433
- }
479
+ } ;
434
480
435
481
// tslint:disable-next-line:member-ordering
436
482
public render ( ) {
@@ -474,7 +520,8 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
474
520
const { placementName } = this . getHorizontalBoolAndPlacementName ( ) ;
475
521
// 百分比与像素动画不同步,第一次打用后全用像素动画。
476
522
// const defaultValue = !this.contentDom || !level ? '100%' : `${value}px`;
477
- const placementPos = placement === 'left' || placement === 'top' ? '-100%' : '100%' ;
523
+ const placementPos =
524
+ placement === 'left' || placement === 'top' ? '-100%' : '100%' ;
478
525
const transform = open ? '' : `${ placementName } (${ placementPos } )` ;
479
526
480
527
const handlerChildren =
@@ -531,7 +578,9 @@ class DrawerChild extends React.Component<IDrawerChildProps, IState> {
531
578
ref = { c => {
532
579
this . contentDom = c as HTMLElement ;
533
580
} }
534
- onTouchStart = { open && showMask ? this . removeStartHandler : undefined } // 跑用例用
581
+ onTouchStart = {
582
+ open && showMask ? this . removeStartHandler : undefined
583
+ } // 跑用例用
535
584
onTouchMove = { open && showMask ? this . removeMoveHandler : undefined } // 跑用例用
536
585
>
537
586
{ children }
0 commit comments