~ 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
259 lines · 7.2 KB Raw History
<script lang="ts">
	import { translateOf } from '@hud/renderer';
	import type { Snippet } from 'svelte';

	// A screen drawn as a sheet along the bottom, over everything (the tabs,
	// a pager too), as Grain shows a story's comments: most of the screen's
	// height, its top corners rounded, a grabber at its top, what it holds
	// scrolling inside it. Over the screens, it is as wide as the frame's
	// column and under it; over a pager (`centred`: a story, full screen,
	// its picture in the middle of the window), it is a phone's column in
	// the middle of the window, over the picture, as Grain's web shows a
	// story's comments in the story's column. It rises on opening; ✕,
	// Escape, a tap on what it covers or a drag of its top downwards (past
	// a quarter of its height, or flicked) lowers it and then closes it
	// (`onclose`). Its top row holds its title, as its tree's Bar names it,
	// and that Bar's buttons (`bar`).

	let {
		title,
		bar,
		centred = false,
		onclose,
		children
	}: {
		/** What it is, drawn at its top and read aloud; null where its view names nothing. */
		title: string | null;
		/** Its tree's `Bar`'s buttons. */
		bar?: Snippet;
		/** Over a pager, full screen: a phone's column in the middle of the window, not the frame's column. */
		centred?: boolean;
		onclose: () => void;
		children: Snippet;
	} = $props();

	const SETTLE = 280;
	let height = $state(1);
	/** How far it is lowered: all the way while it rises, then with the finger. */
	let dy = $state(100_000);
	let settling = $state(false);
	let closing = false;
	let drag: { id: number; y: number; t: number; moved: boolean } | null = null;

	function settle(to: number, then?: () => void) {
		settling = true;
		dy = to;
		setTimeout(() => {
			settling = false;
			then?.();
		}, SETTLE);
	}

	function close() {
		if (closing) return;
		closing = true;
		settle(height, onclose);
	}

	/** A press on its top (not on its buttons) may become a drag. */
	function down(event: PointerEvent) {
		const on = event.target as Element;
		if (settling || !event.isPrimary || !on.closest('.sheet-top') || on.closest('button')) return;
		drag = { id: event.pointerId, y: event.clientY, t: performance.now(), moved: false };
	}

	function move(event: PointerEvent) {
		if (!drag || event.pointerId !== drag.id) return;
		const y = event.clientY - drag.y;
		if (!drag.moved) {
			if (Math.abs(y) < 6) return;
			drag.moved = true;
			(event.currentTarget as HTMLElement).setPointerCapture(event.pointerId);
		}
		dy = Math.max(0, y);
	}

	function up(event: PointerEvent) {
		if (!drag || event.pointerId !== drag.id) return;
		const { moved, t } = drag;
		drag = null;
		if (!moved) return;
		const speed = dy / Math.max(performance.now() - t, 1);
		if (dy > height * 0.25 || speed > 0.6) close();
		else settle(0);
	}

	/** Escape lowers it, but where a dialog is open over it (a comment's form, a question), whose Escape it is. */
	function key(event: KeyboardEvent) {
		if (event.key !== 'Escape' || document.querySelector('dialog[open]')) return;
		event.preventDefault();
		close();
	}

	/** Drawn at the frame's end (the body's, outside one), over the tabs and a pager, risen, and focused for the keys. */
	function overAll(element: HTMLElement) {
		(document.querySelector('.hud-frame') ?? document.body).append(element);
		element.querySelector<HTMLElement>('.sheet')?.focus();
		requestAnimationFrame(() => settle(0));
		return () => element.remove();
	}
</script>

<svelte:window onkeydown={key} />

