Skip to content

Commit df192ee

Browse files
authored
chore: fix ssr render (#392)
1 parent fc40ae0 commit df192ee

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/Image.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
114114

115115
const {
116116
src: previewSrc,
117-
open: previewOpen = undefined,
117+
open: previewOpen,
118118
onOpenChange: onPreviewOpenChange,
119119
cover,
120120
classNames: previewClassNames = {},

src/Preview/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import CSSMotion from '@rc-component/motion';
22
import Portal, { type PortalProps } from '@rc-component/portal';
33
import { useEvent } from '@rc-component/util';
4+
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
45
import KeyCode from '@rc-component/util/lib/KeyCode';
56
import classnames from 'classnames';
67
import React, { useContext, useEffect, useRef, useState } from 'react';
7-
import type { ImgInfo } from '../Image';
88
import { PreviewGroupContext } from '../context';
99
import type { TransformAction, TransformType } from '../hooks/useImageTransform';
1010
import useImageTransform from '../hooks/useImageTransform';
1111
import useMouseEvent from '../hooks/useMouseEvent';
1212
import useStatus from '../hooks/useStatus';
1313
import useTouchEvent from '../hooks/useTouchEvent';
14+
import type { ImgInfo } from '../Image';
1415
import { BASE_SCALE_RATIO } from '../previewConfig';
1516
import CloseBtn from './CloseBtn';
1617
import 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

Comments
 (0)