chadtmiller.com/group-host
atmosphere group host
0
0
A form that edits in place says it is changed, saving, saved, or why not
.claude/skills/ui/SKILL.md+16−1
| @@ -182,7 +182,22 @@ controls. | ||
inside it; elsewhere the page's notice says why, scrolled to. Never | ||
a plain post that reloads the page at its top. Only a form that goes | ||
somewhere else (signing out, starting a group, approving an app) may | ||
post plainly. | ||
post plainly. While it is on its way, the button that sent it is held | ||
(disabled, `aria-busy`), so it is never sent twice. | ||
- _Saving._ A form that edits what is there in place (a group's profile, | ||
who sees it) is a `Saving` (`#lib/saving.svelte.ts`), sent by | ||
`saving.submit` and watched by `use:saving.track`, and ends in | ||
`SaveBar`: Save, and beside it what has happened, in words a screen | ||
reader is told ("Unsaved changes", "Saving…", "Saved", or why not). | ||
Save is quiet until something has changed, then the one that acts: | ||
the one place a view may hold two primary buttons is two such forms | ||
both changed. It is never disabled, so it can always be reached. A refusal stays beside | ||
Save until it saves. Leaving the page with changes unsaved asks first | ||
(`LeaveGuard`): "Leave without saving?", its quiet button "Keep | ||
editing", as Cancel there could mean throwing the changes away. An act whose result shows on the page (a link added, a | ||
request approved) says nothing more: the page drawn again is the | ||
answer. No toasts. They show far from what was pressed, and go before | ||
they are read. | ||
- _Confirming._ An act that cannot be undone (leaving, removing | ||
someone, deleting a rule or role, removing a key, revoking, | ||
disconnecting) opens a `ConfirmDialog`: its title the question | ||
harness/e2e/flows.spec.mjs+38−15
| @@ -352,11 +352,9 @@ test('joining a group anyone can join', async () => { | ||
const settings = url('/g/hikers.groups.test/settings'); | ||
await page.goto(settings); | ||
await page.getByRole('combobox', { name: 'How people join' }).selectOption('open'); | ||
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; | ||
const profile = page.locator('form[action="?/profile"]'); | ||
await profile.getByRole('button', { name: 'Save' }).click(); | ||
await expect(profile.getByRole('status')).toHaveText('Saved'); | ||
// Bob, removed, joins it at once. | ||
await bob.goto(url('/g/hikers.groups.test')); | ||
await expect(bob.getByRole('heading', { name: 'Join Hikers' })).toBeVisible(); | ||
| @@ -428,11 +426,16 @@ test("changing a group's profile", async () => { | ||
// 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; | ||
// Changed, it says so beside Save, and Save is the thing to press. | ||
const profile = page.locator('form[action="?/profile"]'); | ||
const said = profile.getByRole('status'); | ||
const save = profile.getByRole('button', { name: 'Save' }); | ||
await expect(said).toHaveText('Unsaved changes'); | ||
await expect(save).not.toHaveClass(/quiet/); | ||
await save.click(); | ||
// Saved, it says that, until something is changed again. | ||
await expect(said).toHaveText('Saved'); | ||
await expect(save).toHaveClass(/quiet/); | ||
// 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'); | ||
| @@ -492,6 +495,28 @@ const PIXEL = Buffer.from( | ||
'base64' | ||
); | ||
| ||
test('being asked before leaving changes unsaved', async () => { | ||
const settings = url('/g/hikers.groups.test/settings'); | ||
await page.goto(settings); | ||
const name = page.getByRole('textbox', { name: 'Name', exact: true }); | ||
const was = await name.inputValue(); | ||
await name.fill('Not saved'); | ||
// Going to another page asks first, and staying keeps what was typed. | ||
await page.getByRole('link', { name: 'Rules' }).click(); | ||
const ask = page.getByRole('dialog', { name: 'Leave without saving?' }); | ||
await ask.getByRole('button', { name: 'Keep editing' }).click(); | ||
await expect(name).toHaveValue('Not saved'); | ||
// Put back as it was, there is nothing to ask about. | ||
await name.fill(was); | ||
await expect(page.locator('form[action="?/profile"]').getByRole('status')).toHaveText(''); | ||
await name.fill('Not saved'); | ||
await page.getByRole('link', { name: 'Rules' }).click(); | ||
await ask.getByRole('button', { name: 'Leave' }).click(); | ||
await expect(page).toHaveURL(/\/settings\/rules$/); | ||
await page.goto(settings); | ||
await expect(name).toHaveValue(was); | ||
}); | ||
| ||
test('giving a group a picture, and taking it away', async () => { | ||
await page.goto(url('/g/hikers.groups.test/settings')); | ||
await page.getByRole('button', { name: 'Add the avatar' }).click(); | ||
| @@ -536,11 +561,9 @@ test("hiding a group's profile, and showing it again", async () => { | ||
const save = async (label) => { | ||
await page.goto(settings); | ||
await page.getByLabel(label).check(); | ||
const saved = page.waitForResponse( | ||
(r) => r.request().method() === 'POST' && r.url().includes('/visibility') | ||
); | ||
await page.locator('form[action="?/visibility"]').getByRole('button', { name: 'Save' }).click(); | ||
await saved; | ||
const seen = page.locator('form[action="?/visibility"]'); | ||
await seen.getByRole('button', { name: 'Save' }).click(); | ||
await expect(seen.getByRole('status')).toHaveText('Saved'); | ||
await page.goto(settings); | ||
await expect(page.getByLabel(label)).toBeChecked(); | ||
}; | ||
web/src/lib/molecules/LeaveGuard.sveltenew+43−0
| @@ -0,0 +1,43 @@ | ||
<script lang="ts"> | ||
import { beforeNavigate, goto } from '$app/navigation'; | ||
import Dialog from '#lib/molecules/Dialog.svelte'; | ||
import { anyUnsaved } from '#lib/saving.svelte.ts'; | ||
/** | ||
* Asking before the reader leaves a page holding changes not saved yet. | ||
* Going elsewhere in the console, a dialog asks; closing the tab or | ||
* leaving the site, the browser asks in its own words, as only it may. | ||
* Its quiet button is "Keep editing", not Cancel: here Cancel could as | ||
* well mean throwing the changes away. | ||
*/ | ||
let going = $state<URL | null>(null); | ||
let leaving = false; | ||
beforeNavigate(({ type, to, cancel }) => { | ||
if (leaving || !anyUnsaved()) return; | ||
// The browser's own question, for a tab closed or reloaded. | ||
if (type === 'leave') return cancel(); | ||
// A form sent is the page saving, not left behind. | ||
if (type === 'form' || !to?.url) return; | ||
cancel(); | ||
going = to.url; | ||
document.getElementById('leave-unsaved')?.showPopover(); | ||
}); | ||
async function leave() { | ||
const to = going; | ||
if (!to) return; | ||
leaving = true; | ||
document.getElementById('leave-unsaved')?.hidePopover(); | ||
if (to.origin === location.origin) await goto(to); | ||
else location.href = to.href; | ||
leaving = false; | ||
} | ||
</script> | ||
| ||
<Dialog id="leave-unsaved" title="Leave without saving?" onclose={() => (going = null)}> | ||
<p class="mute">You’ll lose the changes you haven’t saved.</p> | ||
<div class="actions"> | ||
<button class="quiet" type="button" popovertarget="leave-unsaved" popovertargetaction="hide" | ||
>Keep editing</button | ||
> | ||
<button class="danger" type="button" onclick={leave}>Leave</button> | ||
</div> | ||
</Dialog> | ||
web/src/lib/molecules/SaveBar.sveltenew+32−0
| @@ -0,0 +1,32 @@ | ||
<script lang="ts"> | ||
import type { Saving } from '#lib/saving.svelte.ts'; | ||
/** | ||
* A form's Save button, the end of a form that edits what is there in | ||
* place, and beside it where the form stands: changes not saved yet, | ||
* saving, saved, or why it was not. The button acts, in the one colour | ||
* that acts, only once there is something to save; it is never | ||
* disabled, so it can always be reached. `what` names what it saves to | ||
* a screen reader, as a page may have two. | ||
*/ | ||
let { saving, what }: { saving: Saving; what: string } = $props(); | ||
const said = $derived( | ||
{ | ||
clean: '', | ||
changed: 'Unsaved changes', | ||
saving: 'Saving…', | ||
saved: 'Saved', | ||
failed: saving.why ?? '' | ||
}[saving.standing] | ||
); | ||
</script> | ||
| ||
<div class="save-bar"> | ||
<button class:quiet={!saving.changed} type="submit" aria-label="Save {what}">Save</button> | ||
<span | ||
class="save-said" | ||
class:mute={saving.standing !== 'failed' && saving.standing !== 'saved'} | ||
class:danger={saving.standing === 'failed'} | ||
class:success={saving.standing === 'saved'} | ||
role="status">{said}</span | ||
> | ||
</div> | ||
web/src/lib/organisms/ProfileForm.svelte+5−3
| @@ -1,7 +1,8 @@ | ||
<script lang="ts"> | ||
import { enhance } from '$app/forms'; | ||
import { inPlace } from '#lib/forms.ts'; | ||
import PickMany from '#lib/molecules/PickMany.svelte'; | ||
import SaveBar from '#lib/molecules/SaveBar.svelte'; | ||
import { Saving } from '#lib/saving.svelte.ts'; | ||
import { ADDRESS, COUNTRIES, LANGUAGES, languageName, type Place } from '#lib/places.ts'; | ||
/** | ||
* A group's profile, to change: its name, its words, where it meets | ||
| @@ -28,6 +29,7 @@ | ||
topics?: { topic: string }[]; | ||
languages?: { language: string }[]; | ||
} = $props(); | ||
const saving = new Saving(); | ||
const policy = $derived(joinPolicy ?? 'approval'); | ||
// A place other than an address (a point on a map) is kept as it is | ||
// until a town or country is given in its place. | ||
| @@ -51,7 +53,7 @@ | ||
]); | ||
</script> | ||
| ||
<form class="stack" method="post" action="?/profile" use:enhance={inPlace}> | ||
<form class="stack" method="post" action="?/profile" use:enhance={saving.submit} use:saving.track> | ||
<label class="field" | ||
>Name <input name="displayName" required maxlength="64" value={displayName ?? ''} /></label | ||
> | ||
| @@ -104,5 +106,5 @@ | ||
</select> | ||
</label> | ||
<label class="field">Its own site <input name="url" type="url" value={url ?? ''} /></label> | ||
<button type="submit">Save</button> | ||
<SaveBar {saving} what="the profile" /> | ||
</form> | ||
web/src/lib/organisms/Visibility.svelte+11−3
| @@ -1,15 +1,23 @@ | ||
<script lang="ts"> | ||
import { enhance } from '$app/forms'; | ||
import { inPlace } from '#lib/forms.ts'; | ||
import SaveBar from '#lib/molecules/SaveBar.svelte'; | ||
import { Saving } from '#lib/saving.svelte.ts'; | ||
/** | ||
* Who sees a group's profile and rules: anyone, when it is also declared | ||
* on the network for others to find, or its members alone, when it shows | ||
* elsewhere by its handle alone. `listed` is which it is. | ||
*/ | ||
let { listed }: { listed: boolean } = $props(); | ||
const saving = new Saving(); | ||
</script> | ||
| ||
<form class="stack" method="post" action="?/visibility" use:enhance={inPlace}> | ||
<form | ||
class="stack" | ||
method="post" | ||
action="?/visibility" | ||
use:enhance={saving.submit} | ||
use:saving.track | ||
> | ||
<label | ||
><input type="radio" name="seen" value="public" checked={listed} /> Anyone, and it can be found</label | ||
> | ||
| @@ -20,5 +28,5 @@ | ||
<p class="mute"> | ||
Hiding it stops new people finding it. It can't undo what people have already seen. | ||
</p> | ||
<button class="quiet" type="submit">Save</button> | ||
<SaveBar {saving} what="who sees it" /> | ||
</form> | ||
web/src/lib/forms.ts+20−2
| @@ -9,6 +9,20 @@ import { tick } from 'svelte'; | ||
/** What `use:enhance` is given: what a form does as it is sent. */ | ||
type SubmitFunction = NonNullable<Parameters<typeof enhance>[1]>; | ||
| ||
/** | ||
* The button that sent a form, held down while it is on its way: it | ||
* cannot be pressed twice, and says it is busy. Returns what lets it go. | ||
*/ | ||
export function hold(submitter: HTMLElement | null) { | ||
if (!(submitter instanceof HTMLButtonElement)) return () => {}; | ||
submitter.disabled = true; | ||
submitter.setAttribute('aria-busy', 'true'); | ||
return () => { | ||
submitter.disabled = false; | ||
submitter.removeAttribute('aria-busy'); | ||
}; | ||
} | ||
| ||
/** Scrolls to `el`, gently unless the reader asks for less motion. */ | ||
function show(el: Element | null) { | ||
const still = matchMedia('(prefers-reduced-motion: reduce)').matches; | ||
| @@ -43,9 +57,11 @@ export function empty(form: HTMLFormElement) { | ||
* reader is, what they typed kept. Refused, the page's notice says why, | ||
* scrolled to; sent elsewhere (a group just made), the page goes there. | ||
*/ | ||
export const inPlace: SubmitFunction = () => { | ||
export const inPlace: SubmitFunction = ({ submitter }) => { | ||
const done = hold(submitter); | ||
return async ({ result, update }) => { | ||
await update({ reset: false }); | ||
done(); | ||
if (result.type !== 'failure') return; | ||
await tick(); | ||
show(document.querySelector('main p.danger')); | ||
| @@ -59,9 +75,11 @@ export const inPlace: SubmitFunction = () => { | ||
* to say in the dialog. | ||
*/ | ||
export function inDialog(id: string, failed: (why: string | null) => void): SubmitFunction { | ||
return () => { | ||
return ({ submitter }) => { | ||
failed(null); | ||
const done = hold(submitter); | ||
return async ({ result, update }) => { | ||
done(); | ||
if (result.type === 'failure') { | ||
const why = (result.data as { failed?: string } | undefined)?.failed; | ||
failed(why ?? 'That didn’t work. Check what you entered.'); | ||
web/src/lib/saving.svelte.tsnew+92−0
| @@ -0,0 +1,92 @@ | ||
// A form that edits what is there in place (a group's profile, who sees | ||
// it), saved by its one Save button: where it stands, said beside that | ||
// button by `SaveBar`, and whether anything in it is not yet saved, which | ||
// `LeaveGuard` asks about before the reader goes. | ||
| ||
import type { enhance } from '$app/forms'; | ||
import { hold } from '#lib/forms.ts'; | ||
import { tick } from 'svelte'; | ||
import { SvelteSet } from 'svelte/reactivity'; | ||
| ||
type SubmitFunction = NonNullable<Parameters<typeof enhance>[1]>; | ||
| ||
/** Where a form stands: as it was, changed, being sent, saved, or refused. */ | ||
export type Standing = 'clean' | 'changed' | 'saving' | 'saved' | 'failed'; | ||
| ||
/** The forms on the page with changes not yet saved. */ | ||
const unsaved = new SvelteSet<Saving>(); | ||
| ||
/** Whether any form on the page has changes not yet saved. */ | ||
export const anyUnsaved = () => unsaved.size > 0; | ||
| ||
/** What a form holds, as it would be sent. */ | ||
function held(form: HTMLFormElement) { | ||
return new URLSearchParams(new FormData(form) as unknown as Record<string, string>).toString(); | ||
} | ||
| ||
export class Saving { | ||
standing = $state<Standing>('clean'); | ||
/** Why it was refused, when it was. */ | ||
why = $state<string | null>(null); | ||
#form: HTMLFormElement | null = null; | ||
#was = ''; | ||
| ||
/** Whether it holds what is not saved yet. */ | ||
get changed() { | ||
return unsaved.has(this); | ||
} | ||
| ||
/** What it holds now, against what it held when last drawn or saved. */ | ||
#compare = async () => { | ||
await tick(); | ||
if (!this.#form || this.standing === 'saving') return; | ||
const now = held(this.#form) !== this.#was; | ||
if (now) unsaved.add(this); | ||
else unsaved.delete(this); | ||
if (now && this.standing !== 'failed') this.standing = 'changed'; | ||
if (!now && this.standing === 'changed') this.standing = 'clean'; | ||
}; | ||
| ||
/** `use:saving.track` on the form: what it holds now becomes what is saved. */ | ||
track = (form: HTMLFormElement) => { | ||
this.#form = form; | ||
this.#was = held(form); | ||
// A chosen language taken off is a click, not an input. | ||
for (const kind of ['input', 'change', 'click']) form.addEventListener(kind, this.#compare); | ||
return { | ||
destroy: () => { | ||
unsaved.delete(this); | ||
for (const kind of ['input', 'change', 'click']) | ||
form.removeEventListener(kind, this.#compare); | ||
} | ||
}; | ||
}; | ||
| ||
/** | ||
* `use:enhance={saving.submit}`: sent in place, as `inPlace` sends. Saved, | ||
* the page is drawn again with what was saved and the form says so; | ||
* refused, what was typed stays, and the form says why. | ||
*/ | ||
submit: SubmitFunction = ({ submitter }) => { | ||
this.standing = 'saving'; | ||
this.why = null; | ||
const done = hold(submitter); | ||
return async ({ result, update }) => { | ||
done(); | ||
if (result.type === 'failure' || result.type === 'error') { | ||
const why = | ||
result.type === 'failure' | ||
? (result.data as { failed?: string } | undefined)?.failed | ||
: result.error?.message; | ||
this.why = why ?? 'That didn’t save. Check what you entered.'; | ||
this.standing = 'failed'; | ||
return; | ||
} | ||
await update({ reset: false }); | ||
await tick(); | ||
if (this.#form) this.#was = held(this.#form); | ||
unsaved.delete(this); | ||
this.standing = 'saved'; | ||
}; | ||
}; | ||
} | ||
web/src/routes/+layout.svelte+2−0
| @@ -2,6 +2,7 @@ | ||
import '../app.css'; | ||
import SiteHeader from '#lib/organisms/SiteHeader.svelte'; | ||
import SiteFooter from '#lib/organisms/SiteFooter.svelte'; | ||
import LeaveGuard from '#lib/molecules/LeaveGuard.svelte'; | ||
import { page } from '$app/state'; | ||
let { data, children } = $props(); | ||
</script> | ||
| @@ -14,3 +15,4 @@ | ||
/> | ||
<main class="wrap">{@render children()}</main> | ||
<SiteFooter host={page.url.hostname} /> | ||
<LeaveGuard /> | ||
web/src/app.css+15−0
| @@ -389,6 +389,10 @@ button:disabled { | ||
opacity: 0.5; | ||
cursor: not-allowed; | ||
} | ||
/* A button whose form is on its way: held, and saying so. */ | ||
button[aria-busy='true'] { | ||
cursor: progress; | ||
} | ||
/* What undoes, removes or turns down: there, but never the thing to press. */ | ||
button.quiet { | ||
background: transparent; | ||
| @@ -400,6 +404,17 @@ button.quiet:hover { | ||
background: var(--soft); | ||
border-color: var(--line-strong); | ||
} | ||
/* A form that edits what is there in place ends in its Save button, | ||
and beside it where the form stands (SaveBar), on every page. */ | ||
.save-bar { | ||
display: flex; | ||
flex-wrap: wrap; | ||
align-items: center; | ||
gap: var(--space-3); | ||
} | ||
.save-said.success { | ||
color: var(--success); | ||
} | ||
form.stack > button, | ||
form.stack > .field + button { | ||
align-self: flex-start; | ||