Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect src alt #56

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/contentful/cypress/component/ContentfulVisual.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,32 @@ describe('contentful visual entry props', () => {
.should('contain', videoAsset.url)
})

it('finds alt on src image', () => {
cy.mount(
<ContentfulVisual
src={{
...visualEntry,
alt: null,
}}
/>
);
cy.get("img").invoke("attr", "alt").should("eq", "Landscape gradient");
})

it("finds alt on src video", () => {
cy.mount(
<ContentfulVisual
src={{
...visualEntry,
image: null,
portraitImage: null,
alt: null,
}}
/>
);
cy.get("video")
.invoke("attr", "aria-label")
.should("eq", "Background loop description");
});

})
11 changes: 9 additions & 2 deletions packages/contentful/src/ContentfulVisual.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@ export default function ContentfulVisual(
responsiveAspectCalculator :
getImageAspect(image || src?.image || src?.portraitImage)
)}
alt={ alt || src?.alt || makeAssetAlt(image) || makeAssetAlt(video)}
alt={
alt
|| src?.alt
|| makeAssetAlt(src?.image)
|| makeAssetAlt(image)
|| makeAssetAlt(src?.video)
|| makeAssetAlt(video)
}
/>
)
}

// Use various asset fields to make the alt from automatically
function makeAssetAlt(asset: ContentfulAsset | undefined): string {
function makeAssetAlt(asset: ContentfulAsset | undefined | null): string {
if (!asset) return ''
return asset.description || asset.title || asset.fileName || ''
}
12 changes: 6 additions & 6 deletions packages/contentful/src/lib/aspectRatio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const orientationMediaQueries = [

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

// Check whether multiple orientations were provided
export function hasResponsiveAssets(
src: ContentfulVisualEntry | undefined
src: ContentfulVisualEntry | undefined | null
): boolean {
if (!src) return false
if (!src) return false;
const hasLandscape = !!(src.image || src.video),
hasPortrait = !!(src.portraitImage || src.portraitVideo)
return hasLandscape && hasPortrait
hasPortrait = !!(src.portraitImage || src.portraitVideo);
return hasLandscape && hasPortrait;
}

// Check whether multiple aspect ratios were provided
export function hasResponsiveAspects(
src: ContentfulVisualEntry | undefined
src: ContentfulVisualEntry | undefined | null
): boolean {
if (!src) return false
const hasLandscapeAspect = !!(src.image?.width &&
Expand Down
26 changes: 13 additions & 13 deletions packages/contentful/src/types/contentfulVisualTypes.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import type { ReactVisualProps } from '@react-visual/react'

export type ContentfulVisualProps = {
image?: ContentfulImageAsset
video?: ContentfulAsset
src?: ContentfulVisualEntry
alt?: string // Can be inferrerd
} & Omit< ReactVisualProps, 'alt' | 'image' | 'video' >
image?: ContentfulImageAsset | null;
video?: ContentfulAsset | null;
src?: ContentfulVisualEntry | null;
alt?: string; // Can be inferrerd
} & Omit<ReactVisualProps, "alt" | "image" | "video">;

export type ContentfulImageAsset = ContentfulAsset & {
width: number
height: number
}

export type ContentfulAsset = {
title?: string
description?: string
title?: string | null
description?: string // Was not nullable in my tests
fileName?: string
url: string
}

export type ContentfulVisualEntry = {
image?: ContentfulImageAsset
portraitImage?: ContentfulImageAsset
video?: ContentfulAsset
portraitVideo?: ContentfulAsset
alt: string
}
image?: ContentfulImageAsset | null
portraitImage?: ContentfulImageAsset | null
video?: ContentfulAsset | null
portraitVideo?: ContentfulAsset | null
alt: string | null
};
Loading