|
1 | 1 | import { useState, useEffect, useRef } from 'react' |
| 2 | +import styled from 'styled-components' |
2 | 3 |
|
3 | | -export default function ProgressiveImage({ preview, image, alt }) { |
4 | | - const [ currentImage, setCurrentImage ] = useState(preview) |
| 4 | +export default function ProgressiveImage({ preview, smallImage, mediumImage, largeImage, sourceImage, alt }) { |
5 | 5 | const [ isLoading, setIsLoading ] = useState(true) |
6 | | - const [ loadingImage, setLoadingImage ] = useState(image) |
7 | | - |
8 | | - useEffect(() => { |
9 | | - fetchImage(image) |
10 | | - },[]) |
11 | 6 |
|
12 | 7 | const isFirstRun = useRef(true) |
13 | | - useEffect(() => { |
14 | | - if (isFirstRun.current) { |
15 | | - isFirstRun.current = false |
16 | | - return |
17 | | - } |
18 | | - setCurrentImage(preview) |
19 | | - setIsLoading(false) |
20 | | - }, [image]) |
21 | 8 |
|
22 | 9 | useEffect(() => { |
23 | | - if (isFirstRun.current) { |
24 | | - isFirstRun.current = false |
25 | | - return |
26 | | - } |
27 | | - fetchImage(image) |
28 | | - }, [currentImage]) |
29 | | - |
30 | | - const fetchImage = src => { |
31 | | - const image = new Image() |
32 | | - image.onload = () => { |
33 | | - setCurrentImage(loadingImage.src) |
| 10 | + if (isFirstRun.current.complete) { |
34 | 11 | setIsLoading(false) |
35 | 12 | } |
36 | | - image.src = src |
37 | | - setLoadingImage(image) |
38 | | - } |
39 | | - |
40 | | - const style = isLoading => { |
41 | | - return { |
42 | | - transition: '0.5s filter linear', |
43 | | - filter: `${isLoading ? 'blur(50px)' : ''}` |
44 | | - } |
45 | | - } |
| 13 | + |
| 14 | + isFirstRun.current.addEventListener('load', () => setIsLoading(false)) |
| 15 | + }, []) |
46 | 16 |
|
47 | 17 | return ( |
48 | | - <img |
49 | | - style={style(isLoading)} |
50 | | - src={currentImage} |
51 | | - alt={alt} |
52 | | - width="1200" |
53 | | - height="800" |
54 | | - /> |
| 18 | + <WrappingDiv isLoading={isLoading}> |
| 19 | + <img |
| 20 | + src={preview} |
| 21 | + alt={alt} |
| 22 | + width="1200" |
| 23 | + height="800" |
| 24 | + /> |
| 25 | + <picture> |
| 26 | + <source |
| 27 | + srcSet={ `${smallImage} 500w, |
| 28 | + ${mediumImage} 750w, |
| 29 | + ${largeImage} 1000w, |
| 30 | + ${sourceImage} 1200w` } |
| 31 | + type="image/jpeg" |
| 32 | + /> |
| 33 | + <img |
| 34 | + ref={isFirstRun} |
| 35 | + src={smallImage} |
| 36 | + srcSet={ `${smallImage} 500w, |
| 37 | + ${mediumImage} 750w, |
| 38 | + ${largeImage} 1000w, |
| 39 | + ${sourceImage} 1200w` } |
| 40 | + sizes="(max-width: 500px) 500px, |
| 41 | + (max-width: 750px) 750px, |
| 42 | + (max-width: 1000px) 1000px, |
| 43 | + 1200px" |
| 44 | + alt={alt} |
| 45 | + width="1200" |
| 46 | + height="800" |
| 47 | + /> |
| 48 | + </picture> |
| 49 | + </WrappingDiv> |
55 | 50 | ) |
56 | 51 | } |
| 52 | + |
| 53 | +const WrappingDiv = styled.div` |
| 54 | + position: relative; |
| 55 | + overflow: hidden; |
| 56 | + transition: 0.5s filter linear; |
| 57 | + filter: ${props => props.isLoading ? 'blur(30px)' : ''}; |
| 58 | + img { |
| 59 | + max-width: 100%; |
| 60 | + height: auto; |
| 61 | + } |
| 62 | + picture > img { |
| 63 | + max-width: 100%; |
| 64 | + height: auto; |
| 65 | + position: absolute; |
| 66 | + top: 0; |
| 67 | + left: 0; |
| 68 | + } |
| 69 | +` |
0 commit comments