Skip to content

Commit 74bc041

Browse files
authored
Merge pull request #1022 from layer5io/vishalvivekm-patch-1
Update CustomImage to support caption using figure element
2 parents e887f28 + a00eb36 commit 74bc041

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

src/custom/CustomImage/CustomImage.tsx

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useState } from 'react';
22
import { Dialog } from '../../base';
3+
import { styled } from '../../theme';
34

45
interface ImageComponentProps {
56
src: string;
@@ -13,35 +14,61 @@ interface ImageComponentProps {
1314
srcSet?: string;
1415
className?: string;
1516
style?: React.CSSProperties;
17+
caption?: string;
18+
align?: 'left' | 'center' | 'right';
1619
onClick?: (event: React.MouseEvent<HTMLImageElement, MouseEvent>) => void;
1720
}
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+
}));
1835

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+
}) => {
2043
const [isZoomed, setIsZoomed] = useState(false);
21-
2244
const handleZoomClick = () => {
2345
setIsZoomed(true);
2446
};
25-
2647
const handleZoomClose = () => {
2748
setIsZoomed(false);
2849
};
2950

3051
return (
3152
<>
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+
4572
<Dialog
4673
open={isZoomed}
4774
onClose={handleZoomClose}
@@ -59,7 +86,7 @@ const CustomImage: React.FC<ImageComponentProps> = ({ src, alt, ...props }) => {
5986
>
6087
<img
6188
src={src}
62-
alt={alt}
89+
alt={alt || caption || ''}
6390
onClick={handleZoomClose}
6491
style={{
6592
objectFit: 'contain',

0 commit comments

Comments
 (0)