{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-settings-danger",
  "type": "registry:block",
  "title": "App, danger zone",
  "description": "Irreversible actions kept apart from the rest of the settings. Each row explains the consequence; its button opens a confirmation where the person types a phrase before the destructive button is enabled. A host acts through onConfirm(index) (a promise: the button holds, then the done text, or the error text with the confirmation left open).",
  "categories": [
    "app"
  ],
  "dependencies": [
    "zod@^4.6.5"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/block-kit.json",
    "https://ui.klarhimmel.com/r/button.json",
    "https://ui.klarhimmel.com/r/editing.json",
    "https://ui.klarhimmel.com/r/input.json",
    "https://ui.klarhimmel.com/r/kit-app.json",
    "https://ui.klarhimmel.com/r/kit-invalid.json"
  ],
  "files": [
    {
      "path": "src/blocks/app-settings-danger/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-settings-danger/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, t } from '../_shared/schema';\n\nconst item = z.object({\n  title: t(60),\n  description: t(200),\n  action: t(30),\n  /** Whether the confirmation starts open. */\n  open: z.boolean(),\n  confirm: z.object({\n    body: t(200),\n    inputLabel: t(80),\n    /** What the person types to unlock the confirm button. */\n    phrase: t(40),\n    submit: t(30),\n    cancel: t(30),\n    /** Said after the confirm button is pressed. */\n    done: t(80),\n    /** Said when the host's onConfirm fails; the confirmation stays open. */\n    error: t(120).optional(),\n  }),\n});\n\nconst schema = z.object({\n  eyebrow: t(60).optional(),\n  title: t(120),\n  description: t(300).optional(),\n  items: z.array(item).min(1).max(3),\n});\n\nexport const spec = defineSpec({\n  type: 'app-settings-danger',\n  aliases: ['app/settings-danger'],\n  name: 'App, danger zone',\n  category: 'app',\n  strict: true,\n  role: 'body',\n  description: 'Irreversible actions kept apart from the rest of the settings. Each row explains the consequence; its button opens a confirmation where the person types a phrase before the destructive button is enabled. A host acts through onConfirm(index) (a promise: the button holds, then the done text, or the error text with the confirmation left open).',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Settings\",\n    \"title\": \"Danger zone\",\n    \"description\": \"Say that these actions cannot be undone and to read each first.\",\n    \"items\": [\n      {\n        \"title\": \"Name the first action\",\n        \"description\": \"Say who is affected and what stays the same afterwards.\",\n        \"action\": \"First action…\",\n        \"open\": false,\n        \"confirm\": {\n          \"body\": \"Say what the consequence is and what to be sure of first.\",\n          \"inputLabel\": \"Type the name to confirm it\",\n          \"phrase\": \"Workspace name\",\n          \"submit\": \"Confirm the first\",\n          \"cancel\": \"Do not go on\",\n          \"done\": \"Say it is done. This is a preview: nothing was changed.\",\n          \"error\": \"Say it did not go through and that nothing changed.\"\n        }\n      },\n      {\n        \"title\": \"Name the second action\",\n        \"description\": \"Say what is deleted and for whom it is deleted.\",\n        \"action\": \"Second action…\",\n        \"open\": false,\n        \"confirm\": {\n          \"body\": \"Say how much is deleted and that there is no way back.\",\n          \"inputLabel\": \"Type the name to confirm it\",\n          \"phrase\": \"Workspace name\",\n          \"submit\": \"Confirm the second\",\n          \"cancel\": \"Do not go on\",\n          \"done\": \"Say it is deleted. This is a preview: nothing was changed.\",\n          \"error\": \"Say it did not go through and that nothing changed.\"\n        }\n      }\n    ]\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-settings-danger/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-settings-danger/index.tsx",
      "content": "import { useEffect, useId, useRef } from 'react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { Button } from '../../ui/button';\nimport { Input } from '../../ui/input';\nimport { useEditing, Container, Section, SectionHeader } from '../_shared/kit';\nimport { AppPanel, StateSlot, useSynced } from '../_shared/app-kit';\nimport { spec } from './spec';\nimport type { BlockProps } from '../types';\nimport type { z } from 'zod';\nimport { InvalidNotice } from '../_shared/invalid-kit';\n\nexport { spec };\ntype Props = z.infer<typeof spec.schema>;\ntype Item = Props['items'][number];\n\n/** What a host passes so a confirmation acts: the block calls it with the item's index when its confirm button is pressed, holds\n *  the button while it runs, then says the item's done text when it resolves, or its error text (the confirmation stays open). */\nexport interface DangerZoneHost {\n  onConfirm?: (index: number) => Promise<void>;\n}\n\nfunction Row({ item, index, id, onConfirm }: { item: Item; index: number; id: string } & DangerZoneHost) {\n  const editing = useEditing();\n  const [open, setOpen] = useSynced(item.open, item);\n  const [typed, setTyped] = useSynced('', item);\n  const region = `${id}-c`;\n  const [done, setDone] = useSynced(false, item);\n  const [working, setWorking] = useSynced(false, item);\n  const [failed, setFailed] = useSynced(false, item);\n  const opener = useRef<HTMLButtonElement>(null);\n  const field = useRef<HTMLInputElement>(null);\n  const wasOpen = useRef(item.open);\n  const touched = useRef(false); // a confirmation that starts open must not take focus on load\n  // Opening moves focus to the confirmation field; closing gives it back to the button that opened it.\n  useEffect(() => {\n    if (open && touched.current) field.current?.focus();\n    else if (!open && wasOpen.current) opener.current?.focus();\n    wasOpen.current = open;\n  }, [open]);\n  const close = () => {\n    setOpen(false);\n    setTyped('');\n  };\n  return (\n    <li className=\"px-6 py-5 @xl:px-8\">\n      <div className=\"flex flex-col gap-3 @xl:flex-row @xl:items-center @xl:justify-between @xl:gap-8\">\n        <div className=\"min-w-0\">\n          <Text path={['items', index, 'title']} value={item.title} as=\"h3\" className=\"text-kh-body font-semibold\" />\n          <Text path={['items', index, 'description']} value={item.description} as=\"p\" className=\"mt-1 text-kh-small text-kh-muted\" />\n        </div>\n        <Button ref={opener} variant=\"secondary\" aria-expanded={open} aria-controls={region} onClick={() => {\n            touched.current = true;\n            setDone(false);\n            setFailed(false);\n            if (open) close();\n            else setOpen(true);\n          }} className=\"self-start @xl:shrink-0\">\n          <Text path={['items', index, 'action']} value={item.action} as=\"span\" />\n        </Button>\n      </div>\n      <StateSlot show={done} className=\"mt-4 text-kh-small text-kh-ink-2\">\n        <p role=\"status\">\n          <Text path={['items', index, 'confirm', 'done']} value={item.confirm.done} as=\"span\" />\n        </p>\n      </StateSlot>\n      <div\n        id={region}\n        role=\"group\"\n        aria-label={item.title}\n        hidden={!open && !editing}\n        onKeyDown={(e) => e.key === 'Escape' && close()}\n        className=\"mt-5 flex flex-col gap-4 rounded-kh-lg border border-kh-line bg-kh-surface-2 p-5\"\n      >\n        <Text path={['items', index, 'confirm', 'body']} value={item.confirm.body} as=\"p\" className=\"text-kh-small\" />\n        {item.confirm.error && (\n          <StateSlot show={failed}>\n            <p role=\"alert\" className=\"text-kh-small text-kh-danger\">\n              <Text path={['items', index, 'confirm', 'error']} value={item.confirm.error} as=\"span\" />\n            </p>\n          </StateSlot>\n        )}\n        <div className=\"flex flex-col gap-1.5\">\n          <label htmlFor={`${region}-i`} className=\"text-kh-small font-medium\">\n            <Text path={['items', index, 'confirm', 'inputLabel']} value={item.confirm.inputLabel} as=\"span\" />{' '}\n            <code className=\"rounded-kh-sm bg-kh-surface-3 px-1.5 font-kh-mono\">\n              <Text path={['items', index, 'confirm', 'phrase']} value={item.confirm.phrase} as=\"span\" />\n            </code>\n          </label>\n          <Input ref={field} id={`${region}-i`} value={typed} onChange={(e) => setTyped(e.target.value)} autoComplete=\"off\" spellCheck={false} />\n        </div>\n        <div className=\"flex flex-wrap justify-end gap-3\">\n          <Button variant=\"ghost\" onClick={close}>\n            <Text path={['items', index, 'confirm', 'cancel']} value={item.confirm.cancel} as=\"span\" />\n          </Button>\n          <Button\n            disabled={typed !== item.confirm.phrase || working}\n            aria-busy={working || undefined}\n            onClick={() => {\n              const finish = () => {\n                setDone(true);\n                close();\n              };\n              if (!onConfirm) return finish();\n              setFailed(false);\n              setWorking(true);\n              onConfirm(index).then(\n                () => {\n                  setWorking(false);\n                  finish();\n                },\n                () => {\n                  setWorking(false);\n                  setFailed(true);\n                },\n              );\n            }}\n          >\n            <Text path={['items', index, 'confirm', 'submit']} value={item.confirm.submit} as=\"span\" />\n          </Button>\n        </div>\n      </div>\n    </li>\n  );\n}\n\nfunction AppSettingsDanger(p: BlockProps<Props> & DangerZoneHost) {\n  // A strict block: the consequences and the phrase to type are never replaced by default text.\n  if (!spec.schema.safeParse(p.props).success) return <InvalidNotice title=\"This danger zone could not be shown\" />;\n  return <DangerZone {...p} />;\n}\n\nfunction DangerZone({ props, onConfirm }: BlockProps<Props> & DangerZoneHost) {\n  const id = useId();\n  return (\n    <Section labelledBy={id}>\n      <Container width=\"narrow\">\n        <SectionHeader id={id} eyebrow={props.eyebrow} title={props.title} description={props.description} />\n        <AppPanel className=\"mt-10 border-kh-danger\">\n          <ul className=\"divide-y divide-kh-line\">\n            {props.items.map((item, i) => (\n              <Row key={i} item={item} index={i} id={`${id}-${i}`} onConfirm={onConfirm} />\n            ))}\n          </ul>\n        </AppPanel>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(AppSettingsDanger);\n"
    }
  ]
}
