Files
shadcn-ui/apps/v4/components/message-animated.tsx
shadcn 18fcf0f766 feat: @shadcn/react (#11022)
* 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
2026-06-26 21:19:31 +04:00

149 lines
4.0 KiB
TypeScript

"use client"
import * as React from "react"
import { motion, useReducedMotion } from "motion/react"
import type { MessageAnimationPreset } from "@/lib/message-animations"
import { MESSAGE_ANIMATIONS } from "@/lib/message-animations"
import { Bubble, BubbleContent } from "@/styles/radix-rhea/ui/bubble"
import { Message, MessageContent } from "@/styles/radix-rhea/ui/message"
import { MessageScrollerItem } from "@/styles/radix-rhea/ui/message-scroller"
type MessageAnimatedPart = {
type: string
text?: string
}
type MessageAnimatedMessage = {
id: string
role: string
text?: string
parts?: ReadonlyArray<MessageAnimatedPart>
}
type MessageAnimatedTextPart = {
key: string
text: string
}
const MotionMessageScrollerItem = motion.create(MessageScrollerItem)
function MessageAnimated({
message,
animationPreset = MESSAGE_ANIMATIONS["slide-up"],
assistantVariant = "ghost",
scrollAnchor,
userVariant = "muted",
...props
}: Omit<
React.ComponentProps<typeof MotionMessageScrollerItem>,
"animate" | "children" | "exit" | "initial" | "messageId" | "variants"
> & {
animationPreset?: MessageAnimationPreset
assistantVariant?: React.ComponentProps<typeof Bubble>["variant"]
message: MessageAnimatedMessage
userVariant?: React.ComponentProps<typeof Bubble>["variant"]
}) {
const shouldReduceMotion = useReducedMotion()
const isUserMessage = message.role === "user"
if (isUserMessage) {
return (
<MotionMessageScrollerItem
messageId={message.id}
scrollAnchor={scrollAnchor ?? true}
variants={animationPreset.variants}
initial={shouldReduceMotion ? false : "initial"}
animate="animate"
exit={shouldReduceMotion ? undefined : "exit"}
{...props}
>
<MessageAnimatedRow
message={message}
assistantVariant={assistantVariant}
userVariant={userVariant}
/>
</MotionMessageScrollerItem>
)
}
return (
<MotionMessageScrollerItem
messageId={message.id}
scrollAnchor={scrollAnchor}
initial={false}
{...props}
>
<MessageAnimatedRow
message={message}
assistantVariant={assistantVariant}
userVariant={userVariant}
/>
</MotionMessageScrollerItem>
)
}
function MessageAnimatedRow({
message,
assistantVariant,
userVariant,
}: {
assistantVariant: React.ComponentProps<typeof Bubble>["variant"]
message: MessageAnimatedMessage
userVariant: React.ComponentProps<typeof Bubble>["variant"]
}) {
const isUserMessage = message.role === "user"
const textParts = getMessageAnimatedTextParts(message)
return (
<Message align={isUserMessage ? "end" : "start"}>
<MessageContent>
{textParts.map((part) => {
const paragraphs = part.text
.split(/\n\s*\n/)
.map((paragraph) => paragraph.trim())
.filter(Boolean)
return (
<Bubble
key={part.key}
variant={isUserMessage ? userVariant : assistantVariant}
>
<BubbleContent className="space-y-2">
{paragraphs.map((paragraph, paragraphIndex) => (
<p
key={`${part.key}-${paragraphIndex}`}
className="whitespace-pre-wrap"
>
{paragraph}
</p>
))}
</BubbleContent>
</Bubble>
)
})}
</MessageContent>
</Message>
)
}
function getMessageAnimatedTextParts(
message: MessageAnimatedMessage
): MessageAnimatedTextPart[] {
if (message.parts) {
return message.parts.flatMap((part, index) => {
if (part.type !== "text" || typeof part.text !== "string") {
return []
}
return [{ key: `${message.id}-${index}`, text: part.text }]
})
}
return typeof message.text === "string"
? [{ key: `${message.id}-text`, text: message.text }]
: []
}
export { MessageAnimated, type MessageAnimatedMessage }