{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "app-team-invite",
  "type": "registry:block",
  "title": "App, invite form",
  "description": "Invite someone: an email field with a hint, a role select and a send button, with four states (idle, sending, sent, error with the field marked and described). Sends only through onInvite(email, role) from its host (a promise: sending while it runs, sent or the error banner after); without one it previews.",
  "categories": [
    "app"
  ],
  "dependencies": [
    "zod@^4.6.5"
  ],
  "registryDependencies": [
    "https://ui.klarhimmel.com/r/banner.json",
    "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/native-select.json"
  ],
  "files": [
    {
      "path": "src/blocks/app-team-invite/spec.ts",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-team-invite/spec.ts",
      "content": "import { z } from 'zod';\nimport { defineSpec, t, req } from '../_shared/schema';\n\nconst schema = z.object({\n  eyebrow: t(60).optional(),\n  title: t(120),\n  description: t(300).optional(),\n  emailLabel: req(40),\n  emailHint: t(120).optional(),\n  /** The email the field starts with. Not editable copy. */\n  sample: t(60),\n  roleLabel: req(40),\n  roles: z.array(t(20)).min(2).max(5),\n  /** The role the select starts on. Not editable copy. */\n  active: t(20),\n  actions: z.object({ send: req(30), sending: t(30) }),\n  /** idle, sending (the button waits), sent (a confirmation), error (the email field is marked). */\n  state: z.enum(['idle', 'sending', 'sent', 'error']),\n  messages: z.object({ sent: t(120), error: t(120), emailError: t(120) }),\n});\n\nexport const spec = defineSpec({\n  type: 'app-team-invite',\n  aliases: ['app/team-invite'],\n  name: 'App, invite form',\n  category: 'app',\n  role: 'body',\n  description: 'Invite someone: an email field with a hint, a role select and a send button, with four states (idle, sending, sent, error with the field marked and described). Sends only through onInvite(email, role) from its host (a promise: sending while it runs, sent or the error banner after); without one it previews.',\n  schema,\n  defaults: {\n    \"eyebrow\": \"Team\",\n    \"title\": \"Say who is invited\",\n    \"description\": \"Say what the person gets and how long the link lasts.\",\n    \"emailLabel\": \"Email address\",\n    \"emailHint\": \"Say which address to use.\",\n    \"sample\": \"\",\n    \"roleLabel\": \"Role\",\n    \"roles\": [\n      \"Admin\",\n      \"Member\",\n      \"Viewer\"\n    ],\n    \"active\": \"Member\",\n    \"actions\": {\n      \"send\": \"Send invite\",\n      \"sending\": \"Sending…\"\n    },\n    \"state\": \"idle\",\n    \"messages\": {\n      \"sent\": \"Say the invite went out and where it shows up.\",\n      \"error\": \"Say the invite was not sent and what to check, then retry.\",\n      \"emailError\": \"Say what a valid email address looks like.\"\n    }\n  },\n});\n"
    },
    {
      "path": "src/blocks/app-team-invite/index.tsx",
      "type": "registry:file",
      "target": "components/klarhimmel/blocks/app-team-invite/index.tsx",
      "content": "import { useId, useRef, type FormEvent } from 'react';\nimport { Text, withHeadingLevel } from '../../editing';\nimport { Banner } from '../../ui/banner';\nimport { Button } from '../../ui/button';\nimport { Input } from '../../ui/input';\nimport { NativeSelect } from '../../ui/native-select';\nimport { Container, Opt, Section, SectionHeader, useNamed } 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';\n\nexport { spec };\ntype Props = z.infer<typeof spec.schema>;\n\n/** What a host passes so the form acts: the block calls it with a valid address and the chosen role, shows 'sending' while it\n *  runs, then the sent banner when it resolves or the error banner when it rejects. Without it the form only previews. */\nexport interface TeamInviteHost {\n  onInvite?: (email: string, role: string) => Promise<void>;\n}\n\nfunction AppTeamInvite({ props, onInvite }: BlockProps<Props> & TeamInviteHost) {\n  const emailNamed = useNamed(props.emailLabel);\n  const roleNamed = useNamed(props.roleLabel);\n  const id = useId();\n  // The form behaves like one: a bad address is said in place, a good one shows the result. `state` is where it starts.\n  // failed: the host's onInvite rejected; the banner says so and the address stays, unmarked (it was valid).\n  const [local, setLocal] = useSynced<{ sent: boolean; bad: boolean; sending?: boolean; failed?: boolean }>({ sent: false, bad: false }, props.state);\n  const state = local.bad || local.failed ? 'error' : local.sending ? 'sending' : local.sent ? 'sent' : props.state;\n  const busy = state === 'sending';\n  const invalid = local.bad || (!local.failed && state === 'error');\n  const role = useRef<HTMLSelectElement>(null);\n  const input = useRef<HTMLInputElement>(null);\n  const banner = useRef<HTMLDivElement>(null);\n  const describedBy = [props.emailHint && `${id}-h`, invalid && `${id}-e`].filter(Boolean).join(' ') || undefined;\n  const submit = (e: FormEvent<HTMLFormElement>) => {\n    e.preventDefault();\n    if (busy) return;\n    const ok = /^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$/.test(input.current?.value.trim() ?? '');\n    if (!ok) {\n      setLocal({ sent: false, bad: true });\n      input.current?.focus();\n      return;\n    }\n    const done = (ok: boolean) => {\n      setLocal({ sent: ok, bad: false, failed: !ok });\n      if (ok && input.current) input.current.value = '';\n      // The fieldset was disabled while sending; focus the banner once it is shown again.\n      requestAnimationFrame(() => banner.current?.focus());\n    };\n    if (!onInvite) return done(true);\n    setLocal({ sent: false, bad: false, sending: true });\n    onInvite(input.current!.value.trim(), role.current?.value ?? props.active).then(() => done(true), () => done(false));\n  };\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\">\n          <form noValidate onSubmit={submit} aria-busy={busy || undefined} className=\"p-6 @xl:p-8\">\n            <fieldset disabled={busy} className=\"m-0 flex min-w-0 flex-col gap-6 border-0 p-0\">\n              <div ref={banner} tabIndex={-1} className=\"outline-none\" hidden={state !== 'sent' && state !== 'error'}>\n                <StateSlot show={state === 'sent'}>\n                  <Banner tone=\"info\"><Text path={['messages', 'sent']} value={props.messages.sent} as=\"span\" /></Banner>\n                </StateSlot>\n                <StateSlot show={state === 'error'}>\n                  <Banner tone=\"error\"><Text path={['messages', 'error']} value={props.messages.error} as=\"span\" /></Banner>\n                </StateSlot>\n              </div>\n              <div className=\"grid gap-6 @xl:grid-cols-[1fr_12rem]\">\n                {emailNamed && (\n                <div className=\"flex flex-col gap-1.5\">\n                  <label htmlFor={`${id}-m`} className=\"text-kh-small font-medium\">\n                    <Text path={['emailLabel']} value={props.emailLabel} as=\"span\" />\n                  </label>\n                  <Input\n                    key={props.sample}\n                    ref={input}\n                    id={`${id}-m`}\n                    name=\"email\"\n                    type=\"email\"\n                    autoComplete=\"off\"\n                    defaultValue={props.sample}\n                    aria-invalid={invalid || undefined}\n                    aria-describedby={describedBy}\n                    onChange={() => (local.bad || local.failed) && setLocal({ sent: false, bad: false })}\n                  />\n                  <div id={`${id}-h`}>\n                    <Opt path={['emailHint']} value={props.emailHint} as=\"p\" className=\"text-kh-small text-kh-muted\" placeholder=\"Hint\" />\n                  </div>\n                  <StateSlot show={invalid}>\n                    <p id={`${id}-e`} className=\"text-kh-small text-kh-danger\">\n                      <Text path={['messages', 'emailError']} value={props.messages.emailError} as=\"span\" />\n                    </p>\n                  </StateSlot>\n                </div>\n                )}\n                {roleNamed && (\n                <div className=\"flex flex-col gap-1.5\">\n                  <label htmlFor={`${id}-r`} className=\"text-kh-small font-medium\">\n                    <Text path={['roleLabel']} value={props.roleLabel} as=\"span\" />\n                  </label>\n                  <NativeSelect ref={role} key={props.active} id={`${id}-r`} name=\"role\" defaultValue={props.active} className=\"w-full\">\n                    {props.roles.map((r, k) => (\n                      <Text key={k} path={['roles', k]} value={r} as=\"option\" />\n                    ))}\n                  </NativeSelect>\n                </div>\n                )}\n              </div>\n              <div className=\"flex justify-end border-t border-kh-line pt-6\">\n                <Button type=\"submit\">\n                  <Text path={['actions', busy ? 'sending' : 'send']} value={busy ? props.actions.sending : props.actions.send} as=\"span\" />\n                </Button>\n              </div>\n            </fieldset>\n            <StateSlot show={false}>\n              <Text path={['actions', busy ? 'send' : 'sending']} value={busy ? props.actions.send : props.actions.sending} as=\"p\" />\n            </StateSlot>\n          </form>\n        </AppPanel>\n      </Container>\n    </Section>\n  );\n}\n\nexport default withHeadingLevel(AppTeamInvite);\n"
    }
  ]
}
