{
  "name": "helpful-rating",
  "type": "registry:component",
  "dependencies": [
    "lucide-react",
    "motion"
  ],
  "files": [
    {
      "type": "registry:component",
      "content": "\"use client\";\n\n/**\n * @author: @kokonutui\n * @description: Helpful Rating\n * @version: 1.0.0\n * @date: 2026-11-08\n * @license: MIT\n * @website: https://kokonutui.com\n * @github: https://github.com/kokonut-labs/kokonutui\n */\n\nimport { Check } from \"lucide-react\";\nimport { AnimatePresence, MotionConfig, motion } from \"motion/react\";\nimport { type KeyboardEvent, useId, useRef, useState } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface HelpfulRatingProps {\n  question?: string;\n  onSubmit?: (feedback: {\n    rating: number;\n    reasons: string[];\n    note: string;\n  }) => void;\n  className?: string;\n}\n\ninterface Level {\n  label: string;\n  color: string;\n  /** How far the mouth bends: below 0 frowns, above 0 smiles. */\n  mouth: number;\n  /** Brows tilt up in the middle when sad; 0 hides them. */\n  brow: number;\n  /** Eye height; small when the face is grinning. */\n  eye: number;\n}\n\nconst LEVELS: Level[] = [\n  { label: \"Awful\", color: \"#f43f5e\", mouth: -13, brow: 6, eye: 6 },\n  { label: \"Meh\", color: \"#f97316\", mouth: -6, brow: 3, eye: 6 },\n  { label: \"Okay\", color: \"#a1a1aa\", mouth: 0, brow: 0, eye: 6 },\n  { label: \"Good\", color: \"#0ea5e9\", mouth: 9, brow: 0, eye: 5.5 },\n  { label: \"Love it\", color: \"#10b981\", mouth: 17, brow: 0, eye: 3 },\n];\n\nconst LOW_REASONS = [\n  \"Hard to follow\",\n  \"Missing an example\",\n  \"Out of date\",\n  \"Code didn't work\",\n];\nconst HIGH_REASONS = [\"Clear steps\", \"Good examples\", \"Found it fast\"];\n\nconst EASE_OUT = [0.23, 1, 0.32, 1] as const;\nconst FACE = { type: \"spring\", stiffness: 400, damping: 30 } as const;\nconst SLIDE = { type: \"spring\", stiffness: 500, damping: 38 } as const;\nconst MIDDLE = 2;\n\nexport default function HelpfulRating({\n  question = \"Was this page helpful?\",\n  onSubmit,\n  className,\n}: HelpfulRatingProps) {\n  const uid = useId();\n  const [rating, setRating] = useState<number | null>(null);\n  const [preview, setPreview] = useState<number | null>(null);\n  const [reasons, setReasons] = useState<string[]>([]);\n  const [note, setNote] = useState(\"\");\n  const [sent, setSent] = useState(false);\n  const buttons = useRef<(HTMLButtonElement | null)[]>([]);\n\n  const shown = preview ?? rating ?? MIDDLE;\n  // Once sent, the face smiles a thank-you in the color of the answer.\n  const level = sent\n    ? { ...LEVELS[4], color: LEVELS[shown].color }\n    : LEVELS[shown];\n  const low = rating !== null && rating < MIDDLE;\n  const choices = low ? LOW_REASONS : HIGH_REASONS;\n\n  const pick = (index: number) => {\n    if (rating !== null && index < MIDDLE !== rating < MIDDLE) {\n      setReasons([]);\n    }\n    setRating(index);\n  };\n\n  // Arrow keys move between answers like a radio group.\n  const onKeyDown = (event: KeyboardEvent<HTMLButtonElement>, i: number) => {\n    const step = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[\n      event.key\n    ];\n    if (step) {\n      event.preventDefault();\n      const next = (i + step + LEVELS.length) % LEVELS.length;\n      buttons.current[next]?.focus();\n      pick(next);\n    }\n  };\n\n  const toggle = (reason: string) =>\n    setReasons((all) =>\n      all.includes(reason) ? all.filter((r) => r !== reason) : [...all, reason]\n    );\n\n  const reset = () => {\n    setRating(null);\n    setPreview(null);\n    setReasons([]);\n    setNote(\"\");\n    setSent(false);\n  };\n\n  const focusable = rating ?? MIDDLE;\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div\n        className={cn(\n          \"w-full max-w-sm overflow-hidden rounded-[24px] border border-zinc-200 bg-white p-5 text-zinc-900 dark:border-zinc-800 dark:bg-zinc-950 dark:text-zinc-100\",\n          className\n        )}\n      >\n        <div className=\"flex flex-col items-center text-center\">\n          <Face level={level} look={shown - MIDDLE} sent={sent} />\n          <div className=\"relative mt-3 h-6 w-full\">\n            <AnimatePresence initial={false} mode=\"popLayout\">\n              <motion.p\n                animate={{ opacity: 1, y: 0, filter: \"blur(0px)\" }}\n                className=\"font-semibold text-[16px] tracking-tight\"\n                exit={{ opacity: 0, y: -6, filter: \"blur(2px)\" }}\n                initial={{ opacity: 0, y: 6, filter: \"blur(2px)\" }}\n                key={sent ? \"sent\" : \"ask\"}\n                transition={{ duration: 0.22, ease: EASE_OUT }}\n              >\n                {sent ? \"Thanks, that helps\" : question}\n              </motion.p>\n            </AnimatePresence>\n          </div>\n          <div className=\"relative mt-0.5 h-5 w-full text-[13px] text-zinc-500 dark:text-zinc-400\">\n            <AnimatePresence initial={false} mode=\"popLayout\">\n              <motion.p\n                animate={{ opacity: 1, y: 0 }}\n                exit={{ opacity: 0, y: -4 }}\n                initial={{ opacity: 0, y: 4 }}\n                key={sent ? \"sent\" : level.label}\n                transition={{ duration: 0.15, ease: EASE_OUT }}\n              >\n                {sent ? \"We read every note.\" : level.label}\n              </motion.p>\n            </AnimatePresence>\n          </div>\n        </div>\n\n        <AnimatePresence initial={false}>\n          {!sent && (\n            <motion.div\n              animate={{ height: \"auto\", opacity: 1 }}\n              className=\"overflow-hidden\"\n              exit={{\n                height: 0,\n                opacity: 0,\n                transition: {\n                  height: { duration: 0.3, ease: EASE_OUT },\n                  opacity: { duration: 0.15 },\n                },\n              }}\n              initial={{ height: 0, opacity: 0 }}\n              transition={{ duration: 0.3, ease: EASE_OUT }}\n            >\n              <div\n                aria-label={question}\n                className=\"mt-4 grid grid-cols-5 gap-1.5\"\n                onPointerLeave={() => setPreview(null)}\n                role=\"radiogroup\"\n              >\n                {LEVELS.map((l, i) => {\n                  const selected = rating === i;\n                  return (\n                    // biome-ignore lint/a11y/useSemanticElements: face buttons acting as a radio group, with arrow keys handled here\n                    <button\n                      aria-checked={selected}\n                      aria-label={l.label}\n                      className={cn(\n                        \"relative flex h-10 items-center justify-center rounded-xl bg-zinc-100 outline-hidden transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-zinc-400/50 dark:bg-zinc-900\",\n                        selected\n                          ? \"text-white\"\n                          : \"text-zinc-500 hover:bg-zinc-200 hover:text-zinc-800 active:bg-zinc-300 dark:text-zinc-400 dark:active:bg-zinc-700 dark:hover:bg-zinc-800 dark:hover:text-zinc-100\"\n                      )}\n                      key={l.label}\n                      onBlur={() => setPreview(null)}\n                      onClick={() => pick(i)}\n                      onFocus={() => setPreview(i)}\n                      onKeyDown={(event) => onKeyDown(event, i)}\n                      onPointerEnter={(event) => {\n                        if (event.pointerType === \"mouse\") {\n                          setPreview(i);\n                        }\n                      }}\n                      ref={(el) => {\n                        buttons.current[i] = el;\n                      }}\n                      role=\"radio\"\n                      tabIndex={i === focusable ? 0 : -1}\n                      type=\"button\"\n                    >\n                      {/* One pill slides to the answer and takes its color. */}\n                      {selected && (\n                        <motion.span\n                          aria-hidden=\"true\"\n                          className=\"absolute inset-0 rounded-xl\"\n                          layoutId={`${uid}-picked`}\n                          style={{\n                            backgroundColor: l.color,\n                            transition: \"background-color 200ms ease-out\",\n                          }}\n                          transition={SLIDE}\n                        />\n                      )}\n                      <MiniFace bend={l.mouth} />\n                    </button>\n                  );\n                })}\n              </div>\n\n              <AnimatePresence initial={false}>\n                {rating !== null && (\n                  <motion.div\n                    animate={{ height: \"auto\", opacity: 1 }}\n                    className=\"overflow-hidden\"\n                    exit={{ height: 0, opacity: 0 }}\n                    initial={{ height: 0, opacity: 0 }}\n                    transition={{\n                      height: { duration: 0.3, ease: EASE_OUT },\n                      opacity: { duration: 0.2, delay: 0.05 },\n                    }}\n                  >\n                    <div className=\"relative h-9\">\n                      <AnimatePresence initial={false} mode=\"popLayout\">\n                        <motion.p\n                          animate={{ opacity: 1, y: 0 }}\n                          className=\"pt-4 text-[13px] text-zinc-500 dark:text-zinc-400\"\n                          exit={{ opacity: 0, y: -4 }}\n                          initial={{ opacity: 0, y: 4 }}\n                          key={low ? \"low\" : \"high\"}\n                          transition={{ duration: 0.18, ease: EASE_OUT }}\n                        >\n                          {low ? \"What went wrong?\" : \"What helped most?\"}\n                        </motion.p>\n                      </AnimatePresence>\n                    </div>\n                    <motion.div\n                      animate=\"shown\"\n                      className=\"mt-2 flex min-h-[72px] flex-wrap content-start gap-1.5\"\n                      initial=\"hidden\"\n                      key={low ? \"low\" : \"high\"}\n                      transition={{ staggerChildren: 0.035 }}\n                    >\n                      {choices.map((reason) => (\n                        <Chip\n                          key={reason}\n                          on={reasons.includes(reason)}\n                          onClick={() => toggle(reason)}\n                          reason={reason}\n                        />\n                      ))}\n                    </motion.div>\n                    <textarea\n                      aria-label=\"Anything else?\"\n                      className=\"mt-1 block h-20 w-full resize-none rounded-xl bg-zinc-100 px-3 py-2 text-[16px] outline-hidden placeholder:text-zinc-400 focus-visible:ring-2 focus-visible:ring-zinc-400/50 sm:text-[14px] dark:bg-zinc-900 dark:placeholder:text-zinc-500\"\n                      onChange={(event) => setNote(event.target.value)}\n                      placeholder=\"Anything else? (optional)\"\n                      value={note}\n                    />\n                    <button\n                      className=\"mt-3 h-10 w-full rounded-xl bg-zinc-900 font-medium text-[14px] text-white outline-hidden transition-colors duration-150 hover:bg-zinc-800 focus-visible:ring-2 focus-visible:ring-zinc-400/50 active:bg-zinc-700 dark:bg-white dark:text-zinc-900 dark:active:bg-zinc-300 dark:hover:bg-zinc-200\"\n                      onClick={() => {\n                        setPreview(null);\n                        setSent(true);\n                        onSubmit?.({ rating: rating + 1, reasons, note });\n                      }}\n                      type=\"button\"\n                    >\n                      Send feedback\n                    </button>\n                  </motion.div>\n                )}\n              </AnimatePresence>\n            </motion.div>\n          )}\n        </AnimatePresence>\n\n        <AnimatePresence initial={false}>\n          {sent && (\n            <motion.button\n              animate={{ opacity: 1 }}\n              className=\"mx-auto mt-3 block text-[12.5px] text-zinc-500 underline-offset-4 outline-hidden hover:text-zinc-900 hover:underline focus-visible:underline dark:text-zinc-400 dark:hover:text-zinc-100\"\n              exit={{ opacity: 0, transition: { duration: 0.1 } }}\n              initial={{ opacity: 0 }}\n              onClick={reset}\n              transition={{ duration: 0.2, delay: 0.3 }}\n              type=\"button\"\n            >\n              Change my answer\n            </motion.button>\n          )}\n        </AnimatePresence>\n      </div>\n    </MotionConfig>\n  );\n}\n\nconst CHIP = {\n  hidden: { opacity: 0, y: 4, filter: \"blur(2px)\" },\n  shown: {\n    opacity: 1,\n    y: 0,\n    filter: \"blur(0px)\",\n    transition: { duration: 0.22, ease: EASE_OUT },\n  },\n};\n\nfunction Chip({\n  reason,\n  on,\n  onClick,\n}: {\n  reason: string;\n  on: boolean;\n  onClick: () => void;\n}) {\n  return (\n    <motion.button\n      aria-pressed={on}\n      className={cn(\n        \"flex h-8 items-center gap-1 rounded-full border px-3 text-[12.5px] outline-hidden transition-colors duration-150 focus-visible:ring-2 focus-visible:ring-zinc-400/50\",\n        on\n          ? \"border-zinc-900 bg-zinc-900 text-white dark:border-white dark:bg-white dark:text-zinc-900\"\n          : \"border-zinc-200 hover:border-zinc-400 dark:border-zinc-800 dark:hover:border-zinc-600\"\n      )}\n      layout\n      onClick={onClick}\n      style={{ borderRadius: 999 }}\n      transition={{ layout: { type: \"spring\", stiffness: 500, damping: 40 } }}\n      type=\"button\"\n      variants={CHIP}\n    >\n      <AnimatePresence initial={false} mode=\"popLayout\">\n        {on && (\n          <motion.span\n            animate={{ opacity: 1, scale: 1 }}\n            className=\"-ml-0.5 flex\"\n            exit={{ opacity: 0, scale: 0.6, transition: { duration: 0.1 } }}\n            initial={{ opacity: 0, scale: 0.6 }}\n            transition={{ duration: 0.18, ease: EASE_OUT }}\n          >\n            <Check aria-hidden=\"true\" className=\"size-3.5\" />\n          </motion.span>\n        )}\n      </AnimatePresence>\n      <motion.span layout=\"position\">{reason}</motion.span>\n    </motion.button>\n  );\n}\n\nconst mouthPath = (bend: number) => `M33 64 Q50 ${64 + bend} 67 64`;\nconst browPath = (side: -1 | 1, tilt: number) => {\n  const outer = side === -1 ? 26 : 74;\n  const inner = side === -1 ? 42 : 58;\n  return `M${outer} ${30 + tilt * 0.4} L${inner} ${30 - tilt}`;\n};\n\n/**\n * One face that bends its mouth, brows and eyes toward the answer, and\n * glances and leans toward the one under the pointer.\n */\nfunction Face({\n  level,\n  look,\n  sent,\n}: {\n  level: Level;\n  look: number;\n  sent: boolean;\n}) {\n  return (\n    <motion.div\n      animate={sent ? { y: [0, -8, 0] } : { y: 0 }}\n      className=\"size-20\"\n      transition={\n        sent ? { duration: 0.45, times: [0, 0.4, 1], ease: EASE_OUT } : FACE\n      }\n    >\n      <motion.svg\n        animate={{ rotate: look * 3 }}\n        aria-hidden=\"true\"\n        className=\"size-20\"\n        initial={false}\n        transition={FACE}\n        viewBox=\"0 0 100 100\"\n      >\n        <circle\n          cx={50}\n          cy={50}\n          r={47}\n          style={{ fill: level.color, transition: \"fill 200ms ease-out\" }}\n        />\n        <motion.g animate={{ x: look * 2.5 }} initial={false} transition={FACE}>\n          {([-1, 1] as const).map((side) => (\n            <motion.path\n              animate={{\n                d: browPath(side, level.brow),\n                opacity: level.brow ? 1 : 0,\n              }}\n              fill=\"none\"\n              initial={false}\n              key={`brow${side}`}\n              stroke=\"white\"\n              strokeLinecap=\"round\"\n              strokeWidth={4}\n              transition={FACE}\n            />\n          ))}\n          {[36, 64].map((cx) => (\n            <motion.ellipse\n              animate={{ ry: level.eye }}\n              cx={cx}\n              cy={44}\n              fill=\"white\"\n              initial={false}\n              key={cx}\n              rx={5}\n              transition={FACE}\n            />\n          ))}\n          <motion.path\n            animate={{ d: mouthPath(level.mouth) }}\n            fill=\"none\"\n            initial={false}\n            stroke=\"white\"\n            strokeLinecap=\"round\"\n            strokeWidth={5}\n            transition={FACE}\n          />\n        </motion.g>\n      </motion.svg>\n    </motion.div>\n  );\n}\n\nfunction MiniFace({ bend }: { bend: number }) {\n  return (\n    <svg\n      aria-hidden=\"true\"\n      className=\"relative size-6\"\n      fill=\"none\"\n      viewBox=\"0 0 24 24\"\n    >\n      <circle cx={8.5} cy={9} fill=\"currentColor\" r={1.8} />\n      <circle cx={15.5} cy={9} fill=\"currentColor\" r={1.8} />\n      <path\n        d={`M7 15 Q12 ${15 + bend * 0.35} 17 15`}\n        stroke=\"currentColor\"\n        strokeLinecap=\"round\"\n        strokeWidth={2.4}\n      />\n    </svg>\n  );\n}\n",
      "path": "/components/kokonutui/helpful-rating.tsx",
      "target": "components/kokonutui/helpful-rating.tsx"
    }
  ]
}