~ 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
451 lines · 14.3 KB Raw History
<script lang="ts" module>
	import type { Source } from './types';

	/** A picture in a gallery: its source (null shows a placeholder), its
	 * full size (which a click opens in a lightbox), its alt text, its
	 * width over its height, if its record gives one, and its own info
	 * (its camera), lines behind an ⓘ; and the sizes it comes in (`srcset`),
	 * drawn the full width. */
	export type GalleryItem = { src: Source | null; full?: string | null; alt: string | null; ratio: number | null; info?: { text: string }[]; srcset?: string };
</script>

<script lang="ts">
	import AltBadge from './AltBadge.svelte';
	import Bursts from './Bursts.svelte';
	import { Hearts, taps } from './double-tap.svelte';
	import InfoBadge from './InfoBadge.svelte';
	import Lightbox from './Lightbox.svelte';
	import Picture from './Picture.svelte';
	import { preload } from './preload';

	// Pictures in order, in justified rows, with style `stack` one under
	// another at the full width, or with style `carousel` one at a time, the
	// full width, swiped (or stepped with arrows where there is a pointer),
	// dots under them saying which, as a photo app's feed shows a gallery.
	// Each picture's box takes its shape from its ratio (square when none)
	// before the image arrives, so nothing moves as they load; a carousel's
	// frame is one shape for all, its narrowest picture's but no narrower
	// than 9:16, each picture whole in it. One with a full size opens the set
	// in a lightbox at it, but in a carousel, whose tap opens nothing (as a
	// tap on a photo in Grain's feeds), unless `zoom` (a gallery's own
	// screen); its strip is marked `data-swipes`, so a list item that opens
	// leaves its swipes and taps to it. With
	// `bleed`, the gallery reaches past the padding
	// of what holds it, its pictures square-cornered, as an Image with bleed
	// does; with `showAlt`, each picture with alt text has "ALT" in its corner;
	// each with info, an ⓘ in the other, but in a carousel, where the info of
	// the picture showing is drawn under it, as Grain draws a photo's camera,
	// swapped as it is swiped: as many lines as the most any picture has, so
	// it keeps its height between pictures with info, and nothing for one
	// without. A carousel draws the picture it shows and the next alone, and
	// each further one once the one before it is shown, so a gallery of
	// twenty loads two; near the screen, it loads that next one ahead too (a
	// browser may leave a lazy picture off the screen in its strip until it
	// is swiped), at the width it is drawn at. Its first picture loads at
	// once where the gallery is first on the screen (./hero.ts), the rest as
	// they near it. A picture in a stack or a
	// carousel with its sizes (`srcset`) loads the smallest that fills
	// `sizes`, how wide it is drawn. With `onlike` (a gallery's ♡), a
	// double-tap on a picture runs it, a heart bursting where it was
	// (./double-tap.svelte.ts), a tap that opens the lightbox then waiting a
	// moment to be sure it is not one (a keyboard's opens it at once); a tap
	// that opens nothing still does.
	let {
		items,
		style = 'rows',
		bleed = false,
		showAlt = false,
		zoom = false,
		sizes,
		onlike
	}: {
		items: GalleryItem[];
		style?: 'rows' | 'stack' | 'carousel';
		bleed?: boolean;
		showAlt?: boolean;
		zoom?: boolean;
		sizes?: string;
		onlike?: () => void;
	} = $props();

	/** A ratio kept between 1:4 and 4:1, so no one picture takes a row alone. */
	const clamp = (ratio: number | null) => (ratio === null ? 1 : Math.min(Math.max(ratio, 0.25), 4));
	/** The picture the lightbox is open at, while it is. */
	let open: number | null = $state(null);
	/** Each picture's overlay open, by its place: its alt text or its info, one at a time. */
	let overlays: Record<number, 'alt' | 'info'> = $state({});
	/** The binding of picture `i`'s overlay `which`: shown opens it in the other's place. */
	const overlay = (i: number, which: 'alt' | 'info') => ({
		get: () => overlays[i] === which,
		set: (shown: boolean) => {
			if (shown) overlays[i] = which;
			else if (overlays[i] === which) delete overlays[i];
		}
	});

	/** A carousel's frame: its narrowest picture's shape, no narrower than 9:16. */
	const frame = $derived(Math.max(Math.min(...items.map((item) => clamp(item.ratio))), 9 / 16));
	let strip: HTMLUListElement | undefined = $state();
	/** The picture a carousel shows. */
	let at = $state(0);
	/** The furthest picture a carousel has shown: those up to the one after it are drawn. */
	let reached = $state(0);
	const onscroll = () => {
		if (strip && strip.clientWidth > 0) at = Math.round(strip.scrollLeft / strip.clientWidth);
		reached = Math.max(reached, at);
	};
	const step = (to: number) => strip?.scrollTo({ left: to * strip.clientWidth, behavior: 'smooth' });
	/** At most five dots, the one showing among them, smaller further from it. */
	const dots = $derived.by(() => {
		const first = Math.max(0, Math.min(at - 2, items.length - 5));
		return items.slice(first, first + 5).map((_, i) => first + i);
	});
	const dot = (i: number) => ['on', '', 'near', 'far'][Math.min(Math.abs(i - at), 3)];
	/** A carousel's room for the info under it: the most lines any picture has. */
	const infoLines = $derived(style === 'carousel' ? Math.max(0, ...items.map((item) => item.info?.length ?? 0)) : 0);
	/** Whether a carousel is near the screen. */
	let near = $state(false);
	function nearing(element: HTMLElement) {
		const observer = new IntersectionObserver(([entry]) => (near = entry.isIntersecting), { rootMargin: '50% 0px' });
		observer.observe(element);
		return () => observer.disconnect();
	}
	// Near the screen, the picture after the one showing, as wide as the strip it is drawn the width of (its `auto`).
	$effect(() => {
		const next = items[at + 1];
		if (!near || !strip || !next?.src) return;
		preload(next.src.src, next.srcset, next.srcset && sizes ? `${strip.clientWidth}px` : undefined);
	});
	/** The frame the pictures are in, where a double-tap's heart bursts. */
	let framed: HTMLDivElement | undefined = $state();
	const hearts = new Hearts();
	/** A tap on picture `i` where a double-tap likes: it opens the lightbox a moment later where it `opens`, unless a second makes it a double-tap. */
	const tapped = taps<{ i: number; opens: boolean; event: MouseEvent }>(
		({ i, opens }) => {
			if (opens) open = i;
		},
		({ event }) => {
			onlike?.();
			if (framed) hearts.add(event, framed);
		}
	);
