-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
148 lines (142 loc) · 6.04 KB
/
index.jsx
File metadata and controls
148 lines (142 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import React, {useMemo} from 'react'
import {Helmet} from 'react-helmet'
import PropTypes from 'prop-types'
import {Box, useTheme} from '@salesforce/retail-react-app/app/components/shared/ui'
import {Img} from '@salesforce/retail-react-app/app/components/shared/ui'
import {getResponsivePictureAttributes} from '@salesforce/retail-react-app/app/utils/responsive-image'
import {
getImageAttributes,
getImageLinkAttributes
} from '@salesforce/retail-react-app/app/utils/image'
import {isServer} from '@salesforce/retail-react-app/app/components/image/utils'
/**
* Responsive image component optimized to work with the Dynamic Imaging Service.
* Via this component it's easy to create a `<picture>` element with related
* theme-aware `<source>` elements and responsive preloading for high-priority
* images.
* @example Widths without a unit defined as array (interpreted as px values)
* <DynamicImage
* src="http://example.com/image.jpg[?sw={width}&q=60]"
* widths={[100, 360, 720]} />
* @example Widths without a unit defined as object (interpreted as px values)
* <DynamicImage
* src="http://example.com/image.jpg[?sw={width}&q=60]"
* widths={{base: 100, sm: 360, md: 720}} />
* @example Widths with mixed px and vw units defined as array
* <DynamicImage
* src="http://example.com/image.jpg[?sw={width}&q=60]"
* widths={['50vw', '100vw', '500px']} />
* @example Eagerly load image with high priority and responsive preloading
* <DynamicImage
* src="http://example.com/image.jpg[?sw={width}&q=60]"
* widths={['50vw', '50vw', '20vw', '20vw', '25vw']}
* imageProps={{loading: 'eager'}}
* />
* @see {@link https://web.dev/learn/design/responsive-images}
* @see {@link https://web.dev/learn/design/picture-element}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/picture}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Guides/Responsive_images}
* @see {@link https://help.salesforce.com/s/articleView?id=cc.b2c_image_transformation_service.htm&type=5}
*/
const DynamicImage = ({src, widths, densities, imageProps, as, ...rest}) => {
const Component = as ? as : Img
const theme = useTheme()
const [responsiveImageProps, numSources, effectiveImageProps, responsiveLinks] = useMemo(() => {
const responsiveImageProps = getResponsivePictureAttributes({
src,
widths,
densities,
breakpoints: theme.breakpoints
})
const effectiveImageProps = getImageAttributes(imageProps)
const fetchPriority = effectiveImageProps.fetchPriority
const responsiveLinks =
!responsiveImageProps.links.length && fetchPriority === 'high'
? [
getImageLinkAttributes({
...effectiveImageProps,
fetchPriority, // React <18 vs. >=19 issue
src: responsiveImageProps.src
})
]
: responsiveImageProps.links.reduce((acc, link) => {
const linkProps = getImageLinkAttributes({
...effectiveImageProps,
...link,
fetchPriority, // React <18 vs. >=19 issue
src: responsiveImageProps.src
})
if (linkProps) {
acc.push(linkProps)
}
return acc
}, [])
return [
responsiveImageProps,
responsiveImageProps.sources.length,
effectiveImageProps,
responsiveLinks
]
}, [src, widths, densities, theme.breakpoints])
return (
<Box {...rest}>
{numSources > 0 ? (
<picture>
{responsiveImageProps.sources.map(({srcSet, sizes, media}, idx) => {
if (idx < numSources - 1) {
return <source key={idx} media={media} sizes={sizes} srcSet={srcSet} />
}
return (
<Component
key={idx}
{...effectiveImageProps}
sizes={sizes}
srcSet={srcSet}
src={responsiveImageProps.src}
/>
)
})}
</picture>
) : (
<Component {...effectiveImageProps} src={responsiveImageProps.src} />
)}
{isServer() && responsiveLinks.length > 0 && (
<Helmet>
{responsiveLinks.map((responsiveLinkProps, idx) => {
const {href, ...rest} = responsiveLinkProps
return <link key={idx} {...rest} href={href} />
})}
</Helmet>
)}
</Box>
)
}
DynamicImage.propTypes = {
/**
* Dynamic src having an optional param that can vary with widths. For example: `image[_{width}].jpg` or `image.jpg[?sw={width}&q=60]`
*/
src: PropTypes.string,
/**
* Image widths relative to the breakpoints, whose units can either be px or vw or unit-less. They will be mapped to the corresponding `sizes` and `srcSet`.
*/
widths: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
/**
* Image density factors to apply relative to the breakpoints. Will be mapped to the corresponding `srcSet`.
*/
densities: PropTypes.oneOfType([PropTypes.array, PropTypes.object]),
/**
* Props to pass to the inner image component
*/
imageProps: PropTypes.object,
/**
* Override with your chosen image component
*/
as: PropTypes.elementType
}
export default DynamicImage