Skip to content

Commit da44b9b

Browse files
committed
Progressive responsive image, deleted smooth-scroll
1 parent d04c0fa commit da44b9b

File tree

3 files changed

+60
-45
lines changed

3 files changed

+60
-45
lines changed

components/article/Markdown.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export default function Markdown({ data }) {
1818
<Figure>
1919
<ProgressiveImage
2020
preview={data.image[0].formats.thumbnail.url}
21-
image={data.image[0].url}
21+
smallImage={data.image[0].formats.small.url}
22+
mediumImage={data.image[0].formats.medium.url}
23+
largeImage={data.image[0].formats.large.url}
24+
sourceImage={data.image[0].url}
2225
alt={data.image[0].alternativeText}
2326
/>
2427
<figcaption><em>{data.image[0].caption}</em></figcaption>
Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,69 @@
11
import { useState, useEffect, useRef } from 'react'
2+
import styled from 'styled-components'
23

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 }) {
55
const [ isLoading, setIsLoading ] = useState(true)
6-
const [ loadingImage, setLoadingImage ] = useState(image)
7-
8-
useEffect(() => {
9-
fetchImage(image)
10-
},[])
116

127
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])
218

229
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) {
3411
setIsLoading(false)
3512
}
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+
}, [])
4616

4717
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>
5550
)
5651
}
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+
`

pages/_app.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ const GlobalStyle = createGlobalStyle`
9696
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
9797
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, Georgia, sans-serif;
9898
height: 100%;
99-
scroll-behavior: smooth;
10099
background-color: #FFFFFF;
101100
}
102101

0 commit comments

Comments
 (0)