11import { ImageVariant } from '@crystallize/js-api-client' ;
22import { FunctionComponent } from 'react' ;
3+ import { ContentTransformer } from '../content-transformer' ;
34import { ImageProps } from './types' ;
45
56function getVariantSrc ( variant : ImageVariant ) : string {
@@ -16,6 +17,8 @@ export const Image: FunctionComponent<ImageProps> = ({ children, ...restOfAllPro
1617 alt : altPassed ,
1718 fallbackAlt,
1819 caption,
20+ captionPassed,
21+ fallbackCaption,
1922 className,
2023 media,
2124 _availableSizes,
@@ -32,6 +35,12 @@ export const Image: FunctionComponent<ImageProps> = ({ children, ...restOfAllPro
3235 // otherwise we set empty for W3C validation
3336 const alt = altPassed || altText || fallbackAlt || '' ;
3437
38+ //if the caption is passed to the component we use that: highest priority
39+ // if there is nothing from the API response (caption) then we use the fallbackCaption
40+ // otherwise we set empty for W3C validation
41+
42+ let captionString = captionPassed || caption ?. html ?. [ 0 ] || caption ?. plainText ?. [ 0 ] || fallbackCaption || '' ;
43+
3544 // Naive rendering POC
3645 if ( url && _availableSizes && _availableFormats ) {
3746 vars = [ ] ;
@@ -89,7 +98,6 @@ export const Image: FunctionComponent<ImageProps> = ({ children, ...restOfAllPro
8998 // Ensure fallback src for older browsers
9099 src : src || url || ( hasVariants ? std [ 0 ] . url : undefined ) ,
91100 alt,
92- caption,
93101 width : width ?? biggestImage ?. width ,
94102 height : height ?? biggestImage ?. height ,
95103 } ;
@@ -126,8 +134,6 @@ export const Image: FunctionComponent<ImageProps> = ({ children, ...restOfAllPro
126134 } ) ;
127135 }
128136
129- const captionString = caption ?. html ?. [ 0 ] || caption ?. plainText ?. [ 0 ] || '' ;
130-
131137 return (
132138 < figure className = { className } >
133139 < picture >
@@ -139,7 +145,14 @@ export const Image: FunctionComponent<ImageProps> = ({ children, ...restOfAllPro
139145 { /* eslint-disable-next-line jsx-a11y/alt-text */ }
140146 < img { ...commonProps } { ...rest } />
141147 </ picture >
142- { captionString && < figcaption dangerouslySetInnerHTML = { { __html : captionString } } /> }
148+
149+ { ! captionPassed && caption ?. json ?. [ 0 ] ? (
150+ < figcaption >
151+ < ContentTransformer json = { caption ?. json ?. [ 0 ] } />
152+ </ figcaption >
153+ ) : (
154+ < figcaption dangerouslySetInnerHTML = { { __html : captionString } } />
155+ ) }
143156 </ figure >
144157 ) ;
145158} ;
0 commit comments