mirror of
https://github.com/SteamDeckHomebrew/decky-frontend-lib.git
synced 2026-05-19 17:40:08 +02:00
feat(custom-components): add SuspensefulImage
This commit is contained in:
43
src/custom-components/SuspensefulImage.tsx
Normal file
43
src/custom-components/SuspensefulImage.tsx
Normal 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;
|
||||
1
src/custom-components/index.ts
Normal file
1
src/custom-components/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './SuspensefulImage';
|
||||
@@ -1,4 +1,5 @@
|
||||
// export * from './deck-libs';
|
||||
export * from './custom-components';
|
||||
export * from './deck-components';
|
||||
export * from './plugin';
|
||||
export * from './webpack';
|
||||
|
||||
Reference in New Issue
Block a user