- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Calendar
- Card
- Carousel
- Chart
- Checkbox
- Code Block
- Collapsible
- Combobox
- Command
- Context Menu
- Data Table
- Date Picker
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Kbd
- Label
- Marker
- Message
- Message Scroller
- Native Select
- Pagination
- Popover
- Progress
- Questionnaire
- Radio Group
- Resizable
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Sonner
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
- Trace
- Typography
"use client"
import * as React from "react""use client"
import * as React from "react"
import { useChat } from "@ai-sdk/react"
import type { UIDataTypes, UIMessage } from "ai"
import {
ArrowUpIcon,
CircleCheckIcon,
GlobeIcon,
LoaderCircleIcon,
MessageCircleDashedIcon,
PaperclipIcon,
PlusIcon,
RotateCwIcon,
TelescopeIcon,
TerminalIcon,
} from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { createChat, getMessageText } from "@/lib/ai"
import { MESSAGE_ANIMATIONS } from "@/lib/message-animations"
import { Markdown } from "@/components/markdown"
import { Bubble, BubbleContent } from "@/components/ui/bubble"
import { Button } from "@/components/ui/button"
import { CodeBlock, CodeBlockContent } from "@/components/ui/code-block"
import {
DropdownMenu,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import {
Empty,
EmptyDescription,
EmptyHeader,
EmptyMedia,
EmptyTitle,
} from "@/components/ui/empty"
import {
InputGroup,
InputGroupAddon,
InputGroupButton,
InputGroupTextarea,
} from "@/components/ui/input-group"
import { Message, MessageContent } from "@/components/ui/message"
import {
MessageScroller,
MessageScrollerButton,
MessageScrollerContent,
MessageScrollerItem,
MessageScrollerProvider,
MessageScrollerViewport,
} from "@/components/ui/message-scroller"
import { Tooltip, TooltipTrigger } from "@/components/ui/tooltip"
import {
Trace,
TraceCollapsible,
TraceCollapsibleContent,
TraceCollapsibleTrigger,
TraceIcon,
TraceLine,
TraceTitle,
} from "@/components/ui/trace"
type ChatMessage = UIMessage<
unknown,
UIDataTypes,
{
runCommand: {
input: { command: string }
output: { stdout: string }
}
}
>
type ChatPart = ChatMessage["parts"][number]
type ToolPart = Extract<ChatPart, { type: "tool-runCommand" }>
const chat = createChat<ChatMessage>()
.user("Add a dark mode toggle to the settings page.")
.sleep(600)
.assistant(({ writer }) => {
writer.reasoning(
"I need to know how themes are set up before I add a toggle. If next-themes is already in the layout, the toggle only has to call setTheme."
)
writer.sleep(400)
writer
.tool("runCommand", { input: { command: "rg -l ThemeProvider app" } })
.sleep(800)
.output({ stdout: "app/layout.tsx" })
writer.sleep(500)
writer.text(`next-themes is already set up in \`app/layout.tsx\`, so the toggle only has to call \`setTheme\`.
**Changes**
- Added \`components/mode-toggle.tsx\` with a Light and Dark option.
- Put \`<ModeToggle />\` next to the Appearance heading in \`app/settings/page.tsx\`.
\`\`\`tsx
const { setTheme } = useTheme()
\`\`\`
`)
})
.user("Make it follow the system setting by default.")
.sleep(600)
.assistant(({ writer }) => {
writer.reasoning(
"The default theme lives on the ThemeProvider. I should check what it is set to now."
)
writer.sleep(400)
writer
.tool("runCommand", {
input: { command: "rg -n defaultTheme app/layout.tsx" },
})
.sleep(800)
.output({
stdout: '12: <ThemeProvider attribute="class" defaultTheme="light">',
})
writer.sleep(500)
writer.text(`It was set to \`light\`. I updated the \`ThemeProvider\` in \`app/layout.tsx\`:
\`\`\`tsx
<ThemeProvider attribute="class" defaultTheme="system" enableSystem>
\`\`\`
New visitors now get the theme from their OS. The toggle also has a **System** option, so anyone who picked Light or Dark can switch back.
`)
})
.user("Run the build before I ship it.")
.sleep(600)
.assistant(({ writer }) => {
writer
.tool("runCommand", { input: { command: "pnpm build" } })
.sleep(1600)
.output({
stdout:
"▲ Next.js 16.0.0\n\n✓ Compiled successfully in 4.2s\n✓ Linting and checking validity of types\n✓ Generating static pages (12/12)",
})
writer.sleep(500)
writer.text(`The build passes, so it is ready to ship.
- Compiled in **4.2s**.
- No lint or type errors.
- Generated all **12** static pages.
`)
})
const initialMessages = chat.get(0)
const transport = chat.transport({
delayMs: 16,
fallback:
"This demo only knows the scripted turns. Press reset to start over.",
})
const MotionMessageScrollerItem = motion.create(MessageScrollerItem)
export function ChatDemo() {
const { messages, sendMessage, setMessages, status } = useChat({
messages: initialMessages,
transport,
})
const [draft, setDraft] = React.useState<string | null>(null)
const formRef = React.useRef<HTMLFormElement>(null)
const shouldReduceMotion = useReducedMotion()
const nextMessage = chat.next(messages)
const busy = status === "submitted" || status === "streaming"
const text = draft ?? (nextMessage ? getMessageText(nextMessage) : "")
const canSend = text.trim().length > 0 && !busy
function onSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault()
if (!canSend) {
return
}
if (draft === null && nextMessage) {
void sendMessage(nextMessage)
} else {
void sendMessage({ text: text.trim() })
}
setDraft(null)
}
return (
<MessageScrollerProvider>
<div className="relative flex w-full flex-col gap-4 [[data-slot=preview]:has(&)]:bg-background [[data-slot=preview]:has(&)>div]:p-0!">
<div className="flex h-200 w-full flex-col gap-4">
<div className="flex items-start justify-between gap-4 p-8 pb-4">
<div className="flex flex-col gap-1">
<div className="font-medium">New Thread</div>
<div className="text-sm text-muted-foreground">
Ask the agent to change your code.
</div>
</div>
<TooltipTrigger>
<Button
variant="outline"
size="icon"
aria-label="Reset conversation"
onPress={() => {
setMessages(initialMessages)
setDraft(null)
}}
isDisabled={busy}
>
<RotateCwIcon />
</Button>
<Tooltip>
<p>Reset</p>
</Tooltip>
</TooltipTrigger>
</div>
<div className="min-h-0 flex-1">
{messages.length === 0 ? (
<Empty className="h-full">
<EmptyHeader>
<EmptyMedia variant="icon">
<MessageCircleDashedIcon />
</EmptyMedia>
<EmptyTitle>What should we build?</EmptyTitle>
<EmptyDescription>
The agent can read your code, run commands and make changes.
Press send to start.
</EmptyDescription>
</EmptyHeader>
</Empty>
) : (
<MessageScroller>
<MessageScrollerViewport>
<MessageScrollerContent
aria-busy={busy}
className="gap-6 px-8 py-0"
>
{messages.map((message) => {
if (message.role === "user") {
return (
<MotionMessageScrollerItem
key={message.id}
messageId={message.id}
scrollAnchor
variants={MESSAGE_ANIMATIONS["slide-up"].variants}
initial={shouldReduceMotion ? false : "initial"}
animate="animate"
>
<Message align="end">
<MessageContent>
<Bubble align="end" variant="muted">
<BubbleContent>
{getMessageText(message)}
</BubbleContent>
</Bubble>
</MessageContent>
</Message>
</MotionMessageScrollerItem>
)
}
const streaming = busy && message === messages.at(-1)
const steps = message.parts.filter(
(part) =>
part.type === "reasoning" ||
part.type === "tool-runCommand"
)
const reply = getMessageText(message)
return (
<MessageScrollerItem
key={message.id}
messageId={message.id}
>
<Message>
<MessageContent className="gap-3">
{steps.length > 0 && (
<Trace>
{steps.map((part, index) => (
<StepRow
key={index}
part={part}
streaming={streaming}
/>
))}
</Trace>
)}
{reply && <Markdown>{reply}</Markdown>}
</MessageContent>
</Message>
</MessageScrollerItem>
)
})}
</MessageScrollerContent>
</MessageScrollerViewport>
<MessageScrollerButton />
</MessageScroller>
)}
</div>
<div className="p-8 pt-0">
<form ref={formRef} onSubmit={onSubmit} className="w-full">
<InputGroup>
<InputGroupTextarea
value={text}
onChange={(event) => {
setDraft(
typeof event === "string"
? event
: event.currentTarget.value
)
}}
placeholder="Ask the agent..."
aria-label="Message"
className="min-h-14"
onKeyDown={(event) => {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault()
formRef.current?.requestSubmit()
}
}}
/>
<InputGroupAddon align="block-end" className="pt-1">
<DropdownMenuTrigger>
<InputGroupButton
aria-label="Add context"
type="button"
size="icon-sm"
variant="outline"
>
<PlusIcon />
</InputGroupButton>
<DropdownMenu placement="top start" className="w-44">
<DropdownMenuItem>
<PaperclipIcon />
Add Photos & Files
</DropdownMenuItem>
<DropdownMenuSeparator />
<DropdownMenuItem>
<TelescopeIcon />
Deep Research
</DropdownMenuItem>
<DropdownMenuItem>
<GlobeIcon />
Web Search
</DropdownMenuItem>
</DropdownMenu>
</DropdownMenuTrigger>
<InputGroupButton
type="submit"
variant="default"
size="icon-sm"
isDisabled={!canSend}
className="ml-auto"
>
<ArrowUpIcon />
<span className="sr-only">Send</span>
</InputGroupButton>
</InputGroupAddon>
</InputGroup>
</form>
</div>
</div>
</div>
</MessageScrollerProvider>
)
}
function StepRow({ part, streaming }: { part: ChatPart; streaming: boolean }) {
if (part.type === "reasoning") {
return (
<ReasoningRow text={part.text} running={part.state === "streaming"} />
)
}
if (part.type === "tool-runCommand") {
return <ToolRow part={part} streaming={streaming} />
}
return null
}
function ReasoningRow({ text, running }: { text: string; running: boolean }) {
const [open, setOpen] = React.useState<boolean>()
return (
<TraceCollapsible isExpanded={open ?? running} onExpandedChange={setOpen}>
<TraceCollapsibleTrigger>
<TraceLine status={running ? "running" : undefined}>
<TraceIcon>
{running ? (
<LoaderCircleIcon className="animate-spin" />
) : (
<CircleCheckIcon />
)}
</TraceIcon>
<TraceTitle>{running ? "Thinking" : "Thought"}</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent className="whitespace-pre-wrap">
{text}
</TraceCollapsibleContent>
</TraceCollapsible>
)
}
function ToolRow({ part, streaming }: { part: ToolPart; streaming: boolean }) {
const command = part.input?.command
if (part.state === "output-available" || part.state === "output-error") {
const failed = part.state === "output-error"
return (
<TraceCollapsible>
<TraceCollapsibleTrigger>
<TraceLine status={failed ? "error" : undefined}>
<TraceIcon>
<TerminalIcon />
</TraceIcon>
<TraceTitle>
{failed ? "Failed to run" : "Ran"} {command}
</TraceTitle>
</TraceLine>
</TraceCollapsibleTrigger>
<TraceCollapsibleContent>
<CodeBlock size="sm">
<CodeBlockContent wrap className="max-h-64">
{failed ? part.errorText : part.output.stdout}
</CodeBlockContent>
</CodeBlock>
</TraceCollapsibleContent>
</TraceCollapsible>
)
}
return (
<TraceLine status={streaming ? "running" : "error"}>
<TraceIcon>
<TerminalIcon />
</TraceIcon>
<TraceTitle>
{streaming ? "Running" : "Failed to run"} {command}
</TraceTitle>
</TraceLine>
)
}
Chat is the thread of a conversation with the agent. Each turn is a Message inside Message Scroller. The person's message is the scroll anchor, so a new question settles near the top and the reply streams in under it. The field is a Prompt Input.
Installation#
Chat is built with the Message Scroller, Message, Bubble, and Input Group components.
pnpm dlx shadcn@latest add message-scroller message bubble input-group
Install the required shadcn/ui dependencies:
pnpm dlx shadcn@latest add message-scroller message bubble input-group
Copy and paste the following code into your project.
"use client"
import * as React from "react"
import {
MessageScroller as MessageScrollerPrimitive,
useMessageScroller,
useMessageScrollerScrollable,
useMessageScrollerVisibility,
} from "@shadcn/react/message-scroller"
import { cn } from "cn"
import { ArrowDownIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
function MessageScrollerProvider(
props: React.ComponentProps<typeof MessageScrollerPrimitive.Provider>
) {
return <MessageScrollerPrimitive.Provider {...props} />
}
function MessageScroller({
className,
...props
}: React.ComponentProps<typeof MessageScrollerPrimitive.Root>) {
return (
<MessageScrollerPrimitive.Root
data-slot="message-scroller"
className={cn(
"group/message-scroller relative flex size-full min-h-0 flex-col overflow-hidden",
className
)}
{...props}
/>
)
}
function MessageScrollerViewport({
className,
...props
}: React.ComponentProps<typeof MessageScrollerPrimitive.Viewport>) {
return (
<MessageScrollerPrimitive.Viewport
data-slot="message-scroller-viewport"
className={cn(
"size-full min-h-0 min-w-0 scroll-fade-b scrollbar-thin scrollbar-gutter-stable overflow-y-auto overscroll-contain contain-content data-autoscrolling:scrollbar-thumb-transparent data-autoscrolling:scrollbar-track-transparent data-pending-scroll:invisible",
className
)}
{...props}
/>
)
}
function MessageScrollerContent({
className,
...props
}: React.ComponentProps<typeof MessageScrollerPrimitive.Content>) {
return (
<MessageScrollerPrimitive.Content
data-slot="message-scroller-content"
className={cn("flex h-max min-h-full flex-col gap-8", className)}
{...props}
/>
)
}
function MessageScrollerItem({
className,
scrollAnchor = false,
...props
}: React.ComponentProps<typeof MessageScrollerPrimitive.Item>) {
return (
<MessageScrollerPrimitive.Item
data-slot="message-scroller-item"
scrollAnchor={scrollAnchor}
className={cn(
"min-w-0 shrink-0 [contain-intrinsic-size:auto_10rem] [content-visibility:auto]",
className
)}
{...props}
/>
)
}
function MessageScrollerButton({
direction = "end",
className,
children,
render,
variant = "secondary",
size = "icon-sm",
...props
}: React.ComponentProps<typeof MessageScrollerPrimitive.Button> &
Pick<React.ComponentProps<typeof Button>, "variant" | "size">) {
return (
<MessageScrollerPrimitive.Button
data-slot="message-scroller-button"
data-direction={direction}
data-variant={variant}
data-size={size}
direction={direction}
className={cn(
"absolute inset-s-1/2 -translate-x-1/2 border-border bg-background text-foreground transition-[translate,scale,opacity] duration-200 hover:bg-muted hover:text-foreground data-[active=false]:pointer-events-none data-[active=false]:scale-95 data-[active=false]:opacity-0 data-[active=false]:duration-400 data-[active=false]:ease-[cubic-bezier(0.7,0,0.84,0)] data-[active=true]:translate-y-0 data-[active=true]:scale-100 data-[active=true]:opacity-100 data-[active=true]:ease-[cubic-bezier(0.23,1,0.32,1)] data-[direction=end]:bottom-4 data-[direction=end]:data-[active=false]:translate-y-full data-[direction=start]:top-4 data-[direction=start]:data-[active=false]:-translate-y-full rtl:translate-x-1/2 data-[direction=start]:[&_svg]:rotate-180",
className
)}
render={render ?? <Button variant={variant} size={size} />}
{...props}
>
{children ?? (
<>
<ArrowDownIcon />
<span className="sr-only">
{direction === "end" ? "Scroll to end" : "Scroll to start"}
</span>
</>
)}
</MessageScrollerPrimitive.Button>
)
}
export {
MessageScrollerProvider,
MessageScroller,
MessageScrollerViewport,
MessageScrollerContent,
MessageScrollerItem,
MessageScrollerButton,
useMessageScroller,
useMessageScrollerScrollable,
useMessageScrollerVisibility,
}
Update the import paths to match your project setup.
Usage#
<MessageScrollerProvider autoScroll>
<MessageScroller>
<MessageScrollerViewport>
<MessageScrollerContent>
<MessageScrollerItem messageId={id} scrollAnchor>
<Message align="end">
<MessageContent>
<Bubble align="end" variant="muted">
<BubbleContent>{text}</BubbleContent>
</Bubble>
</MessageContent>
</Message>
</MessageScrollerItem>
</MessageScrollerContent>
</MessageScrollerViewport>
<MessageScrollerButton />
</MessageScroller>
</MessageScrollerProvider>scrollAnchor goes on the person's turn. autoScroll follows the reply while the person stays at the live edge.
See Message Scroller.
AI SDK Helper#
To build without a model, script the conversation with createChat. Each agent turn streams reasoning, a tool call and a reply. chat.next() fills the field with the next scripted message, and a string fallback replies once the script runs out.
const chat = createChat()
.user("Add a dark mode toggle to the settings page.")
.assistant(({ writer }) => {
writer.reasoning("I need to know how themes are set up first.")
writer
.tool("runCommand", { input: { command: "rg -l ThemeProvider app" } })
.output({ stdout: "app/layout.tsx" })
writer.text("next-themes is already set up, so I added a ModeToggle.")
})
const transport = chat.transport({
fallback:
"This demo only knows the scripted turns. Press reset to start over.",
})
const nextMessage = chat.next(messages)Migrating from AI SDK Elements#
If you added Conversation from AI SDK Elements, your project has a components/ai-elements/conversation.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Message Scroller. Every export keeps its name and props, so the code that uses it keeps working.
Migrate components/ai-elements/conversation.tsx from AI SDK Elements to the shadcn/ui Message Scroller component.
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
1. Install Message Scroller: npx shadcn@latest add message-scroller
2. Read https://ui.shadcn.com/docs/ai-elements/aria/chat.md and build Chat the same way.
3. Rebuild each part:
- Conversation renders a MessageScrollerProvider with autoScroll and a MessageScroller, instead of StickToBottom.
- ConversationContent renders a MessageScrollerViewport and a MessageScrollerContent.
- ConversationScrollButton renders a MessageScrollerButton.
- Keep ConversationEmptyState, ConversationDownload and messagesToMarkdown as they are.
4. In the code that renders the thread, wrap each message in a MessageScrollerItem with its messageId, and set scrollAnchor on the person's messages. This is the only change outside the file.
5. Remove use-stick-to-bottom from the dependencies.
6. Remove the imports the file no longer uses.
7. Run the type checker and fix any errors.