1- import useMergedState from '@rc-component/util/lib/hooks/useMergedState ' ;
2- import classnames from 'classnames ' ;
1+ import useControlledState from '@rc-component/util/lib/hooks/useControlledState ' ;
2+ import { clsx } from 'clsx ' ;
33import * as React from 'react' ;
44import { useContext , useMemo , useState } from 'react' ;
55import type { InternalPreviewConfig , PreviewSemanticName , ToolbarRenderInfoType } from './Preview' ;
@@ -19,8 +19,12 @@ export interface ImgInfo {
1919 height : string | number ;
2020}
2121
22+ export interface CoverConfig {
23+ coverNode ?: React . ReactNode ;
24+ placement ?: 'top' | 'bottom' | 'center' ;
25+ }
2226export interface PreviewConfig extends Omit < InternalPreviewConfig , 'countRender' > {
23- cover ?: React . ReactNode ;
27+ cover ?: React . ReactNode | CoverConfig ;
2428
2529 // Similar to InternalPreviewConfig but not have `current`
2630 imageRender ?: (
@@ -121,10 +125,18 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
121125 ...restProps
122126 } : PreviewConfig = preview && typeof preview === 'object' ? preview : { } ;
123127
128+ const coverPlacement =
129+ typeof cover === 'object' && ( cover as CoverConfig ) . placement
130+ ? ( cover as CoverConfig ) . placement || 'center'
131+ : 'center' ;
132+
133+ const coverNode =
134+ typeof cover === 'object' && ( cover as CoverConfig ) . coverNode
135+ ? ( cover as CoverConfig ) . coverNode
136+ : ( cover as React . ReactNode ) ;
137+
124138 // ============================ Open ============================
125- const [ isShowPreview , setShowPreview ] = useMergedState ( ! ! previewOpen , {
126- value : previewOpen ,
127- } ) ;
139+ const [ isShowPreview , setShowPreview ] = useControlledState ( ! ! previewOpen , previewOpen ) ;
128140
129141 const [ mousePosition , setMousePosition ] = useState < null | { x : number ; y : number } > ( null ) ;
130142
@@ -196,7 +208,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
196208 < >
197209 < div
198210 { ...otherProps }
199- className = { classnames ( prefixCls , rootClassName , classNames . root , {
211+ className = { clsx ( prefixCls , rootClassName , classNames . root , {
200212 [ `${ prefixCls } -error` ] : status === 'error' ,
201213 } ) }
202214 onClick = { canPreview ? onPreview : onClick }
@@ -208,7 +220,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
208220 >
209221 < img
210222 { ...imgCommonProps }
211- className = { classnames (
223+ className = { clsx (
212224 `${ prefixCls } -img` ,
213225 {
214226 [ `${ prefixCls } -img-placeholder` ] : placeholder === true ,
@@ -237,13 +249,17 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
237249 { /* Preview Click Mask */ }
238250 { cover !== false && canPreview && (
239251 < div
240- className = { classnames ( `${ prefixCls } -cover` , classNames . cover ) }
252+ className = { clsx (
253+ `${ prefixCls } -cover` ,
254+ classNames . cover ,
255+ `${ prefixCls } -cover-${ coverPlacement } ` ,
256+ ) }
241257 style = { {
242258 display : style ?. display === 'none' ? 'none' : undefined ,
243259 ...styles . cover ,
244260 } }
245261 >
246- { cover }
262+ { coverNode }
247263 </ div >
248264 ) }
249265 </ div >
@@ -262,7 +278,7 @@ const ImageInternal: CompoundedComponent<ImageProps> = props => {
262278 { ...restProps }
263279 classNames = { classNames ?. popup }
264280 styles = { styles ?. popup }
265- rootClassName = { classnames ( previewRootClassName , rootClassName ) }
281+ rootClassName = { clsx ( previewRootClassName , rootClassName ) }
266282 />
267283 ) }
268284 </ >
0 commit comments