{
  "schemaVersion": 1,
  "target": "react",
  "name": "message-actions",
  "version": "0.1.1",
  "category": "ai-elements",
  "title": "Message Actions",
  "description": "The action row under an AI reply: copy with a drawn check, thumbs up and down with a reason picker for thumbs down, regenerate, edit, branch, read aloud, and a version pager.",
  "tags": [
    "ai",
    "chat",
    "message",
    "feedback",
    "copy",
    "regenerate",
    "actions"
  ],
  "requires": {
    "projectTypes": [
      "next",
      "vite-react"
    ],
    "styling": "tailwind-v4"
  },
  "npmDependencies": {
    "@base-ui/react": "^1.9.0",
    "motion": "^14.14.0"
  },
  "registryDependencies": [
    {
      "name": "popover",
      "version": "0.2.2"
    },
    {
      "name": "tooltip",
      "version": "0.1.1"
    }
  ],
  "docsPath": "/components/message-actions",
  "props": [
    {
      "name": "text",
      "type": "string",
      "description": "Text for Copy and Read aloud. Without it there is no Copy action."
    },
    {
      "name": "onCopy",
      "type": "(text) => void",
      "description": "Called after copying."
    },
    {
      "name": "onRetry / onEdit / onBranch",
      "type": "() => void",
      "description": "Each one shows its action: Regenerate, Edit, Branch in new chat."
    },
    {
      "name": "readAloud",
      "type": "boolean",
      "default": "false",
      "description": "Shows Read aloud using the browser's speech synthesis on text."
    },
    {
      "name": "onReadAloud / speaking",
      "type": "() => void / boolean",
      "description": "Handle read aloud yourself and pass the speaking state."
    },
    {
      "name": "feedback / defaultFeedback",
      "type": "\"up\" | \"down\" | null",
      "description": "Controlled or initial feedback."
    },
    {
      "name": "onFeedback",
      "type": "(feedback, details?: { reasons, comment }) => void",
      "description": "Called when a thumb is pressed or cleared, and again with details when the thumbs down form is sent."
    },
    {
      "name": "reasons",
      "type": "string[]",
      "description": "Choices in the thumbs down form."
    },
    {
      "name": "versions",
      "type": "{ current, total, onChange }",
      "description": "Shows a pager for regenerated replies."
    },
    {
      "name": "visibility",
      "type": "\"hover\" | \"always\"",
      "default": "\"hover\"",
      "description": "hover shows the row while an ancestor with group/message is hovered or focused, and always on touch screens."
    },
    {
      "name": "children",
      "type": "ReactNode",
      "description": "Extra actions; use MessageAction for matching icon buttons."
    }
  ],
  "accessibility": [
    "The row is role=\"toolbar\" labeled Message actions; Left, Right, Home, and End move between actions, and Tab reaches each one.",
    "Every action is an icon button with an aria-label and a tooltip on hover and focus. Thumbs and Read aloud are toggles with aria-pressed.",
    "Thumbs down opens a Base UI Popover form (focus moves in, Escape closes) with reason toggles, an optional labeled textarea, Skip, and Send feedback. Pressing thumbs down again clears it.",
    "Copied, feedback sent, and feedback removed are announced in a polite live region.",
    "In hover mode the row also appears whenever focus is inside the message, while feedback is set, and on devices without hover.",
    "With reduced motion the copy check, thumb fill, and chip entrances are instant, and the thumb tip, ring, and regenerate turn do not play."
  ],
  "states": [
    "idle",
    "copied",
    "liked",
    "disliked",
    "speaking"
  ],
  "files": [
    {
      "path": "agent/message-actions.tsx",
      "content": "\"use client\";\n\nimport { useAnimate, useReducedMotion } from \"motion/react\";\nimport { type ComponentProps, type ReactNode, useEffect, useId, useRef, useState } from \"react\";\nimport { Popover, PopoverContent, PopoverTitle, PopoverTrigger } from \"../ui/popover\";\nimport { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from \"../ui/tooltip\";\n\nfunction cx(...classes: (string | false | null | undefined)[]) {\n  return classes.filter(Boolean).join(\" \");\n}\n\nconst ease = \"ease-[cubic-bezier(0.23,1,0.32,1)]\";\nconst quint = [0.23, 1, 0.32, 1] as const;\n\n/*\n * Message actions: the row under an AI reply.\n *\n * Copy swaps to a check that draws itself. A thumb tips in its direction and settles while it\n * fills, with one faint ring spreading from it; thumbs down then opens a small reason picker.\n * Regenerate turns its arrow once. Under reduced motion the swaps and fills are instant and\n * nothing tips, spins, or spreads.\n */\n\nexport type MessageFeedback = \"up\" | \"down\" | null;\n\nexport interface FeedbackDetails {\n  /** The reasons picked in the thumbs down form. */\n  reasons: string[];\n  /** Free text from the form, trimmed. */\n  comment: string;\n}\n\nexport interface MessageVersions {\n  /** 1 based index of the version shown. */\n  current: number;\n  total: number;\n  /** Called with the 1 based version to show. */\n  onChange: (version: number) => void;\n}\n\nexport interface MessageActionsProps extends Omit<ComponentProps<\"div\">, \"children\" | \"onCopy\"> {\n  /** Text for Copy and Read aloud. Without it there is no Copy action. */\n  text?: string;\n  onCopy?: (text: string) => void;\n  /** Shows Regenerate. */\n  onRetry?: () => void;\n  /** Shows Edit. */\n  onEdit?: () => void;\n  /** Shows Branch, which starts a new chat from this message. */\n  onBranch?: () => void;\n  /** Shows Read aloud using the browser's speech synthesis on `text`. */\n  readAloud?: boolean;\n  /** Handles Read aloud yourself, for example with your own text to speech. Pair with `speaking`. */\n  onReadAloud?: () => void;\n  /** Controlled speaking state for `onReadAloud`. */\n  speaking?: boolean;\n  /** Controlled feedback. */\n  feedback?: MessageFeedback;\n  defaultFeedback?: MessageFeedback;\n  /**\n   * Called when a thumb is pressed or cleared, and again with details when the thumbs down form\n   * is sent. Shows the thumbs.\n   */\n  onFeedback?: (feedback: MessageFeedback, details?: FeedbackDetails) => void;\n  /** Choices in the thumbs down form. */\n  reasons?: string[];\n  /** Shows a version pager, for replies that were regenerated. */\n  versions?: MessageVersions;\n  /**\n   * `hover` shows the row while the message (an ancestor with `group/message`) is hovered or holds\n   * focus, and always on touch screens. `always` keeps it visible.\n   */\n  visibility?: \"hover\" | \"always\";\n  /** Extra actions after the built in ones. Use `MessageAction`. */\n  children?: ReactNode;\n}\n\nconst defaultReasons = [\"Not accurate\", \"Didn't follow instructions\", \"Too long\", \"Out of date\", \"Unsafe or harmful\"];\n\n/** The action row under an assistant reply: copy, feedback, regenerate, edit, branch, read aloud, and a version pager. */\nexport function MessageActions({\n  text,\n  onCopy,\n  onRetry,\n  onEdit,\n  onBranch,\n  readAloud = false,\n  onReadAloud,\n  speaking: speakingProp,\n  feedback: feedbackProp,\n  defaultFeedback = null,\n  onFeedback,\n  reasons = defaultReasons,\n  versions,\n  visibility = \"hover\",\n  className,\n  children,\n  ...props\n}: MessageActionsProps) {\n  const [localFeedback, setLocalFeedback] = useState<MessageFeedback>(defaultFeedback);\n  const feedback = feedbackProp !== undefined ? feedbackProp : localFeedback;\n  const [formOpen, setFormOpen] = useState(false);\n  const [announcement, setAnnouncement] = useState(\"\");\n  const [thanks, setThanks] = useState(false);\n  const thanksTimer = useRef<ReturnType<typeof setTimeout>>(undefined);\n  const speech = useSpeech(text);\n  const speaking = speakingProp ?? speech.speaking;\n  const showFeedback = onFeedback !== undefined || feedbackProp !== undefined;\n  useEffect(() => () => clearTimeout(thanksTimer.current), []);\n\n  function setFeedback(next: MessageFeedback, details?: FeedbackDetails) {\n    if (feedbackProp === undefined) setLocalFeedback(next);\n    onFeedback?.(next, details);\n  }\n\n  function sayThanks() {\n    setThanks(true);\n    setAnnouncement(\"Thanks for the feedback\");\n    clearTimeout(thanksTimer.current);\n    thanksTimer.current = setTimeout(() => setThanks(false), 2400);\n  }\n\n  function pressUp() {\n    const next = feedback === \"up\" ? null : \"up\";\n    setFeedback(next);\n    if (next) sayThanks();\n    else setAnnouncement(\"Feedback removed\");\n  }\n\n  const active = feedback !== null || formOpen || speaking || thanks;\n\n  return (\n    <div\n      role=\"toolbar\"\n      aria-label=\"Message actions\"\n      data-active={active || undefined}\n      onKeyDown={(event) => {\n        // Toolbar pattern: arrows, Home, and End move between the actions.\n        const keys = [\"ArrowLeft\", \"ArrowRight\", \"Home\", \"End\"];\n        if (!keys.includes(event.key)) return;\n        const buttons = [...event.currentTarget.querySelectorAll<HTMLButtonElement>(\"button:not(:disabled)\")];\n        const at = buttons.indexOf(document.activeElement as HTMLButtonElement);\n        if (at < 0) return;\n        event.preventDefault();\n        const next = event.key === \"Home\" ? 0 : event.key === \"End\" ? buttons.length - 1 : (at + (event.key === \"ArrowRight\" ? 1 : -1) + buttons.length) % buttons.length;\n        buttons[next]?.focus();\n      }}\n      className={cx(\n        \"flex min-h-8 flex-wrap items-center gap-0.5 text-zinc-500\",\n        visibility === \"hover\" &&\n          cx(\n            \"opacity-0 transition-opacity duration-150 ease-out motion-reduce:transition-none\",\n            \"group-hover/message:opacity-100 group-focus-within/message:opacity-100 focus-within:opacity-100 data-active:opacity-100 [@media(hover:none)]:opacity-100\",\n          ),\n        className,\n      )}\n      {...props}\n    >\n      <TooltipProvider>\n        {versions ? <VersionPager {...versions} /> : null}\n        {text ? <CopyButton text={text} onCopy={onCopy} onAnnounce={setAnnouncement} /> : null}\n        {showFeedback ? (\n          <>\n            <ThumbButton direction=\"up\" pressed={feedback === \"up\"} onClick={pressUp} />\n            <Popover\n              open={formOpen}\n              onOpenChange={(open, details) => {\n                if (open && details.reason === \"trigger-press\" && feedback === \"down\") {\n                  // A second press clears the thumbs down instead of reopening the form.\n                  setFeedback(null);\n                  setAnnouncement(\"Feedback removed\");\n                  return;\n                }\n                if (open && feedback !== \"down\") setFeedback(\"down\");\n                setFormOpen(open);\n              }}\n            >\n              <ThumbButton direction=\"down\" pressed={feedback === \"down\"} popover />\n              <PopoverContent side=\"bottom\" align=\"start\" className=\"w-[min(20rem,calc(100vw-2rem))] p-0\">\n                <ReasonForm\n                  reasons={reasons}\n                  onCancel={() => setFormOpen(false)}\n                  onSubmit={(details) => {\n                    setFeedback(\"down\", details);\n                    setFormOpen(false);\n                    sayThanks();\n                  }}\n                />\n              </PopoverContent>\n            </Popover>\n          </>\n        ) : null}\n        {readAloud || onReadAloud ? (\n          <IconButton\n            label={speaking ? \"Stop reading\" : \"Read aloud\"}\n            pressed={speaking}\n            onClick={() => (onReadAloud ? onReadAloud() : speech.toggle())}\n            disabled={!onReadAloud && !speech.supported}\n          >\n            {speaking ? (\n              <>\n                <path d=\"M2.75 6.25h2l3-2.5v8.5l-3-2.5h-2z\" />\n                <rect x=\"10.25\" y=\"5.75\" width=\"4\" height=\"4.5\" rx=\"1\" />\n              </>\n            ) : (\n              <path d=\"M2.75 6.25h2l3-2.5v8.5l-3-2.5h-2zM10.5 6a2.75 2.75 0 0 1 0 4M12.25 4.25a5.25 5.25 0 0 1 0 7.5\" />\n            )}\n          </IconButton>\n        ) : null}\n        {onEdit ? (\n          <IconButton label=\"Edit\" onClick={onEdit}>\n            <path d=\"M10.25 3.25 12.75 5.75M3.25 12.75l.6-2.75 7.4-7.4a1.4 1.4 0 0 1 2 2l-7.4 7.4z\" />\n          </IconButton>\n        ) : null}\n        {onBranch ? (\n          <IconButton label=\"Branch in new chat\" onClick={onBranch}>\n            <circle cx=\"4.75\" cy=\"3.75\" r=\"1.5\" />\n            <circle cx=\"4.75\" cy=\"12.25\" r=\"1.5\" />\n            <circle cx=\"11.25\" cy=\"5.75\" r=\"1.5\" />\n            <path d=\"M4.75 5.25v5.5M11.25 7.25c0 2.25-1.5 3-3.5 3.25-1.5.2-2.6.6-3 1.3\" />\n          </IconButton>\n        ) : null}\n        {onRetry ? <RetryButton onRetry={onRetry} /> : null}\n        {children}\n      </TooltipProvider>\n      {thanks ? (\n        <span\n          aria-hidden=\"true\"\n          className={cx(\"ml-1.5 text-xs text-zinc-500 transition-[opacity,translate] duration-300 motion-reduce:transition-none\", ease, \"starting:-translate-x-1 starting:opacity-0\")}\n        >\n          Thanks for the feedback\n        </span>\n      ) : null}\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {announcement}\n      </span>\n    </div>\n  );\n}\n\n// ---------------------------------------------------------------------------\n// Buttons\n\nconst buttonClass = cx(\n  \"relative inline-flex size-8 shrink-0 cursor-pointer items-center justify-center rounded-lg text-zinc-500\",\n  \"transition-[background-color,color,scale] duration-150 ease-out active:duration-100 motion-safe:active:scale-[0.94] motion-reduce:transition-none\",\n  \"hover:bg-zinc-950/[0.04] hover:text-zinc-950 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n  \"aria-pressed:text-zinc-950 dark:aria-pressed:text-white\",\n  \"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n  \"disabled:pointer-events-none disabled:opacity-45\",\n);\n\nexport interface MessageActionProps extends Omit<ComponentProps<\"button\">, \"children\"> {\n  /** Accessible name and tooltip text. */\n  label: string;\n  /** For toggles. */\n  pressed?: boolean;\n  /** SVG content in a 16 unit box, drawn with a 1.5 stroke. */\n  children: ReactNode;\n}\n\n/** A 32px icon button with a tooltip, matching the built in actions. Pass extras as children of MessageActions. */\nexport function MessageAction({ label, pressed, children, className, ...props }: MessageActionProps) {\n  return <IconButton label={label} pressed={pressed} className={className} {...props}>{children}</IconButton>;\n}\n\nfunction IconButton({\n  label,\n  pressed,\n  children,\n  className,\n  ...props\n}: Omit<ComponentProps<\"button\">, \"children\"> & { label: string; pressed?: boolean; children: ReactNode }) {\n  return (\n    <Tooltip>\n      <TooltipTrigger\n        render={<button type=\"button\" />}\n        aria-label={label}\n        aria-pressed={pressed}\n        className={cx(buttonClass, className)}\n        {...props}\n      >\n        <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-4\">\n          {children}\n        </svg>\n      </TooltipTrigger>\n      <TooltipContent side=\"top\">{label}</TooltipContent>\n    </Tooltip>\n  );\n}\n\nfunction CopyButton({ text, onCopy, onAnnounce }: { text: string; onCopy?: (text: string) => void; onAnnounce: (message: string) => void }) {\n  const [copied, setCopied] = useState(false);\n  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);\n  useEffect(() => () => clearTimeout(timer.current), []);\n\n  async function copy() {\n    try {\n      await navigator.clipboard.writeText(text);\n      setCopied(true);\n      onAnnounce(\"Copied\");\n      onCopy?.(text);\n      clearTimeout(timer.current);\n      timer.current = setTimeout(() => setCopied(false), 1600);\n    } catch {\n      onAnnounce(\"Could not copy\");\n    }\n  }\n\n  const swap = cx(\"origin-center transition-[opacity,scale,filter] duration-200 motion-reduce:transition-none\", ease);\n  return (\n    <Tooltip>\n      <TooltipTrigger render={<button type=\"button\" />} aria-label={copied ? \"Copied\" : \"Copy\"} onClick={copy} className={buttonClass}>\n        <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-4 overflow-visible\">\n          <g className={cx(swap, copied ? \"scale-50 opacity-0 blur-[1px]\" : \"scale-100 opacity-100\")}>\n            <rect x=\"5.25\" y=\"5.25\" width=\"8\" height=\"8\" rx=\"1.75\" />\n            <path d=\"M10.75 5.25V3.5c0-.97-.78-1.75-1.75-1.75H3.5c-.97 0-1.75.78-1.75 1.75V9c0 .97.78 1.75 1.75 1.75h1.75\" />\n          </g>\n          {/* The check draws itself: dashoffset runs from the full length to 0. */}\n          <path\n            d=\"m3.5 8.5 3 3 6-7\"\n            pathLength={1}\n            className={cx(\n              \"text-emerald-600 [stroke-dasharray:1] dark:text-emerald-400\",\n              \"transition-[stroke-dashoffset,opacity] motion-reduce:transition-none\",\n              ease,\n              copied ? \"opacity-100 [stroke-dashoffset:0] delay-75 duration-300\" : \"opacity-0 [stroke-dashoffset:1] duration-150\",\n            )}\n          />\n        </svg>\n      </TooltipTrigger>\n      <TooltipContent side=\"top\">{copied ? \"Copied\" : \"Copy\"}</TooltipContent>\n    </Tooltip>\n  );\n}\n\nfunction ThumbButton({\n  direction,\n  pressed,\n  popover = false,\n  onClick,\n}: {\n  direction: \"up\" | \"down\";\n  pressed: boolean;\n  popover?: boolean;\n  onClick?: () => void;\n}) {\n  const [scope, animate] = useAnimate();\n  const reduce = useReducedMotion();\n  const was = useRef(pressed);\n\n  useEffect(() => {\n    if (pressed && !was.current && !reduce && scope.current) {\n      const tip = direction === \"up\" ? -18 : 18;\n      const lift = direction === \"up\" ? -2 : 2;\n      // Tip toward its direction, then settle; the ring spreads once.\n      void animate(\"[data-thumb]\", { rotate: [0, tip, 0], y: [0, lift, 0], scale: [1, 1.12, 1] }, { duration: 0.42, ease: quint, times: [0, 0.35, 1] });\n      void animate(\"[data-ring]\", { scale: [0.6, 1.5], opacity: [0.35, 0] }, { duration: 0.5, ease: quint });\n    }\n    was.current = pressed;\n  }, [pressed, reduce, direction, animate, scope]);\n\n  const label = direction === \"up\" ? \"Good response\" : \"Bad response\";\n  const path =\n    \"M5.25 7.25 7.6 2.6a1.35 1.35 0 0 1 2.5.9l-.5 2.75h2.9a1.5 1.5 0 0 1 1.47 1.8l-.9 4.5a1.5 1.5 0 0 1-1.47 1.2H5.25zM5.25 7.25h-2a.5.5 0 0 0-.5.5v5.5a.5.5 0 0 0 .5.5h2\";\n\n  const content = (\n    <span ref={scope} className=\"relative flex size-4 items-center justify-center\">\n      <span data-ring aria-hidden=\"true\" className=\"pointer-events-none absolute -inset-2 rounded-full border border-current opacity-0\" />\n      <svg\n        data-thumb\n        aria-hidden=\"true\"\n        viewBox=\"0 0 16 16\"\n        stroke=\"currentColor\"\n        strokeWidth=\"1.5\"\n        strokeLinecap=\"round\"\n        strokeLinejoin=\"round\"\n        className=\"size-4 overflow-visible\"\n        style={{ transformOrigin: \"50% 55%\" }}\n      >\n        {/* Thumbs down is the same glyph turned over, so both tip toward where they point. */}\n        <path\n          d={path}\n          transform={direction === \"down\" ? \"rotate(180 8 8)\" : undefined}\n          fill=\"currentColor\"\n          style={{ fillOpacity: pressed ? 0.9 : 0 }}\n          className={cx(\"transition-[fill-opacity] duration-200 motion-reduce:transition-none\", ease)}\n        />\n      </svg>\n    </span>\n  );\n\n  const shared = { \"aria-label\": label, \"aria-pressed\": pressed, className: buttonClass };\n  return (\n    <Tooltip>\n      <TooltipTrigger\n        render={popover ? <PopoverTrigger render={<button type=\"button\" />} /> : <button type=\"button\" onClick={onClick} />}\n        {...shared}\n      >\n        {content}\n      </TooltipTrigger>\n      <TooltipContent side=\"top\">{label}</TooltipContent>\n    </Tooltip>\n  );\n}\n\nfunction RetryButton({ onRetry }: { onRetry: () => void }) {\n  const [scope, animate] = useAnimate();\n  const reduce = useReducedMotion();\n  return (\n    <Tooltip>\n      <TooltipTrigger\n        render={<button type=\"button\" />}\n        aria-label=\"Regenerate\"\n        className={buttonClass}\n        onClick={() => {\n          if (!reduce && scope.current) void animate(scope.current, { rotate: [0, -360] }, { duration: 0.5, ease: quint });\n          onRetry();\n        }}\n      >\n        <svg ref={scope} aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-4\">\n          <path d=\"M12.75 8a4.75 4.75 0 1 1-1.39-3.36M12.75 2.75v2.5h-2.5\" />\n        </svg>\n      </TooltipTrigger>\n      <TooltipContent side=\"top\">Regenerate</TooltipContent>\n    </Tooltip>\n  );\n}\n\nfunction VersionPager({ current, total, onChange }: MessageVersions) {\n  const arrow = cx(buttonClass, \"size-7\");\n  return (\n    <span className=\"mr-1 flex items-center border-r border-zinc-950/[0.07] pr-1 dark:border-white/[0.08]\">\n      <button type=\"button\" aria-label=\"Previous version\" disabled={current <= 1} onClick={() => onChange(current - 1)} className={arrow}>\n        <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-4\">\n          <path d=\"M9.75 4.5 6.25 8l3.5 3.5\" />\n        </svg>\n      </button>\n      <span className=\"min-w-8 text-center font-mono text-xs text-zinc-600 tabular-nums dark:text-zinc-400\">\n        <span className=\"sr-only\">Version </span>\n        {current}/{total}\n      </span>\n      <button type=\"button\" aria-label=\"Next version\" disabled={current >= total} onClick={() => onChange(current + 1)} className={arrow}>\n        <svg aria-hidden=\"true\" viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" className=\"size-4\">\n          <path d=\"m6.25 4.5 3.5 3.5-3.5 3.5\" />\n        </svg>\n      </button>\n    </span>\n  );\n}\n\n// ---------------------------------------------------------------------------\n// Thumbs down form\n\nfunction ReasonForm({ reasons, onSubmit, onCancel }: { reasons: string[]; onSubmit: (details: FeedbackDetails) => void; onCancel: () => void }) {\n  const [picked, setPicked] = useState<string[]>([]);\n  const [comment, setComment] = useState(\"\");\n  const commentId = useId();\n\n  return (\n    <form\n      className=\"flex flex-col\"\n      onSubmit={(event) => {\n        event.preventDefault();\n        onSubmit({ reasons: picked, comment: comment.trim() });\n      }}\n    >\n      <div className=\"px-4 pt-3.5 pb-3\">\n        <PopoverTitle>What went wrong?</PopoverTitle>\n        <div role=\"group\" aria-label=\"Reasons\" className=\"mt-3 flex flex-wrap gap-1.5\">\n          {reasons.map((reason, index) => {\n            const on = picked.includes(reason);\n            return (\n              <button\n                key={reason}\n                type=\"button\"\n                aria-pressed={on}\n                onClick={() => setPicked((current) => (on ? current.filter((item) => item !== reason) : [...current, reason]))}\n                style={{ transitionDelay: `0ms, 0ms, 0ms, 0ms, ${index * 24}ms, ${index * 24}ms` }}\n                className={cx(\n                  \"inline-flex h-7 cursor-pointer items-center gap-1 rounded-md border px-2 text-xs font-medium select-none\",\n                  \"transition-[background-color,border-color,color,scale,opacity,translate] duration-200 motion-reduce:transition-none\",\n                  ease,\n                  \"starting:translate-y-1 starting:opacity-0 motion-safe:active:scale-[0.97]\",\n                  \"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n                  on\n                    ? \"border-zinc-950 bg-zinc-950 text-white dark:border-white dark:bg-white dark:text-black\"\n                    : \"border-zinc-950/12 text-zinc-700 hover:border-zinc-950/25 hover:bg-zinc-950/[0.03] dark:border-white/15 dark:text-zinc-300 dark:hover:border-white/30 dark:hover:bg-white/[0.06]\",\n                )}\n              >\n                {reason}\n              </button>\n            );\n          })}\n        </div>\n        <label htmlFor={commentId} className=\"mt-3 block text-xs font-medium text-zinc-500\">\n          Details, optional\n        </label>\n        <textarea\n          id={commentId}\n          value={comment}\n          onChange={(event) => setComment(event.target.value)}\n          rows={2}\n          placeholder=\"What should the answer have said?\"\n          className={cx(\n            \"mt-1.5 block w-full resize-none rounded-lg border border-zinc-950/12 bg-white px-2.5 py-1.5 text-sm text-zinc-950 placeholder:text-zinc-500\",\n            \"transition-[border-color] duration-150 ease-out hover:border-zinc-950/20 motion-reduce:transition-none\",\n            \"dark:border-white/15 dark:bg-black dark:text-white dark:hover:border-white/25\",\n            \"focus-visible:border-sky-500/60 focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-0 focus-visible:outline-sky-500/40\",\n          )}\n        />\n      </div>\n      <div className=\"flex justify-end gap-1.5 border-t border-zinc-950/[0.07] px-3 py-2.5 dark:border-white/[0.08]\">\n        <button\n          type=\"button\"\n          onClick={onCancel}\n          className={cx(\n            \"inline-flex h-8 cursor-pointer items-center rounded-lg px-3 text-sm font-medium text-zinc-600 select-none\",\n            \"transition-[background-color,color,scale] duration-150 ease-out motion-safe:active:scale-[0.97] motion-reduce:transition-none\",\n            \"hover:bg-zinc-950/[0.04] hover:text-zinc-950 dark:text-zinc-400 dark:hover:bg-white/[0.06] dark:hover:text-white\",\n            \"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n          )}\n        >\n          Skip\n        </button>\n        <button\n          type=\"submit\"\n          className={cx(\n            \"inline-flex h-8 cursor-pointer items-center rounded-lg px-3 text-sm font-medium select-none\",\n            \"bg-zinc-950 text-white shadow-[inset_0_1px_0_rgb(255_255_255/0.12),0_1px_2px_rgb(0_0_0/0.2)] hover:bg-zinc-800\",\n            \"dark:bg-white dark:text-black dark:shadow-[inset_0_-1px_0_rgb(0_0_0/0.12)] dark:hover:bg-zinc-200\",\n            \"transition-[background-color,scale] duration-150 ease-out motion-safe:active:scale-[0.97] motion-reduce:transition-none\",\n            \"focus-visible:outline-2 focus-visible:outline-solid focus-visible:outline-offset-2 focus-visible:outline-sky-500\",\n          )}\n        >\n          Send feedback\n        </button>\n      </div>\n    </form>\n  );\n}\n\n// ---------------------------------------------------------------------------\n// Speech\n\nfunction useSpeech(text: string | undefined) {\n  const [speaking, setSpeaking] = useState(false);\n  const [supported, setSupported] = useState(false);\n  useEffect(() => {\n    setSupported(typeof window !== \"undefined\" && \"speechSynthesis\" in window);\n    return () => {\n      if (typeof window !== \"undefined\" && \"speechSynthesis\" in window) window.speechSynthesis.cancel();\n    };\n  }, []);\n\n  function toggle() {\n    if (!supported || !text) return;\n    const synth = window.speechSynthesis;\n    if (speaking) {\n      synth.cancel();\n      setSpeaking(false);\n      return;\n    }\n    synth.cancel();\n    const utterance = new SpeechSynthesisUtterance(text);\n    utterance.onend = () => setSpeaking(false);\n    utterance.onerror = () => setSpeaking(false);\n    synth.speak(utterance);\n    setSpeaking(true);\n  }\n\n  return { speaking, supported, toggle };\n}\n"
    }
  ],
  "example": {
    "content": "\"use client\";\n\nimport { useState } from \"react\";\nimport { MessageActions, type MessageFeedback } from \"@/components/shaken/agent/message-actions\";\n\nconst replies = [\n  \"The invoice is off by a cent because proration rounds each line before adding them up. Two half cents both round down, so the total shows $12.33 instead of $12.34. Rounding once, after the sum, fixes it.\",\n  \"Proration rounds every line item on its own, then adds the rounded amounts. With two lines ending in half a cent, both round down and the invoice loses a cent. Sum first and round the total once.\",\n];\n\nexport default function MessageActionsExample() {\n  const [version, setVersion] = useState(2);\n  const [feedback, setFeedback] = useState<MessageFeedback>(null);\n  const text = replies[version - 1]!;\n\n  return (\n    <div className=\"group/message w-full max-w-xl\">\n      <p className=\"text-sm leading-relaxed text-zinc-800 dark:text-zinc-200\">{text}</p>\n      <MessageActions\n        className=\"mt-1.5 -ml-1.5\"\n        visibility=\"always\"\n        text={text}\n        feedback={feedback}\n        onFeedback={(next) => setFeedback(next)}\n        versions={{ current: version, total: replies.length, onChange: setVersion }}\n        onRetry={() => {}}\n        onEdit={() => {}}\n        onBranch={() => {}}\n        readAloud\n      />\n    </div>\n  );\n}\n"
  }
}
