AI Feedback

Responding to a reply: a message action bar with copy and retry, thumbs that open a reason picker, and a five-point rating.

Installation

Installation

One command adds ai-feedback and the shared AI Kit layer it is built on (tokens, faces, marks and brand icons). No animation library, light and dark built in.

$npx shadcn@latest add https://tweenui.dev/r/ai-feedback.json
ai-feedback-demo
components/ui/ai-feedback-demo.tsx
"use client"

import * as React from "react"

import { AiDemo } from "@/lib/ai-demo"
import { Face, typeInto, useAiLoop } from "@/lib/ai-kit"
import {
  AiMessageActions,
  AiRating,
  AiThumbs,
  type AiThumbStage,
  type AiThumbValue,
} from "@/registry/tween/ai-feedback"

const ANSWER =
  "The headline now reveals word by word. It uses text-skew-reveal with a 0.05s stagger, so the whole line lands in under a second."

const reply: React.CSSProperties = {
  display: "grid",
  gridTemplateColumns: "28px minmax(0, 1fr)",
  gap: 10,
  width: "min(100%, 400px)",
  textAlign: "left",
}
const body: React.CSSProperties = {
  display: "grid",
  gap: 8,
  fontSize: 14,
  lineHeight: "22px",
}

function Reply({ children }: { children: React.ReactNode }) {
  return (
    <div style={reply}>
      <Face size={28} shape="orb" />
      <div style={body}>
        <p>{ANSWER}</p>
        {children}
      </div>
    </div>
  )
}

function Actions() {
  const root = React.useRef<HTMLDivElement>(null)
  const [copied, setCopied] = React.useState(false)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setCopied(false)
      await sleep(1400)
      setCopied(true)
      await sleep(1700)
      setCopied(false)
    },
    { gap: 1400, enabled: !hands, still: () => setCopied(true) }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", width: "min(100%, 400px)" }}
      onPointerDown={() => setHands(true)}
      onKeyDown={() => setHands(true)}
    >
      <Reply>
        <AiMessageActions
          text={ANSWER}
          copied={hands ? undefined : copied}
          meta="Mira · 1.8s"
          style={{ marginLeft: -6 }}
        />
      </Reply>
    </div>
  )
}

function Thumbs() {
  const root = React.useRef<HTMLDivElement>(null)
  const [value, setValue] = React.useState<AiThumbValue>("down")
  const [stage, setStage] = React.useState<AiThumbStage>("reason")
  const [selected, setSelected] = React.useState<string[]>(["Too long"])
  const [note, setNote] = React.useState("")
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      setValue(null)
      setStage("rate")
      setSelected([])
      setNote("")
      await sleep(1300)
      setValue("down")
      setStage("reason")
      await sleep(900)
      setSelected(["Too long"])
      await sleep(500)
      setSelected(["Too long", "Missed the point"])
      await sleep(500)
      await typeInto(setNote, "Skip the intro next time", sleep, 30)
      await sleep(700)
      setStage("thanks")
      await sleep(2200)
    },
    { gap: 600, enabled: !hands }
  )

  return (
    <div
      ref={root}
      style={{
        display: "grid",
        width: "min(100%, 360px)",
        minHeight: 200,
        alignContent: "start",
      }}
      onPointerDown={() => setHands(true)}
      onKeyDown={() => setHands(true)}
    >
      <AiThumbs
        value={value}
        onValueChange={setValue}
        stage={stage}
        onStageChange={setStage}
        selected={selected}
        onSelectedChange={setSelected}
        note={note}
        onNoteChange={setNote}
      />
    </div>
  )
}

function Rating() {
  const root = React.useRef<HTMLDivElement>(null)
  const [value, setValue] = React.useState(4)
  const [hands, setHands] = React.useState(false)

  useAiLoop(
    root,
    async ({ sleep }) => {
      for (const score of [0, 1, 2, 3, 4, 5]) {
        setValue(score)
        await sleep(score === 0 ? 1000 : score === 5 ? 1800 : 850)
      }
    },
    { gap: 700, enabled: !hands, still: () => setValue(4) }
  )

  return (
    <div
      ref={root}
      style={{ display: "grid", justifyItems: "center", width: "100%" }}
      onPointerDown={() => setHands(true)}
      onKeyDown={() => setHands(true)}
      onMouseEnter={() => setHands(true)}
    >
      <AiRating value={value} onValueChange={setValue} />
    </div>
  )
}

export function AiFeedbackDemo({ only }: { only?: string }) {
  return (
    <AiDemo
      only={only}
      tabs={[
        { name: "Actions", node: <Actions /> },
        { name: "Thumbs", node: <Thumbs /> },
        { name: "Rating", node: <Rating /> },
      ]}
    />
  )
}

Installation

pnpm dlx shadcn@latest add https://tweenui.dev/r/ai-feedback.json

Usage

import {
  AiMessageActions,
  AiRating,
  AiThumbs,
} from "@/components/ui/ai-feedback"
const [vote, setVote] = useState<"up" | "down" | null>(null)
const [score, setScore] = useState(0)
 
<AiMessageActions text={answer} meta="Mira · 1.8s" onRetry={regenerate} />
 
<AiThumbs
  value={vote}
  onValueChange={setVote}
  onSubmit={(feedback) => save(feedback)}
/>
 
<AiRating value={score} onValueChange={setScore} />

Versions

  • AiMessageActions is the bar under a reply. Copy writes text to the clipboard, swaps its icon for a check and says "Copied" for a moment. Retry, share and more call your handlers.
  • AiThumbs asks one question. A thumbs up goes straight to a thank-you. A thumbs down opens a reason picker with chips and a short field: Enter sends, Escape cancels.
  • AiRating is a five-point scale. The face's mood and colour follow the hovered or chosen score, and the arrow keys move between points.

Props

AiMessageActions

PropTypeDefaultDescription
textstringThe text Copy places on the clipboard.
onCopy(text: string) => voidCalled after a copy.
onRetry() => voidCalled by the retry button.
onShare() => voidCalled by the share button.
onMore() => voidCalled by the more button.
copiedbooleanForces the copied state. Omit to let the bar manage it.
metastringA quiet line at the end, such as the model and the time.

AiThumbs

PropTypeDefaultDescription
value"up" | "down" | nullThe current vote.
onValueChange(value) => voidCalled when a thumb is pressed or the vote is cleared.
onSubmit({ value, reasons, note }) => voidCalled on a thumbs up, or when a thumbs down is sent.
reasonsstring[]four common reasonsThe chips offered after a thumbs down.
stage"rate" | "reason" | "thanks"Controls the step. Omit to let the control move itself.
selected, notestring[], stringControl the chosen reasons and the note. Omit to leave them to the control.
questionstring"Was this helpful?"The line shown before a vote.
thanksstringThe line shown once feedback is in.

AiRating

PropTypeDefaultDescription
valuenumberThe score from 1 to 5, or 0 for none.
onValueChange(value: number) => voidCalled with the new score.
labelsstring[]five default linesOne line per score, lowest first.
promptstring"How was this answer?"Shown before a score is picked.
shapeface shape"blob"The face's shape.