Skip to content

Commit

Permalink
feat(frontend:components): Add ProgressiveImg component
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyno-zeta committed Jan 1, 2024
1 parent 428c7d9 commit 5200c50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions frontend/src/components/ProgressiveImg/ProgressiveImg.tsx
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;
3 changes: 3 additions & 0 deletions frontend/src/components/ProgressiveImg/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ProgressiveImg from './ProgressiveImg';

export default ProgressiveImg;

0 comments on commit 5200c50

Please sign in to comment.