-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathindex.js
478 lines (429 loc) · 12.9 KB
/
index.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
import React from "react"
import PropTypes from "prop-types"
// Handle legacy names for image queries.
const convertProps = props => {
let convertedProps = { ...props }
if (convertedProps.resolutions) {
convertedProps.fixed = convertedProps.resolutions
delete convertedProps.resolutions
}
if (convertedProps.sizes) {
convertedProps.fluid = convertedProps.sizes
delete convertedProps.sizes
}
return convertedProps
}
// Cache if we've seen an image before so we don't both with
// lazy-loading & fading in on subsequent mounts.
const imageCache = {}
const inImageCache = props => {
const convertedProps = convertProps(props)
// Find src
const src = convertedProps.fluid
? convertedProps.fluid.src
: convertedProps.fixed.src
if (imageCache[src]) {
return true
} else {
imageCache[src] = true
return false
}
}
let io
const listeners = []
function getIO() {
if (
typeof io === `undefined` &&
typeof window !== `undefined` &&
window.IntersectionObserver
) {
io = new window.IntersectionObserver(
entries => {
entries.forEach(entry => {
listeners.forEach(l => {
if (l[0] === entry.target) {
// Edge doesn't currently support isIntersecting, so also test for an intersectionRatio > 0
if (entry.isIntersecting || entry.intersectionRatio > 0) {
io.unobserve(l[0])
l[1]()
}
}
})
})
},
{ rootMargin: `200px` }
)
}
return io
}
const listenToIntersections = (el, cb) => {
getIO().observe(el)
listeners.push([el, cb])
}
let isWebpSupportedCache = null
const isWebpSupported = () => {
if (isWebpSupportedCache !== null) {
return isWebpSupportedCache
}
const elem =
typeof window !== `undefined` ? window.document.createElement(`canvas`) : {}
if (elem.getContext && elem.getContext(`2d`)) {
isWebpSupportedCache =
elem.toDataURL(`image/webp`).indexOf(`data:image/webp`) === 0
} else {
isWebpSupportedCache = false
}
return isWebpSupportedCache
}
const noscriptImg = props => {
// Check if prop exists before adding each attribute to the string output below to prevent
// HTML validation issues caused by empty values like width="" and height=""
const src = props.src ? `src="${props.src}" ` : `src="" ` // required attribute
const srcSet = props.srcSet ? `srcset="${props.srcSet}" ` : ``
const sizes = props.sizes ? `sizes="${props.sizes}" ` : ``
const title = props.title ? `title="${props.title}" ` : ``
const alt = props.alt ? `alt="${props.alt}" ` : `alt="" ` // required attribute
const width = props.width ? `width="${props.width}" ` : ``
const height = props.height ? `height="${props.height}" ` : ``
const opacity = props.opacity ? props.opacity : `1`
const transitionDelay = props.transitionDelay ? props.transitionDelay : `0.5s`
return `<img ${width}${height}${src}${srcSet}${alt}${title}${sizes}style="position:absolute;top:0;left:0;transition:opacity 0.5s;transition-delay:${transitionDelay};opacity:${opacity};width:100%;height:100%;object-fit:cover;object-position:center"/>`
}
const Img = props => {
const { style, onLoad, onError, ...otherProps } = props
return (
<img
{...otherProps}
onLoad={onLoad}
onError={onError}
style={{
position: `absolute`,
top: 0,
left: 0,
transition: `opacity 0.5s`,
width: `100%`,
height: `100%`,
objectFit: `cover`,
objectPosition: `center`,
...style,
}}
/>
)
}
Img.propTypes = {
style: PropTypes.object,
onError: PropTypes.func,
onLoad: PropTypes.func,
}
class Image extends React.Component {
constructor(props) {
super(props)
// If this browser doesn't support the IntersectionObserver API
// we default to start downloading the image right away.
let isVisible = true
let imgLoaded = true
let IOSupported = false
// If this image has already been loaded before then we can assume it's
// already in the browser cache so it's cheap to just show directly.
const seenBefore = inImageCache(props)
if (
!seenBefore &&
typeof window !== `undefined` &&
window.IntersectionObserver
) {
isVisible = false
imgLoaded = false
IOSupported = true
}
// Always don't render image while server rendering
if (typeof window === `undefined`) {
isVisible = false
imgLoaded = false
}
this.state = {
isVisible,
imgLoaded,
IOSupported,
}
this.handleRef = this.handleRef.bind(this)
}
handleRef(ref) {
if (this.state.IOSupported && ref) {
listenToIntersections(ref, () => {
this.setState({ isVisible: true, imgLoaded: false })
})
}
}
render() {
const {
title,
alt,
className,
outerWrapperClassName,
style = {},
imgStyle = {},
placeholderStyle = {},
fluid,
fixed,
backgroundColor,
Tag,
} = convertProps(this.props)
let bgColor
if (typeof backgroundColor === `boolean`) {
bgColor = `lightgray`
} else {
bgColor = backgroundColor
}
const imagePlaceholderStyle = {
opacity: this.state.imgLoaded ? 0 : 1,
transitionDelay: `0.25s`,
...imgStyle,
...placeholderStyle,
}
const imageStyle = {
opacity: this.state.imgLoaded || this.props.fadeIn === false ? 1 : 0,
...imgStyle,
}
if (fluid) {
const image = fluid
// Use webp by default if browser supports it
if (image.srcWebp && image.srcSetWebp && isWebpSupported()) {
image.src = image.srcWebp
image.srcSet = image.srcSetWebp
}
// The outer div is necessary to reset the z-index to 0.
return (
<Tag
className={`${
outerWrapperClassName ? outerWrapperClassName : ``
} gatsby-image-outer-wrapper`}
style={{
// Let users set component to be absolutely positioned.
position: style.position === `absolute` ? `initial` : `relative`,
}}
>
<Tag
className={`${className ? className : ``} gatsby-image-wrapper`}
style={{
position: `relative`,
overflow: `hidden`,
...style,
}}
ref={this.handleRef}
>
{/* Preserve the aspect ratio. */}
<Tag
style={{
width: `100%`,
paddingBottom: `${100 / image.aspectRatio}%`,
}}
/>
{/* Show the blury base64 image. */}
{image.base64 && (
<Img
alt={alt}
title={title}
src={image.base64}
style={imagePlaceholderStyle}
/>
)}
{/* Show the traced SVG image. */}
{image.tracedSVG && (
<Img
alt={alt}
title={title}
src={image.tracedSVG}
style={imagePlaceholderStyle}
/>
)}
{/* Show a solid background color. */}
{bgColor && (
<Tag
title={title}
style={{
backgroundColor: bgColor,
position: `absolute`,
top: 0,
bottom: 0,
opacity: !this.state.imgLoaded ? 1 : 0,
transitionDelay: `0.35s`,
right: 0,
left: 0,
}}
/>
)}
{/* Once the image is visible (or the browser doesn't support IntersectionObserver), start downloading the image */}
{this.state.isVisible && (
<Img
alt={alt}
title={title}
srcSet={image.srcSet}
src={image.src}
sizes={image.sizes}
style={imageStyle}
onLoad={() => {
this.state.IOSupported && this.setState({ imgLoaded: true })
this.props.onLoad && this.props.onLoad()
}}
onError={this.props.onError}
/>
)}
{/* Show the original image during server-side rendering if JavaScript is disabled */}
<noscript
dangerouslySetInnerHTML={{
__html: noscriptImg({ alt, title, ...image }),
}}
/>
</Tag>
</Tag>
)
}
if (fixed) {
const image = fixed
const divStyle = {
position: `relative`,
overflow: `hidden`,
display: `inline-block`,
width: image.width,
height: image.height,
...style,
}
if (style.display === `inherit`) {
delete divStyle.display
}
// Use webp by default if browser supports it
if (image.srcWebp && image.srcSetWebp && isWebpSupported()) {
image.src = image.srcWebp
image.srcSet = image.srcSetWebp
}
// The outer div is necessary to reset the z-index to 0.
return (
<Tag
className={`${
outerWrapperClassName ? outerWrapperClassName : ``
} gatsby-image-outer-wrapper`}
style={{
// Let users set component to be absolutely positioned.
position: style.position === `absolute` ? `initial` : `relative`,
}}
>
<Tag
className={`${className ? className : ``} gatsby-image-wrapper`}
style={divStyle}
ref={this.handleRef}
>
{/* Show the blury base64 image. */}
{image.base64 && (
<Img
alt={alt}
title={title}
src={image.base64}
style={imagePlaceholderStyle}
/>
)}
{/* Show the traced SVG image. */}
{image.tracedSVG && (
<Img
alt={alt}
title={title}
src={image.tracedSVG}
style={imagePlaceholderStyle}
/>
)}
{/* Show a solid background color. */}
{bgColor && (
<Tag
title={title}
style={{
backgroundColor: bgColor,
width: image.width,
opacity: !this.state.imgLoaded ? 1 : 0,
transitionDelay: `0.25s`,
height: image.height,
}}
/>
)}
{/* Once the image is visible, start downloading the image */}
{this.state.isVisible && (
<Img
alt={alt}
title={title}
width={image.width}
height={image.height}
srcSet={image.srcSet}
src={image.src}
style={imageStyle}
onLoad={() => {
this.setState({ imgLoaded: true })
this.props.onLoad && this.props.onLoad()
}}
onError={this.props.onError}
/>
)}
{/* Show the original image during server-side rendering if JavaScript is disabled */}
<noscript
dangerouslySetInnerHTML={{
__html: noscriptImg({
alt,
title,
width: image.width,
height: image.height,
...image,
}),
}}
/>
</Tag>
</Tag>
)
}
return null
}
}
Image.defaultProps = {
fadeIn: true,
alt: ``,
Tag: `div`,
}
const fixedObject = PropTypes.shape({
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
src: PropTypes.string.isRequired,
srcSet: PropTypes.string.isRequired,
base64: PropTypes.string,
tracedSVG: PropTypes.string,
srcWebp: PropTypes.string,
srcSetWebp: PropTypes.string,
})
const fluidObject = PropTypes.shape({
aspectRatio: PropTypes.number.isRequired,
src: PropTypes.string.isRequired,
srcSet: PropTypes.string.isRequired,
sizes: PropTypes.string.isRequired,
base64: PropTypes.string,
tracedSVG: PropTypes.string,
srcWebp: PropTypes.string,
srcSetWebp: PropTypes.string,
})
Image.propTypes = {
resolutions: fixedObject,
sizes: fluidObject,
fixed: fixedObject,
fluid: fluidObject,
fadeIn: PropTypes.bool,
title: PropTypes.string,
alt: PropTypes.string,
className: PropTypes.oneOfType([PropTypes.string, PropTypes.object]), // Support Glamor's css prop.
outerWrapperClassName: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object,
]),
style: PropTypes.object,
imgStyle: PropTypes.object,
placeholderStyle: PropTypes.object,
position: PropTypes.string,
backgroundColor: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
onLoad: PropTypes.func,
onError: PropTypes.func,
Tag: PropTypes.string,
}
export default Image