-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathindex.tsx
More file actions
227 lines (218 loc) · 6.46 KB
/
index.tsx
File metadata and controls
227 lines (218 loc) · 6.46 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { Fragment, PropsWithChildren, useState, use } from 'react';
import { Global } from '@emotion/react';
import { Helmet } from 'react-helmet';
import styles from './index.styles';
import { RequestContext } from '../../contexts/RequestContext';
import { HOME_PAGE } from '../../routes/utils/pageTypes';
export type ImageProps = {
alt: string;
aspectRatio?: [x: number, y: number];
attribution?: string;
className?: string;
fallbackMediaType?: string;
fallbackSrcSet?: string;
height?: number;
lazyLoad?: boolean;
placeholder?: boolean;
darkPlaceholder?: boolean;
preload?: boolean;
mediaType?: string;
srcSet?: string;
sizes?: string;
src: string;
width?: number;
fetchPriority?: 'high';
hasCaption?: boolean;
isPortraitOrientation?: boolean;
};
const roundNumber = (num: number) => Math.round(num * 100) / 100;
const getLegacyBrowserAspectRatio = (x: number, y: number) =>
roundNumber((y / x) * 100)
.toString()
.concat('%');
const Image = ({
alt,
aspectRatio,
attribution,
className,
fallbackMediaType,
fallbackSrcSet,
height,
lazyLoad = false,
placeholder = true,
darkPlaceholder = false,
preload = false,
mediaType,
srcSet,
sizes,
src,
width,
children,
fetchPriority,
hasCaption,
isPortraitOrientation,
}: PropsWithChildren<ImageProps>) => {
const { pageType, isLite, isAmp, isOfflineMode } = use(RequestContext);
const [isLoaded, setIsLoaded] = useState(false);
if (isLite) return null;
const showPlaceholder = placeholder && (!isLoaded || isOfflineMode);
const hasDimensions = width && height;
const hasFixedAspectRatio = !!aspectRatio || !!hasDimensions;
const [aspectRatioX, aspectRatioY] = aspectRatio ||
(hasDimensions && [width, height]) || [null, null];
const legacyBrowserAspectRatio = getLegacyBrowserAspectRatio(
aspectRatioX as number,
aspectRatioY as number,
);
const hasFallback = srcSet && fallbackSrcSet && pageType === HOME_PAGE;
const ImageWrapper = hasFallback ? 'picture' : Fragment;
const ampImgLayout = hasDimensions ? 'responsive' : 'fill';
const getImgSrcSet = () => {
if (!hasFallback) return srcSet;
if (pageType !== HOME_PAGE) {
return fallbackSrcSet;
}
return undefined;
};
const getImgSizes = () => {
if ((!hasFallback && srcSet) || pageType !== HOME_PAGE) {
return sizes;
}
return undefined;
};
const imgSrcSet = getImgSrcSet();
const imgSizes = getImgSizes();
// TODO: TBC how the images should be shown
if (isOfflineMode) {
return (
<div
className={className}
css={theme => [
styles.wrapper,
hasFixedAspectRatio
? styles.wrapperFixedAspectRatio
: styles.wrapperResponsiveRatio,
isPortraitOrientation && styles.portraitOrientation,
placeholder && [
styles.placeholder,
{
backgroundColor: darkPlaceholder
? theme.palette.SHADOW
: theme.palette.LUNAR,
},
],
]}
style={{
paddingBottom: hasFixedAspectRatio ? legacyBrowserAspectRatio : 0,
...(!hasCaption && { overflow: 'hidden' }),
}}
>
{children}
</div>
);
}
return (
<>
{preload && (
<Helmet>
<link
rel="preload"
as="image"
href={src}
imageSrcSet={srcSet}
imageSizes={sizes}
{...(fetchPriority && { fetchPriority })}
/>
</Helmet>
)}
<div
className={className}
css={theme => [
styles.wrapper,
hasFixedAspectRatio
? styles.wrapperFixedAspectRatio
: styles.wrapperResponsiveRatio,
isPortraitOrientation && styles.portraitOrientation,
showPlaceholder && [
styles.placeholder,
{
backgroundColor: darkPlaceholder
? theme.palette.SHADOW
: theme.palette.LUNAR,
},
],
]}
style={{
paddingBottom: hasFixedAspectRatio ? legacyBrowserAspectRatio : 0,
...(!hasCaption && { overflow: 'hidden' }),
}}
>
{isAmp ? (
<>
{!hasDimensions && (
// ensures amp-img will render when width and height is not provided
// https://amp.dev/documentation/examples/style-layout/how_to_support_images_with_unknown_dimensions/
<Global
styles={{
'.bbc-image img': {
objectFit: 'cover',
},
}}
/>
)}
<amp-img
class="bbc-image"
layout={ampImgLayout}
alt={alt}
src={src}
width={width}
height={height}
fallback=""
attribution={attribution}
{...(srcSet && { srcSet: imgSrcSet })}
{...(imgSizes && { sizes: imgSizes })}
{...(preload && { 'data-hero': 'true' })}
/>
</>
) : (
<ImageWrapper>
{hasFallback && pageType === HOME_PAGE && (
<>
<source srcSet={srcSet} type={mediaType} sizes={sizes} />
<source
srcSet={fallbackSrcSet}
type={fallbackMediaType}
sizes={sizes}
/>
</>
)}
<img
onLoad={() => setIsLoaded(true)}
src={src}
{...(srcSet && { srcSet: imgSrcSet })}
{...(imgSizes && { sizes: imgSizes })}
alt={alt}
loading={lazyLoad ? 'lazy' : 'eager'}
width={width}
height={height}
css={[
styles.image,
hasFixedAspectRatio
? styles.imageFixedAspectRatio
: styles.imageResponsiveRatio,
]}
fetchPriority={fetchPriority}
style={{
aspectRatio: hasFixedAspectRatio
? `${aspectRatioX} / ${aspectRatioY}`
: 'auto',
}} // aspectRatio used in combination with the objectFit:cover will center the image horizontally and vertically if aspectRatio prop is different from image's intrinsic aspect ratio
/>
</ImageWrapper>
)}
{children}
</div>
</>
);
};
export default Image;