feat: add thumbnail spec
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
type ImageStatus = 'loading' | 'loaded' | 'error';
|
||||
import type { HTMLImgAttributes } from 'svelte/elements';
|
||||
|
||||
let { src, ...props }: HTMLImgAttributes = $props();
|
||||
let { src, fallback, ...props }: HTMLImgAttributes & { fallback?: string } = $props();
|
||||
let status: ImageStatus = $state('loading');
|
||||
let useFallback = $state(false);
|
||||
|
||||
const currentSrc = $derived(useFallback && fallback ? fallback : src);
|
||||
</script>
|
||||
|
||||
{#if status === 'loading'}
|
||||
@@ -11,11 +14,18 @@
|
||||
{/if}
|
||||
|
||||
<img
|
||||
{src}
|
||||
src={currentSrc}
|
||||
hidden={status === 'loading'}
|
||||
onload={() => {
|
||||
status = 'loaded';
|
||||
}}
|
||||
onerror={() => (status = 'error')}
|
||||
onerror={() => {
|
||||
if (!useFallback && fallback) {
|
||||
useFallback = true;
|
||||
status = 'loading';
|
||||
} else {
|
||||
status = 'error';
|
||||
}
|
||||
}}
|
||||
{...props}
|
||||
/>
|
||||
|
||||
Reference in New Issue
Block a user