{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "settings-page",
  "title": "Settings Page",
  "description": "Grouped settings with field validation, a sticky save state, and a guarded danger zone.",
  "dependencies": [
    "class-variance-authority"
  ],
  "registryDependencies": [
    "https://uaiblocks.dev/r/uai-theme.json",
    "https://uaiblocks.dev/r/confirmation-dialog.json",
    "https://uaiblocks.dev/r/form-field.json",
    "https://uaiblocks.dev/r/unsaved-changes-bar.json",
    "https://uaiblocks.dev/r/uai-utils.json"
  ],
  "files": [
    {
      "path": "src/registry/uai/blocks/settings-page.tsx",
      "content": "\"use client\";\n\nimport { cva } from \"class-variance-authority\";\nimport {\n  type ComponentProps,\n  createContext,\n  type RefObject,\n  useContext,\n  useEffect,\n  useId,\n  useRef,\n  useState,\n} from \"react\";\nimport {\n  ConfirmationDialog,\n  type ConfirmationDialogProps,\n  type ConfirmationDialogVariant,\n} from \"@/components/ui/uai/confirmation-dialog\";\nimport {\n  FormField,\n  type FormFieldProps,\n  type FormFieldVariant,\n} from \"@/components/ui/uai/form-field\";\nimport {\n  UnsavedChangesBar,\n  UnsavedChangesBarActions,\n  UnsavedChangesBarDiscard,\n  UnsavedChangesBarMessage,\n  type UnsavedChangesBarProps,\n  UnsavedChangesBarSave,\n  type UnsavedChangesBarVariant,\n} from \"@/components/ui/uai/unsaved-changes-bar\";\nimport { cn } from \"@/lib/uai-utils\";\n\nexport const SETTINGS_PAGE_VARIANTS = [\"stacked\", \"split\", \"compact\"] as const;\nexport type SettingsPageVariant = (typeof SETTINGS_PAGE_VARIANTS)[number];\nexport type SettingsPageProps = ComponentProps<\"section\"> & { variant?: SettingsPageVariant };\n\ntype PageContext = {\n  id: string;\n  variant: SettingsPageVariant;\n  rootRef: RefObject<HTMLElement | null>;\n};\nconst Context = createContext<PageContext | null>(null);\nfunction usePage(part: string) {\n  const context = useContext(Context);\n  if (!context) throw new Error(`${part} must be used within SettingsPage`);\n  return context;\n}\ntype SectionContext = { id: string; tone: \"default\" | \"danger\" };\nconst Section = createContext<SectionContext | null>(null);\nfunction useSection(part: string) {\n  const context = useContext(Section);\n  if (!context) throw new Error(`${part} must be used within SettingsPageSection`);\n  return context;\n}\n\nconst fieldVariants: Record<SettingsPageVariant, FormFieldVariant> = {\n  stacked: \"outlined\",\n  split: \"outlined\",\n  compact: \"compact\",\n};\nconst barVariants: Record<SettingsPageVariant, UnsavedChangesBarVariant> = {\n  stacked: \"floating\",\n  split: \"bar\",\n  compact: \"compact\",\n};\nconst dialogVariants: Record<SettingsPageVariant, ConfirmationDialogVariant> = {\n  stacked: \"centered\",\n  split: \"centered\",\n  compact: \"compact\",\n};\n\nconst settingsPageVariants = cva(\n  \"@container grid w-full max-w-[820px] min-w-0 content-start text-[13px]/[18px] text-foreground\",\n  {\n    variants: {\n      variant: {\n        stacked: \"gap-4.5\",\n        split: \"gap-6\",\n        compact: \"gap-3.5\",\n      },\n    },\n  },\n);\n\nexport function SettingsPage({ variant = \"stacked\", className, ...props }: SettingsPageProps) {\n  const id = useId();\n  const rootRef = useRef<HTMLElement>(null);\n  return (\n    <Context.Provider value={{ id, variant, rootRef }}>\n      <section\n        ref={rootRef}\n        aria-labelledby={`${id}-title`}\n        data-slot=\"settings-page\"\n        data-variant={variant}\n        className={cn(settingsPageVariants({ variant }), className)}\n        {...props}\n      />\n    </Context.Provider>\n  );\n}\n\nexport function SettingsPageHeader({ className, ...props }: ComponentProps<\"div\">) {\n  usePage(\"SettingsPageHeader\");\n  return (\n    <div\n      data-slot=\"settings-page-header\"\n      className={cn(\"grid min-w-0 gap-1\", className)}\n      {...props}\n    />\n  );\n}\n\nexport function SettingsPageTitle({ className, ...props }: ComponentProps<\"h2\">) {\n  const context = usePage(\"SettingsPageTitle\");\n  return (\n    <h2\n      data-slot=\"settings-page-title\"\n      className={cn(\n        \"m-0 font-semibold tracking-[-0.01em]\",\n        context.variant === \"compact\" ? \"text-[15px]/5\" : \"text-lg/6\",\n        className,\n      )}\n      {...props}\n      id={`${context.id}-title`}\n    />\n  );\n}\n\nexport function SettingsPageDescription({ className, ...props }: ComponentProps<\"p\">) {\n  return (\n    <p\n      data-slot=\"settings-page-description\"\n      className={cn(\"m-0 text-pretty text-muted-foreground\", className)}\n      {...props}\n    />\n  );\n}\n\n/** A group of related settings. `tone=\"danger\"` marks destructive actions. */\nexport function SettingsPageSection({\n  tone = \"default\",\n  className,\n  ...props\n}: ComponentProps<\"section\"> & { tone?: \"default\" | \"danger\" }) {\n  const { variant } = usePage(\"SettingsPageSection\");\n  const id = useId();\n  return (\n    <Section.Provider value={{ id, tone }}>\n      <section\n        aria-labelledby={`${id}-title`}\n        aria-describedby={`${id}-description`}\n        data-slot=\"settings-page-section\"\n        className={cn(\n          \"grid min-w-0 content-start\",\n          variant === \"split\"\n            ? \"items-start gap-x-7 gap-y-2.5 @[600px]:grid-cols-[220px_minmax(0,1fr)]\"\n            : \"gap-2.5\",\n          className,\n        )}\n        {...props}\n        data-tone={tone}\n      />\n    </Section.Provider>\n  );\n}\n\nexport function SettingsPageSectionHeader({ className, ...props }: ComponentProps<\"div\">) {\n  const { variant } = usePage(\"SettingsPageSectionHeader\");\n  useSection(\"SettingsPageSectionHeader\");\n  return (\n    <div\n      data-slot=\"settings-page-section-header\"\n      className={cn(\n        \"min-w-0\",\n        variant === \"split\"\n          ? \"grid content-start gap-1 @[600px]:pt-0.5\"\n          : \"flex flex-wrap items-baseline gap-x-2.5 gap-y-0.5\",\n        className,\n      )}\n      {...props}\n    />\n  );\n}\n\nexport function SettingsPageSectionTitle({ className, ...props }: ComponentProps<\"h3\">) {\n  const section = useSection(\"SettingsPageSectionTitle\");\n  return (\n    <h3\n      data-slot=\"settings-page-section-title\"\n      className={cn(\"m-0 text-sm/5 font-medium\", className)}\n      {...props}\n      id={`${section.id}-title`}\n    />\n  );\n}\n\nexport function SettingsPageSectionDescription({ className, ...props }: ComponentProps<\"p\">) {\n  const section = useSection(\"SettingsPageSectionDescription\");\n  return (\n    <p\n      data-slot=\"settings-page-section-description\"\n      className={cn(\"m-0 text-[12.5px]/[18px] text-pretty text-muted-foreground\", className)}\n      {...props}\n      id={`${section.id}-description`}\n    />\n  );\n}\n\nconst rowPadding: Record<SettingsPageVariant, string> = {\n  stacked: \"px-4 py-3.5\",\n  split: \"px-4 py-3.5\",\n  compact: \"px-3 py-2.5\",\n};\n\n/**\n * The section card. Each child is a row, separated by hairlines. In a danger section the\n * card holds one row: the consequence (a `<strong>` and a `<p>`) and the confirm trigger.\n */\nexport function SettingsPageSectionContent({ className, ...props }: ComponentProps<\"div\">) {\n  const { variant } = usePage(\"SettingsPageSectionContent\");\n  const section = useSection(\"SettingsPageSectionContent\");\n  const danger = section.tone === \"danger\";\n  return (\n    <div\n      data-slot=\"settings-page-section-content\"\n      className={cn(\n        \"min-w-0 bg-card text-card-foreground shadow-[0_0_0_1px_var(--border)]\",\n        variant === \"compact\" ? \"rounded-xl\" : \"rounded-[14px]\",\n        danger\n          ? cn(\n              \"flex flex-wrap items-center gap-x-4 gap-y-3\",\n              rowPadding[variant],\n              \"[&>div:first-child]:min-w-0 [&>div:first-child]:flex-[1_1_240px] [&_strong]:font-medium\",\n              \"[&_p]:m-0 [&_p]:mt-0.5 [&_p]:text-[12.5px]/[18px] [&_p]:text-pretty [&_p]:text-muted-foreground\",\n              // Only the button carries danger: a 12% tint with ink text, 18% on hover.\n              \"[&_[data-slot=confirmation-dialog-trigger]]:h-7.5 [&_[data-slot=confirmation-dialog-trigger]]:px-3.5 [&_[data-slot=confirmation-dialog-trigger]]:text-[12.5px]/[18px] [&_[data-slot=confirmation-dialog-trigger]]:text-destructive-ink [&_[data-slot=confirmation-dialog-trigger]]:hover:text-destructive-ink\",\n            )\n          : \"grid divide-y divide-solid divide-border\",\n        className,\n      )}\n      {...props}\n      data-tone={section.tone}\n    />\n  );\n}\n\ntype FieldContext = { edited: boolean };\nconst Field = createContext<FieldContext | null>(null);\n\nconst fieldRow = cn(\n  \"min-w-0 gap-1.5\",\n  // Required is the default, so only optional fields carry a marker.\n  \"[&_[data-slot=form-field-label]]:flex [&_[data-slot=form-field-label]]:items-center [&_[data-slot=form-field-label]]:gap-2\",\n  \"[&_[data-slot=form-field-label]>span:not([data-slot])]:hidden\",\n  // The error names the fix and carries its icon.\n  \"[&_[data-slot=form-field-error]]:flex [&_[data-slot=form-field-error]]:items-center [&_[data-slot=form-field-error]]:gap-1.5 [&_[data-slot=form-field-error]_svg]:size-3.25 [&_[data-slot=form-field-error]_svg]:shrink-0\",\n  // A disabled row dims as a whole; the hint says why.\n  \"has-[[data-slot=form-field-input]:disabled]:cursor-not-allowed has-[[data-slot=form-field-input]:disabled]:opacity-55\",\n  \"[&_[data-slot=form-field-input]]:disabled:opacity-100\",\n  // Pressing Save with an invalid field shakes it once.\n  \"[&_[data-slot=form-field-input]]:data-shake:motion-safe:animate-shake\",\n);\nconst fieldInput: Record<SettingsPageVariant, string> = {\n  stacked: cn(\n    \"[&_[data-slot=form-field-input]]:h-9 [&_[data-slot=form-field-input]]:border-transparent [&_[data-slot=form-field-input]]:px-2.75 [&_[data-slot=form-field-input]]:py-0\",\n    \"[&_[data-slot=form-field-input]]:focus-visible:border-border-strong [&_[data-slot=form-field-input]]:focus-visible:ring-primary/18\",\n    \"[&_[data-slot=form-field-input]]:aria-invalid:border-destructive\",\n  ),\n  split: cn(\n    \"[&_[data-slot=form-field-input]]:h-9 [&_[data-slot=form-field-input]]:border-transparent [&_[data-slot=form-field-input]]:px-2.75 [&_[data-slot=form-field-input]]:py-0\",\n    \"[&_[data-slot=form-field-input]]:focus-visible:border-border-strong [&_[data-slot=form-field-input]]:focus-visible:ring-primary/18\",\n    \"[&_[data-slot=form-field-input]]:aria-invalid:border-destructive\",\n  ),\n  compact: \"[&_[data-slot=form-field-input]]:aria-invalid:border-destructive\",\n};\nconst optionalMarker =\n  \"[&_[data-slot=form-field-label]]:after:text-xs [&_[data-slot=form-field-label]]:after:font-normal [&_[data-slot=form-field-label]]:after:text-subtle-foreground [&_[data-slot=form-field-label]]:after:content-['(optional)']\";\n\n/**\n * A Form Field sized for the page, as one row of the section card. Compose Form Field parts\n * inside it. `edited` shows the Edited marker (`SettingsPageFieldEdited`) while the value\n * differs from the saved one.\n */\nexport function SettingsPageField({\n  edited = false,\n  className,\n  ...props\n}: Omit<FormFieldProps, \"variant\"> & { edited?: boolean }) {\n  const context = usePage(\"SettingsPageField\");\n  return (\n    <Field.Provider value={{ edited }}>\n      <FormField\n        {...props}\n        data-edited={edited || undefined}\n        className={cn(\n          fieldRow,\n          rowPadding[context.variant],\n          fieldInput[context.variant],\n          !props.required && optionalMarker,\n          className,\n        )}\n        variant={fieldVariants[context.variant]}\n      />\n    </Field.Provider>\n  );\n}\n\n/** \"Edited\" marker for a field label. Fades in while the field's value differs from the saved one. */\nexport function SettingsPageFieldEdited({\n  children = \"Edited\",\n  className,\n  ...props\n}: ComponentProps<\"span\">) {\n  const field = useContext(Field);\n  if (!field) throw new Error(\"SettingsPageFieldEdited must be used within SettingsPageField\");\n  return (\n    <span\n      data-slot=\"settings-page-field-edited\"\n      data-state={field.edited ? \"on\" : \"off\"}\n      aria-hidden={!field.edited || undefined}\n      className={cn(\n        \"inline-flex items-center gap-1.25 text-[11.5px]/4 font-normal text-primary-ink\",\n        \"opacity-0 transition-opacity duration-160 ease-out data-[state=on]:opacity-100 motion-reduce:transition-none\",\n        className,\n      )}\n      {...props}\n    >\n      <span aria-hidden=\"true\" className=\"size-1.5 shrink-0 rounded-full bg-primary\" />\n      {children}\n    </span>\n  );\n}\n\n/** Confirmation for a destructive setting. Compose Confirmation Dialog parts inside it. */\nexport function SettingsPageConfirm(props: Omit<ConfirmationDialogProps, \"variant\">) {\n  const context = usePage(\"SettingsPageConfirm\");\n  return <ConfirmationDialog {...props} variant={dialogVariants[context.variant]} />;\n}\n\nconst barClass: Record<SettingsPageVariant, string> = {\n  stacked:\n    \"h-11 w-fit max-w-full justify-self-center gap-2 rounded-full bg-popover py-0 pr-1.5 pl-4 [&_[data-slot=unsaved-changes-bar-message]]:flex-none\",\n  split: \"\",\n  compact: \"\",\n};\n\nfunction shake(element: HTMLElement) {\n  element.removeAttribute(\"data-shake\");\n  void element.offsetWidth;\n  element.setAttribute(\"data-shake\", \"\");\n  element.addEventListener(\"animationend\", () => element.removeAttribute(\"data-shake\"), {\n    once: true,\n  });\n}\n\n/**\n * Save state for the whole page. Renders the default message and actions when given no\n * children. Save stays enabled while a field is invalid: pressing it focuses and shakes the\n * first invalid field instead of calling `onSave`.\n */\nexport function SettingsPageSaveBar({\n  children,\n  onSave,\n  className,\n  ...props\n}: Omit<UnsavedChangesBarProps, \"variant\">) {\n  const context = usePage(\"SettingsPageSaveBar\");\n  const [blocked, setBlocked] = useState(0);\n  useEffect(() => {\n    if (!blocked) return;\n    const invalid = context.rootRef.current?.querySelectorAll('[aria-invalid=\"true\"]').length ?? 0;\n    if (invalid !== blocked) setBlocked(invalid);\n  });\n  function save() {\n    const invalid = Array.from(\n      context.rootRef.current?.querySelectorAll<HTMLElement>('[aria-invalid=\"true\"]') ?? [],\n    );\n    setBlocked(invalid.length);\n    const first = invalid[0];\n    if (!first) return onSave?.();\n    first.focus();\n    shake(first);\n  }\n  return (\n    <UnsavedChangesBar\n      {...props}\n      onSave={save}\n      data-invalid={blocked ? \"\" : undefined}\n      className={cn(\n        barClass[context.variant],\n        \"[&_[data-slot=unsaved-changes-bar-message]]:font-normal\",\n        \"[&:not([data-invalid])_[data-slot=unsaved-changes-bar-message][role=status]]:text-muted-foreground\",\n        \"[&[data-invalid]_[data-slot=unsaved-changes-bar-message]]:text-destructive-ink\",\n        className,\n      )}\n      variant={barVariants[context.variant]}\n    >\n      {children ?? (\n        <>\n          <UnsavedChangesBarMessage>\n            {blocked ? `Fix ${blocked} ${blocked === 1 ? \"field\" : \"fields\"} to save` : undefined}\n          </UnsavedChangesBarMessage>\n          <UnsavedChangesBarActions>\n            <UnsavedChangesBarDiscard />\n            <UnsavedChangesBarSave />\n          </UnsavedChangesBarActions>\n        </>\n      )}\n    </UnsavedChangesBar>\n  );\n}\n",
      "type": "registry:component",
      "target": "@components/uai/settings-page.tsx"
    }
  ],
  "categories": [
    "application"
  ],
  "type": "registry:block"
}