Thinking UI

Message Actions

Copy, regenerate and feedback buttons with small rewards.

Code

components/thinking-ui/message-actions.tsx
"use client";

import { AnimatePresence, motion } from "motion/react";
import { useEffect, useState, type ReactNode } from "react";
import { useMotion } from "@/lib/motion";
import { cn } from "@/lib/utils";

type Feedback = "up" | "down" | null;

export type MessageActionsProps = {
  onCopy?: () => void;
  onRegenerate?: () => void;
  onFeedback?: (value: Feedback) => void;
  className?: string;
};

export function MessageActions({ onCopy, onRegenerate, onFeedback, className }: MessageActionsProps) {
  const m = useMotion();
  const [copied, setCopied] = useState(false);
  const [spins, setSpins] = useState(0);
  const [feedback, setFeedback] = useState<Feedback>(null);
  const { ms } = m;

  useEffect(() => {
    if (!copied) return;
    const id = setTimeout(() => setCopied(false), ms(1600));
    return () => clearTimeout(id);
  }, [copied, ms]);

  const vote = (value: "up" | "down") => {
    const next = feedback === value ? null : value;
    setFeedback(next);
    onFeedback?.(next);
  };

  const pop = m.pop();

  return (
    <div role="toolbar" aria-label="Message actions" className={cn("inline-flex items-center gap-0.5", className)}>
      <ActionButton
        label={copied ? "Copied" : "Copy"}
        onClick={() => {
          setCopied(true);
          onCopy?.();
        }}
      >
        {/* Both icons share one grid cell, so they crossfade in place: popLayout
            can't measure SVG elements and would stack them for a frame */}
        <span className="grid place-items-center [&>*]:[grid-area:1/1]">
          <AnimatePresence mode="popLayout" initial={false}>
            {copied ? (
              <motion.svg key="check" viewBox="0 0 16 16" className="size-icon-sm text-fg-success" {...pop}>
                <path d="M3.5 8.5l3 3 6-7" fill="none" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
              </motion.svg>
            ) : (
              <motion.svg key="copy" viewBox="0 0 16 16" className="size-icon-sm" {...pop}>
                <rect x="5" y="5" width="8.5" height="8.5" rx="2" fill="none" stroke="currentColor" strokeWidth="1.4" />
                <path d="M11 5V4a1.5 1.5 0 00-1.5-1.5H4A1.5 1.5 0 002.5 4v5.5A1.5 1.5 0 004 11h1" fill="none" stroke="currentColor" strokeWidth="1.4" />
              </motion.svg>
            )}
          </AnimatePresence>
        </span>
      </ActionButton>

      <ActionButton
        label="Regenerate"
        onClick={() => {
          setSpins((s) => s + 1);
          onRegenerate?.();
        }}
      >
        <motion.svg viewBox="0 0 16 16" className="size-icon-sm" animate={{ rotate: spins * 360 }} transition={m.spring("emphasis")}>
          <path d="M13.5 8a5.5 5.5 0 11-1.6-3.9M13.5 2.5v3h-3" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
        </motion.svg>
      </ActionButton>

      <span aria-hidden className="mx-1 h-4 w-px bg-line" />

      {(["up", "down"] as const).map((dir) => {
        const active = feedback === dir;
        return (
          <ActionButton key={dir} label={dir === "up" ? "Good response" : "Bad response"} pressed={active} onClick={() => vote(dir)}>
            <span className="relative grid place-items-center">
              <motion.svg
                viewBox="0 0 16 16"
                className={cn("size-icon-sm", active && (dir === "up" ? "text-fg-success" : "text-fg-danger"))}
                style={{ rotate: dir === "down" ? 180 : 0 }}
                animate={{ scale: active ? [1, 1.35, 1] : 1, y: active ? [0, dir === "up" ? -3 : 3, 0] : 0 }}
                // Keyframed pop: springs only take two keyframes
                transition={m.tween("slower", "out")}
              >
                <path
                  d="M5 7v6.5H3a.5.5 0 01-.5-.5V7.5A.5.5 0 013 7h2zm0 0l2.6-4.4a1.2 1.2 0 012.2.8L9.4 6h3.1a1.2 1.2 0 011.2 1.4l-.9 5a1.2 1.2 0 01-1.2 1H5"
                  fill={active ? "currentColor" : "none"}
                  fillOpacity={0.2}
                  stroke="currentColor"
                  strokeWidth="1.3"
                  strokeLinejoin="round"
                />
              </motion.svg>
              <AnimatePresence>{active && <Burst key={dir} tone={dir === "up" ? "success" : "danger"} />}</AnimatePresence>
            </span>
          </ActionButton>
        );
      })}
    </div>
  );
}

function ActionButton({
  label,
  pressed,
  onClick,
  children,
}: {
  label: string;
  pressed?: boolean;
  onClick: () => void;
  children: ReactNode;
}) {
  const m = useMotion();
  return (
    <motion.button
      type="button"
      aria-label={label}
      title={label}
      aria-pressed={pressed}
      onClick={onClick}
      whileTap={m.press}
      transition={m.spring("micro")}
      className="transition-ui grid size-control-sm place-items-center rounded-control text-fg-muted hover:bg-fill-muted hover:text-fg"
    >
      {children}
    </motion.button>
  );
}

/** Six dots that fly out once — a tiny reward for feedback. */
function Burst({ tone }: { tone: "success" | "danger" }) {
  const m = useMotion();
  if (m.reduce) return null;
  return (
    <span aria-hidden className="pointer-events-none absolute inset-0">
      {Array.from({ length: 6 }, (_, i) => {
        const angle = (i / 6) * Math.PI * 2;
        const dist = 10 + m.distance.md;
        return (
          <motion.span
            key={i}
            className={cn("absolute left-1/2 top-1/2 -ml-0.5 -mt-0.5 size-1 rounded-full", tone === "success" ? "bg-success" : "bg-danger")}
            initial={{ x: 0, y: 0, opacity: 1, scale: 1 }}
            animate={{ x: Math.cos(angle) * dist, y: Math.sin(angle) * dist, opacity: 0, scale: 0.4 }}
            exit={{ opacity: 0 }}
            transition={m.tween("slow")}
          />
        );
      })}
    </span>
  );
}

Motion notes

  • Copy swaps to a check with a pop, then reverts after 1.6s.
  • Regenerate spins a full turn on the emphasis spring. Playful overshoots, Precise doesn't.
  • Feedback bounces the thumb and fires a six-dot burst. The burst is skipped with reduced motion.

Installation

  1. 1

    Install dependencies

    Terminal
    npm install motion clsx tailwind-merge
  2. 2

    Add the design tokens and motion system (once per project)

    Copy styles/tokens/*, lib/motion.tsx and lib/utils.ts from Get started.

  3. 3

    Paste the source into components/thinking-ui/message-actions.tsx

    Copy it from the Code tab, or use Copy prompt to let your coding agent do all of this.

Usage

Example
<MessageActions onCopy={copy} onRegenerate={retry} onFeedback={rate} />

Props

PropTypeDefaultDescription
onCopy() => void—Copy handler.
onRegenerate() => void—Regenerate handler.
onFeedback(value: "up" | "down" | null) => void—Called when a vote changes.