Skip to content

Commit acddfb4

Browse files
caption fixes
1 parent e56a27e commit acddfb4

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/image/component.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ImageVariant } from '@crystallize/js-api-client';
22
import { FunctionComponent } from 'react';
3+
import { ContentTransformer } from '../content-transformer';
34
import { ImageProps } from './types';
45

56
function 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
};

src/image/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface ImageProps {
1717
// The `html` content has higher priority than `plainText` because it has richer content.
1818
// In case of getting both, the `html` is the one that will be displayed.
1919
caption?: RichTextContent;
20+
captionPassed?: string;
21+
fallbackCaption?: string;
2022
variants?: ImageVariant[];
2123
loading?: 'eager' | 'lazy';
2224
_availableSizes?: number[];

0 commit comments

Comments
 (0)