Skip to content

Commit d22ccb9

Browse files
authored
Merge pull request #56 from BKWLD/detect-src-alt
Detect src alt
2 parents 3c2f05e + 828a2bb commit d22ccb9

File tree

4 files changed

+56
-21
lines changed

4 files changed

+56
-21
lines changed

packages/contentful/cypress/component/ContentfulVisual.cy.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,32 @@ describe('contentful visual entry props', () => {
113113
.should('contain', videoAsset.url)
114114
})
115115

116+
it('finds alt on src image', () => {
117+
cy.mount(
118+
<ContentfulVisual
119+
src={{
120+
...visualEntry,
121+
alt: null,
122+
}}
123+
/>
124+
);
125+
cy.get("img").invoke("attr", "alt").should("eq", "Landscape gradient");
126+
})
127+
128+
it("finds alt on src video", () => {
129+
cy.mount(
130+
<ContentfulVisual
131+
src={{
132+
...visualEntry,
133+
image: null,
134+
portraitImage: null,
135+
alt: null,
136+
}}
137+
/>
138+
);
139+
cy.get("video")
140+
.invoke("attr", "aria-label")
141+
.should("eq", "Background loop description");
142+
});
143+
116144
})

packages/contentful/src/ContentfulVisual.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,20 @@ export default function ContentfulVisual(
5555
responsiveAspectCalculator :
5656
getImageAspect(image || src?.image || src?.portraitImage)
5757
)}
58-
alt={ alt || src?.alt || makeAssetAlt(image) || makeAssetAlt(video)}
58+
alt={
59+
alt
60+
|| src?.alt
61+
|| makeAssetAlt(src?.image)
62+
|| makeAssetAlt(image)
63+
|| makeAssetAlt(src?.video)
64+
|| makeAssetAlt(video)
65+
}
5966
/>
6067
)
6168
}
6269

6370
// Use various asset fields to make the alt from automatically
64-
function makeAssetAlt(asset: ContentfulAsset | undefined): string {
71+
function makeAssetAlt(asset: ContentfulAsset | undefined | null): string {
6572
if (!asset) return ''
6673
return asset.description || asset.title || asset.fileName || ''
6774
}

packages/contentful/src/lib/aspectRatio.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const orientationMediaQueries = [
99

1010
// Get the aspect ratio from an image asset if it exists
1111
export function getImageAspect(
12-
image: ContentfulImageAsset | undefined
12+
image: ContentfulImageAsset | undefined | null
1313
): number | undefined {
1414
if (!image) return undefined
1515
return image.width / image.height
@@ -28,17 +28,17 @@ export const responsiveAspectCalculator: AspectCalculator = (
2828

2929
// Check whether multiple orientations were provided
3030
export function hasResponsiveAssets(
31-
src: ContentfulVisualEntry | undefined
31+
src: ContentfulVisualEntry | undefined | null
3232
): boolean {
33-
if (!src) return false
33+
if (!src) return false;
3434
const hasLandscape = !!(src.image || src.video),
35-
hasPortrait = !!(src.portraitImage || src.portraitVideo)
36-
return hasLandscape && hasPortrait
35+
hasPortrait = !!(src.portraitImage || src.portraitVideo);
36+
return hasLandscape && hasPortrait;
3737
}
3838

3939
// Check whether multiple aspect ratios were provided
4040
export function hasResponsiveAspects(
41-
src: ContentfulVisualEntry | undefined
41+
src: ContentfulVisualEntry | undefined | null
4242
): boolean {
4343
if (!src) return false
4444
const hasLandscapeAspect = !!(src.image?.width &&
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
import type { ReactVisualProps } from '@react-visual/react'
22

33
export type ContentfulVisualProps = {
4-
image?: ContentfulImageAsset
5-
video?: ContentfulAsset
6-
src?: ContentfulVisualEntry
7-
alt?: string // Can be inferrerd
8-
} & Omit< ReactVisualProps, 'alt' | 'image' | 'video' >
4+
image?: ContentfulImageAsset | null;
5+
video?: ContentfulAsset | null;
6+
src?: ContentfulVisualEntry | null;
7+
alt?: string; // Can be inferrerd
8+
} & Omit<ReactVisualProps, "alt" | "image" | "video">;
99

1010
export type ContentfulImageAsset = ContentfulAsset & {
1111
width: number
1212
height: number
1313
}
1414

1515
export type ContentfulAsset = {
16-
title?: string
17-
description?: string
16+
title?: string | null
17+
description?: string // Was not nullable in my tests
1818
fileName?: string
1919
url: string
2020
}
2121

2222
export type ContentfulVisualEntry = {
23-
image?: ContentfulImageAsset
24-
portraitImage?: ContentfulImageAsset
25-
video?: ContentfulAsset
26-
portraitVideo?: ContentfulAsset
27-
alt: string
28-
}
23+
image?: ContentfulImageAsset | null
24+
portraitImage?: ContentfulImageAsset | null
25+
video?: ContentfulAsset | null
26+
portraitVideo?: ContentfulAsset | null
27+
alt: string | null
28+
};

0 commit comments

Comments
 (0)