mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-07-08 14:35:09 +00:00
* feat(@shadcn/react): add message-scroller package Add the @shadcn/react headless primitives package with MessageScroller scroll anchoring, streaming follow, history prepend, and jump-to-message behavior. Includes geometry helpers, use-render utility, and unit, browser, and perf tests. * feat(registry): add chat components Add MessageScroller, Message, Bubble, Attachment, and Marker registry sources for base and radix, style variants, preview-03 chat blocks, and registry index wiring. * feat(v4): integrate chat components into docs site Wire chat components into the v4 app with docs routes, example preview pages, message part renderers, markdown support, registry build updates, and supporting lib utilities. * feat(examples): add chat component demos Add base and radix example demos for MessageScroller, Message, Bubble, Attachment, Marker, scroll-fade, and shimmer. * docs: add chat component documentation Add component and utility docs for the chat component set, update docs navigation, and add the June 2026 chat components changelog entry. * chore: regenerate registry JSON output Rebuild public registry artifacts for all style variants with the new chat components. * chore(release): add @shadcn/react publish and CI pipeline Add Changesets prerelease workflow, browser test job, RELEASING docs, and monorepo wiring for publishing @shadcn/react independently from the shadcn CLI. * docs: fix display of component preview on mobile * fix * fix * docs: add message scroller docs * style: format * fix
183 lines
6.2 KiB
TypeScript
183 lines
6.2 KiB
TypeScript
"use client"
|
|
|
|
import * as React from "react"
|
|
import { useChat } from "@ai-sdk/react"
|
|
import {
|
|
ArrowUpIcon,
|
|
MessageCircleDashedIcon,
|
|
RotateCwIcon,
|
|
} from "lucide-react"
|
|
|
|
import { createChat } from "@/lib/ai"
|
|
import {
|
|
MESSAGE_ANIMATIONS,
|
|
type MessageAnimationId,
|
|
} from "@/lib/message-animations"
|
|
import { MessageAnimated } from "@/components/message-animated"
|
|
import { Button } from "@/styles/base-rhea/ui/button"
|
|
import {
|
|
Card,
|
|
CardAction,
|
|
CardContent,
|
|
CardDescription,
|
|
CardFooter,
|
|
CardHeader,
|
|
CardTitle,
|
|
} from "@/styles/base-rhea/ui/card"
|
|
import {
|
|
Empty,
|
|
EmptyDescription,
|
|
EmptyHeader,
|
|
EmptyMedia,
|
|
EmptyTitle,
|
|
} from "@/styles/base-rhea/ui/empty"
|
|
import {
|
|
MessageScroller,
|
|
MessageScrollerButton,
|
|
MessageScrollerContent,
|
|
MessageScrollerProvider,
|
|
MessageScrollerViewport,
|
|
} from "@/styles/base-rhea/ui/message-scroller"
|
|
import {
|
|
Select,
|
|
SelectContent,
|
|
SelectGroup,
|
|
SelectItem,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/styles/base-rhea/ui/select"
|
|
|
|
const chat = createChat()
|
|
.user("Can user messages pop in like iMessage without breaking anchoring?")
|
|
.sleep(1000)
|
|
.assistant(
|
|
"Yes. Animate the user row with transform and opacity, and let the assistant response stream normally below it.\n\nThat keeps the row measurement predictable while still giving the newly sent bubble a more tactile entrance."
|
|
)
|
|
.user("What makes the animation feel more like iMessage?")
|
|
.sleep(1000)
|
|
.assistant(
|
|
"Use a quick spring from the trailing edge: a little scale, a small upward move, and no layout animation.\n\nThe bubble feels tactile, but the measured row stays predictable, so anchoring and auto-scroll do not have to fight a changing layout."
|
|
)
|
|
.user("Can I switch between presets while testing the same thread?")
|
|
.sleep(1000)
|
|
.assistant(
|
|
"Yes. Keep the conversation in place while you change the preset, then send the next message to compare the new entrance against the same context.\n\nThat makes it easier to judge the difference between a subtle fade, a snappy pop, and a more dramatic 3D tilt without rebuilding the scenario each time."
|
|
)
|
|
|
|
const initialMessages = chat.get({ count: 0 })
|
|
const transport = chat.transport({ chunkDelayMs: 15 })
|
|
|
|
export function MessageScrollerAnimation() {
|
|
const { messages, sendMessage, setMessages, status } = useChat({
|
|
messages: initialMessages,
|
|
transport,
|
|
})
|
|
const [presetId, setPresetId] = React.useState<MessageAnimationId>("fade")
|
|
const nextMessage = chat.next({ after: messages })
|
|
const isBusy = status === "submitted" || status === "streaming"
|
|
const preset = MESSAGE_ANIMATIONS[presetId as MessageAnimationId]
|
|
|
|
return (
|
|
<div className="relative flex flex-col gap-4">
|
|
<Card className="mx-auto h-140 w-full max-w-sm gap-0">
|
|
<CardHeader className="border-b">
|
|
<CardTitle>Animation</CardTitle>
|
|
<CardDescription>
|
|
Choose how user messages are animated when they are added to the
|
|
conversation.
|
|
</CardDescription>
|
|
<CardAction className="flex items-center gap-2">
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
size="icon"
|
|
aria-label="Reset animated messages"
|
|
disabled={messages.length === 0 || isBusy}
|
|
onClick={() => setMessages(initialMessages)}
|
|
>
|
|
<RotateCwIcon />
|
|
</Button>
|
|
</CardAction>
|
|
</CardHeader>
|
|
<CardContent className="min-h-0 flex-1 overflow-hidden p-0">
|
|
{messages.length === 0 ? (
|
|
<Empty className="h-full">
|
|
<EmptyHeader>
|
|
<EmptyMedia variant="icon">
|
|
<MessageCircleDashedIcon />
|
|
</EmptyMedia>
|
|
<EmptyTitle>No Messages Yet</EmptyTitle>
|
|
<EmptyDescription>
|
|
Click the button below to send the first message.
|
|
</EmptyDescription>
|
|
</EmptyHeader>
|
|
</Empty>
|
|
) : (
|
|
<MessageScrollerProvider>
|
|
<MessageScroller>
|
|
<MessageScrollerViewport>
|
|
<MessageScrollerContent
|
|
aria-busy={isBusy}
|
|
className="p-(--card-spacing)"
|
|
>
|
|
{messages.map((message) => (
|
|
<MessageAnimated
|
|
key={message.id}
|
|
message={message}
|
|
animationPreset={preset}
|
|
userVariant="muted"
|
|
assistantVariant="ghost"
|
|
/>
|
|
))}
|
|
</MessageScrollerContent>
|
|
</MessageScrollerViewport>
|
|
<MessageScrollerButton />
|
|
</MessageScroller>
|
|
</MessageScrollerProvider>
|
|
)}
|
|
</CardContent>
|
|
<CardFooter className="border-t">
|
|
<Select
|
|
value={presetId}
|
|
onValueChange={(value) => {
|
|
setPresetId(value as MessageAnimationId)
|
|
}}
|
|
>
|
|
<SelectTrigger aria-label="Animation preset">
|
|
<SelectValue>{preset.name}</SelectValue>
|
|
</SelectTrigger>
|
|
<SelectContent align="start" side="top">
|
|
<SelectGroup>
|
|
{Object.values(MESSAGE_ANIMATIONS).map((animation) => (
|
|
<SelectItem key={animation.id} value={animation.id}>
|
|
{animation.name}
|
|
</SelectItem>
|
|
))}
|
|
</SelectGroup>
|
|
</SelectContent>
|
|
</Select>
|
|
<Button
|
|
type="button"
|
|
size="icon"
|
|
className="ml-auto"
|
|
disabled={!nextMessage || isBusy}
|
|
onClick={() => {
|
|
if (!nextMessage || isBusy) {
|
|
return
|
|
}
|
|
|
|
void sendMessage(nextMessage)
|
|
}}
|
|
>
|
|
<ArrowUpIcon />
|
|
<span className="sr-only">Send Message</span>
|
|
</Button>
|
|
</CardFooter>
|
|
</Card>
|
|
<div className="mx-auto max-w-sm px-0.5 text-center text-xs text-balance text-muted-foreground">
|
|
Select an animation then click send to see it in action.
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|