AI
Response Versions
Several versions of an AI answer with smooth switching and a regenerate shimmer.
A haiku is a tiny Japanese poem: three lines, about seventeen syllables, catching a single moment.
2 / 2
Installation
npx shadcn@latest add https://relic-ui.vercel.app/r/response-versions.jsonOr straight from GitHub: npx shadcn@latest add relicui/relic-ui/response-versions
Manual installation
1. Install the dependencies:
npm install motion2. Copy this code into components/ui/response-versions.tsx:
"use client"
/**
* Relic UI · Response Versions
* https://relic-ui.vercel.app/docs/response-versions
*
* Copyright (c) 2026 Shubham Tambe
* Open source under the MIT License: https://relic-ui.vercel.app/license
*/
import { useState } from "react"
import { AnimatePresence, MotionConfig, motion, type Variants } from "motion/react"
import { cn } from "@/lib/utils"
const TRANSITIONS: Record<"blur" | "slide" | "fade", Variants> = {
blur: {
enter: { opacity: 0, filter: "blur(6px)" },
center: { opacity: 1, filter: "blur(0px)" },
exit: { opacity: 0, filter: "blur(6px)" },
},
slide: {
enter: (dir: number) => ({ opacity: 0, x: dir * 28 }),
center: { opacity: 1, x: 0 },
exit: (dir: number) => ({ opacity: 0, x: dir * -28 }),
},
fade: {
enter: { opacity: 0 },
center: { opacity: 1 },
exit: { opacity: 0 },
},
}
export interface ResponseVersionsProps {
/** Every version of the answer, oldest first. */
versions: React.ReactNode[]
/** The version shown (controlled), counting from 0. */
index?: number
/** The version shown at first. Defaults to the newest. */
defaultIndex?: number
onIndexChange?: (index: number) => void
/** Called by the Regenerate button. Add the new answer to `versions`. */
onRegenerate?: () => void
/** A new version is being written: shows a shimmer instead of the text. */
regenerating?: boolean
/** How switching between versions looks. */
transition?: keyof typeof TRANSITIONS
/** Show "2 / 3". */
showCounter?: boolean
/** Jump to a new version as soon as it's added. */
followNewest?: boolean
/** Accessible name. */
label?: string
className?: string
}
/**
* Several versions of an AI answer in one card. Step through them with
* ‹ › and the card glides to each one's height, or ask for another; while it
* writes, the text gives way to a soft shimmer.
*/
export function ResponseVersions({
versions,
index: indexProp,
defaultIndex,
onIndexChange,
onRegenerate,
regenerating = false,
transition = "blur",
showCounter = true,
followNewest = true,
label = "Response",
className,
}: ResponseVersionsProps) {
const [internal, setInternal] = useState(defaultIndex ?? Math.max(versions.length - 1, 0))
const [direction, setDirection] = useState(1)
const [seen, setSeen] = useState(versions.length)
// A new version arrived: show it (when following the newest).
if (seen !== versions.length) {
const grew = versions.length > seen
setSeen(versions.length)
if (grew && followNewest && indexProp === undefined) {
setInternal(versions.length - 1)
setDirection(1)
}
}
const count = versions.length
const current = Math.min(Math.max(indexProp ?? internal, 0), Math.max(count - 1, 0))
function go(delta: number) {
const next = Math.min(Math.max(current + delta, 0), count - 1)
if (next === current) return
setDirection(delta)
if (indexProp === undefined) setInternal(next)
onIndexChange?.(next)
}
return (
<MotionConfig reducedMotion="user">
<motion.section
layout
aria-label={label}
data-slot="response-versions"
transition={{ layout: { type: "spring", stiffness: 300, damping: 32 } }}
className={cn("w-full overflow-hidden rounded-xl border bg-card text-sm text-card-foreground", className)}
>
<div className="relative px-4 py-3.5 leading-relaxed">
<AnimatePresence mode="popLayout" initial={false} custom={direction}>
{regenerating ? (
<motion.div
key="writing"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
className="flex flex-col gap-2.5 py-1"
aria-label="Writing a new version"
role="status"
>
{["100%", "92%", "64%"].map((w, i) => (
<motion.span
key={i}
className="h-3 rounded-full bg-muted"
style={{ width: w }}
animate={{ opacity: [0.45, 1, 0.45] }}
transition={{ duration: 1.3, repeat: Infinity, delay: i * 0.15 }}
/>
))}
</motion.div>
) : (
<motion.div
key={current}
custom={direction}
variants={TRANSITIONS[transition]}
initial="enter"
animate="center"
exit="exit"
transition={{ duration: 0.28, ease: [0.22, 1, 0.36, 1] }}
>
{versions[current]}
</motion.div>
)}
</AnimatePresence>
</div>
<motion.div layout="position" className="flex items-center gap-1 border-t px-2 py-1.5 text-xs text-muted-foreground">
<button
type="button"
aria-label="Previous version"
disabled={current === 0 || regenerating}
onClick={() => go(-1)}
className="inline-flex size-7 items-center justify-center rounded-md transition-colors outline-none hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50 disabled:opacity-35 disabled:hover:bg-transparent"
>
<svg aria-hidden viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth={1.6} strokeLinecap="round" className="size-3.5">
<path d="M10 4l-4 4 4 4" />
</svg>
</button>
{showCounter && (
<span aria-live="polite" className="min-w-10 text-center font-mono tabular-nums">
{current + 1} / {count}
</span>
)}
<button
type="button"
aria-label="Next version"
disabled={current >= count - 1 || regenerating}
onClick={() => go(1)}
className="inline-flex size-7 items-center justify-center rounded-md transition-colors outline-none hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50 disabled:opacity-35 disabled:hover:bg-transparent"
>
<svg aria-hidden viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth={1.6} strokeLinecap="round" className="size-3.5">
<path d="M6 4l4 4-4 4" />
</svg>
</button>
{onRegenerate && (
<button
type="button"
onClick={onRegenerate}
disabled={regenerating}
className="ml-auto inline-flex h-7 items-center gap-1.5 rounded-md px-2 transition-colors outline-none hover:bg-muted hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring/50 disabled:opacity-60"
>
<motion.svg
aria-hidden
viewBox="0 0 16 16"
fill="none"
stroke="currentColor"
strokeWidth={1.6}
strokeLinecap="round"
strokeLinejoin="round"
className="size-3.5"
animate={regenerating ? { rotate: 360 } : { rotate: 0 }}
transition={regenerating ? { duration: 0.9, repeat: Infinity, ease: "linear" } : { duration: 0.3 }}
>
<path d="M13.5 8a5.5 5.5 0 1 1-1.6-3.9M13.5 2.5v3h-3" />
</motion.svg>
{regenerating ? "Writing…" : "Regenerate"}
</button>
)}
</motion.div>
</motion.section>
</MotionConfig>
)
}Usage
"use client"
import { useState } from "react"
import { ResponseVersions } from "@/components/ui/response-versions"
const ANSWERS = [
"A haiku is a short poem in three lines of five, seven and five syllables, often about nature.",
"A haiku is a tiny Japanese poem: three lines, about seventeen syllables, catching a single moment.",
"Think of a haiku as a photograph in words: three short lines that capture one fleeting scene.",
"Haiku come from Japan. Three lines, roughly 5-7-5 syllables, usually with a seasonal image.",
]
export default function ResponseVersionsDemo() {
const [count, setCount] = useState(2)
const [writing, setWriting] = useState(false)
function regenerate() {
setWriting(true)
// Pretend the AI takes a moment to write a new answer.
setTimeout(() => {
setCount((c) => Math.min(c + 1, ANSWERS.length))
setWriting(false)
}, 1400)
}
return (
<div className="w-full max-w-md">
<ResponseVersions
versions={ANSWERS.slice(0, count)}
regenerating={writing}
onRegenerate={count < ANSWERS.length ? regenerate : undefined}
/>
</div>
)
}Customize
Try the options below, then copy the code.
Water boils at 100 °C at sea level.
1 / 3
transition
Step through the versions: the card glides to each height.
<ResponseVersions
versions={answers}
onRegenerate={regenerate}
/>Props
| Prop | Type | Default |
|---|---|---|
versionsEvery version of the answer, oldest first. | React.ReactNode[] | — |
index / defaultIndexThe version shown (from 0), controlled or at first. | number | newest |
onIndexChangeCalled when the reader switches versions. | (index: number) => void | — |
onRegenerateShows a Regenerate button. Add the new answer to versions. | () => void | — |
regeneratingA new version is being written: shows a shimmer. | boolean | false |
transitionHow switching between versions looks. | "blur" | "slide" | "fade" | "blur" |
showCounterShow “2 / 3”. | boolean | true |
followNewestJump to a new version as soon as it's added. | boolean | true |
labelAccessible name of the card. | string | "Response" |
Accessibility
- Previous and Next are labelled buttons, disabled at the ends.
- The version counter is announced when it changes.
- While writing, a status region tells screen readers a new version is coming.
- Animation turns off automatically for users who enable reduced motion in their system settings.