Skip to content

Commit e4e977e

Browse files
committed
Improve ExampleScreenshot when image is not available, with fallback icon
1 parent 57db60d commit e4e977e

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

docs/src/lib/components/ComponentLink.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,14 @@
4949

5050
<ImageLink {href} {variant} {...restProps}>
5151
{#snippet image()}
52-
<ExampleScreenshot {component} {example} {aspect} background {viewTransitionName} />
52+
<ExampleScreenshot
53+
{component}
54+
{example}
55+
{aspect}
56+
background
57+
{viewTransitionName}
58+
fallbackIcon={LucideBlocks}
59+
/>
5360
{/snippet}
5461

5562
{#snippet label()}

docs/src/lib/components/ExampleScreenshot.svelte

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
<script lang="ts">
22
import { cls } from '@layerstack/tailwind';
3+
import type { Component } from 'svelte';
34
45
let {
56
component,
67
example,
78
aspect = 'initial',
89
background = false,
910
viewTransitionName = null,
11+
fallbackIcon,
1012
class: className
1113
}: {
1214
component: string;
1315
example: string;
1416
aspect?: 'initial' | 'video' | 'square' | 'screenshot';
1517
background?: boolean;
1618
viewTransitionName?: string | null;
19+
fallbackIcon?: Component;
1720
class?: string;
1821
} = $props();
1922
23+
let hasError = $state(false);
24+
2025
const basePath = $derived(`/screenshots/${component}/${example}`);
2126
2227
const sizes = [
@@ -41,17 +46,25 @@
4146
)}
4247
style:view-transition-name={viewTransitionName}
4348
>
44-
{#each ['light', 'dark'] as mode}
45-
{#each sizes as size}
46-
<img
47-
src="{basePath}-{mode}-{size.width}.webp"
48-
alt="{component} - {example}"
49-
class={cls(
50-
'w-full h-full object-scale-down object-center p-2',
51-
mode === 'light' ? size.light : 'hidden ' + size.dark
52-
)}
53-
loading="lazy"
54-
/>
49+
{#if hasError && fallbackIcon}
50+
{@const FallbackIcon = fallbackIcon}
51+
<div class="w-full h-full flex items-center justify-center text-surface-content/30">
52+
<FallbackIcon class="size-12" />
53+
</div>
54+
{:else if !hasError}
55+
{#each ['light', 'dark'] as mode (mode)}
56+
{#each sizes as size (size.width)}
57+
<img
58+
src="{basePath}-{mode}-{size.width}.webp"
59+
alt="{component} - {example}"
60+
class={cls(
61+
'w-full h-full object-scale-down object-center p-2',
62+
mode === 'light' ? size.light : 'hidden ' + size.dark
63+
)}
64+
loading="lazy"
65+
onerror={() => (hasError = true)}
66+
/>
67+
{/each}
5568
{/each}
56-
{/each}
69+
{/if}
5770
</div>

0 commit comments

Comments
 (0)