Skip to content

Commit fd304d6

Browse files
committed
feat(reactjs-components): Add a fallback Alt to handle empty alt from API
1 parent 110f316 commit fd304d6

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/image/component.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const Image: FunctionComponent<ImageProps> = ({ children, ...restOfAllPro
1414
variants,
1515
altText,
1616
alt: altPassed,
17+
fallbackAlt,
1718
caption,
1819
className,
1920
media,
@@ -23,7 +24,11 @@ export const Image: FunctionComponent<ImageProps> = ({ children, ...restOfAllPro
2324
} = restOfAllProps;
2425

2526
let vars = (variants || []).filter((v) => !!v);
26-
const alt = typeof altPassed === 'string' ? altPassed : altText;
27+
28+
// if the alt is passed to the component we use that: highest priority
29+
// if there is nothing from the API response (altText) then we use the fallbackAlt
30+
// otherwise we set empty for W3C validation
31+
const alt = altPassed || altText || fallbackAlt || '';
2732

2833
// Naive rendering POC
2934
if (url && _availableSizes && _availableFormats) {

src/image/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface ImageProps {
88
sizes?: string;
99
altText?: string;
1010
alt?: string;
11+
fallbackAlt?: string;
1112
media?: string;
1213
style?: React.CSSProperties;
1314
className?: string;

0 commit comments

Comments
 (0)