~ 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
157 lines · 4.5 KB Raw History
<script lang="ts">
	import { opens } from './open';
	import Picture from './Picture.svelte';
	import PickMark from './PickMark.svelte';
	import type { Cover } from './Covers.svelte';

	// One cover as a tile: its picture alone, square and cropped to fill, its
	// words only its accessible name (`name`); marked in its corner where it
	// holds more than one picture. A click opens what it opens (a link,
	// where it has an address). One its author labelled shows a content
	// warning instead of its picture, and loads none of it: the first click
	// shows it, the next opens it. While covers are picked (`picked` there),
	// a click picks it or lets it go, its mark in the other corner; one that
	// can't be picked is dimmed.
	let { cover, name, sizes }: { cover: Cover; name: string; sizes?: string } = $props();

	let revealed = $state(false);
	const picking = $derived(cover.picked !== undefined);
	const hidden = $derived(cover.labels.length > 0 && !revealed);
	const clickable = $derived(picking ? cover.picked !== null : hidden || cover.open !== null);
	/** What a click opens as a link: its open, where it has an address, and a click opens it (none picked, nothing hidden). */
	const link = $derived(!picking && !hidden && cover.open?.href !== undefined ? cover.open : null);

	function click() {
		if (!picking && hidden) revealed = true;
		else cover.open?.run();
	}
</script>

{#snippet body()}
	{#if hidden}
		<span class="warning">
			<span class="warning-title">Content warning</span>
			<span class="labels">{cover.labels.join(', ')}</span>
		</span>
	{:else}
		<Picture src={cover.src} alt="" placeholder={cover.src === null ? 'No picture' : undefined} srcset={cover.srcset} {sizes} />
		{#if cover.many}
			<svg class="many" viewBox="0 0 24 24" aria-hidden="true">
				<path class="back" d="M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2" />
				<rect x="8" y="8" width="14" height="14" rx="2" />
			</svg>
		{/if}
	{/if}
	{#if picking && cover.picked !== null}<PickMark picked={cover.picked === true} />{/if}
{/snippet}

{#if clickable}
	<svelte:element
		this={link ? 'a' : 'button'}
		{...link ? opens(link) : { type: 'button', onclick: click }}
		class="tile"
		class:picked={cover.picked === true}
		aria-pressed={picking ? cover.picked === true : undefined}
		aria-label={!picking && hidden ? `Show ${name}: content warning, ${cover.labels.join(', ')}` : name}
	>
		{@render body()}
	</svelte:element>
{:else}
	<div class="tile" class:dimmed={picking} role="img" aria-label={name}>{@render body()}</div>
{/if}

<style>
	.tile {
		position: relative;
		display: grid;
		width: 100%;
		/* A portrait cell the picture fills, as Grain's grid draws them.
		   overflow other than visible drops the content-based minimum height
		   an aspect-ratio box would have, so large images cannot stretch it. */
		aspect-ratio: 3 / 4;
		padding: 0;
		border: 0;
		border-radius: 0;
		background: var(--color-background-tertiary);
		color: var(--color-text-primary);
		font: inherit;
		overflow: hidden;
	}

	:is(a, button).tile {
		cursor: pointer;
	}

	/* A link: sized as the button is, no line of a link's in it. */
	a.tile {
		box-sizing: border-box;
		text-decoration: none;
	}

	:is(a, button).tile:hover :global(img) {
		opacity: 0.9;
	}

	:is(a, button).tile:focus-visible {
		outline: 2px solid var(--color-ring-primary);
		outline-offset: -2px;
	}

	/* Picked: inset a little, framed in the accent; one that can't be picked, faded. */
	.tile.picked :global(img) {
		scale: 0.94;
		border-radius: var(--border-radius-sm);
	}

	.tile.picked {
		box-shadow: inset 0 0 0 2px var(--color-text-info);
	}

	.tile.dimmed {
		opacity: 0.4;
	}

	/* Stacked squares, in the glyph's white with a soft shadow, so it reads
	   on any picture. */
	.many {
		position: absolute;
		top: var(--hud-space-2);
		right: var(--hud-space-2);
		width: 1.125rem;
		height: 1.125rem;
		fill: var(--hud-tint-glyph);
		stroke: var(--hud-tint-glyph);
		stroke-width: 2;
		stroke-linecap: round;
		stroke-linejoin: round;
		filter: drop-shadow(0 0 2px rgb(0 0 0 / 0.5));
		pointer-events: none;
	}

	.back {
		fill: none;
	}

	.warning {
		display: grid;
		align-content: center;
		justify-items: center;
		gap: var(--hud-space-1);
		padding: var(--hud-space-2);
		background: var(--color-background-warning);
		text-align: center;
		overflow: hidden;
	}

	.warning-title {
		color: var(--color-text-warning);
		font-size: var(--font-text-xs-size);
		font-weight: var(--font-weight-semibold);
	}

	.labels {
		color: var(--color-text-primary);
		font-size: var(--font-text-xs-size);
		overflow-wrap: anywhere;
	}
</style>