"use client"; import { ChevronDown } from "lucide-react"; import { animate, type HTMLMotionProps, motion, useMotionTemplate, useMotionValue, useReducedMotion, } from "motion/react"; import { type ComponentPropsWithRef, cloneElement, createContext, type ReactElement, type ReactNode, type Ref, useCallback, useContext, useId, useLayoutEffect, useRef, useState, } from "react"; import { MessageSideContext } from "@/components/agents/message-context"; import { EASE_OUT, SPRING_LAYOUT, SPRING_SWAP } from "@/lib/ease"; import { useMessages } from "@/lib/i18n"; import { mergeRefs } from "@/lib/merge-refs"; import { cn } from "@/lib/utils"; export type MessageBubbleVariant = | "solid" | "soft" | "tint" | "outline" | "ghost" | "danger"; export type MessageBubbleAlign = "start" | "end"; /** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */ export const messageBubbleMessages = { showMore: "Show more", showLess: "Show less", }; export type MessageBubbleMessages = typeof messageBubbleMessages; interface MessageBubbleContextValue { align?: MessageBubbleAlign; animateIn: boolean; variant: MessageBubbleVariant; labels?: Partial; } const MessageBubbleContext = createContext({ animateIn: true, variant: "soft", }); export interface MessageBubbleProps extends Omit, "children"> { /** Fill and border style of the bubble. Default: "soft" */ variant?: MessageBubbleVariant; /** Defaults to the surrounding Message alignment when omitted. */ align?: MessageBubbleAlign; /** Plays the bubble entrance once when this component mounts. */ animateIn?: boolean; /** Override any built-in string. */ labels?: Partial; children?: ReactNode; } export interface MessageBubbleContentProps extends ComponentPropsWithRef<"div"> { /** Replaces the content element while preserving bubble styling. */ render?: ReactElement; } export interface MessageBubbleGroupProps extends ComponentPropsWithRef<"div"> { /** Gap between bubbles. Default: "compact" */ spacing?: "compact" | "default"; } export interface MessageBubbleCollapsibleProps extends ComponentPropsWithRef<"div"> { /** Whether the full text shows, for controlled use. */ open?: boolean; /** Whether the full text shows at first when uncontrolled. Default: false */ defaultOpen?: boolean; /** Called with the new state when the text expands or collapses. */ onOpenChange?: (open: boolean) => void; /** Lines shown while collapsed. Default: 4 */ collapsedLines?: 2 | 3 | 4 | 5 | 6; /** Text of the expand button. Default: "Show more" */ moreLabel?: ReactNode; /** Text of the collapse button. Default: "Show less" */ lessLabel?: ReactNode; /** Classes merged onto the clamped text wrapper. */ contentClassName?: string; /** Classes merged onto the show more button. */ triggerClassName?: string; children?: ReactNode; } const BUBBLE_CONTENT_REVEAL = { duration: 0.12, ease: EASE_OUT, delay: 0.04, } as const; // Expanded text wipes in top to bottom, quick enough to read straight on. const REVEAL = { duration: 0.32, ease: EASE_OUT } as const; // Clamped text stays solid to here (%), then fades out by the bottom. const CLAMP_SOLID = 68; // How soft the moving edge of the wipe is, in % of the text's height. const WIPE_EDGE = 12; // Sent bubbles should pop into place quickly with one restrained overshoot. const BUBBLE_POP = { type: "spring", stiffness: 520, damping: 27, mass: 0.52, } as const; export function MessageBubble({ variant = "soft", align, animateIn = false, labels, className, children, initial, animate, exit, transition, layout, ...props }: MessageBubbleProps) { const reduce = useReducedMotion() ?? false; const messageSide = useContext(MessageSideContext); const resolvedAlign = align ?? messageSide ?? "start"; return ( {children} ); } function bubbleContentClass( variant: MessageBubbleVariant, align: MessageBubbleAlign, interactive: boolean, ) { return cn( "relative z-0 min-w-9 max-w-[82%] rounded-2xl px-3.5 py-2.5 text-sm leading-6 text-foreground", // The corner by the speaker is tighter, so a bubble points at who said it. align === "end" ? "rounded-ee-md" : "rounded-es-md", "[&_a]:font-medium [&_a]:underline [&_a]:underline-offset-4 [&_code]:rounded [&_code]:bg-background/60 [&_code]:px-1 [&_code]:py-0.5 [&_code]:font-mono [&_code]:text-[0.9em] [&_ol]:my-2 [&_ol]:list-decimal [&_ol]:pl-5 [&_p+p]:mt-2 [&_pre]:my-2 [&_pre]:overflow-x-auto [&_pre]:rounded-xl [&_pre]:bg-background/60 [&_pre]:p-3 [&_ul]:my-2 [&_ul]:list-disc [&_ul]:pl-5", variant === "solid" && "text-primary-foreground [&_code]:bg-primary-foreground/15 [&_pre]:bg-primary-foreground/15", variant === "ghost" && "w-full max-w-none rounded-none px-0 py-0", variant === "danger" && "text-destructive", interactive && "cursor-pointer text-left outline-none transition-[background-color,color,transform] duration-150 hover:brightness-[0.98] focus-visible:ring-2 focus-visible:ring-ring active:scale-[0.99]", ); } function bubbleSurfaceClass( variant: MessageBubbleVariant, align: MessageBubbleAlign, ) { return cn( "pointer-events-none absolute inset-0 -z-10 rounded-[inherit]", align === "end" ? "origin-bottom-right" : "origin-bottom-left", variant === "solid" && "bg-primary", variant === "soft" && "bg-muted", variant === "tint" && "bg-muted", variant === "outline" && "border border-border/70 bg-background", variant === "danger" && "bg-destructive/10", ); } export function MessageBubbleContent({ render, className, children, ref, ...props }: MessageBubbleContentProps) { const reduce = useReducedMotion() ?? false; const { align = "start", animateIn, variant } = useContext(MessageBubbleContext); const interactive = render?.type === "button" || render?.type === "a"; const classes = cn( bubbleContentClass(variant, align, interactive), className, ); const composedChildren = ( <> {/* The surface pops in once, then always matches its text: a surface that springs after its content leaves streamed lines and expanded text hanging outside the bubble. */} {variant !== "ghost" ? (