Skip to content

Commit 5b718ce

Browse files
committed
use getImageSource for FullWidthImage and StoryImage
1 parent 7e6ae6c commit 5b718ce

File tree

4 files changed

+24
-77
lines changed

4 files changed

+24
-77
lines changed

components/Image/FullWidthImage.tsx

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useMemo } from 'react';
22
import { cnb } from 'cnbuilder';
3-
import { getProcessedImage } from '@/utilities/getProcessedImage';
43
import { getSbImageSize } from '@/utilities/getSbImageSize';
54
import { getImageSources } from '@/utilities/getImageSources';
65
import { type SbImageType } from '@/components/Storyblok/Storyblok.types';
@@ -12,16 +11,6 @@ export type FullWidthImageProps = SbImageType & React.HTMLAttributes<HTMLImageEl
1211
visibleHorizontal?: styles.VisibleHorizontalType;
1312
};
1413

15-
// Define breakpoints for generating responsive images
16-
const responsiveBreakpoints = [
17-
{ cropWidth: 2000, minWidth: 1500 },
18-
{ cropWidth: 1500, minWidth: 1200 },
19-
{ cropWidth: 1200, minWidth: 992 },
20-
{ cropWidth: 1000, minWidth: 768 },
21-
{ cropWidth: 800, minWidth: 461 },
22-
{ cropWidth: 460, minWidth: 0 }, // Mobile/smallest size
23-
];
24-
2514
export const FullWidthImage = ({
2615
filename,
2716
alt,
@@ -32,42 +21,9 @@ export const FullWidthImage = ({
3221
}: FullWidthImageProps) => {
3322
const { width: originalWidth, height: originalHeight } = getSbImageSize(filename);
3423

35-
// Find corresponding image sizes for responsive images
24+
// Get corresponding image sources for responsive images
3625
const imageSources = useMemo(() => {
37-
const sources = [];
38-
39-
// If the original image width is < 2000px, find out what breakpoint range it falls into
40-
const largestBp = responsiveBreakpoints.find(bp => originalWidth >= bp.minWidth && originalWidth < bp.cropWidth);
41-
42-
// If we found an appropriate breakpoint, insert the original image at that breakpoint
43-
// For example, if the original image is 1100px, it will be used for the min-width: 992px breakpoint
44-
if (largestBp) {
45-
sources.push({
46-
srcSet: getProcessedImage(filename), // Original size
47-
media: `(min-width: ${largestBp.minWidth}px)`,
48-
width: originalWidth,
49-
height: originalHeight,
50-
});
51-
}
52-
53-
// Add all smaller sizes that are relevant
54-
responsiveBreakpoints
55-
// First pass: always include the mobile size, and keep all the breakpoints with minWidth < the original image width
56-
.filter(bp => bp.cropWidth < originalWidth || bp.cropWidth === 460)
57-
// If the original image is wider than 2000px (no largestBp assigned), keep all the breakpoints from the first pass
58-
// Otherwise, keep only the breakpoints that are smaller than the largestBp
59-
.filter(bp => !largestBp || bp.minWidth < largestBp.minWidth)
60-
.forEach(bp => {
61-
const cropSize = `${bp.cropWidth}x0`;
62-
63-
sources.push({
64-
srcSet: getProcessedImage(filename, cropSize),
65-
// The smaller source uses max-width while the larger uses min-width for the media attribute
66-
media: bp.minWidth > 0 ? `(min-width: ${bp.minWidth}px)` : `(max-width: ${bp.cropWidth}px)`,
67-
});
68-
});
69-
70-
return sources;
26+
return getImageSources(filename, originalWidth, originalHeight);
7127
}, [originalWidth, filename, originalHeight]);
7228

7329
return (
@@ -95,4 +51,3 @@ export const FullWidthImage = ({
9551
</div>
9652
);
9753
};
98-

components/Image/StoryImage.tsx

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { cnb } from 'cnbuilder';
2+
import { useMemo } from 'react';
23
import { MediaWrapper, type MediaWrapperProps } from '@/components/Media';
34
import { type LightPageBgColorsType } from '@/utilities/datasource';
4-
import { getProcessedImage } from '@/utilities/getProcessedImage';
5+
import { getImageSources } from '@/utilities/getImageSources';
56
import { getSbImageSize } from '@/utilities/getSbImageSize';
67
import * as styles from './Image.styles';
78

@@ -27,7 +28,11 @@ export const StoryImage = ({
2728
...props
2829
}: StoryImageProps) => {
2930
const { width: originalWidth, height: originalHeight } = getSbImageSize(imageSrc);
30-
const cropSize = styles.imageCropsDesktop['free'];
31+
32+
// Get corresponding image sources for responsive images
33+
const imageSources = useMemo(() => {
34+
return getImageSources(imageSrc, originalWidth, originalHeight);
35+
}, [originalWidth, imageSrc, originalHeight]);
3136

3237
return (
3338
<MediaWrapper
@@ -43,27 +48,18 @@ export const StoryImage = ({
4348
>
4449
{!!imageSrc && (
4550
<picture>
46-
<source
47-
srcSet={getProcessedImage(imageSrc, cropSize)}
48-
media="(min-width: 1500px)"
49-
/>
50-
{/* <source
51-
srcSet={getProcessedImage(imageSrc, styles.imageCropsSmallDesktop[aspectRatio], imageFocus)}
52-
media="(min-width: 992px)"
53-
/>
54-
<source
55-
srcSet={getProcessedImage(imageSrc, styles.imageCropsTablet[aspectRatio], imageFocus)}
56-
media="(min-width: 576px)"
57-
/>
58-
<source
59-
srcSet={getProcessedImage(imageSrc, styles.imageCropsMobile[aspectRatio], imageFocus)}
60-
media="(max-width: 575px)"
61-
/> */}
51+
{imageSources.map(({ width, srcSet, media }) => (
52+
<source
53+
key={`source-${width}`}
54+
srcSet={srcSet}
55+
media={media}
56+
/>
57+
))}
6258
<img
63-
src={getProcessedImage(imageSrc, cropSize)}
59+
src={imageSources[0].srcSet}
6460
loading="lazy"
65-
// width={cropWidth}
66-
// height={cropHeight}
61+
width={originalWidth}
62+
height={originalHeight}
6763
alt={alt || ''}
6864
className={cnb(styles.image, styles.objectPositions('center', visibleVertical))}
6965
/>

utilities/getImageFocus.ts

Whitespace-only changes.

utilities/getImageSources.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { getProcessedImage } from './getProcessedImage';
22

3-
export type ResponsiveBreakpoint = {
3+
type ResponsiveBreakpoint = {
44
cropWidth: number;
55
minWidth: number;
66
};
77

8-
export type ImageSource = {
8+
type ImageSource = {
99
srcSet: string;
1010
media: string;
1111
width?: number;
1212
height?: number;
1313
};
1414

15-
// Define standard breakpoints for generating responsive images
15+
// Default breakpoints for generating responsive images
1616
const defaultResponsiveBreakpoints: ResponsiveBreakpoint[] = [
1717
{ cropWidth: 2000, minWidth: 1500 },
1818
{ cropWidth: 1500, minWidth: 1200 },
@@ -28,15 +28,13 @@ const defaultResponsiveBreakpoints: ResponsiveBreakpoint[] = [
2828
* @param filename - The image filename from Storyblok
2929
* @param originalWidth - Original width of the image
3030
* @param originalHeight - Original height of the image
31-
* @param imageFocus - Optional focus point for image cropping
3231
* @param customBreakpoints - Optional custom breakpoints to override defaults
3332
* @returns Array of image sources with srcSet and media queries
3433
*/
3534
export const getImageSources = (
3635
filename: string,
3736
originalWidth: number,
3837
originalHeight: number,
39-
imageFocus?: string,
4038
customBreakpoints?: ResponsiveBreakpoint[],
4139
): ImageSource[] => {
4240
const sources: ImageSource[] = [];
@@ -49,7 +47,7 @@ export const getImageSources = (
4947
// For example, if the original image is 1100px, it will be used for the min-width: 992px breakpoint
5048
if (largestBp) {
5149
sources.push({
52-
srcSet: getProcessedImage(filename, '', imageFocus), // Original size
50+
srcSet: getProcessedImage(filename), // Original size
5351
media: `(min-width: ${largestBp.minWidth}px)`,
5452
width: originalWidth,
5553
height: originalHeight,
@@ -67,11 +65,9 @@ export const getImageSources = (
6765
const cropSize = `${bp.cropWidth}x0`;
6866

6967
sources.push({
70-
srcSet: getProcessedImage(filename, cropSize, imageFocus),
68+
srcSet: getProcessedImage(filename, cropSize),
7169
// The smaller source uses max-width while the larger uses min-width for the media attribute
7270
media: bp.minWidth > 0 ? `(min-width: ${bp.minWidth}px)` : `(max-width: ${bp.cropWidth}px)`,
73-
width: bp.cropWidth,
74-
height: Math.round(bp.cropWidth * (originalHeight / originalWidth)), // Maintain aspect ratio
7571
});
7672
});
7773

0 commit comments

Comments
 (0)