chadtmiller.com/group-host
atmosphere group host
0
0
Languages are typed and picked from what matches, above them those chosen, in one field
harness/e2e/flows.spec.mjs+15−4
| @@ -350,17 +350,28 @@ test("changing a group's profile", async () => { | ||
await page.getByRole('combobox', { name: 'Country' }).selectOption({ label: 'Poland' }) | ||
await page.getByRole('checkbox', { name: 'It meets online' }).check() | ||
await page.getByRole('textbox', { name: 'Topics' }).fill('hiking, , ridge walks, hiking') | ||
// Its languages, each added from a list and taken off by its ×. | ||
const add = page.getByRole('combobox', { name: 'Add a language' }) | ||
for (const language of ['Polish', 'English', 'German']) await add.selectOption({ label: language }) | ||
// Its languages: typed and picked from what matches, by Enter or by a | ||
// click, the field cleared for the next; each taken off by its button. | ||
const languages = page.getByRole('combobox', { name: 'Languages it uses' }) | ||
await languages.fill('pol') | ||
await languages.press('Enter') | ||
await expect(languages).toHaveValue('') | ||
await languages.fill('eng') | ||
await page.getByRole('option', { name: 'English' }).click() | ||
await languages.fill('Germ') | ||
await languages.press('Enter') | ||
await expect(page.getByText('Choose up to five.')).toBeVisible() | ||
await page.getByRole('button', { name: 'Remove German' }).click() | ||
// Focus comes back to the field, but its list stays shut, off the form below. | ||
await expect(languages).toBeFocused() | ||
await expect(page.getByRole('listbox', { name: 'Languages it uses' })).toHaveCount(0) | ||
const saved = page.waitForResponse((r) => r.request().method() === 'POST' && r.url().includes('/profile')) | ||
await page.locator('form[action="?/profile"]').getByRole('button', { name: 'Save' }).click() | ||
await saved | ||
// Drawn again as saved: each topic once, none blank. | ||
await expect(page.getByRole('textbox', { name: 'Topics' })).toHaveValue('hiking, ridge walks') | ||
await expect(page.getByRole('combobox', { name: 'Country' })).toHaveValue('PL') | ||
await expect(page.getByRole('button', { name: /^Remove / })).toHaveText(['×', '×']) | ||
await expect(page.getByRole('list', { name: 'Chosen' }).getByRole('button')).toHaveText([/^Polish/, /^English/]) | ||
// Its links, each added from a dialog: with what it is, or without. | ||
const addLink = async (address, title) => { | ||
await page.getByRole('button', { name: 'Add a link' }).click() | ||
web/src/lib/molecules/HandleInput.svelte+2−2
| @@ -87,9 +87,9 @@ | ||
<input bind:this={field} {id} {name} {placeholder} {autocomplete} 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={typed} onfocus={() => (open = true)} onblur={close} onkeydown={keydown} /> | ||
{#if showing} | ||
<!-- Over what is under the field, rather than pushing it down. --> | ||
<div id={list} class="handle-list" role="listbox" aria-label="Accounts"> | ||
<div id={list} class="drop-list" role="listbox" aria-label="Accounts"> | ||
{#each found as a, i (a.did)} | ||
<div id="{list}-{i}" class="handle-option" role="option" tabindex="-1" aria-selected={i === at} onmousedown={(e) => press(e, a)} onmouseenter={() => (at = i)}> | ||
<div id="{list}-{i}" class="drop-option" role="option" tabindex="-1" aria-selected={i === at} onmousedown={(e) => press(e, a)} onmouseenter={() => (at = i)}> | ||
<Avatar src={a.avatar} name={a.handle} /> | ||
<span class="who"> | ||
{#if a.displayName}<strong>{a.displayName}</strong>{/if} | ||
web/src/lib/molecules/PickMany.sveltenew+119−0
| @@ -0,0 +1,119 @@ | ||
<script lang="ts"> | ||
import { tick } from 'svelte'; | ||
/** | ||
* A field for choosing a few of many, labelled `label`: what is chosen, | ||
* each a button that takes it off, over a field to type into, with what | ||
* matches dropping down to pick from, once it is clicked, typed into | ||
* or arrowed down, never only for having focus. Picking one clears the | ||
* field and keeps the list open for the next; Enter picks, never sends | ||
* the form. | ||
* The form is sent each chosen as `name`, after a blank one, so a form | ||
* with none chosen still says so. Under it, how many it takes; at | ||
* `max`, the field goes, and the hint says why. | ||
*/ | ||
let { | ||
id, | ||
label, | ||
name, | ||
options, | ||
chosen = $bindable([]), | ||
max, | ||
placeholder, | ||
most | ||
}: { | ||
id: string; | ||
label: string; | ||
name: string; | ||
options: { value: string; name: string }[]; | ||
chosen?: string[]; | ||
max: number; | ||
placeholder: string; | ||
/** How many at most, in words, for the hint under it: "five". */ | ||
most: string; | ||
} = $props(); | ||
let field = $state<HTMLInputElement>(); | ||
let typed = $state(''); | ||
let open = $state(false); | ||
// Which one Enter picks: the first that matches, till the arrows choose another. | ||
let at = $state(0); | ||
// What was last added or taken off, said to a screen reader. | ||
let said = $state(''); | ||
const list = $derived(`${id}-list`); | ||
const nameOf = (value: string) => options.find((o) => o.value === value)?.name ?? value; | ||
const found = $derived.by(() => { | ||
const q = typed.trim().toLowerCase(); | ||
const left = options.filter((o) => !chosen.includes(o.value)); | ||
if (!q) return left; | ||
// Those that start with what is typed, then those that have it anywhere. | ||
const starts = left.filter((o) => o.name.toLowerCase().startsWith(q) || o.value.toLowerCase() === q); | ||
return [...starts, ...left.filter((o) => !starts.includes(o) && o.name.toLowerCase().includes(q))]; | ||
}); | ||
const full = $derived(chosen.length >= max); | ||
const showing = $derived(open && !full && found.length > 0); | ||
function pick(value: string) { | ||
chosen = [...chosen, value]; | ||
said = `${nameOf(value)} added.`; | ||
typed = ''; | ||
at = 0; | ||
} | ||
function drop(value: string) { | ||
chosen = chosen.filter((v) => v !== value); | ||
said = `${nameOf(value)} removed.`; | ||
// The field, not the page's top, once the button pressed is gone. | ||
tick().then(() => field?.focus()); | ||
} | ||
function keydown(e: KeyboardEvent) { | ||
if (e.key === 'Enter') { | ||
// Never sends the form from here: picks what is chosen in the list, if anything. | ||
e.preventDefault(); | ||
if (showing && found[at]) pick(found[at].value); | ||
} else if (e.key === 'ArrowDown' || e.key === 'ArrowUp') { | ||
e.preventDefault(); | ||
if (!open) return void (open = true); | ||
const n = found.length; | ||
if (n) at = e.key === 'ArrowDown' ? (at + 1) % n : (at - 1 + n) % n; | ||
} else if (e.key === 'Escape' && showing) { | ||
// Closes the list, not the dialog around it. | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
open = false; | ||
} | ||
} | ||
function press(e: MouseEvent, value: string) { | ||
// Picked on the press, before the field loses focus and the list closes. | ||
e.preventDefault(); | ||
pick(value); | ||
} | ||
// The one chosen by the arrows is kept in sight as the list scrolls. | ||
$effect(() => { | ||
if (showing) document.getElementById(`${list}-${at}`)?.scrollIntoView({ block: 'nearest' }); | ||
}); | ||
</script> | ||
| ||
<div class="field" role="group" aria-labelledby="{id}-label"> | ||
<label id="{id}-label" for={id}>{label}</label> | ||
<div class="pick-many"> | ||
<input type="hidden" {name} value="" /> | ||
{#each chosen as value (value)}<input type="hidden" {name} {value} />{/each} | ||
{#if chosen.length} | ||
<ul class="chosen" aria-label="Chosen"> | ||
{#each chosen as value (value)} | ||
<li><button class="chip" type="button" aria-label="Remove {nameOf(value)}" onclick={() => drop(value)}>{nameOf(value)}<span aria-hidden="true">×</span></button></li> | ||
{/each} | ||
</ul> | ||
{/if} | ||
{#if !full} | ||
<input bind:this={field} {id} {placeholder} autocomplete="off" role="combobox" aria-expanded={showing} aria-controls={list} aria-autocomplete="list" aria-describedby="{id}-hint" aria-activedescendant={showing ? `${list}-${at}` : undefined} bind:value={typed} oninput={() => ((open = true), (at = 0))} onclick={() => (open = true)} onblur={() => (open = false)} onkeydown={keydown} /> | ||
{/if} | ||
{#if showing} | ||
<!-- Over what is under the field, rather than pushing it down. --> | ||
<div id={list} class="drop-list" role="listbox" aria-label={label}> | ||
{#each found as o, i (o.value)} | ||
<div id="{list}-{i}" class="drop-option" role="option" tabindex="-1" aria-selected={i === at} onmousedown={(e) => press(e, o.value)} onmouseenter={() => (at = i)}>{o.name}</div> | ||
{/each} | ||
</div> | ||
{/if} | ||
</div> | ||
<span class="mute small" id="{id}-hint">{full ? `That's ${most}, the most it can have. Remove one to add another.` : `Choose up to ${most}.`}</span> | ||
<span class="sr-only" aria-live="polite">{said}</span> | ||
</div> | ||
web/src/lib/organisms/ProfileForm.svelte+4−22
| @@ -1,4 +1,5 @@ | ||
<script lang="ts"> | ||
import PickMany from '#lib/molecules/PickMany.svelte'; | ||
import { ADDRESS, COUNTRIES, LANGUAGES, languageName, type Place } from '#lib/places.ts'; | ||
/** | ||
* A group's profile, to change: its name, its words, where it meets | ||
| @@ -39,12 +40,8 @@ | ||
$effect.pre(() => { | ||
spoken = languages.map((l) => l.language); | ||
}); | ||
const left = $derived(LANGUAGES.filter((l) => !spoken.includes(l.tag))); | ||
function add(event: Event & { currentTarget: HTMLSelectElement }) { | ||
const tag = event.currentTarget.value; | ||
if (tag) spoken = [...spoken, tag]; | ||
event.currentTarget.value = ''; | ||
} | ||
// Those offered, and any it has that are not, by name. | ||
const choices = $derived([...LANGUAGES.map((l) => ({ value: l.tag, name: l.name })), ...languages.filter((l) => !LANGUAGES.some((o) => o.tag === l.language)).map((l) => ({ value: l.language, name: languageName(l.language) }))]); | ||
</script> | ||
| ||
<form class="stack" method="post" action="?/profile"> | ||
| @@ -67,22 +64,7 @@ | ||
<input name="topics" maxlength="700" value={topics.map((t) => t.topic).join(', ')} aria-describedby="topics-hint" /> | ||
<span class="mute small" id="topics-hint">Separate up to ten with commas, like climbing, choir or renters' union.</span> | ||
</label> | ||
<div class="field" role="group" aria-labelledby="languages-label"> | ||
<span id="languages-label">Languages it uses</span> | ||
<input type="hidden" name="language" value="" /> | ||
<div class="choices"> | ||
{#each spoken as tag (tag)} | ||
<input type="hidden" name="language" value={tag} /> | ||
<span class="chip">{languageName(tag)}<button class="quiet" type="button" aria-label={`Remove ${languageName(tag)}`} onclick={() => (spoken = spoken.filter((t) => t !== tag))}>×</button></span> | ||
{/each} | ||
{#if spoken.length < 5} | ||
<select aria-label="Add a language" onchange={add}> | ||
<option value="">Add a language</option> | ||
{#each left as l (l.tag)}<option value={l.tag}>{l.name}</option>{/each} | ||
</select> | ||
{/if} | ||
</div> | ||
</div> | ||
<PickMany id="languages" label="Languages it uses" name="language" options={choices} bind:chosen={spoken} max={5} most="five" placeholder="Type a language" /> | ||
<label class="field"> | ||
How people join | ||
<select name="joinPolicy" value={policy}> | ||
web/src/app.css+19−10
| @@ -177,12 +177,12 @@ span.av::before{content:attr(data-initial)} | ||
its edge. */ | ||
.handle-input{position:relative} | ||
.dialog:has(.handle-input){overflow:visible} | ||
.handle-list{position:absolute;z-index:1;top:calc(100% + var(--space-1));left:0;right:0;padding:var(--space-1);background:var(--panel);border:var(--border);border-radius:var(--radius-md);box-shadow:var(--shadow-pop);font-weight:var(--weight-normal)} | ||
.handle-option{display:flex;align-items:center;gap:var(--space-3);padding:var(--space-2) var(--space-3);border-radius:var(--radius-sm);cursor:pointer} | ||
.handle-option[aria-selected='true']{background:var(--soft)} | ||
.handle-option .av{width:var(--avatar-pick);height:var(--avatar-pick)} | ||
.handle-option .who{display:flex;flex-direction:column;min-width:0;font-size:var(--text-sm);line-height:var(--leading-tight);gap:var(--space-1)} | ||
.handle-option .who > *{overflow:hidden;text-overflow:ellipsis;white-space:nowrap} | ||
.drop-list{position:absolute;z-index:1;top:calc(100% + var(--space-1));left:0;right:0;max-height:var(--drop-list);overflow-y:auto;padding:var(--space-1);background:var(--panel);border:var(--border);border-radius:var(--radius-md);box-shadow:var(--shadow-pop);font-weight:var(--weight-normal)} | ||
.drop-option{display:flex;align-items:center;gap:var(--space-3);padding:var(--space-2) var(--space-3);border-radius:var(--radius-sm);cursor:pointer} | ||
.drop-option[aria-selected='true']{background:var(--soft)} | ||
.handle-input .drop-option .av{width:var(--avatar-pick);height:var(--avatar-pick)} | ||
.handle-input .drop-option .who{display:flex;flex-direction:column;min-width:0;font-size:var(--text-sm);line-height:var(--leading-tight);gap:var(--space-1)} | ||
.handle-input .drop-option .who > *{overflow:hidden;text-overflow:ellipsis;white-space:nowrap} | ||
| ||
/* ── Templates ────────────────────────────────────────────────────── */ | ||
| ||
| @@ -253,10 +253,19 @@ code.key,.activity .mono{overflow-wrap:anywhere;min-width:0} | ||
.pair > *{min-width:0} | ||
.pair :is(input,select){width:100%} | ||
@media (max-width: 640px){.pair{grid-template-columns:1fr}} | ||
/* What has been chosen of a few, each taken off by its ×, beside a list to add another. */ | ||
.chip{display:inline-flex;align-items:center;gap:var(--space-1);min-height:var(--control-sm);padding:0 var(--space-1) 0 var(--space-3);border:var(--border);border-radius:var(--radius-pill);background:var(--soft);font-size:var(--text-sm)} | ||
.chip button{min-height:0;width:var(--space-6);height:var(--space-6);padding:0;border:0;border-radius:50%;background:transparent;color:var(--mute);font-weight:var(--weight-normal)} | ||
.chip button:hover{background:var(--line);color:var(--fg)} | ||
/* Choosing a few of many: what is chosen, each a button that takes it off, | ||
over a field to type into, all in one box that looks like a field. */ | ||
.pick-many{position:relative;display:flex;flex-direction:column;gap:var(--space-1);padding:var(--space-1);background:var(--panel);border:1px solid var(--line-strong);border-radius:var(--radius-sm);font-weight:var(--weight-normal);transition:border-color var(--speed),box-shadow var(--speed)} | ||
.pick-many:focus-within{border-color:var(--primary);box-shadow:0 0 0 var(--ring) var(--primary-soft)} | ||
.pick-many > input:not([type='hidden']){width:100%;min-height:calc(var(--control) - var(--space-2) - 2px);padding:0 var(--space-2);border:0;box-shadow:none;background:transparent} | ||
.pick-many .drop-list{left:-1px;right:-1px} | ||
.pick-many > ul.chosen:last-child{padding-bottom:var(--space-1)} | ||
ul.chosen[class]{display:flex;flex-wrap:wrap;gap:var(--space-2);padding:var(--space-1) var(--space-2) 0} | ||
ul.chosen[class] > li{padding:0;border:0} | ||
.chip{display:inline-flex;align-items:center;gap:var(--space-2);min-height:var(--control-sm);padding:0 var(--space-3);border:var(--border);border-radius:var(--radius-pill);background:var(--soft);color:var(--fg);font-size:var(--text-sm);font-weight:var(--weight-medium)} | ||
.chip:hover{background:var(--line);border-color:var(--line-strong)} | ||
.chip span{color:var(--fg);font-size:var(--text-base);line-height:1} | ||
.sr-only{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip-path:inset(50%);white-space:nowrap;border:0} | ||
/* What a group is about, where it meets and what it speaks, under its words. */ | ||
.facts{font-size:var(--text-sm);color:var(--mute)} | ||
/* What a group is about: tags, set apart from the roles people hold. */ | ||
web/src/tokens.css+1−0
| @@ -183,6 +183,7 @@ | ||
--dialog: 26rem; | ||
/* A menu dropped from the header. */ | ||
--menu: 15rem; | ||
--drop-list: 16rem; | ||
/* A line of prose at its longest, set larger than the body. */ | ||
--measure: 32rem; | ||
--ring: 3px; | ||