@@ -20,7 +20,11 @@ import {
20
20
function noop ( ) { }
21
21
22
22
const currentDrawer = { }
23
- const windowIsUndefined = typeof window === 'undefined'
23
+ const windowIsUndefined = ! (
24
+ typeof window !== 'undefined' &&
25
+ window . document &&
26
+ window . document . createElement
27
+ )
24
28
Vue . use ( antRefDirective )
25
29
const Drawer = {
26
30
mixins : [ BaseMixin ] ,
@@ -114,6 +118,7 @@ const Drawer = {
114
118
this . $nextTick ( ( ) => {
115
119
// dom 没渲染时,重走一遍。
116
120
if ( ! this . sFirstEnter && this . container ) {
121
+ console . log ( 1 )
117
122
this . $forceUpdate ( )
118
123
this . sFirstEnter = true
119
124
}
@@ -224,7 +229,7 @@ const Drawer = {
224
229
}
225
230
} ,
226
231
setLevelDomTransform ( open , openTransition , placementName , value ) {
227
- const { placement, levelMove, duration, ease } = this . $props
232
+ const { placement, levelMove, duration, ease, getContainer } = this . $props
228
233
if ( ! windowIsUndefined ) {
229
234
this . levelDom . forEach ( dom => {
230
235
if ( this . isOpenChange || openTransition ) {
@@ -243,104 +248,108 @@ const Drawer = {
243
248
}
244
249
} )
245
250
// 处理 body 滚动
246
- const eventArray = [ 'touchstart' ]
247
- const domArray = [ document . body , this . maskDom , this . handlerdom , this . contentDom ]
248
- const right = getScrollBarSize ( 1 )
249
- let widthTransition = `width ${ duration } ${ ease } `
250
- const trannsformTransition = `transform ${ duration } ${ ease } `
251
- if ( open && document . body . style . overflow !== 'hidden' ) {
252
- document . body . style . overflow = 'hidden'
253
- if ( right ) {
254
- document . body . style . position = 'relative'
255
- document . body . style . width = `calc(100% - ${ right } px)`
256
- this . dom . style . transition = 'none'
257
- switch ( placement ) {
258
- case 'right' :
259
- this . dom . style . transform = `translateX(-${ right } px)`
260
- this . dom . style . msTransform = `translateX(-${ right } px)`
261
- break
262
- case 'top' :
263
- case 'bottom' :
264
- this . dom . style . width = `calc(100% - ${ right } px)`
265
- this . dom . style . transform = 'translateZ(0)'
266
- break
267
- default :
268
- break
251
+ if ( getContainer === 'body' ) {
252
+ const eventArray = [ 'touchstart' ]
253
+ const domArray = [ document . body , this . maskDom , this . handlerdom , this . contentDom ]
254
+ const right = document . body . scrollHeight > ( window . innerHeight || document . documentElement . clientHeight ) &&
255
+ window . innerWidth > document . body . offsetWidth
256
+ ? getScrollBarSize ( 1 ) : 0
257
+ let widthTransition = `width ${ duration } ${ ease } `
258
+ const trannsformTransition = `transform ${ duration } ${ ease } `
259
+ if ( open && document . body . style . overflow !== 'hidden' ) {
260
+ document . body . style . overflow = 'hidden'
261
+ if ( right ) {
262
+ document . body . style . position = 'relative'
263
+ document . body . style . width = `calc(100% - ${ right } px)`
264
+ this . dom . style . transition = 'none'
265
+ switch ( placement ) {
266
+ case 'right' :
267
+ this . dom . style . transform = `translateX(-${ right } px)`
268
+ this . dom . style . msTransform = `translateX(-${ right } px)`
269
+ break
270
+ case 'top' :
271
+ case 'bottom' :
272
+ this . dom . style . width = `calc(100% - ${ right } px)`
273
+ this . dom . style . transform = 'translateZ(0)'
274
+ break
275
+ default :
276
+ break
277
+ }
278
+ clearTimeout ( this . timeout )
279
+ this . timeout = setTimeout ( ( ) => {
280
+ this . dom . style . transition = `${ trannsformTransition } ,${ widthTransition } `
281
+ this . dom . style . width = ''
282
+ this . dom . style . transform = ''
283
+ this . dom . style . msTransform = ''
284
+ } )
269
285
}
270
- clearTimeout ( this . timeout )
271
- this . timeout = setTimeout ( ( ) => {
272
- this . dom . style . transition = `${ trannsformTransition } ,${ widthTransition } `
273
- this . dom . style . width = ''
274
- this . dom . style . transform = ''
275
- this . dom . style . msTransform = ''
286
+ // 手机禁滚
287
+ domArray . forEach ( ( item , i ) => {
288
+ if ( ! item ) {
289
+ return
290
+ }
291
+ addEventListener (
292
+ item ,
293
+ eventArray [ i ] || 'touchmove' ,
294
+ i ? this . removeMoveHandler : this . removeStartHandler ,
295
+ this . passive
296
+ )
276
297
} )
277
- }
278
- // 手机禁滚
279
- domArray . forEach ( ( item , i ) => {
280
- if ( ! item ) {
281
- return
282
- }
283
- addEventListener (
284
- item ,
285
- eventArray [ i ] || 'touchmove' ,
286
- i ? this . removeMoveHandler : this . removeStartHandler ,
287
- this . passive
288
- )
289
- } )
290
- } else if ( this . getCrrentDrawerSome ( ) ) {
291
- document . body . style . overflow = ''
292
- if ( ( this . isOpenChange || openTransition ) && right ) {
293
- document . body . style . position = ''
294
- document . body . style . width = ''
295
- if ( trnasitionStr ) {
296
- document . body . style . overflowX = 'hidden'
297
- }
298
- this . dom . style . transition = 'none'
299
- let heightTransition
300
- switch ( placement ) {
301
- case 'right' : {
302
- this . dom . style . transform = `translateX(${ right } px)`
303
- this . dom . style . msTransform = `translateX(${ right } px)`
304
- this . dom . style . width = '100%'
305
- widthTransition = `width 0s ${ ease } ${ duration } `
306
- if ( this . maskDom ) {
307
- this . maskDom . style . left = `-${ right } px`
308
- this . maskDom . style . width = `calc(100% + ${ right } px)`
309
- }
310
- break
298
+ } else if ( this . getCrrentDrawerSome ( ) ) {
299
+ document . body . style . overflow = ''
300
+ if ( ( this . isOpenChange || openTransition ) && right ) {
301
+ document . body . style . position = ''
302
+ document . body . style . width = ''
303
+ if ( trnasitionStr ) {
304
+ document . body . style . overflowX = 'hidden'
311
305
}
312
- case 'top' :
313
- case 'bottom' : {
314
- this . dom . style . width = `calc(100% + ${ right } px)`
315
- this . dom . style . height = '100%'
316
- this . dom . style . transform = 'translateZ(0)'
317
- heightTransition = `height 0s ${ ease } ${ duration } `
318
- break
306
+ this . dom . style . transition = 'none'
307
+ let heightTransition
308
+ switch ( placement ) {
309
+ case 'right' : {
310
+ this . dom . style . transform = `translateX(${ right } px)`
311
+ this . dom . style . msTransform = `translateX(${ right } px)`
312
+ this . dom . style . width = '100%'
313
+ widthTransition = `width 0s ${ ease } ${ duration } `
314
+ if ( this . maskDom ) {
315
+ this . maskDom . style . left = `-${ right } px`
316
+ this . maskDom . style . width = `calc(100% + ${ right } px)`
317
+ }
318
+ break
319
+ }
320
+ case 'top' :
321
+ case 'bottom' : {
322
+ this . dom . style . width = `calc(100% + ${ right } px)`
323
+ this . dom . style . height = '100%'
324
+ this . dom . style . transform = 'translateZ(0)'
325
+ heightTransition = `height 0s ${ ease } ${ duration } `
326
+ break
327
+ }
328
+ default :
329
+ break
319
330
}
320
- default :
321
- break
331
+ clearTimeout ( this . timeout )
332
+ this . timeout = setTimeout ( ( ) => {
333
+ this . dom . style . transition = `${ trannsformTransition } ,${
334
+ heightTransition ? `${ heightTransition } ,` : '' } ${ widthTransition } `
335
+ this . dom . style . transform = ''
336
+ this . dom . style . msTransform = ''
337
+ this . dom . style . width = ''
338
+ this . dom . style . height = ''
339
+ } )
322
340
}
323
- clearTimeout ( this . timeout )
324
- this . timeout = setTimeout ( ( ) => {
325
- this . dom . style . transition = `${ trannsformTransition } ,${
326
- heightTransition ? `${ heightTransition } ,` : '' } ${ widthTransition } `
327
- this . dom . style . transform = ''
328
- this . dom . style . msTransform = ''
329
- this . dom . style . width = ''
330
- this . dom . style . height = ''
341
+ domArray . forEach ( ( item , i ) => {
342
+ if ( ! item ) {
343
+ return
344
+ }
345
+ removeEventListener (
346
+ item ,
347
+ eventArray [ i ] || 'touchmove' ,
348
+ i ? this . removeMoveHandler : this . removeStartHandler ,
349
+ this . passive
350
+ )
331
351
} )
332
352
}
333
- domArray . forEach ( ( item , i ) => {
334
- if ( ! item ) {
335
- return
336
- }
337
- removeEventListener (
338
- item ,
339
- eventArray [ i ] || 'touchmove' ,
340
- i ? this . removeMoveHandler : this . removeStartHandler ,
341
- this . passive
342
- )
343
- } )
344
353
}
345
354
}
346
355
const { change } = this . $listeners
@@ -475,28 +484,44 @@ const Drawer = {
475
484
return this . open !== undefined ? this . open : this . sOpen
476
485
} ,
477
486
getTouchParentScroll ( root , currentTarget , differX , differY ) {
478
- /**
479
- * 增加 rect。
480
- * 当父级 dom 的 overflow 未开启滚动时,scrollLeft 或 scrollTop 为 0, 而 scrollWidth 增加了,
481
- * 父级是跟随子级的 rect, 直到父级设定了滚动.
482
- */
483
- const rect = currentTarget . getBoundingClientRect ( )
484
487
if ( ! currentTarget ) {
485
488
return false
486
- } else if (
487
- ( ( ( currentTarget . scrollTop + currentTarget . offsetHeight + currentTarget . offsetTop >=
488
- currentTarget . scrollHeight + rect . top &&
489
- differY < 0 ) ||
490
- ( currentTarget . scrollTop <= 0 && differY > 0 ) ) &&
491
- Math . max ( Math . abs ( differX ) , Math . abs ( differY ) ) === Math . abs ( differY ) ) ||
492
- ( ( ( currentTarget . scrollLeft + currentTarget . offsetWidth + currentTarget . offsetLeft >=
493
- currentTarget . scrollWidth + rect . left &&
494
- differX < 0 ) ||
495
- ( currentTarget . scrollLeft <= 0 && differX > 0 ) ) &&
496
- Math . max ( Math . abs ( differX ) , Math . abs ( differY ) ) === Math . abs ( differX ) )
489
+ }
490
+ // root 为 drawer-content 设定了 overflow, 判断为 root 的 parent 时结束滚动;
491
+ if ( currentTarget === root . parentNode ) {
492
+ return true
493
+ }
494
+
495
+ const isY = Math . max ( Math . abs ( differX ) , Math . abs ( differY ) ) === Math . abs ( differY )
496
+ const isX = Math . max ( Math . abs ( differX ) , Math . abs ( differY ) ) === Math . abs ( differX )
497
+
498
+ const scrollY = currentTarget . scrollHeight - currentTarget . clientHeight
499
+ const scrollX = currentTarget . scrollWidth - currentTarget . clientWidth
500
+ /**
501
+ * <div style="height: 300px">
502
+ * <div style="height: 900px"></div>
503
+ * </div>
504
+ * 在没设定 overflow: auto 或 scroll 时,currentTarget 里获取不到 scrollTop 或 scrollLeft,
505
+ * 预先用 scrollTo 来滚动,如果取出的值跟滚动前取出不同,则 currnetTarget 被设定了 overflow; 否则就是上面这种。
506
+ */
507
+ const t = currentTarget . scrollTop
508
+ const l = currentTarget . scrollLeft
509
+ currentTarget . scrollTo ( currentTarget . scrollLeft + 1 , currentTarget . scrollTop + 1 )
510
+ const currentT = currentTarget . scrollTop
511
+ const currentL = currentTarget . scrollLeft
512
+ currentTarget . scrollTo ( currentTarget . scrollLeft - 1 , currentTarget . scrollTop - 1 )
513
+
514
+ if (
515
+ isY && ( ! scrollY || ! ( currentT - t ) ||
516
+ ( scrollY && ( currentTarget . scrollTop >= scrollY && differY < 0 ||
517
+ currentTarget . scrollTop <= 0 && differY > 0 ) )
518
+ ) ||
519
+ isX && ( ! scrollX || ! ( currentL - l ) ||
520
+ ( scrollX && ( currentTarget . scrollLeft >= scrollX && differX < 0 ||
521
+ currentTarget . scrollLeft <= 0 && differX > 0 ) )
522
+ )
497
523
) {
498
- return root === currentTarget ||
499
- this . getTouchParentScroll ( root , currentTarget . parentNode , differX , differY )
524
+ return this . getTouchParentScroll ( root , currentTarget . parentNode , differX , differY )
500
525
}
501
526
return false
502
527
} ,
@@ -546,7 +571,7 @@ const Drawer = {
546
571
const { getContainer, wrapperClassName } = this . $props
547
572
const open = this . getOpen ( )
548
573
currentDrawer [ this . drawerId ] = open ? this . container : open
549
- this . children = this . getChildToRender ( this . sFirstEnter ? open : false )
574
+ const children = this . getChildToRender ( this . sFirstEnter ? open : false )
550
575
if ( ! getContainer ) {
551
576
const directives = [ {
552
577
name : 'ant-ref' ,
@@ -559,21 +584,20 @@ const Drawer = {
559
584
class = { wrapperClassName }
560
585
{ ...{ directives } }
561
586
>
562
- { this . children }
587
+ { children }
563
588
</ div >
564
589
)
565
590
}
566
591
if ( ! this . container || ! open && ! this . sFirstEnter ) {
567
592
return null
568
593
}
569
-
570
594
return (
571
595
< ContainerRender
572
596
parent = { this }
573
597
visible
574
598
autoMount
575
599
autoDestroy = { false }
576
- getComponent = { ( ) => this . children }
600
+ getComponent = { ( ) => children }
577
601
getContainer = { this . getSelfContainer }
578
602
children = { ( { renderComponent, removeContainer } ) => {
579
603
this . renderComponent = renderComponent
0 commit comments