1
1
import { useMemo } from 'react' ;
2
2
import { cnb } from 'cnbuilder' ;
3
- import { getProcessedImage } from '@/utilities/getProcessedImage' ;
4
3
import { getSbImageSize } from '@/utilities/getSbImageSize' ;
5
4
import { getImageSources } from '@/utilities/getImageSources' ;
6
5
import { type SbImageType } from '@/components/Storyblok/Storyblok.types' ;
@@ -12,16 +11,6 @@ export type FullWidthImageProps = SbImageType & React.HTMLAttributes<HTMLImageEl
12
11
visibleHorizontal ?: styles . VisibleHorizontalType ;
13
12
} ;
14
13
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
-
25
14
export const FullWidthImage = ( {
26
15
filename,
27
16
alt,
@@ -32,42 +21,9 @@ export const FullWidthImage = ({
32
21
} : FullWidthImageProps ) => {
33
22
const { width : originalWidth , height : originalHeight } = getSbImageSize ( filename ) ;
34
23
35
- // Find corresponding image sizes for responsive images
24
+ // Get corresponding image sources for responsive images
36
25
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 ) ;
71
27
} , [ originalWidth , filename , originalHeight ] ) ;
72
28
73
29
return (
@@ -95,4 +51,3 @@ export const FullWidthImage = ({
95
51
</ div >
96
52
) ;
97
53
} ;
98
-
0 commit comments