11import PropTypes from './vue-types' ;
2- import switchScrollingEffect from './switchScrollingEffect' ;
3- import setStyle from './setStyle' ;
42import Portal from './Portal' ;
53import {
64 defineComponent ,
@@ -11,10 +9,12 @@ import {
119 onUpdated ,
1210 getCurrentInstance ,
1311 nextTick ,
12+ computed ,
1413} from 'vue' ;
1514import canUseDom from './canUseDom' ;
16- import ScrollLocker from '../vc-util/Dom/scrollLocker' ;
1715import raf from './raf' ;
16+ import { booleanType } from './type' ;
17+ import useScrollLocker from './hooks/useScrollLocker' ;
1818
1919let openCount = 0 ;
2020const supportDom = canUseDom ( ) ;
@@ -24,10 +24,6 @@ export function getOpenCount() {
2424 return process . env . NODE_ENV === 'test' ? openCount : 0 ;
2525}
2626
27- // https://github.com/ant-design/ant-design/issues/19340
28- // https://github.com/ant-design/ant-design/issues/19332
29- let cacheOverflow = { } ;
30-
3127const getParent = ( getContainer : GetContainer ) => {
3228 if ( ! supportDom ) {
3329 return null ;
@@ -57,20 +53,20 @@ export default defineComponent({
5753 forceRender : { type : Boolean , default : undefined } ,
5854 getContainer : PropTypes . any ,
5955 visible : { type : Boolean , default : undefined } ,
56+ autoLock : booleanType ( ) ,
57+ didUpdate : Function ,
6058 } ,
6159
6260 setup ( props , { slots } ) {
6361 const container = shallowRef < HTMLElement > ( ) ;
6462 const componentRef = shallowRef ( ) ;
6563 const rafId = shallowRef < number > ( ) ;
66- const scrollLocker = new ScrollLocker ( {
67- container : getParent ( props . getContainer ) as HTMLElement ,
68- } ) ;
6964
7065 const removeCurrentContainer = ( ) => {
7166 // Portal will remove from `parentNode`.
7267 // Let's handle this again to avoid refactor issue.
7368 container . value ?. parentNode ?. removeChild ( container . value ) ;
69+ container . value = null ;
7470 } ;
7571 const attachToParent = ( force = false ) => {
7672 if ( force || ( container . value && ! container . value . parentNode ) ) {
@@ -86,13 +82,13 @@ export default defineComponent({
8682 return true ;
8783 } ;
8884 // attachToParent();
89-
85+ const defaultContainer = document . createElement ( 'div' ) ;
9086 const getContainer = ( ) => {
9187 if ( ! supportDom ) {
9288 return null ;
9389 }
9490 if ( ! container . value ) {
95- container . value = document . createElement ( 'div' ) ;
91+ container . value = defaultContainer ;
9692 attachToParent ( true ) ;
9793 }
9894 setWrapperClassName ( ) ;
@@ -108,30 +104,19 @@ export default defineComponent({
108104 setWrapperClassName ( ) ;
109105 attachToParent ( ) ;
110106 } ) ;
111- /**
112- * Enhance ./switchScrollingEffect
113- * 1. Simulate document body scroll bar with
114- * 2. Record body has overflow style and recover when all of PortalWrapper invisible
115- * 3. Disable body scroll when PortalWrapper has open
116- *
117- * @memberof PortalWrapper
118- */
119- const switchScrolling = ( ) => {
120- if ( openCount === 1 && ! Object . keys ( cacheOverflow ) . length ) {
121- switchScrollingEffect ( ) ;
122- // Must be set after switchScrollingEffect
123- cacheOverflow = setStyle ( {
124- overflow : 'hidden' ,
125- overflowX : 'hidden' ,
126- overflowY : 'hidden' ,
127- } ) ;
128- } else if ( ! openCount ) {
129- setStyle ( cacheOverflow ) ;
130- cacheOverflow = { } ;
131- switchScrollingEffect ( true ) ;
132- }
133- } ;
107+
134108 const instance = getCurrentInstance ( ) ;
109+
110+ useScrollLocker (
111+ computed ( ( ) => {
112+ return (
113+ props . autoLock &&
114+ props . visible &&
115+ canUseDom ( ) &&
116+ ( container . value === document . body || container . value === defaultContainer )
117+ ) ;
118+ } ) ,
119+ ) ;
135120 onMounted ( ( ) => {
136121 let init = false ;
137122 watch (
@@ -157,17 +142,6 @@ export default defineComponent({
157142 ) {
158143 removeCurrentContainer ( ) ;
159144 }
160- // updateScrollLocker
161- if (
162- visible &&
163- visible !== prevVisible &&
164- supportDom &&
165- getParent ( getContainer ) !== scrollLocker . getContainer ( )
166- ) {
167- scrollLocker . reLock ( {
168- container : getParent ( getContainer ) as HTMLElement ,
169- } ) ;
170- }
171145 }
172146 init = true ;
173147 } ,
@@ -192,22 +166,30 @@ export default defineComponent({
192166 removeCurrentContainer ( ) ;
193167 raf . cancel ( rafId . value ) ;
194168 } ) ;
195-
169+ watch (
170+ [ ( ) => props . visible , ( ) => props . forceRender ] ,
171+ ( ) => {
172+ const { forceRender, visible } = props ;
173+ if ( visible === false && ! forceRender ) {
174+ removeCurrentContainer ( ) ;
175+ }
176+ } ,
177+ { flush : 'post' } ,
178+ ) ;
196179 return ( ) => {
197180 const { forceRender, visible } = props ;
198181 let portal = null ;
199182 const childProps = {
200183 getOpenCount : ( ) => openCount ,
201184 getContainer,
202- switchScrollingEffect : switchScrolling ,
203- scrollLocker,
204185 } ;
205-
186+ if ( visible === false && ! forceRender ) return null ;
206187 if ( forceRender || visible || componentRef . value ) {
207188 portal = (
208189 < Portal
209190 getContainer = { getContainer }
210191 ref = { componentRef }
192+ didUpdate = { props . didUpdate }
211193 v-slots = { { default : ( ) => slots . default ?.( childProps ) } }
212194 > </ Portal >
213195 ) ;
0 commit comments