-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend:components): Add ProgressiveImg component
- Loading branch information
1 parent
428c7d9
commit 5200c50
Showing
2 changed files
with
27 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, { ComponentProps, useEffect, useState } from 'react'; | ||
|
||
type Props = { | ||
placeholderSrc: string; | ||
src: string; | ||
alt: string; | ||
} & ComponentProps<'img'>; | ||
|
||
// Inspired from: https://blog.logrocket.com/progressive-image-loading-react-tutorial/ | ||
function ProgressiveImg({ placeholderSrc, src, alt, ...props }: Props) { | ||
const [imgSrc, setImgSrc] = useState(placeholderSrc || src); | ||
|
||
useEffect(() => { | ||
const img = new Image(); | ||
img.src = src; | ||
img.onload = () => { | ||
setImgSrc(src); | ||
}; | ||
}, [src]); | ||
|
||
return <img src={imgSrc} alt={alt} {...props} />; | ||
} | ||
|
||
export default ProgressiveImg; |
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,3 @@ | ||
import ProgressiveImg from './ProgressiveImg'; | ||
|
||
export default ProgressiveImg; |