feat: add thumbnail spec

This commit is contained in:
2026-01-20 20:11:27 -08:00
parent 2bc6d1bdb0
commit 09f6f49390
7 changed files with 22 additions and 9 deletions

View File

@@ -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}
/>