~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
134 lines · 4.8 KB Raw History
<script lang="ts">
	import { firstOf, heroOf } from './hero';
	import { sizesIn } from './slot';
	import type { Source } from './types';

	// An image, filling the box its parent sizes, so the layout holds whether
	// it loads, is slow or fails. A `Source` that fails loads once from its
	// fallback (a public blob's proxy, behind the image CDN); one that fails
	// there too, or has no URL, shows a quiet placeholder in its place rather
	// than the browser's broken image: with `person` (an account's picture),
	// a silhouette. The tree's hero (`hero.ts`) loads at once and first; with
	// `eager` (an Image, a gallery's first picture), one first on the screen
	// by its place (a feed's first card) loads at once too, but where the
	// tree has a hero, at low priority, so it never takes the hero's
	// bandwidth (a gallery's author's face beside its first photo); the rest
	// as they near the screen. With `srcset`, the sizes it comes in,
	// and `sizes`, how wide it is drawn at most, the browser loads the
	// smallest that fills it, a lazy one at the width it is laid out at
	// where the browser can (`auto`); its fallback is one size alone. Once
	// it is drawn, loaded or given up for its placeholder, it says so
	// (`onready`), in a browser alone: a picture the browser had loaded
	// before the page was hydrated says so as it is hydrated.
	let {
		src,
		alt,
		fit = 'cover',
		placeholder = 'Image unavailable',
		person = false,
		eager = false,
		srcset,
		sizes,
		onready
	}: { src: Source | string | null; alt: string; fit?: 'cover' | 'contain'; placeholder?: string; person?: boolean; eager?: boolean; srcset?: string; sizes?: string; onready?: () => void } = $props();

	const first = $derived(typeof src === 'string' ? src : (src?.src ?? null));
	const fallback = $derived(typeof src === 'string' ? null : (src?.fallback ?? null));
	/** How many times this picture's source has failed: a new source starts again. */
	let failed: { of: string | null; times: number } = $state({ of: null, times: 0 });
	const times = $derived(failed.of === first ? failed.times : 0);
	const shown = $derived(times === 0 ? first : times === 1 ? fallback : null);
	const hero = heroOf();
	const onTop = firstOf();
	const leads = $derived(first !== null && first === hero());
	/** Whether it loads at once: the hero, or one first on the screen by its place. */
	const now = $derived(leads || (eager && onTop()));
	/** How soon it loads: the hero first, one loading at once beside a hero after it. */
	const priority = $derived(leads ? 'high' : now && hero() !== undefined ? 'low' : undefined);

	// Nothing to load (no source, or both failed): drawn as it is, for each source.
	$effect(() => {
		void first;
		if (shown === null) onready?.();
	});

	// An <img> taken out of the page keeps loading, and says so after: by
	// then it is no one's (the screen it was on let go), so what it says is
	// heard only while the picture is drawn, never reading its props or
	// state once they are gone (Svelte's derived_inert).
	let drawn = true;
	$effect(() => () => {
		drawn = false;
	});
</script>

{#if shown === null && person}
	<span class="missing person" role="img" aria-label={alt || 'No picture'}
		><svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><circle cx="12" cy="8" r="4.5" /><path d="M3.5 21.5c0-4.6 3.8-7.8 8.5-7.8s8.5 3.2 8.5 7.8z" /></svg></span
	>
{:else if shown === null}
	<span class="missing" class:glyph={[...placeholder].length <= 2} role="img" aria-label={alt || placeholder}>{placeholder}</span>
{:else}
	<!-- How it loads and its sizes before its src, so a browser drawing it fetches the size it needs alone. -->
	<img
		class={fit}
		loading={now ? 'eager' : 'lazy'}
		fetchpriority={priority}
		decoding="async"
		sizes={times === 0 && srcset && sizes ? sizesIn(sizes, !now) : undefined}
		srcset={times === 0 ? srcset : undefined}
		src={shown}
		{alt}
		onload={() => drawn && onready?.()}
		onerror={() => drawn && (failed = { of: first, times: times + 1 })}
	/>
{/if}

<style>
	img,
	.missing {
		display: block;
		width: 100%;
		height: 100%;
		background: var(--color-background-tertiary);
	}

	.cover {
		object-fit: cover;
	}

	/* Whole in its box: no fill in the bands beside it (a carousel's frame
	   shaped for its narrowest photo), as Grain letterboxes. */
	.contain {
		object-fit: contain;
		background: none;
	}

	.missing {
		display: grid;
		place-items: center;
		padding: var(--hud-space-2);
		color: var(--color-text-tertiary);
		font-size: var(--font-text-xs-size);
		line-height: var(--font-text-xs-line-height);
		text-align: center;
	}

	/* A one-glyph placeholder, such as ♪, takes the size its box sets. */
	.glyph {
		padding: 0;
		font-size: inherit;
		line-height: 1;
	}

	/* An account with no picture: a grey head and shoulders, half its box. */
	.person {
		padding: 0;
		color: var(--color-text-tertiary);
	}

	.person svg {
		width: 50%;
		height: 50%;
	}
</style>