{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "settings-page",
  "title": "Settings page",
  "description": "A page pattern: a whole settings page: a header with the light and dark toggle, a profile form the browser validates, and a danger zone that confirms before it deletes.",
  "registryDependencies": [
    "@ouro/button",
    "@ouro/card",
    "@ouro/color-scheme-toggle",
    "@ouro/confirm-dialog",
    "@ouro/form",
    "@ouro/lib-form-answer",
    "@ouro/page-header",
    "@ouro/select",
    "@ouro/stack",
    "@ouro/switch",
    "@ouro/text",
    "@ouro/text-field"
  ],
  "files": [
    {
      "path": "registry/ouro/patterns/settings-page/settings-page.tsx",
      "content": "\"use client\";\n\nimport { useRef, useState } from \"react\";\nimport { Button } from \"@/registry/ouro/components/ouro/button/button\";\nimport { Card } from \"@/registry/ouro/components/ouro/card/card\";\nimport { ColorSchemeToggle } from \"@/registry/ouro/components/ouro/color-scheme-toggle/color-scheme-toggle\";\nimport { ConfirmDialog } from \"@/registry/ouro/components/ouro/dialog/confirm-dialog\";\nimport { Form } from \"@/registry/ouro/components/ouro/form/form\";\nimport { PageHeader } from \"@/registry/ouro/components/ouro/page-header/page-header\";\nimport { Select } from \"@/registry/ouro/components/ouro/select/select\";\nimport { Stack } from \"@/registry/ouro/components/ouro/stack/stack\";\nimport { Switch } from \"@/registry/ouro/components/ouro/switch/switch\";\nimport { Text } from \"@/registry/ouro/components/ouro/text/text\";\nimport { TextField } from \"@/registry/ouro/components/ouro/text-field/text-field\";\nimport { formErrors, type FormErrors } from \"@/registry/ouro/lib/ouro/form-answer\";\n\n/** What the page saves. Replace it with your app's settings. */\nexport interface Settings {\n  name: string;\n  email: string;\n  language: string;\n  notifications: boolean;\n}\n\nconst languages = [\n  { value: \"en\", label: \"English\" },\n  { value: \"es\", label: \"Español\" },\n  { value: \"fr\", label: \"Français\" },\n];\n\n/** The fields' starting values, and what a deleted account goes back to. Replace them with the person's own. */\nconst empty: Settings = { name: \"\", email: \"\", language: \"en\", notifications: true };\n\n/**\n * A whole settings page: a header with the light and dark toggle, a profile form the browser validates, and a danger\n * zone that confirms before it deletes. It draws itself from Ouro's theme, with layout classes only. Render it, then\n * change its fields and `onSave` to what your app keeps.\n *\n * What it already does, which Ouro's own tests cover, so your tests need only cover what you add: the browser's\n * required and email checks run before `onSave`; a name under 2 letters and the errors `onSave` returns show on\n * their fields; \"Settings saved.\" is announced after each save, and not for a save a later submit or a delete\n * overtook; the delete waits for the person to confirm, and a delete that fails keeps the dialog open.\n *\n * What the app wires up: the toggle needs `ColorSchemeProvider` around the app, and the script that sets light or\n * dark before the first paint, as the agent quickstart sets up: https://ui.ouroboros.bot/docs/agent-quickstart.md.\n *\n * To change it, edit this file: the `Settings` type, the `empty` values, and the fields. The parts it uses take:\n * - `TextField`: `label`, `name`, `type`, `required`, `defaultValue`, `autoComplete`, `description`.\n * - `Select`: `label`, `name`, `options` (`{ value, label }[]`), `defaultValue`.\n * - `Switch`: `label`, `name`, `defaultChecked`; the form reads it as `\"on\"` when it's on.\n * - `Form`: `errors` (field name to message) and `onSubmit`; the page passes `formErrors(answer, answer?.errors)` as `errors`.\n * - `ConfirmDialog`: `title`, `description`, `confirmLabel`, `tone`, `trigger`, and `onConfirm`, whose promise keeps it\n *   open until it settles.\n * - `ColorSchemeToggle`: `variant=\"segmented\"` shows light, dark and system side by side instead of a menu button.\n * - `Card`: `title`, `description`, `headingLevel`. `PageHeader`: `title`, `description`, `actions`. `Stack`: `gap`.\n * A component's whole API is on its page, https://ui.ouroboros.bot/docs/<item>.md.\n *\n * @claim \"What it already does, which Ouro's own\" test/patterns.test.tsx verify:testExists(test/patterns.test.tsx::SettingsPage > saves only once the browser's checks and its own pass, shows a returned error on its field, and says when it saved)\n * @claim \"What it already does, which Ouro's own\" test/patterns.test.tsx verify:testExists(test/patterns.test.tsx::SettingsPage > empties its status for a frame before a repeat save that resolves at once, so a screen reader hears it again)\n * @claim \"What it already does, which Ouro's own\" test/patterns.test.tsx verify:testExists(test/patterns.test.tsx::SettingsPage > lets a save that finishes late change nothing once a later submit has answered)\n * @claim \"What it already does, which Ouro's own\" test/patterns.test.tsx verify:testExists(test/patterns.test.tsx::SettingsPage > lets a save still running when the account is deleted change nothing on the emptied form)\n * @claim \"What it already does, which Ouro's own\" test/patterns.test.tsx verify:testExists(test/patterns.test.tsx::SettingsPage > deletes the account only once the person confirms, then starts the form over)\n * @claim \"What it already does, which Ouro's own\" test/patterns.test.tsx verify:testExists(test/patterns.test.tsx::SettingsPage > keeps a running save's answer when the delete fails, since the account is still there)\n *\n * @example\n * import { SettingsPage } from \"@/components/patterns/settings-page\";\n * <SettingsPage onSave={async (settings) => api.saveSettings(settings)} onDelete={() => api.deleteAccount()} />\n */\nexport function SettingsPage({\n  initial = empty,\n  onSave,\n  onDelete,\n}: {\n  /** The settings the form starts with. */\n  initial?: Settings;\n  /** Saves the settings; return field errors (`{ email: \"Taken.\" }`) to show them on their fields. */\n  onSave?: (settings: Settings) => FormErrors | void | Promise<FormErrors | void>;\n  /** Deletes the account, after the person confirms. */\n  onDelete?: () => void | Promise<void>;\n}) {\n  // Each submit's own answer, so the same mistake shows again after an edit.\n  const [answer, setAnswer] = useState<{ errors: FormErrors }>();\n  const [saved, setSaved] = useState(false);\n  const [form, setForm] = useState(0);\n  const frame = useRef(0);\n  // Which submit or delete came last, so a save that finishes late changes nothing.\n  const latest = useRef(0);\n  // After a delete the fields start empty, not from the deleted account's settings.\n  const [deleted, setDeleted] = useState(false);\n  const start = deleted ? empty : initial;\n\n  return (\n    <main className=\"mx-auto w-full max-w-2xl px-4 py-10\">\n      <Stack gap=\"lg\">\n        <PageHeader title=\"Settings\" description=\"Your profile and how we reach you.\" actions={<ColorSchemeToggle />} />\n        <Card title=\"Profile\" description=\"Shown to the people you work with.\" headingLevel={2}>\n          <Form\n            key={form}\n            errors={formErrors(answer, answer?.errors)}\n            onSubmit={async (event) => {\n              // The browser's own checks (required, type=\"email\") pass before this runs.\n              event.preventDefault();\n              // Cleared first, so a pending or failed save never shows the last success.\n              cancelAnimationFrame(frame.current);\n              setSaved(false);\n              const mine = ++latest.current;\n              const data = new FormData(event.currentTarget);\n              const settings: Settings = {\n                name: String(data.get(\"name\")).trim(),\n                email: String(data.get(\"email\")).trim(),\n                language: String(data.get(\"language\")),\n                notifications: data.get(\"notifications\") === \"on\",\n              };\n              const errors: FormErrors = settings.name.length < 2 ? { name: \"Use at least 2 letters.\" } : ((await onSave?.(settings)) ?? {});\n              if (mine !== latest.current) return;\n              setAnswer({ errors });\n              // A frame after the clear, so a screen reader hears each save, even one that resolves at once.\n              if (Object.keys(errors).length === 0) frame.current = requestAnimationFrame(() => setSaved(true));\n            }}\n          >\n            <Stack gap=\"md\">\n              <TextField label=\"Display name\" name=\"name\" defaultValue={start.name} required autoComplete=\"nickname\" />\n              <TextField label=\"Email\" name=\"email\" type=\"email\" defaultValue={start.email} required autoComplete=\"email\" />\n              <Select label=\"Language\" name=\"language\" defaultValue={start.language} options={languages} />\n              <Switch label=\"Email notifications\" name=\"notifications\" defaultChecked={start.notifications} />\n              <div>\n                <Button type=\"submit\">Save</Button>\n              </div>\n              <Text role=\"status\" size=\"sm\" tone=\"muted\">\n                {saved ? \"Settings saved.\" : \"\"}\n              </Text>\n            </Stack>\n          </Form>\n        </Card>\n        <Card title=\"Delete account\" description=\"This deletes your profile and settings. It can't be undone.\" headingLevel={2}>\n          <div>\n            <ConfirmDialog\n              title=\"Delete your account?\"\n              description=\"This deletes your profile and settings. It can't be undone.\"\n              confirmLabel=\"Delete account\"\n              tone=\"destructive\"\n              onConfirm={async () => {\n                await onDelete?.();\n                // Only once it's deleted, so a failed delete leaves the last save's answer.\n                latest.current++;\n                cancelAnimationFrame(frame.current);\n                setAnswer(undefined);\n                setSaved(false);\n                setDeleted(true);\n                setForm((n) => n + 1);\n              }}\n              trigger={<Button tone=\"destructive\" variant=\"soft\">Delete account</Button>}\n            />\n          </div>\n        </Card>\n      </Stack>\n    </main>\n  );\n}\n",
      "type": "registry:component",
      "target": "@components/patterns/settings-page.tsx"
    }
  ],
  "docs": "SettingsPage is in components/patterns/settings-page.tsx: render <SettingsPage /> and change it to what you are building. The comment above it says what it already does (Ouro tests that, so test only what you add), what your app wires up, and the props of the parts it uses. Docs: https://ui.ouroboros.bot/docs/patterns.md Ouro UI v1.3.341.",
  "type": "registry:block"
}