{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "kit-form",
  "type": "registry:lib",
  "title": "Kit form",
  "description": "Helpers shared by the form blocks.",
  "dependencies": [],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/block-kit.json",
    "https://ui.klarhimmel.com/r/editing.json"
  ],
  "files": [
    {
      "path": "src/blocks/_shared/form-kit.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/_shared/form-kit.tsx",
      "content": "// Native-form plumbing shared by the form blocks. No fetching, no scripts: attributes and empty containers only.\nimport { useEffect, useRef, useState, type FormEvent } from 'react';\nimport { Text } from '../../editing';\nimport { useEditing } from './kit';\n\nexport interface FormSettings {\n  action?: string;\n  method?: 'post' | 'get';\n  hidden?: Record<string, string>;\n  captcha?: boolean;\n  captchaSitekey?: string;\n}\n\n/** An action the form may carry: http(s), same-site path or query. Anything else (javascript:, //host, bare words) is dropped. */\nconst SAFE_ACTION = /^(https?:\\/\\/|\\/(?!\\/)|\\?)/i;\nexport const safeAction = (action: string | undefined) => {\n  const a = (action ?? '').trim();\n  return SAFE_ACTION.test(a) ? a : undefined;\n};\n\n/**\n * Props for the <form>. A safe `action` is always written, with `method` post unless set (a form without JS must never fall back to\n * a GET with the message in the URL). An explicit `method` makes it a plain HTML form that submits natively after hydration too;\n * without one the hydrated block cancels the submit and the host wires its own handler. An unsafe action is dropped entirely.\n * In an editor the form never submits.\n */\nexport function useFormProps({ action, method }: FormSettings) {\n  const editing = useEditing();\n  const safe = safeAction(action);\n  const native = !!(method && safe) && !editing;\n  return {\n    action: safe,\n    method: safe ? (method ?? 'post') : undefined,\n    onSubmit: native ? undefined : (e: { preventDefault: () => void }) => e.preventDefault(),\n  };\n}\n\n/** Hidden inputs (name -> value) and the Turnstile slot. Render inside the <form>, before the submit row. */\nexport function FormExtras({ hidden, captcha, captchaSitekey }: FormSettings) {\n  return (\n    <>\n      {Object.entries(hidden ?? {}).map(([name, value]) => (\n        <input key={name} type=\"hidden\" name={name} value={value} />\n      ))}\n      {captcha && <div className=\"kh-captcha cf-turnstile min-h-16\" data-kh-captcha data-sitekey={captchaSitekey || undefined} />}\n    </>\n  );\n}\n\n/** A bare domain or a full address: an optional http(s) scheme, a host with at least one dot, an optional path. Owners type \"yourcompany.se\"; type=url would refuse it. Written to compile with the v flag browsers use for `pattern`. */\nexport const DOMAIN_PATTERN = String.raw`(https?://)?([\\p{L}\\p{N}\\-]+\\.)+\\p{L}{2,}(:\\d+)?(/\\S*)?`;\n\n/** An address with a dot after the @ (\"a@b\" is not one), as a pattern the browser enforces without script. */\nexport const EMAIL_PATTERN = String.raw`[^@\\s]+@[^@\\s]+\\.[^@\\s]+`;\nexport const URL_PATTERN = String.raw`https?://[^\\s\\/]+\\.[^\\s]+`; // a / inside a class is escaped so it compiles with the v flag\n\nexport const ERROR_WORDS = {\n  required: 'Fill in this field.',\n  email: 'Enter an email address like name@company.example.',\n  url: 'Enter an address that starts with https://, like https://example.com.',\n  domain: 'Enter a site address like yourcompany.se.',\n};\nexport type FieldKind = 'email' | 'url' | 'domain' | 'text';\n\n/**\n * Inline per-field errors from the browser's own validation. The `invalid` event is cancelled (no native bubble), the reason is shown\n * under the field, the first bad field gets focus, and typing clears it. Without script the browser's own validation still applies.\n * `idOf(field)` is the id of the control; its error is `idOf(field)-e` (see FieldError).\n */\nexport function useFieldErrors(words: Record<'required' | 'email' | 'url' | 'domain', string>, idOf: (field: string) => string) {\n  const [errs, setErrs] = useState<Record<string, string>>({});\n  const fieldProps = (field: string, kind: FieldKind) => ({\n    'aria-invalid': errs[field] ? (true as const) : undefined,\n    'aria-describedby': errs[field] ? `${idOf(field)}-e` : undefined,\n    onInvalid: (e: FormEvent<HTMLInputElement | HTMLTextAreaElement>) => {\n      e.preventDefault();\n      const el = e.currentTarget;\n      setErrs((x) => ({ ...x, [field]: el.validity.valueMissing || kind === 'text' ? words.required : words[kind] }));\n      if (el.form?.querySelector(':invalid') === el) el.focus();\n    },\n    onInput: () => setErrs((x) => (x[field] ? { ...x, [field]: '' } : x)),\n  });\n  return { errs, fieldProps };\n}\n\n/** The error line under a field: a polite live region that stays in the DOM so the reason is announced when it fills. */\nexport function FieldError({ id, text }: { id: string; text?: string }) {\n  return (\n    <p id={`${id}-e`} aria-live=\"polite\" className=\"mt-1.5 text-kh-small text-kh-ink empty:hidden\">\n      {text || ''}\n    </p>\n  );\n}\n\nconst STATUS_WORDS = { sent: 'Thank you. We have your request and will reply soon.', error: 'Something went wrong. Please try again.' };\n\n/**\n * The sent or error line of a form (see formStatus). Sent is a polite status that takes focus once when it appears (the fields are\n * hidden by the block meanwhile); error is an alert and the form stays live. In an editor both show so their words can be edited.\n */\nexport function FormStatus({ status, messages }: { status?: 'idle' | 'sent' | 'error'; messages?: { sent: string; error: string } }) {\n  const editing = useEditing();\n  const words = { ...STATUS_WORDS, ...messages };\n  const box = 'rounded-kh-lg border px-4 py-3 text-kh-small outline-none focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--kh-focus)';\n  const sent = useRef<HTMLParagraphElement>(null);\n  useEffect(() => {\n    if (status === 'sent') sent.current?.focus();\n  }, [status]);\n  return (\n    <>\n      {(status === 'sent' || editing) && (\n        <p ref={sent} role=\"status\" tabIndex={-1} className={`${box} border-kh-line-strong bg-kh-accent-soft text-kh-accent-ink`}>\n          <Text path={['messages', 'sent']} value={words.sent} as=\"span\" />\n        </p>\n      )}\n      {(status === 'error' || editing) && (\n        <p role=\"alert\" className={`${box} border-kh-danger text-kh-ink`}>\n          <Text path={['messages', 'error']} value={words.error} as=\"span\" />\n        </p>\n      )}\n    </>\n  );\n}\n"
    }
  ]
}
