190 lines
4.2 KiB
Svelte
190 lines
4.2 KiB
Svelte
<script lang="ts">
|
|
import type { NamedPlace } from '@rosea/engine';
|
|
import { en } from '../copy/en.js';
|
|
import type { SunsetChoice } from './types.js';
|
|
|
|
/**
|
|
* "Where do you want your sunset?" A search over towns on the device, and a way
|
|
* to say not now. Decides nothing: it reports the answer (sunset-place.md).
|
|
*/
|
|
let {
|
|
guess,
|
|
current = null,
|
|
search,
|
|
focusOnOpen = false,
|
|
onchoose,
|
|
oncancel
|
|
}: {
|
|
guess: NamedPlace | null;
|
|
/** The town already set, when the player is changing it. */
|
|
current?: NamedPlace | null;
|
|
search: (query: string) => Promise<readonly NamedPlace[]>;
|
|
focusOnOpen?: boolean;
|
|
onchoose: (choice: SunsetChoice) => void;
|
|
oncancel?: () => void;
|
|
} = $props();
|
|
|
|
const uid = $props.id();
|
|
let query = $state('');
|
|
// Raw: the towns are handed on as they came, never wrapped in a state Proxy.
|
|
let results = $state.raw<readonly NamedPlace[]>([]);
|
|
let input = $state<HTMLInputElement>();
|
|
|
|
// Only the latest query's answer is shown, however the searches finish.
|
|
let asked = 0;
|
|
$effect(() => {
|
|
const q = query;
|
|
const ticket = ++asked;
|
|
if (q.trim() === '') {
|
|
results = [];
|
|
return;
|
|
}
|
|
search(q).then(
|
|
(found) => {
|
|
if (ticket === asked) results = found;
|
|
},
|
|
(error: unknown) => {
|
|
console.error('sunset: the town search failed', error);
|
|
if (ticket === asked) results = [];
|
|
}
|
|
);
|
|
});
|
|
|
|
$effect(() => {
|
|
if (focusOnOpen) input?.focus();
|
|
});
|
|
|
|
const where = (t: NamedPlace): string => [t.region, t.country].filter(Boolean).join(', ');
|
|
</script>
|
|
|
|
<section class="ask" aria-labelledby="{uid}-q">
|
|
<h2 id="{uid}-q">{en.sunset.question}</h2>
|
|
<p class="why">{en.sunset.why}</p>
|
|
|
|
<label for="{uid}-search">{en.sunset.searchLabel}</label>
|
|
<input
|
|
id="{uid}-search"
|
|
type="search"
|
|
autocomplete="off"
|
|
spellcheck="false"
|
|
enterkeyhint="search"
|
|
bind:value={query}
|
|
bind:this={input}
|
|
/>
|
|
<p class="found" role="status" aria-live="polite">{query.trim() === '' ? '' : en.sunset.found(results.length)}</p>
|
|
|
|
{#if results.length > 0}
|
|
<ul>
|
|
{#each results as town (`${town.name}|${town.lat}|${town.lon}`)}
|
|
<li>
|
|
<button type="button" onclick={() => onchoose({ kind: 'town', town })}>
|
|
<span class="name">{town.name}</span>
|
|
<span class="where">{where(town)}</span>
|
|
</button>
|
|
</li>
|
|
{/each}
|
|
</ul>
|
|
{/if}
|
|
|
|
<div class="other">
|
|
{#if current && oncancel}
|
|
<button type="button" class="quiet" onclick={oncancel}>{en.sunset.keep(current.name)}</button>
|
|
{/if}
|
|
<button type="button" class="quiet" onclick={() => onchoose({ kind: 'device-zone' })}>
|
|
{guess ? en.sunset.skip(guess.name) : en.sunset.skipNoGuess}
|
|
</button>
|
|
</div>
|
|
</section>
|
|
|
|
<style>
|
|
.ask {
|
|
display: grid;
|
|
gap: var(--space-2);
|
|
width: min(28rem, 100%);
|
|
margin-inline: auto;
|
|
text-align: start;
|
|
}
|
|
|
|
h2 {
|
|
margin: 0;
|
|
font-family: var(--font-display);
|
|
font-weight: 400;
|
|
font-size: 1.4rem;
|
|
color: var(--ink);
|
|
}
|
|
|
|
.why,
|
|
.found {
|
|
margin: 0;
|
|
color: var(--ink-soft);
|
|
}
|
|
|
|
.found:empty {
|
|
display: none;
|
|
}
|
|
|
|
label {
|
|
font-size: 0.95rem;
|
|
}
|
|
|
|
input {
|
|
font: inherit;
|
|
padding: var(--space-2);
|
|
border: 1px solid var(--ink-soft);
|
|
border-radius: 0.5rem;
|
|
background: var(--paper);
|
|
color: var(--ink);
|
|
}
|
|
|
|
ul {
|
|
list-style: none;
|
|
margin: 0;
|
|
padding: 0;
|
|
display: grid;
|
|
gap: var(--space-1);
|
|
}
|
|
|
|
button {
|
|
font: inherit;
|
|
cursor: pointer;
|
|
border-radius: 0.5rem;
|
|
color: var(--ink);
|
|
}
|
|
|
|
li button {
|
|
width: 100%;
|
|
display: flex;
|
|
justify-content: space-between;
|
|
gap: var(--space-2);
|
|
padding: var(--space-2);
|
|
text-align: start;
|
|
background: var(--paper);
|
|
border: 1px solid color-mix(in srgb, var(--ink-soft) 40%, transparent);
|
|
}
|
|
|
|
.where {
|
|
color: var(--ink-soft);
|
|
text-align: end;
|
|
}
|
|
|
|
.other {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: var(--space-2);
|
|
}
|
|
|
|
.quiet {
|
|
background: none;
|
|
border: none;
|
|
padding: var(--space-1) 0;
|
|
color: var(--rose);
|
|
text-decoration: underline;
|
|
}
|
|
|
|
button:focus-visible,
|
|
input:focus-visible {
|
|
outline: 2px solid var(--rose);
|
|
outline-offset: 2px;
|
|
}
|
|
</style>
|