Files
RuneStone/src/lib/terminal/TerminalPicker.svelte
T

44 lines
2.7 KiB
Svelte
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts">
import { onMount, tick } from "svelte";
let { title, items, onPick, onClose, empty = "No entries yet." }: {
title: string; items: { label: string; detail?: string; value: string }[];
onPick: (value: string) => void; onClose: () => void; empty?: string;
} = $props();
let query = $state("");
let selected = $state(0);
let dialog: HTMLDialogElement;
let input: HTMLInputElement;
const matches = $derived(items.filter(item => `${item.label} ${item.detail || ""}`.toLowerCase().includes(query.toLowerCase())));
async function pick(value?: string) { if (value !== undefined) { const choose = onPick; onClose(); await tick(); choose(value); } }
onMount(() => { dialog.showModal(); input.focus(); });
</script>
<dialog bind:this={dialog} oncancel={onClose} aria-label={title}>
<header>{title}<button aria-label="Close picker" onclick={onClose}>×</button></header>
<input bind:this={input} bind:value={query} aria-label={title} placeholder="Type to filter…" oninput={() => selected = 0} onkeydown={event => {
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
event.preventDefault(); selected = Math.max(0, Math.min(matches.length - 1, selected + (event.key === "ArrowDown" ? 1 : -1)));
dialog.querySelector(`[data-index="${selected}"]`)?.scrollIntoView({ block: "nearest" });
}
if (event.key === "Enter") { event.preventDefault(); pick(matches[selected]?.value); }
}} />
<div class="results">
{#each matches as item, index}
<button data-index={index} class:selected={index === selected} onclick={() => pick(item.value)}><span>{item.label}</span>{#if item.detail}<small>{item.detail}</small>{/if}</button>
{/each}
{#if !matches.length}<p>{query ? "No matching entries." : empty}</p>{/if}
</div>
<footer>↑ ↓ select · Enter choose · Esc close</footer>
</dialog>
<style>
dialog { margin-top: 12vh; padding: 0; width: min(640px, 85vw); background: var(--rs-bg-elevated); color: var(--rs-text); border: 1px solid var(--rs-border); border-radius: 8px; }
dialog::backdrop { background: #0008; }
header { display: flex; align-items: center; justify-content: space-between; padding: 8px 16px; }
input { width: 100%; padding: 14px; background: var(--rs-bg); color: var(--rs-text-strong); border: 0; }
.results { max-height: 50vh; overflow: auto; padding: 6px; }
button { background: transparent; border: 0; padding: 8px; }
.results button { display: flex; flex-direction: column; width: 100%; text-align: left; gap: 4px; overflow-wrap: anywhere; white-space: pre-wrap; }
button.selected, button:hover { background: var(--rs-bg-active); }
small, footer, p { color: var(--rs-text-dim); }
footer { padding: 10px 14px; border-top: 1px solid var(--rs-border); font-size: 12px; }
</style>