~ 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
36 lines · 1.5 KB Raw History
<script lang="ts">
	import { onMount, type Snippet } from 'svelte';
	import Icon from '../atoms/Icon.svelte';

	/**
	 * A dialog over the dimmed page, open while it is mounted: its `title`,
	 * a button to close it, then `children`; a press on the page around it,
	 * or Escape, closes it too, and `onclose` hears it closed. `class` names
	 * it for its own styles. While `held`, it cannot be closed, as while
	 * what it began is under way.
	 */
	let { id, title, class: kind = '', held = false, onclose, children }: { id: string; title: string; class?: string; held?: boolean; onclose: () => void; children: Snippet } = $props();

	let dialog: HTMLDialogElement | undefined = $state();

	onMount(() => dialog?.showModal());

	/** A press on the backdrop, outside the dialog's box: its padding is the dialog too. */
	function outside(event: MouseEvent) {
		if (held || event.target !== dialog) return;
		const box = dialog.getBoundingClientRect();
		if (event.clientX < box.left || event.clientX > box.right || event.clientY < box.top || event.clientY > box.bottom) dialog.close();
	}
</script>

<dialog bind:this={dialog} class={['dialog', kind]} {id} aria-labelledby="{id}-title" {onclose} oncancel={(event) => held && event.preventDefault()} onclick={outside}>
	<h2 id="{id}-title">{title}</h2>
	<button type="button" class="icon-button close" aria-label="Close" disabled={held} onclick={() => dialog?.close()}><Icon name="close" /></button>
	{@render children()}
</dialog>

<style>
	dialog {
		position: fixed;
	}
</style>