1
1
import React , { useState } from 'react' ;
2
2
import { Dialog } from '../../base' ;
3
+ import { styled } from '../../theme' ;
3
4
4
5
interface ImageComponentProps {
5
6
src : string ;
@@ -13,35 +14,61 @@ interface ImageComponentProps {
13
14
srcSet ?: string ;
14
15
className ?: string ;
15
16
style ?: React . CSSProperties ;
17
+ caption ?: string ;
18
+ align ?: 'left' | 'center' | 'right' ;
16
19
onClick ?: ( event : React . MouseEvent < HTMLImageElement , MouseEvent > ) => void ;
17
20
}
21
+ interface AlignProps {
22
+ align : 'left' | 'center' | 'right' ;
23
+ }
24
+ const Figure = styled ( 'figure' ) < AlignProps > ( ( { align } ) => ( {
25
+ margin : '1rem 0' ,
26
+ textAlign : align ,
27
+ width : '100%' ,
28
+ } ) ) ;
29
+ const FigCaption = styled ( 'figcaption' ) < AlignProps > ( ( { align } ) => ( {
30
+ fontSize : '0.9rem' ,
31
+ color : '#666' ,
32
+ marginTop : '0.5rem' ,
33
+ textAlign : align
34
+ } ) ) ;
18
35
19
- const CustomImage : React . FC < ImageComponentProps > = ( { src, alt, ...props } ) => {
36
+ const CustomImage : React . FC < ImageComponentProps > = ( {
37
+ src,
38
+ alt,
39
+ caption,
40
+ align = 'left' ,
41
+ ...props
42
+ } ) => {
20
43
const [ isZoomed , setIsZoomed ] = useState ( false ) ;
21
-
22
44
const handleZoomClick = ( ) => {
23
45
setIsZoomed ( true ) ;
24
46
} ;
25
-
26
47
const handleZoomClose = ( ) => {
27
48
setIsZoomed ( false ) ;
28
49
} ;
29
50
30
51
return (
31
52
< >
32
- < img
33
- src = { src }
34
- alt = { alt }
35
- loading = "lazy"
36
- onClick = { handleZoomClick }
37
- { ...props }
38
- style = { {
39
- cursor : 'pointer' ,
40
- maxWidth : '100%' ,
41
- height : 'auto' ,
42
- ...props . style
43
- } }
44
- />
53
+ < Figure align = { align } >
54
+ < img
55
+ src = { src }
56
+ alt = { alt || caption || '' }
57
+ loading = "lazy"
58
+ onClick = { handleZoomClick }
59
+ { ...props }
60
+ style = { {
61
+ cursor : 'pointer' ,
62
+ maxWidth : '100%' ,
63
+ height : 'auto' ,
64
+ boxShadow : 'rgba(149, 157, 165, 0.2) 0px 8px 24px' ,
65
+ borderRadius : '15px' ,
66
+ width : '50%'
67
+ } }
68
+ />
69
+ { caption && < FigCaption align = { align } > { caption } </ FigCaption > }
70
+ </ Figure >
71
+
45
72
< Dialog
46
73
open = { isZoomed }
47
74
onClose = { handleZoomClose }
@@ -59,7 +86,7 @@ const CustomImage: React.FC<ImageComponentProps> = ({ src, alt, ...props }) => {
59
86
>
60
87
< img
61
88
src = { src }
62
- alt = { alt }
89
+ alt = { alt || caption || '' }
63
90
onClick = { handleZoomClose }
64
91
style = { {
65
92
objectFit : 'contain' ,
0 commit comments