Recipes
Settings page
Two kinds of setting behave differently: toggles that take effect immediately, and fields that are a draft until you save. Make the difference obvious.
Example
Account settings
Switches apply now (with Undo); the profile form saves explicitly.
import { Button, Card, Fieldset, HStack, Input, Select, Stack, Switch, Text, ToastProvider, useToast } from '@reactloom/ui';
import { useEffect, useState } from 'react';
interface Profile {
name: string;
email: string;
timezone: string | null;
}
const saved: Profile = { name: 'Ada Lovelace', email: 'ada@acme.com', timezone: 'Europe/London' };
const timezones = ['Europe/London', 'Europe/Berlin', 'Asia/Kolkata', 'America/New_York'].map((v) => ({ value: v, label: v.replace('_', ' ') }));
/** Switches apply immediately: confirm with a toast that offers Undo. */
function Notifications() {
const toast = useToast();
const [prefs, setPrefs] = useState({ digest: true, mentions: true, product: false });
const set = (key: keyof typeof prefs, label: string) => (on: boolean) => {
setPrefs((p) => ({ ...p, [key]: on }));
toast.show({
id: `pref-${key}`,
title: `${label} turned ${on ? 'on' : 'off'}`,
duration: 6000,
action: { label: 'Undo', onAction: () => setPrefs((p) => ({ ...p, [key]: !on })) },
});
};
return (
<Card>
<Fieldset legend="Email notifications" description="Changes apply immediately.">
<Switch label="Weekly digest" checked={prefs.digest} onCheckedChange={set('digest', 'Weekly digest')} />
<Switch label="When someone mentions me" checked={prefs.mentions} onCheckedChange={set('mentions', 'Mentions')} />
<Switch label="Product updates" checked={prefs.product} onCheckedChange={set('product', 'Product updates')} />
</Fieldset>
</Card>
);
}
/** Fields are a draft until saved: show that there are unsaved changes and warn before leaving. */
function ProfileForm() {
const toast = useToast();
const [draft, setDraft] = useState<Profile>(saved);
const [current, setCurrent] = useState<Profile>(saved);
const [error, setError] = useState<string | undefined>();
const dirty = JSON.stringify(draft) !== JSON.stringify(current);
useEffect(() => {
if (!dirty) return;
const warn = (e: BeforeUnloadEvent) => e.preventDefault();
window.addEventListener('beforeunload', warn);
return () => window.removeEventListener('beforeunload', warn);
}, [dirty]);
return (
<Card>
<form
noValidate
onSubmit={(e) => {
e.preventDefault();
if (!draft.name.trim()) {
setError('Enter your name.');
e.currentTarget.querySelector<HTMLInputElement>('input[name="name"]')?.focus();
return;
}
setError(undefined);
setCurrent(draft);
toast.show({ title: 'Profile saved', tone: 'success' });
}}
>
<Stack gap="4">
<Fieldset legend="Profile">
<Input label="Name" name="name" value={draft.name} onChange={(e) => setDraft({ ...draft, name: e.target.value })} error={error} />
<Input label="Email" name="email" type="email" value={draft.email} onChange={(e) => setDraft({ ...draft, email: e.target.value })} />
<Select label="Time zone" name="timezone" options={timezones} value={draft.timezone} onValueChange={(v) => setDraft({ ...draft, timezone: v })} />
</Fieldset>
<HStack gap="2" align="center">
{/* Never disabled: saving with no changes just says so. */}
<Button type="submit">Save changes</Button>
<Button type="button" variant="secondary" onClick={() => { setDraft(current); setError(undefined); }}>
Discard
</Button>
<Text size="sm" tone="muted" role="status">
{dirty ? 'Unsaved changes' : ''}
</Text>
</HStack>
</Stack>
</form>
</Card>
);
}
export default function Example() {
return (
<ToastProvider>
<Stack gap="4" style={{ width: '100%', maxWidth: 520 }}>
<Notifications />
<ProfileForm />
</Stack>
</ToastProvider>
);
}Details that matter
- Immediate settings (Switch) need no Save button. Confirm with a toast that offers Undo — the toast is announced, and a mistaken toggle is one click to reverse.
- Drafted settings (text, selects) are saved together. Say when there are unsaved changes, offer Discard, and warn before the page is closed (
beforeunload) only while changes are pending. - Do not mix the two in one form: a switch inside a Save form is ambiguous about when it applies.
- Never disable Save: validate on submit and focus the first problem (see Forms & validation).