Skip to content

Commit 8a5b4c9

Browse files
committed
add alt text and use generate image url
1 parent 22383f4 commit 8a5b4c9

1 file changed

Lines changed: 38 additions & 4 deletions

File tree

dotcom-rendering/src/components/Masthead/Titlepiece/CustomSubNav.tsx

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
textSansBold14,
1313
} from '@guardian/source/foundations';
1414
import { grid } from '../../../grid';
15+
import { generateImageURL } from '../../../lib/image';
1516
import { nestedOphanComponents } from '../../../lib/ophan-helpers';
1617
import { palette as themePalette } from '../../../palette';
1718
import type {
@@ -155,7 +156,28 @@ const imageListStyles = css`
155156
const byBreakpointWidthDesc = (a: CustomSubnavImage, b: CustomSubnavImage) =>
156157
breakpoints[b.breakpoint] - breakpoints[a.breakpoint];
157158

158-
const HeaderImage = ({ images }: { images: CustomSubnavImage[] }) => {
159+
/**
160+
* Builds a `1x, 2x` srcSet via the Fastly Image Optimiser so each breakpoint's
161+
* image is served at the right width and pixel density (mirrors DirectoryPageNav).
162+
*/
163+
const buildSrcSet = ({ imageSrc, breakpoint }: CustomSubnavImage) =>
164+
`${generateImageURL({
165+
mainImage: imageSrc,
166+
imageWidth: breakpoints[breakpoint],
167+
resolution: 'low',
168+
})}, ${generateImageURL({
169+
mainImage: imageSrc,
170+
imageWidth: breakpoints[breakpoint],
171+
resolution: 'high',
172+
})} 2x`;
173+
174+
const HeaderImage = ({
175+
images,
176+
headerText,
177+
}: {
178+
images: CustomSubnavImage[];
179+
headerText: string;
180+
}) => {
159181
const sorted = [...images].sort(byBreakpointWidthDesc);
160182
/** Smallest breakpoint is the <img> fallback; the rest become <source>s. */
161183
const fallback = sorted.at(-1);
@@ -168,10 +190,19 @@ const HeaderImage = ({ images }: { images: CustomSubnavImage[] }) => {
168190
<source
169191
key={image.breakpoint}
170192
media={`(min-width: ${breakpoints[image.breakpoint]}px)`}
171-
srcSet={image.imageSrc}
193+
srcSet={buildSrcSet(image)}
172194
/>
173195
))}
174-
<img src={fallback.imageSrc} alt="" css={headerImageStyles} />
196+
<img
197+
src={generateImageURL({
198+
mainImage: fallback.imageSrc,
199+
imageWidth: breakpoints[fallback.breakpoint],
200+
resolution: 'low',
201+
})}
202+
srcSet={buildSrcSet(fallback)}
203+
alt={`${headerText} subnav`}
204+
css={headerImageStyles}
205+
/>
175206
</picture>
176207
);
177208
};
@@ -260,7 +291,10 @@ export const CustomSubNav = ({
260291
css={imageNavStyles}
261292
>
262293
<div css={imageWrapperStyles}>
263-
<HeaderImage images={webImages} />
294+
<HeaderImage
295+
images={webImages}
296+
headerText={customSubNav.header.headerText}
297+
/>
264298
<span css={imageHeaderTextStyles}>
265299
{customSubNav.header.headerText}
266300
</span>

0 commit comments

Comments
 (0)