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

	/** One picture of a lightbox: what it was drawn at, its full size, its alt text. */
	export type LightboxItem = { src: Source | null; full?: string | null; alt: string | null };
</script>

<script lang="ts">
	import { onMount } from 'svelte';
	import { ICONS } from './icons';

	// A set of pictures shown whole over everything, as the iOS app's
	// lightbox: on black, at its full size over what it was drawn at until
	// that loads, gone through with the arrows (on screen and on the
	// keyboard) or a swipe, closed by its button, Escape or a click beside
	// the picture; its alt text and where it is in the set at the foot. A
	// modal dialog, so focus stays in it and comes back where it was.
	let { items, at: start, onclose }: { items: readonly LightboxItem[]; at: number; onclose: () => void } = $props();

	let dialog: HTMLDialogElement | undefined = $state();
	// svelte-ignore state_referenced_locally
	let at = $state(start);
	const item = $derived(items[at]);
	const many = $derived(items.length > 1);
	/** Where a swipe started, while one is under way. */
	let swiping: { x: number; y: number } | null = null;
	/** Whether the pointer that just lifted swiped: its click is not one beside the picture. */
	let swiped = false;

	onMount(() => {
		dialog?.showModal();
		return () => dialog?.close();
	});

	const go = (by: number) => {
		at = (at + by + items.length) % items.length;
	};

	function keydown(event: KeyboardEvent) {
		if (!many) return;
		if (event.key === 'ArrowRight') go(1);
		else if (event.key === 'ArrowLeft') go(-1);
		else return;
		event.preventDefault();
	}

	/** A click on the dialog itself, beside the picture and its controls, closes it. */
	function click(event: MouseEvent) {
		if (swiped) {
			swiped = false;
			return;
		}
		if (event.target === event.currentTarget || (event.target as Element).classList.contains('stage')) dialog?.close();
	}

	function down(event: PointerEvent) {
		if (event.pointerType !== 'mouse') swiping = { x: event.clientX, y: event.clientY };
	}

	/** Sideways, the next or the one before; down (or up), closed. */
	function up(event: PointerEvent) {
		if (!swiping) return;
		const dx = event.clientX - swiping.x;
		const dy = event.clientY - swiping.y;
		swiping = null;
		swiped = Math.abs(dx) > 48 || Math.abs(dy) > 48;
		if (Math.abs(dx) > 48 && Math.abs(dx) > Math.abs(dy)) {
			if (many) go(dx < 0 ? 1 : -1);
		} else if (Math.abs(dy) > 96) {
			dialog?.close();
		}
	}
</script>

{#snippet icon(paths: readonly string[])}
	<svg viewBox="0 0 24 24" aria-hidden="true">
		{#each paths as d (d)}<path {d} />{/each}
	</svg>
{/snippet}

<dialog
	bind:this={dialog}
	class="lightbox"
	aria-label={item?.alt || `Picture ${at + 1} of ${items.length}`}
	{onclose}
	onclick={click}
	onkeydown={keydown}
	onpointerdown={down}
	onpointerup={up}
	onpointercancel={() => (swiping = null)}
>
	<div class="stage">
		{#key at}
			{@const small = item?.src?.src ?? null}
			<img
				class="picture"
				src={item?.full ?? small ?? ''}
				alt={item?.alt ?? ''}
				style:background-image={small && item?.full ? `url("${small}")` : undefined}
				draggable="false"
			/>
		{/key}
	</div>
	<button type="button" class="control close" aria-label="Close" onclick={() => dialog?.close()}>{@render icon(ICONS.x)}</button>
	{#if many}
		<button type="button" class="control before" aria-label="Previous picture" onclick={() => go(-1)}>{@render icon(['m15 18-6-6 6-6'])}</button>
		<button type="button" class="control after" aria-label="Next picture" onclick={() => go(1)}>{@render icon(['m9 18 6-6-6-6'])}</button>
	{/if}
	{#if many || item?.alt}
		<div class="foot">
			{#if many}<p class="count">{at + 1} of {items.length}</p>{/if}
			{#if item?.alt}<p class="alt">{item.alt}</p>{/if}
		</div>
	{/if}
</dialog>

<style>
	.lightbox {
		position: fixed;
		inset: 0;
		width: 100%;
		max-width: none;
		height: 100%;
		max-height: none;
		margin: 0;
		padding: 0;
		border: 0;
		background: #000;
		color: #fff;
		color-scheme: dark;
		overscroll-behavior: contain;
	}

	.lightbox::backdrop {
		background: #000;
	}

	.stage {
		display: grid;
		place-items: center;
		width: 100%;
		height: 100%;
		padding: var(--hud-space-6) 0;
		box-sizing: border-box;
		touch-action: pinch-zoom;
	}

	/* The whole stage, the picture contained in it, so any shape fits the
	   screen; what it was drawn at shows behind until the full size loads. */
	.picture {
		width: 100%;
		height: 100%;
		min-height: 0;
		object-fit: contain;
		background-size: contain;
		background-position: center;
		background-repeat: no-repeat;
		user-select: none;
	}

	.control {
		position: absolute;
		display: grid;
		place-items: center;
		width: 2.75rem;
		height: 2.75rem;
		padding: 0;
		border: 0;
		border-radius: var(--border-radius-full);
		background: rgb(255 255 255 / 0.16);
		color: #fff;
		cursor: pointer;
	}

	.control:hover {
		background: rgb(255 255 255 / 0.28);
	}

	.control:focus-visible {
		outline: 2px solid #fff;
		outline-offset: 2px;
	}

	.control svg {
		width: 1.25rem;
		height: 1.25rem;
		fill: none;
		stroke: currentColor;
		stroke-width: 2;
		stroke-linecap: round;
		stroke-linejoin: round;
	}

	.close {
		top: var(--hud-space-3);
		right: var(--hud-space-3);
	}

	.before,
	.after {
		top: 50%;
		translate: 0 -50%;
	}

	.before {
		left: var(--hud-space-3);
	}

	.after {
		right: var(--hud-space-3);
	}

	/* On a phone a swipe goes through them: the arrows would cover the picture. */
	@media (pointer: coarse) {
		.before,
		.after {
			display: none;
		}
	}

	.foot {
		position: absolute;
		inset: auto 0 0;
		padding: var(--hud-space-5) var(--hud-space-4) var(--hud-space-4);
		background: linear-gradient(transparent, rgb(0 0 0 / 0.7));
		pointer-events: none;
	}

	.foot p {
		margin: 0;
		max-width: 48rem;
	}

	.count {
		font-size: var(--font-text-xs-size);
		font-variant-numeric: tabular-nums;
		color: rgb(255 255 255 / 0.75);
	}

	.alt {
		font-size: var(--font-text-sm-size);
		display: -webkit-box;
		-webkit-line-clamp: 4;
		line-clamp: 4;
		-webkit-box-orient: vertical;
		overflow: hidden;
	}
</style>