{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "intake-survey",
  "type": "registry:block",
  "title": "Intake Survey",
  "description": "A one-question-at-a-time onboarding survey: answer with A–D, a quick blink confirms your pick before the next question glides in, multi-select with a limit, an optional free-text step and an editable summary at the end.",
  "categories": [
    "block",
    "Apps"
  ],
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "utils",
    "https://coss.com/ui/r/button.json",
    "https://coss.com/ui/r/kbd.json"
  ],
  "files": [
    {
      "path": "registry/devl/intake-survey.tsx",
      "type": "registry:component",
      "content": "\"use client\";\n\nimport { ArrowLeftIcon, ArrowRightIcon, CheckIcon, CornerDownLeftIcon, RotateCcwIcon, SparklesIcon } from \"lucide-react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { type KeyboardEvent, useEffect, useId, useLayoutEffect, useRef, useState } from \"react\";\nimport { Button } from \"@/components/ui/button\";\nimport { Kbd } from \"@/components/ui/kbd\";\nimport { cn } from \"@/lib/utils\";\n\nexport type SurveyQuestion =\n  | { id: string; kind: \"single\"; title: string; hint?: string; options: string[] }\n  | { id: string; kind: \"multi\"; title: string; hint?: string; options: string[]; max?: number }\n  | { id: string; kind: \"text\"; title: string; hint?: string; placeholder?: string; optional?: boolean };\n\nexport type SurveyAnswers = Record<string, string | string[]>;\n\nexport interface IntakeSurveyProps {\n  questions: SurveyQuestion[];\n  name?: string;\n  doneTitle?: string;\n  doneText?: string;\n  onComplete?: (answers: SurveyAnswers) => void;\n  className?: string;\n}\n\nconst ease = [0.23, 1, 0.32, 1] as const;\nconst press = \"transition-[box-shadow,scale,background-color] duration-150 active:scale-[0.97]\";\nconst LETTERS = \"ABCDEFGHI\";\n\nfunction useMeasuredHeight<T extends HTMLElement>() {\n  const ref = useRef<T>(null);\n  const [height, setHeight] = useState<number | \"auto\">(\"auto\");\n  useLayoutEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => setHeight(el.offsetHeight));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n  return [ref, height] as const;\n}\n\nexport function IntakeSurvey({\n  questions,\n  name,\n  doneTitle,\n  doneText = \"We’ve set up your workspace around your answers. You can change any of this later in settings.\",\n  onComplete,\n  className,\n}: IntakeSurveyProps) {\n  const reduce = useReducedMotion();\n  const uid = useId();\n  const rootRef = useRef<HTMLElement>(null);\n  const textRef = useRef<HTMLTextAreaElement>(null);\n  const [step, setStep] = useState(0);\n  const [dir, setDir] = useState(1);\n  const [answers, setAnswers] = useState<SurveyAnswers>({});\n  const [flash, setFlash] = useState<string | null>(null);\n  const [limitHit, setLimitHit] = useState(false);\n  const advanceTimer = useRef<ReturnType<typeof setTimeout>>(undefined);\n  const [contentRef, height] = useMeasuredHeight<HTMLDivElement>();\n  const answersRef = useRef(answers);\n  answersRef.current = answers;\n\n  const done = step >= questions.length;\n  const q = questions[step];\n  const progress = done ? 1 : (step + 1) / (questions.length + 1);\n\n  useEffect(() => () => clearTimeout(advanceTimer.current), []);\n  useEffect(() => {\n    if (!limitHit) return;\n    const t = setTimeout(() => setLimitHit(false), 1600);\n    return () => clearTimeout(t);\n  }, [limitHit]);\n\n  useEffect(() => {\n    if (!rootRef.current?.contains(document.activeElement)) return;\n    const t = setTimeout(() => {\n      if (q?.kind === \"text\") textRef.current?.focus();\n      else rootRef.current?.querySelector<HTMLElement>(\"[data-survey-stage]\")?.focus();\n    }, 230);\n    return () => clearTimeout(t);\n  }, [step, q?.kind]);\n\n  const go = (next: number) => {\n    clearTimeout(advanceTimer.current);\n    setFlash(null);\n    setDir(next > step ? 1 : -1);\n    setStep(Math.max(0, Math.min(questions.length, next)));\n    if (next >= questions.length) setTimeout(() => onComplete?.(answersRef.current), 0);\n  };\n\n  const pick = (option: string) => {\n    if (!q || done) return;\n    if (q.kind === \"single\") {\n      setAnswers((a) => ({ ...a, [q.id]: option }));\n      setFlash(option);\n      clearTimeout(advanceTimer.current);\n      advanceTimer.current = setTimeout(() => go(step + 1), reduce ? 200 : 480);\n    } else if (q.kind === \"multi\") {\n      const current = (answers[q.id] as string[] | undefined) ?? [];\n      if (current.includes(option)) {\n        setAnswers((a) => ({ ...a, [q.id]: current.filter((o) => o !== option) }));\n      } else if (q.max && current.length >= q.max) {\n        setLimitHit(true);\n      } else {\n        setAnswers((a) => ({ ...a, [q.id]: [...current, option] }));\n      }\n    }\n  };\n\n  const canContinue =\n    !q ||\n    (q.kind === \"single\" && Boolean(answers[q.id])) ||\n    (q.kind === \"multi\" && ((answers[q.id] as string[] | undefined)?.length ?? 0) > 0) ||\n    (q.kind === \"text\" && (q.optional || Boolean((answers[q.id] as string | undefined)?.trim())));\n\n  const onKeyDown = (e: KeyboardEvent<HTMLElement>) => {\n    if (done || !q || e.metaKey || e.ctrlKey || e.altKey) return;\n    const inText = (e.target as HTMLElement).tagName === \"TEXTAREA\";\n    if (e.key === \"Enter\" && !e.shiftKey) {\n      if (inText || (e.target as HTMLElement).tagName !== \"BUTTON\") {\n        e.preventDefault();\n        if (canContinue) go(step + 1);\n      }\n      return;\n    }\n    if (inText || q.kind === \"text\") return;\n    const i = LETTERS.indexOf(e.key.toUpperCase());\n    if (e.key.length === 1 && i >= 0 && i < q.options.length) {\n      e.preventDefault();\n      pick(q.options[i]);\n    }\n  };\n\n  const variants = {\n    enter: (d: number) => (reduce ? { opacity: 0 } : { opacity: 0, y: d * 16, filter: \"blur(3px)\" }),\n    center: { opacity: 1, y: 0, filter: \"blur(0px)\" },\n    exit: (d: number) => (reduce ? { opacity: 0 } : { opacity: 0, y: d * -16, filter: \"blur(3px)\" }),\n  };\n\n  const first = name?.split(\" \")[0];\n\n  return (\n    <section\n      aria-label=\"Onboarding survey\"\n      className={cn(\"mx-auto w-full max-w-[640px] overflow-hidden rounded-2xl border bg-card text-card-foreground shadow-xs/5\", className)}\n      onKeyDown={onKeyDown}\n      ref={rootRef}\n    >\n      <motion.div\n        animate={{ height }}\n        initial={false}\n        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 380, damping: 40 }}\n      >\n        <div ref={contentRef}>\n      <div className=\"flex items-center gap-3 px-5 pt-5 sm:px-8 sm:pt-6\">\n        <Button\n          aria-label=\"Previous question\"\n          className={cn(\"-ms-2 text-muted-foreground\", press, (step === 0 || done) && \"invisible\")}\n          onClick={() => go(step - 1)}\n          size=\"icon-sm\"\n          variant=\"ghost\"\n        >\n          <ArrowLeftIcon />\n        </Button>\n        <div\n          aria-label=\"Survey progress\"\n          aria-valuemax={questions.length}\n          aria-valuemin={0}\n          aria-valuenow={Math.min(step, questions.length)}\n          className=\"h-1.5 flex-1 overflow-hidden rounded-full bg-muted\"\n          role=\"progressbar\"\n        >\n          <motion.div\n            animate={{ scaleX: progress }}\n            className=\"h-full origin-left rounded-full bg-primary\"\n            initial={false}\n            transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 160, damping: 26 }}\n          />\n        </div>\n        <span className=\"w-10 text-end text-muted-foreground text-xs tabular-nums\">\n          {done ? \"Done\" : `${step + 1}/${questions.length}`}\n        </span>\n      </div>\n\n      <div className=\"relative min-h-[400px] sm:min-h-[380px]\">\n        <AnimatePresence custom={dir} initial={false} mode=\"wait\">\n          {!done && q ? (\n            <motion.div\n              animate=\"center\"\n              aria-labelledby={`${uid}-q`}\n              className=\"px-5 pt-8 pb-6 outline-none sm:px-8\"\n              custom={dir}\n              data-survey-stage\n              exit=\"exit\"\n              initial=\"enter\"\n              key={q.id}\n              role=\"group\"\n              tabIndex={-1}\n              transition={{ duration: 0.2, ease }}\n              variants={variants}\n            >\n              <p className=\"flex items-center gap-1.5 font-medium text-muted-foreground text-xs\">\n                <span className=\"tabular-nums\">{step + 1}</span>\n                <ArrowRightIcon className=\"size-3\" />\n              </p>\n              <h2 className=\"mt-2 text-balance font-semibold text-xl tracking-tight sm:text-2xl\" id={`${uid}-q`}>\n                {q.title}\n              </h2>\n              {q.hint && (\n                <p\n                  className={cn(\n                    \"mt-1.5 text-pretty text-sm transition-colors duration-200\",\n                    limitHit ? \"text-warning-foreground\" : \"text-muted-foreground\",\n                  )}\n                >\n                  {q.hint}\n                </p>\n              )}\n\n              {q.kind === \"text\" ? (\n                <div className=\"mt-6\">\n                  <label className=\"sr-only\" htmlFor={`${uid}-text`}>\n                    {q.title}\n                  </label>\n                  <textarea\n                    className=\"block min-h-28 w-full resize-none rounded-xl border bg-background px-3.5 py-3 text-base shadow-xs/5 outline-none transition-shadow placeholder:text-muted-foreground/72 focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/24 sm:text-sm dark:bg-input/32\"\n                    id={`${uid}-text`}\n                    onChange={(e) => setAnswers((a) => ({ ...a, [q.id]: e.target.value }))}\n                    placeholder={q.placeholder}\n                    ref={textRef}\n                    value={(answers[q.id] as string | undefined) ?? \"\"}\n                  />\n                  <p className=\"mt-2 flex items-center gap-1.5 text-muted-foreground text-xs\">\n                    <Kbd>Shift</Kbd>+<Kbd>Enter</Kbd> for a new line\n                  </p>\n                </div>\n              ) : (\n                <ul className=\"mt-6 grid gap-2\">\n                  {q.options.map((option, i) => {\n                    const selected =\n                      q.kind === \"single\" ? answers[q.id] === option : ((answers[q.id] as string[] | undefined) ?? []).includes(option);\n                    return (\n                      <li key={option}>\n                        <motion.button\n                          animate={flash === option && !reduce ? { opacity: [1, 0.45, 1, 0.45, 1] } : { opacity: 1 }}\n                          aria-pressed={selected}\n                          className={cn(\n                            \"group flex min-h-12 w-full cursor-pointer items-center gap-3 rounded-xl border px-3 py-2.5 text-left text-sm outline-none transition-[background-color,border-color,scale] duration-150 focus-visible:ring-2 focus-visible:ring-ring active:scale-[0.99]\",\n                            selected ? \"border-foreground/70 bg-accent\" : \"hover:border-foreground/20 hover:bg-accent/40\",\n                          )}\n                          onClick={() => pick(option)}\n                          transition={{ duration: 0.36, ease: \"linear\" }}\n                          type=\"button\"\n                        >\n                          <span\n                            className={cn(\n                              \"flex size-6 shrink-0 items-center justify-center rounded-md border font-medium font-mono text-xs transition-colors duration-150\",\n                              selected ? \"border-primary bg-primary text-primary-foreground\" : \"bg-background text-muted-foreground dark:bg-input/32\",\n                            )}\n                          >\n                            {LETTERS[i]}\n                          </span>\n                          <span className=\"min-w-0 flex-1\">{option}</span>\n                          <motion.span\n                            animate={{ opacity: selected ? 1 : 0, scale: selected ? 1 : 0.6 }}\n                            className=\"flex\"\n                            initial={false}\n                            transition={{ duration: 0.16, ease }}\n                          >\n                            <CheckIcon className=\"size-4\" strokeWidth={2.5} />\n                          </motion.span>\n                        </motion.button>\n                      </li>\n                    );\n                  })}\n                </ul>\n              )}\n\n              {q.kind !== \"single\" && (\n                <div className=\"mt-5 flex items-center gap-3\">\n                  <Button className={press} disabled={!canContinue} onClick={() => go(step + 1)}>\n                    {step === questions.length - 1 ? \"Finish\" : \"Continue\"}\n                    {step === questions.length - 1 ? <CheckIcon /> : <ArrowRightIcon />}\n                  </Button>\n                  <span className=\"hidden items-center gap-1.5 text-muted-foreground text-xs sm:flex\">\n                    or press <Kbd>Enter</Kbd>\n                    <CornerDownLeftIcon className=\"size-3\" />\n                  </span>\n                  {q.kind === \"text\" && q.optional && (\n                    <Button className={cn(\"ms-auto text-muted-foreground\", press)} onClick={() => go(step + 1)} variant=\"ghost\">\n                      Skip\n                    </Button>\n                  )}\n                </div>\n              )}\n            </motion.div>\n          ) : (\n            <motion.div\n              animate={{ opacity: 1, y: 0 }}\n              className=\"px-5 pt-10 pb-8 sm:px-8\"\n              initial={reduce ? { opacity: 0 } : { opacity: 0, y: 12 }}\n              key=\"done\"\n              transition={{ duration: 0.28, ease }}\n            >\n              <motion.span\n                animate={{ scale: 1, opacity: 1, rotate: 0 }}\n                className=\"flex size-12 items-center justify-center rounded-2xl bg-violet-500/12 text-violet-700 dark:text-violet-300\"\n                initial={reduce ? false : { scale: 0.8, opacity: 0, rotate: -8 }}\n                transition={{ type: \"spring\", stiffness: 380, damping: 20, delay: 0.08 }}\n              >\n                <SparklesIcon className=\"size-6\" />\n              </motion.span>\n              <h2 className=\"mt-5 text-balance font-semibold text-2xl tracking-tight\">\n                {doneTitle ?? (first ? `You’re all set, ${first}` : \"You’re all set\")}\n              </h2>\n              <p className=\"mt-1.5 max-w-[48ch] text-pretty text-muted-foreground text-sm\">{doneText}</p>\n              <dl className=\"mt-6 divide-y rounded-xl border\">\n                {questions.map((item, i) => {\n                  const a = answers[item.id];\n                  const text = Array.isArray(a) ? a.join(\", \") : a?.trim();\n                  return (\n                    <motion.div\n                      animate={{ opacity: 1 }}\n                      className=\"flex items-start justify-between gap-4 px-3.5 py-2.5 text-sm\"\n                      initial={reduce ? false : { opacity: 0 }}\n                      key={item.id}\n                      transition={{ duration: 0.2, delay: 0.15 + i * 0.04 }}\n                    >\n                      <dt className=\"min-w-0 truncate text-muted-foreground\">{item.title}</dt>\n                      <dd className={cn(\"max-w-[55%] shrink-0 text-end\", !text && \"text-muted-foreground\")}>\n                        <button\n                          className=\"cursor-pointer rounded text-end outline-none hover:underline focus-visible:ring-2 focus-visible:ring-ring\"\n                          onClick={() => go(i)}\n                          type=\"button\"\n                        >\n                          {text || \"Skipped\"}\n                        </button>\n                      </dd>\n                    </motion.div>\n                  );\n                })}\n              </dl>\n              <Button\n                className={cn(\"mt-6 text-muted-foreground\", press)}\n                onClick={() => {\n                  setAnswers({});\n                  setDir(-1);\n                  setStep(0);\n                }}\n                variant=\"ghost\"\n              >\n                <RotateCcwIcon />\n                Start over\n              </Button>\n            </motion.div>\n          )}\n        </AnimatePresence>\n      </div>\n\n      {!done && q && q.kind !== \"text\" && (\n        <div className=\"flex items-center gap-1.5 border-t bg-muted/40 px-5 py-3 text-muted-foreground text-xs sm:px-8\">\n          <span className=\"hidden sm:inline\">Press</span>\n          <span className=\"hidden items-center gap-1 sm:flex\">\n            {q.options.map((_, i) => (\n              <Kbd key={i}>{LETTERS[i]}</Kbd>\n            ))}\n          </span>\n          <span className=\"hidden sm:inline\">to answer</span>\n          <span className=\"sm:hidden\">Tap an answer to continue</span>\n          {q.kind === \"multi\" && q.max && (\n            <span className=\"ms-auto tabular-nums\">\n              {((answers[q.id] as string[] | undefined) ?? []).length} of {q.max} picked\n            </span>\n          )}\n        </div>\n      )}\n        </div>\n      </motion.div>\n    </section>\n  );\n}\n"
    }
  ]
}