~ 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
171 lines · 4.1 KB Raw History
<script lang="ts">
	import Avatar from '../atoms/Avatar.svelte';
	import { findActors, type Found } from '../typeahead';

	/**
	 * A handle, with whoever on atproto matches what is typed so far
	 * dropping down over what is under it. Picking one fills it in and goes
	 * on (`onpick`), as the form's button would. As knotless's.
	 */
	let { id, value = $bindable(''), onpick }: { id: string; value?: string; onpick?: (handle: string) => void } = $props();

	let found = $state<Found[]>([]);
	let open = $state(false);
	/** Which one Enter picks: none till the arrows choose one, so Enter goes on with what is typed. */
	let at = $state(-1);
	const list = $derived(`${id}-list`);
	const showing = $derived(open && found.length > 0);

	// Looked up a moment after typing stops; a lookup overtaken by more typing is let go.
	$effect(() => {
		const q = value;
		let overtaken = false;
		const wait = window.setTimeout(() => {
			void findActors(q).then((actors) => {
				if (overtaken) return;
				found = actors;
				at = -1;
			});
		}, 150);
		return () => {
			overtaken = true;
			window.clearTimeout(wait);
		};
	});

	function pick(actor: Found) {
		value = actor.handle;
		open = false;
		onpick?.(actor.handle);
	}

	function keydown(event: KeyboardEvent) {
		if (!showing) return;
		if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
			event.preventDefault();
			const n = found.length;
			at = event.key === 'ArrowDown' ? (at + 1) % n : (at - 1 + n) % n;
		} else if (event.key === 'Enter' && found[at]) {
			event.preventDefault();
			pick(found[at]);
		} else if (event.key === 'Escape') {
			// Closes the list, not the dialog around it.
			event.preventDefault();
			event.stopPropagation();
			open = false;
		}
	}
</script>

<div class="handle-input">
	<input
		{id}
		class="input"
		name="handle"
		placeholder="alice.bsky.social"
		autocomplete="username"
		autocapitalize="none"
		spellcheck="false"
		required
		role="combobox"
		aria-expanded={showing}
		aria-controls={list}
		aria-autocomplete="list"
		aria-activedescendant={showing && at >= 0 ? `${list}-${at}` : undefined}
		bind:value
		oninput={() => (open = true)}
		onfocus={() => (open = true)}
		onblur={() => (open = false)}
		onkeydown={keydown}
	/>
	{#if showing}
		<!-- Over the dialog, under the field, rather than stretching the dialog to hold it. -->
		<div id={list} class="list" role="listbox" aria-label="Accounts">
			{#each found as actor, i (actor.did)}
				<!-- Picked on the press, before the field loses focus and the list closes. -->
				<div
					id="{list}-{i}"
					class="option"
					role="option"
					tabindex="-1"
					aria-selected={i === at}
					onmousedown={(event) => {
						event.preventDefault();
						pick(actor);
					}}
					onmouseenter={() => (at = i)}
				>
					<Avatar did={actor.did} handle={actor.handle} src={actor.avatar ?? null} name={actor.displayName ?? null} size="md" />
					<span class="who">
						{#if actor.displayName}<b>{actor.displayName}</b>{/if}
						<small>@{actor.handle}</small>
					</span>
				</div>
			{/each}
		</div>
	{/if}
</div>

<style>
	.handle-input {
		position: relative;
		display: grid;
	}

	.list {
		position: absolute;
		z-index: var(--z-menu);
		top: calc(100% + var(--space-1));
		right: 0;
		left: 0;
		padding: var(--space-1);
		border: var(--border);
		border-radius: var(--radius-md);
		background: var(--bg);
		box-shadow: var(--shadow-pop);
	}

	/* On a phone a dialog is a sheet along the bottom: the list opens up, over what is above the field. */
	@media (max-width: 640px) {
		.list {
			top: auto;
			bottom: calc(100% + var(--space-1));
		}
	}

	.option {
		display: flex;
		align-items: center;
		gap: var(--space-2);
		padding: var(--space-1-5) var(--space-2);
		border-radius: var(--radius-sm);
		cursor: pointer;
	}

	.option[aria-selected='true'] {
		background: var(--hover);
	}

	.who {
		display: grid;
		min-width: 0;
		font-size: var(--text-sm);
		line-height: var(--leading-tight);
	}

	.who b,
	.who small {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.who b {
		font-weight: var(--weight-semibold);
	}

	.who small {
		color: var(--mute);
		font-size: var(--text-xs);
	}
</style>