<div class="over" class:settling class:centred {@attach overAll} style:--lowered={Math.min(dy / height, 1)}>
	<button type="button" class="backdrop" tabindex="-1" aria-label="Close" onclick={close}></button>
	<div
		class="sheet"
		role="dialog"
		aria-modal="true"
		aria-label={title ?? 'Sheet'}
		tabindex="-1"
		bind:clientHeight={height}
		style:transform="translateY({dy}px)"
		onpointerdown={down}
		onpointermove={move}
		onpointerup={up}
		onpointercancel={up}
	>
		<header class="sheet-top">
			<span class="grabber" aria-hidden="true"></span>
			<div class="row">
				{#if title}<h2 class="title" translate={translateOf(title)}>{title}</h2>{/if}
				<span class="tools">{@render bar?.()}</span>
				<button type="button" class="close" aria-label="Close" onclick={close}>
					<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M18 6 6 18M6 6l12 12" /></svg>
				</button>
			</div>
		</header>
		<div class="body">{@render children()}</div>
	</div>
</div>

<style>
	/* Over everything: the tabs (3) and a pager (10). */
	.over {
		position: fixed;
		inset: 0;
		z-index: 20;
	}

	.backdrop {
		position: absolute;
		inset: 0;
		padding: 0;
		border: 0;
		background: rgb(0 0 0 / calc(0.45 * (1 - var(--lowered))));
		cursor: default;
	}

	.sheet {
		position: absolute;
		inset: auto 0 0;
		display: flex;
		flex-direction: column;
		width: 100%;
		max-width: var(--hud-frame-column);
		height: 85dvh;
		margin-inline: var(--hud-frame-column-start, auto) auto;
		border-radius: 1rem 1rem 0 0;
		background: var(--color-background-primary);
		color: var(--color-text-primary);
		box-shadow: 0 -8px 32px rgb(0 0 0 / 0.3);
		outline: none;
		will-change: transform;
	}

	/* Over a pager: a phone's column in the middle of the window, over the picture; the whole width on a phone. */
	.centred .sheet {
		max-width: var(--hud-frame-phone);
		margin-inline: auto;
	}

	.settling .sheet,
	.settling .backdrop {
		transition:
			transform 280ms cubic-bezier(0.2, 0.9, 0.25, 1),
			background 280ms cubic-bezier(0.2, 0.9, 0.25, 1);
	}

	/* Its top, the grabber and the row under it, is what a drag lowers it by. */
	.sheet-top {
		display: grid;
		justify-items: center;
		padding: var(--hud-space-2) var(--hud-space-3) 0;
		border-bottom: var(--border-width-regular) solid var(--color-border-secondary);
		touch-action: none;
		cursor: grab;
	}

	.grabber {
		width: 2.25rem;
		height: 0.3125rem;
		border-radius: var(--border-radius-full);
		background: var(--color-border-primary);
	}

	.row {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		width: 100%;
		min-height: 2.75rem;
	}

	/* Its title, as a pushed screen's beside its back button: in the app's display font, on one line. */
	.title {
		flex: 1;
		min-width: 0;
		margin: 0;
		font-family: var(--app-font-display, inherit);
		font-size: var(--font-heading-md-size);
		line-height: var(--font-heading-md-line-height);
		font-weight: var(--font-weight-bold);
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.tools {
		display: flex;
		align-items: center;
		margin-right: auto;
	}

	.close {
		display: grid;
		place-items: center;
		width: 2.25rem;
		height: 2.25rem;
		padding: 0;
		border: 0;
		border-radius: var(--border-radius-full);
		background: var(--color-background-secondary);
		color: var(--color-text-primary);
		cursor: pointer;
	}

	.close svg {
		width: 1.125rem;
		height: 1.125rem;
		fill: none;
		stroke: currentColor;
		stroke-width: 2;
		stroke-linecap: round;
	}

	/* What it holds scrolls inside it, clear of the home indicator. */
	.body {
		--hud-bleed: var(--hud-space-3);
		flex: 1;
		min-height: 0;
		display: flex;
		flex-direction: column;
		gap: var(--hud-space-3);
		overflow-y: auto;
		overscroll-behavior: contain;
		padding: var(--hud-bleed) var(--hud-bleed) calc(var(--hud-bleed) + env(safe-area-inset-bottom));
	}
</style>