forked from SteamDeckHomebrew/decky-frontend-lib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(custom-components): add SuspensefulImage
- Loading branch information
AAGaming
committed
Jun 9, 2022
1 parent
ace3f95
commit 6324282
Showing
3 changed files
with
45 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,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; |
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 @@ | ||
export * from './SuspensefulImage'; |
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