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.
"use client"
import * as React from "react"
import {
ArrowUp,
Check,
Copy,
Ellipsis,
RefreshCw,
Share2,
ThumbsDown,
ThumbsUp,
} from "lucide-react"
import {
AiKitStyles,
AiStyle,
cx,
Face,
Mark,
Roll,
type AiMood,
type AiShape,
type AiTone,
} from "@/lib/ai-kit"
const CSS = `
.aik-fb-bar{display:inline-flex;align-items:center;gap:2px;max-width:100%}
.aik-fb-bar .aik-btn{color:var(--aik-text-2)}
.aik-fb-bar .aik-btn[data-on]{color:var(--aik-green)}
.aik-fb-sw{display:grid;animation:aik-swap var(--aik-d-panel) var(--aik-ease-out)}
.aik-fb-said{min-width:0;margin-left:4px;font-size:12.5px;line-height:18px;font-weight:500;color:var(--aik-green)}
.aik-fb-meta{margin-left:10px;padding-left:10px;border-left:1px solid var(--aik-line-2);font:11.5px/14px var(--aik-mono);color:var(--aik-text-3);white-space:nowrap}
@container (max-width:340px){.aik-fb-meta{display:none}}
.aik-fb-th{display:grid;gap:8px;width:min(100%,360px)}
.aik-fb-row{display:flex;align-items:center;gap:4px;min-height:36px}
.aik-fb-q{flex:1;min-width:0;display:flex;align-items:center;gap:8px;font-size:13.5px;color:var(--aik-text-2)}
.aik-fb-th[data-stage="thanks"] .aik-fb-q{color:var(--aik-text)}
.aik-fb-vote{display:grid;place-items:center;width:36px;height:36px;border-radius:11px;color:var(--aik-text-2);transition:background var(--aik-d-state),color var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-fb-vote:hover{background:var(--aik-hover);color:var(--aik-text)}
.aik-fb-vote:active{transform:scale(.92)}
.aik-fb-vote[aria-pressed="true"]{background:var(--aik-raised);color:var(--aik-text)}
.aik-fb-vote[aria-pressed="true"][data-v="up"]{color:var(--aik-green);background:color-mix(in oklab,var(--aik-green) 12%,transparent)}
.aik-fb-vote[aria-pressed="true"][data-v="down"]{color:var(--aik-red);background:color-mix(in oklab,var(--aik-red) 12%,transparent)}
.aik-fb-vote[aria-pressed="true"] svg{animation:aik-swap var(--aik-d-panel) var(--aik-ease-out)}
.aik-fb-why{display:grid;gap:10px;padding:12px;border-radius:16px;background:var(--aik-card);box-shadow:var(--aik-sh-pop);transform-origin:80% 0;animation:aik-pop var(--aik-d-enter) var(--aik-ease-out) both}
.aik-fb-chips{display:flex;flex-wrap:wrap;gap:6px}
.aik-fb-chips .aik-chip{cursor:pointer;box-shadow:inset 0 0 0 1px transparent;transition:background var(--aik-d-state),color var(--aik-d-state),transform var(--aik-d-press) var(--aik-ease-out)}
.aik-fb-chips .aik-chip:active{transform:scale(.96)}
.aik-fb-chips .aik-chip[aria-pressed="true"]{background:var(--aik-pri);color:var(--aik-pri-ink)}
.aik-fb-field{display:flex;align-items:center;gap:6px;height:36px;padding:0 4px 0 10px;border-radius:10px;background:var(--aik-raised);box-shadow:inset 0 0 0 1px transparent;transition:box-shadow var(--aik-d-state)}
.aik-fb-field:focus-within{box-shadow:inset 0 0 0 1px var(--aik-line-3)}
.aik-fb-field input{flex:1;min-width:0;height:100%;border:0;outline:0;background:none;font-size:13.5px}
.aik-fb-field input::placeholder{color:var(--aik-text-3);opacity:1}
.aik-fb-send{display:grid;place-items:center;width:28px;height:28px;flex:none;border-radius:8px;background:var(--aik-pri);color:var(--aik-pri-ink);transition:transform var(--aik-d-press) var(--aik-ease-out),opacity var(--aik-d-state)}
.aik-fb-send:active{transform:scale(.92)}
.aik-fb-rate{display:grid;justify-items:center;gap:14px;width:min(100%,320px)}
.aik-fb-rate .aik-label{text-align:center}
.aik-fb-pts{display:flex;gap:6px}
.aik-fb-pt{display:grid;place-items:center;width:40px;height:40px;border-radius:13px;background:var(--aik-raised);color:var(--aik-text-3);font:500 13px/1 var(--aik-mono);box-shadow:inset 0 0 0 1px var(--aik-line);
transition:background var(--aik-d-state) var(--aik-ease-out),color var(--aik-d-state) var(--aik-ease-out),transform var(--aik-d-press) var(--aik-ease-out),box-shadow var(--aik-d-state)}
.aik-fb-pt:active{transform:scale(.92)}
.aik-fb-pt[data-on]{background:var(--aik-pri);color:var(--aik-pri-ink);box-shadow:none}
.aik-fb-pt[data-on]{transition-delay:calc(var(--i)*var(--aik-stagger))}
.aik-fb-rate .aik-roll{font-size:13.5px;line-height:20px;font-weight:500;min-height:20px;justify-items:center}
.aik-fb-rate[data-score="0"] .aik-roll{color:var(--aik-text-3);font-weight:400}
@container (max-width:300px){.aik-fb-pt{width:34px;height:34px;border-radius:11px}}
`
function useControlled<T>(
value: T | undefined,
fallback: T,
onChange?: (next: T) => void
) {
const [own, setOwn] = React.useState(fallback)
const current = value === undefined ? own : value
const set = (next: T) => {
if (value === undefined) setOwn(next)
onChange?.(next)
}
return [current, set] as const
}
export interface AiMessageActionsProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onCopy"
> {
/** The text Copy places on the clipboard. */
text: string
/** Called after the text is copied. */
onCopy?: (text: string) => void
onRetry?: () => void
onShare?: () => void
onMore?: () => void
/** Forces the copied state. Omit to let the bar show it for a moment after a copy. */
copied?: boolean
/** A quiet line at the end, such as the model and the time taken. */
meta?: string
}
/** The action bar under a reply: copy that confirms itself, retry, share and more. */
export function AiMessageActions({
text,
onCopy,
onRetry,
onShare,
onMore,
copied,
meta,
className,
...rest
}: AiMessageActionsProps) {
const [own, setOwn] = React.useState(false)
const timer = React.useRef<number | undefined>(undefined)
React.useEffect(() => () => window.clearTimeout(timer.current), [])
const isCopied = copied ?? own
const copy = async () => {
try {
await navigator.clipboard.writeText(text)
} catch {
// The clipboard can be blocked (permissions, insecure origin). The
// callback still fires so the host can offer its own fallback.
}
onCopy?.(text)
setOwn(true)
window.clearTimeout(timer.current)
timer.current = window.setTimeout(() => setOwn(false), 1600)
}
return (
<div
{...rest}
role="toolbar"
aria-label="Message actions"
className={cx("aik aik-fb-bar", className)}
>
<AiKitStyles />
<AiStyle id="ai-feedback" css={CSS} />
<button
type="button"
className="aik-btn icon sm ghost"
aria-label={isCopied ? "Copied" : "Copy"}
data-on={isCopied ? "" : undefined}
onClick={copy}
>
<span key={isCopied ? "done" : "copy"} className="aik-fb-sw">
{isCopied ? <Check size={15} /> : <Copy size={15} />}
</span>
</button>
<button
type="button"
className="aik-btn icon sm ghost"
aria-label="Retry"
onClick={onRetry}
>
<RefreshCw size={15} />
</button>
<button
type="button"
className="aik-btn icon sm ghost"
aria-label="Share"
onClick={onShare}
>
<Share2 size={15} />
</button>
<button
type="button"
className="aik-btn icon sm ghost"
aria-label="More"
onClick={onMore}
>
<Ellipsis size={15} />
</button>
<span className="aik-fb-said" aria-live="polite">
{isCopied ? <Roll>Copied</Roll> : null}
</span>
{meta ? <span className="aik-fb-meta">{meta}</span> : null}
</div>
)
}
export type AiThumbValue = "up" | "down" | null
export type AiThumbStage = "rate" | "reason" | "thanks"
export interface AiThumbsFeedback {
value: AiThumbValue
reasons: string[]
note: string
}
export interface AiThumbsProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onSubmit"
> {
value: AiThumbValue
onValueChange: (value: AiThumbValue) => void
/** Where the control is. Omit to let it move itself from rating to reason to thanks. */
stage?: AiThumbStage
onStageChange?: (stage: AiThumbStage) => void
/** The reasons offered after a thumbs down. */
reasons?: string[]
/** The chosen reasons. Omit to let the control manage them. */
selected?: string[]
onSelectedChange?: (selected: string[]) => void
/** The free-text note. Omit to let the control manage it. */
note?: string
onNoteChange?: (note: string) => void
/** Called when a thumbs up is given, or a thumbs down is sent with its reasons. */
onSubmit?: (feedback: AiThumbsFeedback) => void
/** The question shown before a vote. */
question?: string
/** The line shown once feedback is in. */
thanks?: string
}
const DEFAULT_REASONS = ["Not accurate", "Too long", "Missed the point", "Slow"]
/** Thumbs up and down. A thumbs down opens a reason picker, then both end on a thank-you. */
export function AiThumbs({
value,
onValueChange,
stage,
onStageChange,
reasons = DEFAULT_REASONS,
selected,
onSelectedChange,
note,
onNoteChange,
onSubmit,
question = "Was this helpful?",
thanks = "Thanks. That helps the next answer.",
className,
...rest
}: AiThumbsProps) {
const [at, setAt] = useControlled<AiThumbStage>(stage, "rate", onStageChange)
const [picked, setPicked] = useControlled<string[]>(
selected,
[],
onSelectedChange
)
const [text, setText] = useControlled<string>(note, "", onNoteChange)
const field = React.useRef<HTMLInputElement>(null)
const byHand = React.useRef(false)
React.useEffect(() => {
if (at === "reason" && byHand.current) field.current?.focus()
}, [at])
const vote = (next: "up" | "down") => {
byHand.current = true
if (value === next && at !== "thanks") {
onValueChange(null)
setAt("rate")
return
}
onValueChange(next)
if (next === "up") {
setAt("thanks")
onSubmit?.({ value: "up", reasons: [], note: "" })
} else {
setAt("reason")
}
}
const send = () => {
setAt("thanks")
onSubmit?.({ value: "down", reasons: picked, note: text.trim() })
}
const cancel = () => {
onValueChange(null)
setAt("rate")
}
return (
<div
{...rest}
className={cx("aik aik-fb-th", className)}
data-stage={at}
onKeyDown={(event) => {
rest.onKeyDown?.(event)
if (event.key === "Escape" && at === "reason") cancel()
}}
>
<AiKitStyles />
<AiStyle id="ai-feedback" css={CSS} />
<div className="aik-fb-row">
<span className="aik-fb-q" aria-live="polite">
{at === "thanks" ? <Mark status="done" /> : null}
<Roll>{at === "thanks" ? thanks : question}</Roll>
</span>
<button
type="button"
className="aik-fb-vote"
data-v="up"
aria-label="Good answer"
aria-pressed={value === "up"}
onClick={() => vote("up")}
>
<ThumbsUp size={17} />
</button>
<button
type="button"
className="aik-fb-vote"
data-v="down"
aria-label="Bad answer"
aria-pressed={value === "down"}
aria-expanded={at === "reason"}
onClick={() => vote("down")}
>
<ThumbsDown size={17} />
</button>
</div>
{at === "reason" ? (
<div className="aik-fb-why" role="group" aria-label="What went wrong">
<span className="aik-label">What went wrong</span>
<div className="aik-fb-chips">
{reasons.map((reason) => {
const on = picked.includes(reason)
return (
<button
key={reason}
type="button"
className="aik-chip"
aria-pressed={on}
onClick={() =>
setPicked(
on
? picked.filter((item) => item !== reason)
: [...picked, reason]
)
}
>
{reason}
</button>
)
})}
</div>
<div className="aik-fb-field">
<input
ref={field}
value={text}
placeholder="Anything to add?"
aria-label="Anything to add?"
onChange={(event) => setText(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Enter" && !event.nativeEvent.isComposing) {
event.preventDefault()
send()
}
}}
/>
<button
type="button"
className="aik-fb-send"
aria-label="Send feedback"
onClick={send}
>
<ArrowUp size={14} />
</button>
</div>
</div>
) : null}
</div>
)
}
const SCORE_LOOK: ReadonlyArray<readonly [AiMood, AiTone]> = [
["idle", "ink"],
["oops", "rose"],
["think", "amber"],
["idle", "ink"],
["listen", "teal"],
["happy", "green"],
]
const DEFAULT_LABELS = [
"Missed the point",
"Needs work",
"Fine",
"Good",
"Exactly right",
]
export interface AiRatingProps extends Omit<
React.HTMLAttributes<HTMLDivElement>,
"onChange"
> {
/** The chosen score from 1 to 5, or 0 for none. */
value: number
onValueChange: (value: number) => void
/** One line per score, lowest first. */
labels?: string[]
/** Shown before a score is picked. */
prompt?: string
shape?: AiShape
}
/** A five-point rating. The face's mood and colour follow the hovered or chosen score. */
export function AiRating({
value,
onValueChange,
labels = DEFAULT_LABELS,
prompt = "How was this answer?",
shape = "blob",
className,
...rest
}: AiRatingProps) {
const [hover, setHover] = React.useState(0)
const points = React.useRef<Array<HTMLButtonElement | null>>([])
const shown = hover || value
const [mood, tone] = SCORE_LOOK[shown] ?? SCORE_LOOK[0]!
const move = (next: number) => {
const score = Math.min(5, Math.max(1, next))
onValueChange(score)
points.current[score - 1]?.focus()
}
return (
<div
{...rest}
className={cx("aik aik-fb-rate", className)}
data-score={shown}
>
<AiKitStyles />
<AiStyle id="ai-feedback" css={CSS} />
<Face size={64} mood={mood} tone={tone} shape={shape} />
<div
className="aik-fb-pts"
role="radiogroup"
aria-label={prompt}
onMouseLeave={() => setHover(0)}
>
{[1, 2, 3, 4, 5].map((score) => (
<button
key={score}
ref={(node) => {
points.current[score - 1] = node
}}
type="button"
role="radio"
className="aik-fb-pt"
aria-checked={value === score}
aria-label={`${score}: ${labels[score - 1] ?? ""}`}
tabIndex={(value || 1) === score ? 0 : -1}
data-on={score <= shown ? "" : undefined}
style={{ "--i": score - 1 } as React.CSSProperties}
onMouseEnter={() => setHover(score)}
onClick={() => onValueChange(value === score ? 0 : score)}
onKeyDown={(event) => {
if (event.key === "ArrowRight" || event.key === "ArrowUp") {
event.preventDefault()
move((value || 0) + 1)
} else if (
event.key === "ArrowLeft" ||
event.key === "ArrowDown"
) {
event.preventDefault()
move((value || 2) - 1)
}
}}
>
{score}
</button>
))}
</div>
<Roll>{shown ? (labels[shown - 1] ?? "") : prompt}</Roll>
</div>
)
}
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
"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
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
AiMessageActionsis the bar under a reply. Copy writestextto the clipboard, swaps its icon for a check and says "Copied" for a moment. Retry, share and more call your handlers.AiThumbsasks 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.AiRatingis 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
| Prop | Type | Default | Description |
|---|---|---|---|
text | string | The text Copy places on the clipboard. | |
onCopy | (text: string) => void | Called after a copy. | |
onRetry | () => void | Called by the retry button. | |
onShare | () => void | Called by the share button. | |
onMore | () => void | Called by the more button. | |
copied | boolean | Forces the copied state. Omit to let the bar manage it. | |
meta | string | A quiet line at the end, such as the model and the time. |
AiThumbs
| Prop | Type | Default | Description |
|---|---|---|---|
value | "up" | "down" | null | The current vote. | |
onValueChange | (value) => void | Called when a thumb is pressed or the vote is cleared. | |
onSubmit | ({ value, reasons, note }) => void | Called on a thumbs up, or when a thumbs down is sent. | |
reasons | string[] | four common reasons | The chips offered after a thumbs down. |
stage | "rate" | "reason" | "thanks" | Controls the step. Omit to let the control move itself. | |
selected, note | string[], string | Control the chosen reasons and the note. Omit to leave them to the control. | |
question | string | "Was this helpful?" | The line shown before a vote. |
thanks | string | The line shown once feedback is in. |
AiRating
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | The score from 1 to 5, or 0 for none. | |
onValueChange | (value: number) => void | Called with the new score. | |
labels | string[] | five default lines | One line per score, lowest first. |
prompt | string | "How was this answer?" | Shown before a score is picked. |
shape | face shape | "blob" | The face's shape. |