11import CSSMotion from '@rc-component/motion' ;
22import Portal , { type PortalProps } from '@rc-component/portal' ;
33import { useEvent } from '@rc-component/util' ;
4+ import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect' ;
45import KeyCode from '@rc-component/util/lib/KeyCode' ;
56import classnames from 'classnames' ;
67import React , { useContext , useEffect , useRef , useState } from 'react' ;
7- import type { ImgInfo } from '../Image' ;
88import { PreviewGroupContext } from '../context' ;
99import type { TransformAction , TransformType } from '../hooks/useImageTransform' ;
1010import useImageTransform from '../hooks/useImageTransform' ;
1111import useMouseEvent from '../hooks/useMouseEvent' ;
1212import useStatus from '../hooks/useStatus' ;
1313import useTouchEvent from '../hooks/useTouchEvent' ;
14+ import type { ImgInfo } from '../Image' ;
1415import { BASE_SCALE_RATIO } from '../previewConfig' ;
1516import CloseBtn from './CloseBtn' ;
1617import Footer , { type FooterSemanticName } from './Footer' ;
@@ -367,6 +368,14 @@ const Preview: React.FC<PreviewProps> = props => {
367368 }
368369 } ;
369370
371+ // ========================== Portal ==========================
372+ const [ portalRender , setPortalRender ] = useState ( false ) ;
373+ useLayoutEffect ( ( ) => {
374+ if ( open ) {
375+ setPortalRender ( true ) ;
376+ }
377+ } , [ open ] ) ;
378+
370379 // ========================== Render ==========================
371380 const bodyStyle : React . CSSProperties = {
372381 ...styles . body ,
@@ -376,10 +385,10 @@ const Preview: React.FC<PreviewProps> = props => {
376385 }
377386
378387 return (
379- < Portal open getContainer = { getContainer ?? document . body } autoLock = { lockScroll } >
388+ < Portal open = { portalRender } getContainer = { getContainer ?? document . body } autoLock = { lockScroll } >
380389 < CSSMotion
381390 motionName = { motionName }
382- visible = { open }
391+ visible = { portalRender && open }
383392 motionAppear
384393 motionEnter
385394 motionLeave
0 commit comments