This repository has been archived by the owner on Aug 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add empty lazy image add lazy image add lazy add image ratio feat: add empty lazy image add lazy image add lazy add image ratio Merge branch 'lazy-intersection' of github.com:josteph/tkpd-ta-web-vitals into lazy-intersection * 'lazy-intersection' of github.com:josteph/tkpd-ta-web-vitals: add lazy image feat: add empty lazy image feat: add image transformation improvment: upload image to CDN improvement: manual resize image size improvement: set image height to improve CLS chore: add analyze script feat: remove unused 3rd party lib fix: remove unused wrapper feat: use image object
- Loading branch information
1 parent
7370f2d
commit ffff81b
Showing
7 changed files
with
93 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { useState } from 'react'; | ||
import { string, object } from 'prop-types'; | ||
import useIntersect from '@jackyef/use-intersect'; | ||
|
||
const optionsData = { | ||
root: null, | ||
rootMargin: '0px', | ||
threshold: [0, 0, 0, 0], | ||
}; | ||
|
||
const LazyImage = ({ alt, src, style, width, height }) => { | ||
const placeholder = `https://res.cloudinary.com/irfan-maulana-tkpd/image/fetch/c_fill,g_auto:face,h_50,fl_force_strip.progressive/f_webp/${encodeURIComponent( | ||
'https://res.cloudinary.com/irfan-maulana-tkpd/image/upload/v1597041453/placeholder_qzxxc6.png', | ||
)}`; | ||
|
||
const [showSrc, setShowSrc] = useState(placeholder); | ||
const [loaded, setLoaded] = useState(false); | ||
|
||
const onIntersect = () => { | ||
const imgObj = new Image(); | ||
imgObj.onload = () => { | ||
setLoaded(true); | ||
} | ||
imgObj.src = src; | ||
setShowSrc(src); | ||
}; | ||
|
||
const targetRef = useIntersect(onIntersect, optionsData, true); | ||
|
||
return ( | ||
<img | ||
ref={targetRef} | ||
alt={alt} | ||
className={`img ${loaded ? 'img--loaded' : 'img--loading'}`} | ||
style={style} | ||
src={showSrc} | ||
width={width} | ||
height={height} | ||
/> | ||
); | ||
}; | ||
|
||
LazyImage.propTypes = { | ||
alt: string.isRequired, | ||
height: string, | ||
src: string, | ||
style: object, | ||
width: string, | ||
}; | ||
|
||
LazyImage.defaultProps = { | ||
height: undefined, | ||
src: '', | ||
style: {}, | ||
width: '100%', | ||
}; | ||
|
||
export default LazyImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters