@@ -20,7 +20,11 @@ import {
2020function noop ( ) { }
2121
2222const currentDrawer = { }
23- const windowIsUndefined = typeof window === 'undefined'
23+ const windowIsUndefined = ! (
24+ typeof window !== 'undefined' &&
25+ window . document &&
26+ window . document . createElement
27+ )
2428Vue . use ( antRefDirective )
2529const Drawer = {
2630 mixins : [ BaseMixin ] ,
@@ -114,6 +118,7 @@ const Drawer = {
114118 this . $nextTick ( ( ) => {
115119 // dom 没渲染时,重走一遍。
116120 if ( ! this . sFirstEnter && this . container ) {
121+ console . log ( 1 )
117122 this . $forceUpdate ( )
118123 this . sFirstEnter = true
119124 }
@@ -224,7 +229,7 @@ const Drawer = {
224229 }
225230 } ,
226231 setLevelDomTransform ( open , openTransition , placementName , value ) {
227- const { placement, levelMove, duration, ease } = this . $props
232+ const { placement, levelMove, duration, ease, getContainer } = this . $props
228233 if ( ! windowIsUndefined ) {
229234 this . levelDom . forEach ( dom => {
230235 if ( this . isOpenChange || openTransition ) {
@@ -243,104 +248,108 @@ const Drawer = {
243248 }
244249 } )
245250 // 处理 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+ } )
269285 }
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+ )
276297 } )
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'
311305 }
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
319330 }
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+ } )
322340 }
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+ )
331351 } )
332352 }
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- } )
344353 }
345354 }
346355 const { change } = this . $listeners
@@ -475,28 +484,44 @@ const Drawer = {
475484 return this . open !== undefined ? this . open : this . sOpen
476485 } ,
477486 getTouchParentScroll ( root , currentTarget , differX , differY ) {
478- /**
479- * 增加 rect。
480- * 当父级 dom 的 overflow 未开启滚动时,scrollLeft 或 scrollTop 为 0, 而 scrollWidth 增加了,
481- * 父级是跟随子级的 rect, 直到父级设定了滚动.
482- */
483- const rect = currentTarget . getBoundingClientRect ( )
484487 if ( ! currentTarget ) {
485488 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+ )
497523 ) {
498- return root === currentTarget ||
499- this . getTouchParentScroll ( root , currentTarget . parentNode , differX , differY )
524+ return this . getTouchParentScroll ( root , currentTarget . parentNode , differX , differY )
500525 }
501526 return false
502527 } ,
@@ -546,7 +571,7 @@ const Drawer = {
546571 const { getContainer, wrapperClassName } = this . $props
547572 const open = this . getOpen ( )
548573 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 )
550575 if ( ! getContainer ) {
551576 const directives = [ {
552577 name : 'ant-ref' ,
@@ -559,21 +584,20 @@ const Drawer = {
559584 class = { wrapperClassName }
560585 { ...{ directives } }
561586 >
562- { this . children }
587+ { children }
563588 </ div >
564589 )
565590 }
566591 if ( ! this . container || ! open && ! this . sFirstEnter ) {
567592 return null
568593 }
569-
570594 return (
571595 < ContainerRender
572596 parent = { this }
573597 visible
574598 autoMount
575599 autoDestroy = { false }
576- getComponent = { ( ) => this . children }
600+ getComponent = { ( ) => children }
577601 getContainer = { this . getSelfContainer }
578602 children = { ( { renderComponent, removeContainer } ) => {
579603 this . renderComponent = renderComponent
0 commit comments