feat(custom-components): add SuspensefulImage

This commit is contained in:
AAGaming
2022-06-08 21:59:12 -04:00
parent ace3f95a33
commit 6324282b48
3 changed files with 45 additions and 0 deletions

View File

@@ -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;

View File

@@ -0,0 +1 @@
export * from './SuspensefulImage';

View File

@@ -1,4 +1,5 @@
// export * from './deck-libs';
export * from './custom-components';
export * from './deck-components';
export * from './plugin';
export * from './webpack';