Skip to content

Commit

Permalink
feat(custom-components): add SuspensefulImage
Browse files Browse the repository at this point in the history
  • Loading branch information
AAGaming committed Jun 9, 2022
1 parent ace3f95 commit 6324282
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/custom-components/SuspensefulImage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Spinner } from '../deck-components';
import { useEffect } from 'react';
import { FC, ImgHTMLAttributes, useState } from 'react';

interface SuspensefulImageProps extends ImgHTMLAttributes<HTMLImageElement> {
suspenseWidth?: string | number;
suspenseHeight?: string | number;
}

const SuspensefulImage: FC<SuspensefulImageProps> = (props) => {
const [loading, setLoading] = useState(true);
const [error, setError] = useState(false);

useEffect(() => {
const img = new Image();
img.src = props.src || '';
img.addEventListener('load', () => {
setLoading(false);
});
img.addEventListener('error', () => {
setError(true);
});
}, []);

return loading ? (
<div
style={{
width: props.suspenseWidth || props.style?.width,
height: props.suspenseHeight || props.style?.height,
background: 'rgba(255, 255, 255, 0.2)',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
{error ? 'Missing image' : <Spinner style={{ height: '48px' }} />}
</div>
) : (
<img {...props} />
);
};

export default SuspensefulImage;
1 change: 1 addition & 0 deletions src/custom-components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SuspensefulImage';
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// export * from './deck-libs';
export * from './custom-components';
export * from './deck-components';
export * from './plugin';
export * from './webpack';
Expand Down

0 comments on commit 6324282

Please sign in to comment.