~ 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
424 lines · 10.7 KB Raw History
<script lang="ts">
	import { onMount, untrack } from 'svelte';
	import Icon from '../atoms/Icon.svelte';
	import type * as HudDefs from '../lexicons/place/hud/defs.defs.ts';
	import { COLOURS, colourName, styleOf, themeId, type Palette, type Scheme, type Theme } from '../theme';

	/**
	 * A new theme, made from another: its name, light or dark, and each of
	 * its colours, by what it colours. The dialog draws itself in the
	 * palette as it is made, a tile's worth of the workspace beside the
	 * colours; each text that reads at under 4.5:1 on what it is drawn on
	 * is named as it happens (the server's `checkTheme`), and the theme is
	 * saved only when none does. Saved, it is published in the person's
	 * repository, for anyone to choose, and the workspace is drawn in it.
	 */
	let {
		starts,
		from,
		check,
		onsave,
		onclose
	}: {
		/** The themes a new one can start from: those with a palette. */
		starts: Theme[];
		/** The one it starts from. */
		from: Theme;
		/** The texts of a palette that read too faintly. */
		check: (palette: Palette) => Promise<HudDefs.ContrastProblem[]>;
		onsave: (name: string, scheme: Scheme, palette: Palette) => Promise<void>;
		onclose: () => void;
	} = $props();

	const HEX = /^#[0-9a-f]{6}$/i;

	let dialog: HTMLDialogElement | undefined = $state();
	let name = $state('');
	/** The theme it starts from, as it was opened: choosing another in the dialog changes it. */
	const first = untrack(() => from);
	let start = $state(themeId(first));
	let scheme = $state<Scheme>(first.colorScheme === 'dark' ? 'dark' : 'light');
	let palette = $state<Palette>({ ...(first.palette as Palette) });
	/** What each colour's field says as it is typed, before it is a colour. */
	let typed = $state<Partial<Record<keyof Palette, string>>>({});
	let problems = $state<HudDefs.ContrastProblem[]>([]);
	let checking = $state(false);
	let saving = $state(false);
	let error = $state<string | null>(null);

	const drawn = $derived(styleOf({ colorScheme: scheme, palette }));
	const ready = $derived(name.trim().length > 0 && problems.length === 0 && !checking && !saving);

	/** Start again from another theme: its scheme and every colour. */
	function startFrom(id: string) {
		const theme = starts.find((found) => themeId(found) === id);
		if (!theme?.palette) return;
		scheme = theme.colorScheme === 'dark' ? 'dark' : 'light';
		palette = { ...(theme.palette as Palette) };
		typed = {};
	}

	function set(field: keyof Palette, value: string) {
		typed[field] = value;
		const hex = value.startsWith('#') ? value : `#${value}`;
		if (HEX.test(hex)) palette[field] = hex.toLowerCase();
	}

	// Checked a moment after the last change.
	$effect(() => {
		const asked = { ...palette };
		checking = true;
		const timer = window.setTimeout(async () => {
			try {
				problems = await check(asked);
				error = null;
			} catch (failed) {
				error = failed instanceof Error ? failed.message : String(failed);
			} finally {
				checking = false;
			}
		}, 200);
		return () => window.clearTimeout(timer);
	});

	async function save(event: SubmitEvent) {
		event.preventDefault();
		if (!ready) return;
		saving = true;
		error = null;
		try {
			await onsave(name.trim(), scheme, palette);
			dialog?.close();
		} catch (failed) {
			error = failed instanceof Error ? failed.message : String(failed);
		} finally {
			saving = false;
		}
	}

	onMount(() => dialog?.showModal());
</script>

