diff --git a/src/custom-components/SuspensefulImage.tsx b/src/custom-components/SuspensefulImage.tsx new file mode 100644 index 0000000..7ff5d59 --- /dev/null +++ b/src/custom-components/SuspensefulImage.tsx @@ -0,0 +1,43 @@ +import { Spinner } from '../deck-components'; +import { useEffect } from 'react'; +import { FC, ImgHTMLAttributes, useState } from 'react'; + +interface SuspensefulImageProps extends ImgHTMLAttributes { + suspenseWidth?: string | number; + suspenseHeight?: string | number; +} + +const SuspensefulImage: FC = (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 ? ( +
+ {error ? 'Missing image' : } +
+ ) : ( + + ); +}; + +export default SuspensefulImage; \ No newline at end of file diff --git a/src/custom-components/index.ts b/src/custom-components/index.ts new file mode 100644 index 0000000..ab8b078 --- /dev/null +++ b/src/custom-components/index.ts @@ -0,0 +1 @@ +export * from './SuspensefulImage'; diff --git a/src/index.ts b/src/index.ts index 10d707b..ad884b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ // export * from './deck-libs'; +export * from './custom-components'; export * from './deck-components'; export * from './plugin'; export * from './webpack';