{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "questionnaire",
  "title": "Questionnaire",
  "description": "A multi-step form on @shadcn/react's Questionnaire, as the reference: single choice (the radio group's rows and ink band), multiple choice (checkbox rows), free text (the field frame), required and optional steps, Back, Skip, Next and Submit as Button cells, answers kept when going back, errors described on the current step, focus on the new step's heading, Progress for the step.",
  "dependencies": [
    "@base-ui/react@1.9.0",
    "@shadcn/react@0.3.1"
  ],
  "registryDependencies": [
    "@keyline-ui/keyline",
    "@keyline-ui/radio-group",
    "@keyline-ui/checkbox",
    "utils",
    "@keyline-ui/keyline-render",
    "@keyline-ui/keyline-motion",
    "@keyline-ui/button",
    "@keyline-ui/field",
    "@keyline-ui/progress"
  ],
  "files": [
    {
      "path": "src/registry/keyline/ui/questionnaire.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { Questionnaire as Q } from \"@shadcn/react/questionnaire\";\n\nimport { mergeProps } from \"@base-ui/react/merge-props\";\nimport { useRender } from \"@base-ui/react/use-render\";\n\nimport { cn } from \"@/lib/utils\";\nimport { resolveClassName, resolveStyle, type PartProps } from \"../lib/keyline-render\";\nimport { useStretch } from \"../lib/keyline-motion\";\nimport { Button } from \"./button\";\nimport { checkboxStyles, choiceStyles } from \"./checkbox\";\nimport { FieldFrame, fieldStyles } from \"./field\";\nimport { Progress } from \"./progress\";\nimport { radioStyles } from \"./radio-group\";\n\n/*\n * A questionnaire on @shadcn/react's Questionnaire, as the reference. The library owns the behaviour:\n * the steps and their order, required and optional steps, Skip, validation that blocks Next, answers kept while\n * you go back (inactive steps stay mounted, hidden and inert), errors described on the current step only, the\n * Enter / Cmd+Enter / arrow keys, choice shortcuts, and FormData on submit. Keyline draws it with its own parts:\n * the radio group's rows and stretching ink band, the checkbox's box, the field frame with its ! cell, Progress,\n * and Button cells in the action row. Keyline adds one behaviour: a new step moves focus to its heading.\n *\n *   ┌───────────────────────────────────────────────┐\n *   │ Question 2 of 4                       Optional │  progress cell\n *   │ [▓▓▓▓▓▓▓▓▓▓|                                 ] │\n *   ├───────────────────────────────────────────────┤\n *   │ Which docks can take reefers?                  │  the step: heading (focused), description,\n *   │ ┌───────────────────────────────────────────┐ │  choices or an input, the error\n *   │ │ ■ Rotterdam                             A │ │\n *   ├─┴───────────────────────────────────────────┴─┤\n *   │ Back │                          │ Skip │ Next │  action cells\n *   └───────────────────────────────────────────────┘\n */\n\n/* ------------------------------------------------------------------ *\n * CLASSES\n * ------------------------------------------------------------------ */\n\nconst root = \"kl-frame kl-rows min-w-0\";\n\n/* the padding is physical, as the stylesheet had it: the cut's room is on the left in both directions */\nconst progress = cn(\n  \"grid gap-1.5 pt-3 pr-(--kl-inset) pb-3.5 pl-[calc(var(--kl-inset)+var(--kl-c-room,0px))]\",\n  // the seam under it, while a step shows below (see `item`)\n  \"has-[~[data-slot=questionnaire-item]:not([hidden])]:border-b-(length:--kl-k1) has-[~[data-slot=questionnaire-item]:not([hidden])]:border-b-kl-line\",\n);\nconst count = \"text-[13px] font-(--kl-weight-strong) tabular-nums\";\n\n/* the step: inactive steps keep their answers but are hidden (and inert) by the library. A step draws no seam of its\n   own: WebKit treats its floated legend as the fieldset's rendered legend, so a top border (the frame's row seam) is\n   cut either side of the question. The progress row above owns that edge instead (one edge, one owner). */\nconst item = cn(\n  \"group/questionnaire-item min-w-0 border-t-0 outline-none\",\n  // spacing by the legend's own padding and by margins, not the fieldset's padding or a grid gap: WebKit places the\n  // rendered legend above the padding and outside the grid, Blink inside both; this reads the same in each\n  \"[&:not([hidden])]:grid [&:not([hidden])]:px-(--kl-inset) [&:not([hidden])]:pb-5 [&>*+*]:mt-3\",\n);\nconst legend = \"float-start w-full pt-[18px] [&+*]:clear-both\";\nconst title = cn(\n  \"flex scroll-my-(--kl-gutter) flex-wrap items-baseline gap-x-3 gap-y-1\",\n  \"text-[17px]/[1.3] font-(--kl-weight-display) text-pretty\",\n  \"focus:outline-none focus-visible:outline-(length:--kl-ring) focus-visible:outline-offset-4 focus-visible:outline-kl-focus focus-visible:outline-solid\",\n);\nconst optional = \"kl-code text-[12px] font-normal text-kl-ink-2\";\n\n/* an invalid step: the choice frame turns red, as a field's does */\nconst choices = cn(\n  \"group/questionnaire-choices\",\n  \"group-data-invalid/questionnaire-item:border-kl-red group-data-invalid/questionnaire-item:[--kl-cut-line:var(--kl-red)]\",\n  \"forced-colors:group-data-invalid/questionnaire-item:border-[CanvasText]\",\n);\n/* the native input covers the row (the library's), so its focus marks the row; checkbox rows tint under the pointer\n   even when checked */\nconst choice = cn(\n  \"relative\",\n  \"has-[>[data-slot=questionnaire-choice-input]:focus-visible]:outline-(length:--kl-ring)\",\n  \"has-[>[data-slot=questionnaire-choice-input]:focus-visible]:outline-offset-(--kl-ring-in)\",\n  \"has-[>[data-slot=questionnaire-choice-input]:focus-visible]:outline-kl-focus\",\n  \"has-[>[data-slot=questionnaire-choice-input]:focus-visible]:outline-solid\",\n  \"group-data-multiple/questionnaire-choices:not-data-disabled:hover:bg-kl-tint\",\n);\nconst choiceInput = \"absolute inset-0 z-1 m-0 size-full cursor-[inherit] opacity-0\";\n/* the shortcut cell is a radio meta cell, centred and narrower */\nconst choiceKey = \"justify-center px-0 font-normal [--kl-radio-meta:40px] [&[hidden]]:hidden\";\n\n/* action cells: Back at the start, Skip and Next at the end; hidden ones take no room */\nconst actions = cn(\n  \"kl-cells justify-end [&>[hidden]]:hidden\",\n  \"[&>[data-slot=button][data-kl-layout=cell]]:min-w-24 [&>[data-slot=button][data-kl-layout=cell]]:flex-none\",\n  \"[&>[data-slot=button][data-kl-layout=cell]]:[--kl-button-px:32px]\",\n);\nconst previous = \"me-auto border-e-(length:--kl-k1) border-e-kl-line\";\n\ntype ItemCtx = { headingId: string; required: boolean; multiple: boolean };\nconst ItemContext = React.createContext<ItemCtx>({ headingId: \"\", required: false, multiple: false });\n\n/**\n * The form, in one region frame. `items` declares the steps (`name`, `required`, `choices`) and drives progress,\n * navigation and validation; read the answers from FormData in `onSubmit`. `item` / `onItemChange` control the\n * step; `shortcuts=\"letters\" | \"numbers\"` turns on choice keys.\n */\nfunction Questionnaire({ className, onFocus, ...props }: React.ComponentProps<typeof Q.Root>) {\n  return (\n    <Q.Root\n      data-slot=\"questionnaire\"\n      className={cn(root, className)}\n      onFocus={(e) => {\n        onFocus?.(e);\n        // the library focuses the new step's fieldset; Keyline moves on to its heading, so the question is what\n        // a screen reader reads and where the next Tab starts. (An invalid answer is focused directly: untouched.)\n        const t = e.target as HTMLElement;\n        if (t.matches('fieldset[data-slot=\"questionnaire-item\"]'))\n          t.querySelector<HTMLElement>('[data-slot=\"questionnaire-heading\"]')?.focus();\n      }}\n      {...props}\n    />\n  );\n}\n\n/** \"Question 2 of 4\" over a Keyline Progress whose fill is the steps behind you. */\nfunction QuestionnaireProgress({\n  className,\n  label = (c: number, t: number) => `Question ${c} of ${t}`,\n}: {\n  className?: string;\n  label?: (current: number, total: number) => string;\n}) {\n  return (\n    <Q.Progress\n      render={(_props, s) => (\n        <div data-slot=\"questionnaire-progress\" className={cn(progress, className)}>\n          <span data-slot=\"questionnaire-count\" className={count}>\n            <bdi>{label(s.current, s.total)}</bdi>\n          </span>\n          <Progress\n            label={label(s.current, s.total)}\n            hideLabel\n            value={s.total ? s.current - 1 : 0}\n            max={Math.max(1, s.total)}\n            getAriaValueText={() => label(s.current, s.total)}\n          />\n        </div>\n      )}\n    />\n  );\n}\n\n/** One step: a fieldset named by its title. `required`, `multiple`, `invalid` (your own check) and `name` as the library. */\nfunction QuestionnaireItem({\n  className,\n  children,\n  required = false,\n  multiple = false,\n  ...props\n}: React.ComponentProps<typeof Q.Item>) {\n  const headingId = React.useId();\n  return (\n    <ItemContext.Provider value={{ headingId, required, multiple }}>\n      <Q.Item\n        data-slot=\"questionnaire-item\"\n        required={required}\n        multiple={multiple}\n        className={cn(item, className)}\n        {...props}\n      >\n        {children}\n      </Q.Item>\n    </ItemContext.Provider>\n  );\n}\n\n/**\n * The step's question: a heading inside the fieldset's legend. It takes focus when the step appears. Optional\n * steps say so (`optionalText`, null to hide it).\n */\nfunction QuestionnaireTitle({\n  className,\n  children,\n  level = 2,\n  optionalText = \"Optional\",\n}: {\n  className?: string;\n  children: React.ReactNode;\n  level?: 2 | 3 | 4 | 5 | 6;\n  optionalText?: React.ReactNode;\n}) {\n  const { headingId, required } = React.useContext(ItemContext);\n  const H = `h${level}` as \"h2\";\n  return (\n    <Q.Title data-slot=\"questionnaire-legend\" className={legend}>\n      <H id={headingId} tabIndex={-1} data-slot=\"questionnaire-heading\" className={cn(title, className)}>\n        {children}\n        {!required && optionalText ? (\n          <span data-slot=\"questionnaire-optional\" className={optional}>\n            {optionalText}\n          </span>\n        ) : null}\n      </H>\n    </Q.Title>\n  );\n}\n\nfunction QuestionnaireDescription({ className, ...props }: React.ComponentProps<typeof Q.Description>) {\n  return <Q.Description className={cn(fieldStyles.description, className)} {...props} />;\n}\n\n/**\n * The choices: the radio group's rows in one control frame. A single choice is covered by its ink band, which\n * stretches between rows; several choices (`multiple` on the item) are checkbox rows.\n */\nfunction QuestionnaireChoices({ className, children, ...props }: React.ComponentProps<typeof Q.Choices>) {\n  const { multiple } = React.useContext(ItemContext);\n  const ref = React.useRef<HTMLDivElement>(null);\n  const [chosen, setChosen] = React.useState(-1);\n  // the chosen row is read from the library's data-checked: it changes on a press, a key, Skip, reset or a\n  // controlled value, and only the attribute sees all of them\n  React.useLayoutEffect(() => {\n    const el = ref.current;\n    if (!el || multiple) return;\n    const read = () =>\n      setChosen(Array.from(el.querySelectorAll(\"[data-kl-item]\")).findIndex((r) => r.hasAttribute(\"data-checked\")));\n    read();\n    const mo = new MutationObserver(read);\n    mo.observe(el, { subtree: true, attributes: true, attributeFilter: [\"data-checked\"] });\n    return () => mo.disconnect();\n  }, [multiple]);\n  useStretch(ref, multiple ? -1 : chosen, { axis: \"y\" });\n  return (\n    <Q.Choices\n      ref={ref}\n      data-slot=\"questionnaire-choices\"\n      data-kl-stretch=\"\"\n      data-multiple={multiple || undefined}\n      className={cn(radioStyles.group, choices, className)}\n      {...props}\n    >\n      <span data-kl-band=\"\" className={radioStyles.band} aria-hidden hidden />\n      {children}\n    </Q.Choices>\n  );\n}\n\n/** A choice row: a native radio or checkbox under the row, the box, the label and description, the shortcut cell. */\nfunction QuestionnaireChoice({\n  className,\n  children,\n  description,\n  ...props\n}: React.ComponentProps<typeof Q.Choice> & { description?: React.ReactNode }) {\n  return (\n    <Q.Choice\n      data-slot=\"questionnaire-choice\"\n      data-kl-item=\"\"\n      className={cn(radioStyles.row, choice, className)}\n      {...props}\n      render={(rp, s) => (\n        <label {...rp}>\n          <Q.ChoiceInput data-slot=\"questionnaire-choice-input\" className={choiceInput} />\n          <span data-slot=\"questionnaire-choice-box\" className={choiceStyles.box} aria-hidden>\n            {s.type === \"radio\" ? (\n              <span\n                data-slot=\"questionnaire-choice-mark\"\n                className={radioStyles.indicator}\n                data-checked={s.checked ? \"\" : undefined}\n                data-unchecked={s.checked ? undefined : \"\"}\n              >\n                <span className={radioStyles.dot} />\n              </span>\n            ) : (\n              <span\n                data-slot=\"questionnaire-choice-mark\"\n                className={checkboxStyles.root}\n                data-checked={s.checked ? \"\" : undefined}\n                data-unchecked={s.checked ? undefined : \"\"}\n                data-disabled={s.disabled ? \"\" : undefined}\n              >\n                <span\n                  className={checkboxStyles.indicator}\n                  data-checked={s.checked ? \"\" : undefined}\n                  data-unchecked={s.checked ? undefined : \"\"}\n                />\n              </span>\n            )}\n          </span>\n          <Q.ChoiceLabel data-slot=\"questionnaire-choice-text\" className={choiceStyles.text}>\n            <span data-slot=\"questionnaire-choice-label\" className={cn(choiceStyles.label, \"wrap-anywhere\")}>\n              {children}\n            </span>\n            {description ? (\n              <span data-slot=\"questionnaire-choice-description\" className={radioStyles.description}>\n                {description}\n              </span>\n            ) : null}\n          </Q.ChoiceLabel>\n          <Q.ChoiceShortcut\n            data-slot=\"questionnaire-choice-key\"\n            className={cn(\"kl-code\", radioStyles.meta, choiceKey)}\n          />\n        </label>\n      )}\n    />\n  );\n}\n\n/**\n * A free-text answer in the field frame (the ! cell appears when the step is invalid), named by the question.\n * `type` as the library: text, email, number, url, tel, date and the other input types.\n */\nfunction QuestionnaireInput({ className, ...props }: React.ComponentProps<typeof Q.Input>) {\n  const { headingId } = React.useContext(ItemContext);\n  return (\n    <Q.Input\n      aria-labelledby={headingId}\n      {...props}\n      render={(ip, s) => (\n        <div\n          data-slot=\"questionnaire-field\"\n          className=\"kl-field\"\n          data-invalid={s.invalid ? \"\" : undefined}\n          data-disabled={s.disabled ? \"\" : undefined}\n        >\n          <FieldFrame>\n            <input {...ip} className={cn(\"kl-field-input\", className)} />\n          </FieldFrame>\n        </div>\n      )}\n    />\n  );\n}\n\n/** The step's error: the field error's look, announced (role=alert) and described on the step only while it shows. */\nfunction QuestionnaireError({ className, ...props }: React.ComponentProps<typeof Q.Error>) {\n  return (\n    <Q.Error\n      data-slot=\"questionnaire-error\"\n      className={cn(fieldStyles.error, \"[&[hidden]]:hidden\", className)}\n      {...props}\n      render={(ep) => (\n        <p {...ep}>\n          <span className=\"kl-glyph\" data-kind=\"attention\" aria-hidden />\n          <span>{ep[\"children\"] as React.ReactNode}</span>\n        </p>\n      )}\n    />\n  );\n}\n\n/** The action row: Back at the start; Skip and Next (or Submit on the last step) at the end. */\nfunction QuestionnaireActions({\n  render,\n  className,\n  style,\n  ...props\n}: PartProps<\"div\", Record<string, never>>): React.ReactElement {\n  const state = {};\n  return useRender({\n    defaultTagName: \"div\",\n    render,\n    state,\n    props: mergeProps<\"div\">(props, {\n      className: resolveClassName(actions, className)(state),\n      style: resolveStyle(undefined, style)(state),\n      \"data-slot\": \"questionnaire-actions\",\n    } as React.ComponentProps<\"div\">),\n  });\n}\n\ntype NavProps<T extends React.ElementType> = Omit<React.ComponentProps<T>, \"render\"> &\n  Pick<React.ComponentProps<typeof Button>, \"signal\">;\n\nfunction QuestionnairePrevious({ className, children, signal, ...props }: NavProps<typeof Q.Previous>) {\n  return (\n    <Q.Previous\n      className={cn(previous, className)}\n      render={<Button layout=\"cell\" variant=\"quiet\" signal={signal} />}\n      {...props}\n    >\n      {children ?? \"Back\"}\n    </Q.Previous>\n  );\n}\nfunction QuestionnaireSkip({ className, children, signal, ...props }: NavProps<typeof Q.Skip>) {\n  return (\n    <Q.Skip className={className} render={<Button layout=\"cell\" variant=\"quiet\" signal={signal} />} {...props}>\n      {children ?? \"Skip\"}\n    </Q.Skip>\n  );\n}\nfunction QuestionnaireNext({ className, children, signal, ...props }: NavProps<typeof Q.Next>) {\n  return (\n    <Q.Next className={className} render={<Button layout=\"cell\" variant=\"primary\" signal={signal} />} {...props}>\n      {children ?? \"Next\"}\n    </Q.Next>\n  );\n}\n/** Submits the form on the last step. `signal` shows the submission: pending, done, failed (Button's signals). */\nfunction QuestionnaireSubmit({ className, children, signal, ...props }: NavProps<typeof Q.Submit>) {\n  return (\n    <Q.Submit className={className} render={<Button layout=\"cell\" variant=\"primary\" signal={signal} />} {...props}>\n      {children ?? \"Submit\"}\n    </Q.Submit>\n  );\n}\n\nexport {\n  Questionnaire,\n  QuestionnaireProgress,\n  QuestionnaireItem,\n  QuestionnaireTitle,\n  QuestionnaireDescription,\n  QuestionnaireChoices,\n  QuestionnaireChoice,\n  QuestionnaireInput,\n  QuestionnaireError,\n  QuestionnaireActions,\n  QuestionnairePrevious,\n  QuestionnaireSkip,\n  QuestionnaireNext,\n  QuestionnaireSubmit,\n};\n",
      "type": "registry:ui",
      "target": "components/keyline/ui/questionnaire.tsx"
    }
  ],
  "type": "registry:ui"
}