</script>

<div class="gallery" class:bleed>
	{#if items.length === 0}
		<p class="empty">No pictures.</p>
	{:else}
		<div class="frame" class:carousel={style === 'carousel'} bind:this={framed}>
			<ul
				class={style}
				bind:this={strip}
				onscroll={style === 'carousel' ? onscroll : undefined}
				data-swipes={style === 'carousel' ? '' : undefined}
				style:--frame={frame}
				{@attach style === 'carousel' && items.length > 1 && nearing}
			>
				{#each items as item, i (i)}
					{@const label = item.alt || `Picture ${i + 1} of ${items.length}`}
					{@const fit = style === 'carousel' ? 'contain' : 'cover'}
					{@const sized = style === 'rows' ? {} : { srcset: item.srcset, sizes }}
					<li style:--ratio={clamp(item.ratio)} title={item.alt ?? undefined}>
						{#if style === 'carousel' && i > reached + 1}
							<!-- Not near yet: its place alone, its frame's shape. -->
						{:else if item.full && (style !== 'carousel' || zoom)}
							<!-- A carousel's tap opens nothing unless it zooms: then it opens the lightbox. -->
							<button
								type="button"
								class="open"
								class:liking={onlike !== undefined}
								aria-label="Open {label}"
								onclick={(event) => (onlike && event.detail > 0 ? tapped({ i, opens: true, event }) : (open = i))}
							>
								<Picture src={item.src} alt={label} {fit} eager={i === 0} {...sized} />
							</button>
						{:else if onlike}
							<!-- Its double-tap is a pointer's alone: the ♡ it runs is a button of its own. -->
							<!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
							<div class="liking" onclick={(event) => tapped({ i, opens: false, event })}>
								<Picture src={item.src} alt={label} {fit} eager={i === 0} {...sized} />
							</div>
						{:else}
							<Picture src={item.src} alt={label} {fit} eager={i === 0} {...sized} />
						{/if}
						{#if showAlt && item.alt}{@const alt = overlay(i, 'alt')}<AltBadge alt={item.alt} bind:shown={alt.get, alt.set} />{/if}
						{#if style !== 'carousel' && item.info?.length}{@const info = overlay(i, 'info')}<InfoBadge lines={item.info} bind:shown={info.get, info.set} />{/if}
					</li>
				{/each}
			</ul>
			{#if style === 'carousel' && items.length > 1}
				{#if at > 0}
					<button type="button" class="step back" aria-label="Previous picture" onclick={() => step(at - 1)}
						><svg viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6" /></svg></button
					>
				{/if}
				{#if at < items.length - 1}
					<button type="button" class="step next" aria-label="Next picture" onclick={() => step(at + 1)}
						><svg viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6" /></svg></button
					>
				{/if}
				<div class="dots" aria-label="Picture {at + 1} of {items.length}" role="img">
					{#each dots as i (i)}<span class={dot(i)}></span>{/each}
				</div>
			{/if}
			<Bursts {hearts} />
		</div>
		{#if infoLines > 0 && items[at]?.info?.length}
			<div class="info" translate="no" style:--lines={infoLines}>
				{#each items[at]?.info ?? [] as line, i (i)}<span>{line.text}</span>{/each}
			</div>
		{/if}
	{/if}
	{#if open !== null}
		<Lightbox {items} at={open} onclose={() => (open = null)} />
	{/if}
</div>

<style>
	.gallery {
		container-type: inline-size;
	}

	.bleed {
		--edge: var(--hud-edge, var(--hud-bleed, 0px));
		margin-inline: calc(-1 * var(--edge));
		/* First on its screen (`--hud-lead`, the padding over it), it meets the bar above. */
		margin-top: calc(-1 * var(--hud-lead, 0px));
	}

	/* Square, over the rows', the stack's and the carousel's own rounding
	   below (its frame's rule comes later, so this one must outweigh it). */
	.bleed :is(.rows, .stack) > li,
	.bleed .frame.carousel {
		border-radius: 0;
	}

	li {
		position: relative;
	}

	.open {
		display: block;
		width: 100%;
		height: 100%;
		padding: 0;
		border: 0;
		background: none;
		cursor: zoom-in;
	}

	/* A picture a double-tap likes: the browser's own double-tap zoom off it, and a bare one the box. */
	.liking {
		touch-action: manipulation;
	}

	div.liking {
		width: 100%;
		height: 100%;
	}

	.open:focus-visible {
		outline: 2px solid var(--color-ring-primary);
		outline-offset: 2px;
	}
	.empty {
		margin: 0;
		padding: var(--hud-space-5) 0;
		color: var(--color-text-secondary);
		text-align: center;
	}

	/* The picture showing's info under a carousel: small and muted, a line
	   each, as tall as the most any picture has; inset from the edges a
	   bleeding gallery reaches. */
	.info {
		display: flex;
		flex-direction: column;
		gap: var(--hud-space-1);
		min-height: calc(var(--lines) * 1.4em + (var(--lines) - 1) * var(--hud-space-1));
		padding: var(--hud-space-2) var(--edge, 0px) 0;
		color: var(--color-text-secondary);
		font-size: var(--font-text-xs-size);
		line-height: 1.4;
		font-variant-numeric: tabular-nums;
	}

	/* Justified rows: each picture starts at the row height times its ratio
	   and grows in proportion to it, so a row's pictures share one height; the
	   last row's filler takes the slack rather than stretching its pictures,
	   and no picture grows past twice its start, so one left alone on a row
	   does not fill it. */
	.rows {
		--row: 7.5rem;
		display: flex;
		flex-wrap: wrap;
		gap: 3px;
		margin: 0;
		padding: 0;
		list-style: none;
	}

	.rows::after {
		content: '';
		flex: 1000 1 0;
	}

	/* A gallery of one picture (a post's photo) has no row to keep even: it
	   grows like any picture on a full row, to twice its start. */
	.rows:has(> li:only-child)::after {
		display: none;
	}

	.rows > li {
		flex: var(--ratio) 1 calc(var(--row) * var(--ratio));
		min-width: 0;
		max-width: calc(var(--row) * var(--ratio) * 2);
		aspect-ratio: var(--ratio);
		overflow: hidden;
		border-radius: var(--border-radius-xs);
	}

	@container (min-width: 36rem) {
		.rows {
			--row: 11rem;
		}
	}

	@container (min-width: 60rem) {
		.rows {
			--row: 14rem;
		}
	}

	/* One under another, each the full width at its own shape: a gallery that
	   is what the tile shows. */
	.stack {
		display: flex;
		flex-direction: column;
		gap: var(--hud-space-2);
		margin: 0;
		padding: 0;
		list-style: none;
	}

	.stack > li {
		aspect-ratio: var(--ratio);
		overflow: hidden;
		border-radius: var(--border-radius-sm);
	}

	/* One at a time, the full width, in one frame for all: each picture whole
	   in it, swiped from one to the next and stopping on each. */
	.frame {
		position: relative;
	}

	.frame.carousel {
		overflow: hidden;
		border-radius: var(--border-radius-sm);
	}

	ul.carousel {
		display: flex;
		margin: 0;
		padding: 0;
		overflow-x: auto;
		scroll-snap-type: x mandatory;
		overscroll-behavior-x: contain;
		scrollbar-width: none;
		list-style: none;
	}

	.carousel > li {
		flex: 0 0 100%;
		aspect-ratio: var(--frame);
		scroll-snap-align: start;
		scroll-snap-stop: always;
	}

	/* Arrows at its sides where there is a pointer to click them; a finger swipes. */
	.step {
		display: none;
		position: absolute;
		top: 50%;
		place-items: center;
		width: 1.75rem;
		height: 1.75rem;
		padding: 0;
		border: 0;
		border-radius: 50%;
		background: rgb(255 255 255 / 0.75);
		color: rgb(0 0 0 / 0.7);
		transform: translateY(-50%);
		cursor: pointer;
	}

	@media (hover: hover) {
		.step {
			display: grid;
		}
	}

	.step:hover {
		background: rgb(255 255 255 / 0.95);
	}

	.step.back {
		left: var(--hud-space-2);
	}

	.step.next {
		right: var(--hud-space-2);
	}

	.step svg {
		width: 0.875rem;
		height: 0.875rem;
		fill: none;
		stroke: currentColor;
		stroke-width: 2.5;
		stroke-linecap: round;
		stroke-linejoin: round;
	}

	/* Which one shows: its dot white, the others fainter and smaller further off. */
	.dots {
		display: flex;
		position: absolute;
		bottom: 0.625rem;
		left: 50%;
		align-items: center;
		gap: 5px;
		transform: translateX(-50%);
		pointer-events: none;
	}

	.dots > span {
		width: 6px;
		height: 6px;
		border-radius: 50%;
		background: rgb(255 255 255 / 0.4);
		transition:
			width 0.2s,
			height 0.2s;
	}

	.dots > .on {
		background: #fff;
	}

	.dots > .near {
		width: 4px;
		height: 4px;
		opacity: 0.75;
	}

	.dots > .far {
		width: 3px;
		height: 3px;
		opacity: 0.5;
	}
</style>