<dialog bind:this={dialog} class="dialog editor" style={drawn} data-palette aria-labelledby="editor-title" {onclose}>
	<form onsubmit={save}>
		<h2 id="editor-title">New theme</h2>
		<button type="button" class="icon-button close" aria-label="Close" onclick={() => dialog?.close()}><Icon name="close" /></button>
		<p class="mute">Published in your repository for anyone to use, and drawn here at once.</p>

		<div class="columns">
			<div class="fields">
				<label class="field">
					<span>Name</span>
					<input class="input" type="text" bind:value={name} maxlength="64" placeholder="As Sand, or Night shift" required autocomplete="off" />
				</label>
				<div class="pair">
					<label class="field">
						<span>Start from</span>
						<select class="input" bind:value={start} onchange={() => startFrom(start)}>
							{#each starts as theme (themeId(theme))}
								<option value={themeId(theme)}>{theme.name}</option>
							{/each}
						</select>
					</label>
					<fieldset class="field">
						<legend>Scheme</legend>
						<div class="schemes">
							<label><input type="radio" bind:group={scheme} value="light" /> Light</label>
							<label><input type="radio" bind:group={scheme} value="dark" /> Dark</label>
						</div>
					</fieldset>
				</div>

				{#each COLOURS as group (group.group)}
					<fieldset class="group">
						<legend>{group.group}</legend>
						{#each group.colours as colour (colour.field)}
							<div class="colour">
								<input type="color" value={palette[colour.field]} aria-label={colour.name} oninput={(event) => set(colour.field, event.currentTarget.value)} />
								<span class="grow">{colour.name}</span>
								<input
									class="input hex"
									type="text"
									value={typed[colour.field] ?? palette[colour.field]}
									aria-label="{colour.name}, as #rrggbb"
									spellcheck="false"
									autocomplete="off"
									oninput={(event) => set(colour.field, event.currentTarget.value.trim())}
									onblur={() => delete typed[colour.field]}
								/>
							</div>
						{/each}
					</fieldset>
				{/each}
			</div>

			<div class="preview" aria-label="Preview">
				<div class="tile">
					<div class="head">
						<strong>A tile</strong>
						<span class="mute">@you.bsky.social · 2h</span>
					</div>
					<div class="body">
						<p>Text reads like this, <a href="#preview" onclick={(event) => event.preventDefault()}>a link</a> like this.</p>
						<p class="mute">Muted, for times and counts.</p>
						<p class="faint">Faint, for hints.</p>
						<div class="picked">A picked row</div>
						<div class="pills">
							<span class="pill open">Open</span>
							<span class="pill merged">Merged</span>
							<span class="pill closed">Closed</span>
							<span class="pill running">Running</span>
							<span class="pill neutral">Skipped</span>
						</div>
						<div class="notice">A notice, as an agent’s</div>
						<button type="button" class="btn primary" tabindex="-1">Main button</button>
					</div>
				</div>
			</div>
		</div>

		{#if problems.length}
			<div class="problems" role="alert">
				<p>Some text would be hard to read. Each needs 4.5:1:</p>
				<ul>
					{#each problems as p (`${p.text} ${p.on}`)}
						{@const said = `${colourName(p.text)} on ${colourName(p.on)}: ${p.ratio}:1`}
						<li>{said[0].toUpperCase()}{said.slice(1)}</li>
					{/each}
				</ul>
			</div>
		{/if}
		{#if error}<p class="problem" role="alert">{error}</p>{/if}

		<div class="actions">
			<button type="button" class="btn" onclick={() => dialog?.close()}>Cancel</button>
			<button type="submit" class="btn primary" disabled={!ready}>{saving ? 'Publishing…' : 'Publish and use'}</button>
		</div>
	</form>
</dialog>

<style>
	.editor {
		width: min(var(--dialog-wide), calc(100vw - 2 * var(--space-4)));
		max-height: calc(100vh - 2 * var(--space-8));
		margin-top: var(--space-8);
		overflow-y: auto;
	}

	.columns {
		display: grid;
		grid-template-columns: 1fr 1fr;
		gap: var(--space-5);
		margin-top: var(--space-4);
	}

	.fields {
		display: flex;
		flex-direction: column;
		gap: var(--space-3);
		min-width: 0;
	}

	.field {
		display: flex;
		flex-direction: column;
		gap: var(--space-1);
		min-width: 0;
		margin: 0;
		padding: 0;
		border: 0;
		font-size: var(--text-sm);
	}

	.field legend,
	.field > span {
		color: var(--mute);
	}

	.pair {
		display: grid;
		grid-template-columns: 1fr auto;
		gap: var(--space-3);
	}

	.schemes {
		display: flex;
		gap: var(--space-3);
		align-items: center;
		height: var(--control);
	}

	.group {
		margin: 0;
		padding: var(--space-2) 0 0;
		border: 0;
		border-top: var(--border);
	}

	.group legend {
		padding-right: var(--space-2);
		color: var(--mute);
		font-size: var(--text-xs);
		font-weight: var(--weight-semibold);
	}

	.colour {
		display: flex;
		align-items: center;
		gap: var(--space-2);
		padding: var(--space-0-5) 0;
		font-size: var(--text-sm);
	}

	.colour input[type='color'] {
		flex: none;
		width: var(--control-sm);
		height: var(--control-sm);
		padding: 0;
		border: var(--border);
		border-radius: var(--radius-sm);
		background: none;
		cursor: pointer;
	}

	.grow {
		flex: 1;
		min-width: 0;
	}

	.hex {
		width: var(--hex);
		height: var(--control-sm);
		font-family: var(--font-mono);
		font-size: var(--text-sm);
	}

	.preview {
		position: sticky;
		top: 0;
		align-self: start;
		padding: var(--space-4);
		border-radius: var(--radius-md);
		background: var(--canvas);
	}

	.tile {
		overflow: hidden;
		border: var(--border);
		border-top: var(--progress) solid var(--chain-1);
		border-radius: var(--radius-md);
		background: var(--bg);
		color: var(--fg);
	}

	.head {
		display: flex;
		flex-direction: column;
		padding: var(--space-2) var(--space-3);
		border-bottom: var(--border);
		background: var(--panel);
		font-size: var(--text-sm);
	}

	.head .mute {
		font-size: var(--text-xs);
	}

	.body {
		display: flex;
		flex-direction: column;
		gap: var(--space-2);
		padding: var(--space-3);
		font-size: var(--text-sm);
	}

	.body a {
		color: var(--accent);
		text-decoration-color: var(--link-line);
		text-underline-offset: var(--underline-offset);
	}

	.faint {
		color: var(--faint);
	}

	.picked {
		padding: var(--space-1-5) var(--space-2);
		border-radius: var(--radius-sm);
		background: var(--accent-soft);
	}

	.pills {
		display: flex;
		flex-wrap: wrap;
		gap: var(--space-1);
	}

	.pill {
		padding: var(--space-0-5) var(--space-2);
		border-radius: var(--radius-full);
		color: var(--on-solid);
		font-size: var(--text-xs);
		font-weight: var(--weight-medium);
	}

	.pill.open {
		background: var(--open);
	}

	.pill.merged {
		background: var(--merged);
	}

	.pill.closed {
		background: var(--closed-pull);
	}

	.pill.running {
		background: var(--running);
	}

	.pill.neutral {
		background: var(--closed-issue);
	}

	.notice {
		padding: var(--space-2);
		border-radius: var(--radius-sm);
		background: var(--notice);
		color: var(--notice-fg);
	}

	.body .btn {
		align-self: start;
	}

	.problems {
		margin-top: var(--space-4);
		padding: var(--space-2-5) var(--space-3);
		border-radius: var(--radius-md);
		background: var(--notice);
		color: var(--notice-fg);
		font-size: var(--text-sm);
	}

	.problems ul {
		margin: var(--space-1) 0 0;
		padding-left: var(--space-4);
	}

	@media (max-width: 640px) {
		.editor {
			margin-top: auto;
		}

		.columns {
			grid-template-columns: 1fr;
		}

		.preview {
			position: static;
			order: -1;
		}
	}
</style>