Message Rich Text
Renders the rich text a Chat Input sends, so a sent message looks the way it did in the composer. Apps, tagged agents, skills, and files keep their chips, hints, and previews, and a tagged file opens in the File Viewer.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/message-rich-text.jsonCopies Message Rich Text and the components it uses (File Viewer, Hover Card, Rich Text Editor, and Tooltip) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/message-rich-text. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @hugeicons/core-free-icons @hugeicons/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Add the components it uses: File Viewer, Hover Card, Rich Text Editor, and Tooltip.
Copy and paste the following code into your project.
import { Fragment, useState, type ComponentProps, type CSSProperties, type ReactNode } from 'react'; import { LinkOffIcon } from '@/lib/sprout/icons/link-off-icon'; import { chatReferenceChars } from '@/lib/sprout/chat-reference'; import { cn } from '@/lib/sprout/classes'; import { KeywordHintBody } from '@/lib/sprout/keyword-hint'; import { ReferenceHintBody, ReferencePeek, referenceDetail, referenceHover, type ChatReferenceDetail, type ChatReferenceResolved, } from '@/lib/sprout/reference-hint'; import { hasHint, keywordPattern, objectReplacement, scanText, type KeywordHit, type KeywordPattern, } from '@/lib/sprout/keywords'; import type { RichTextKeyword, RichTextValue } from '@/components/ui/rich-text-editor'; import { FileViewer } from '@/components/ui/file-viewer'; import { HoverCard, HoverCardTrigger } from '@/components/ui/hover-card'; import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip'; export interface MessageRichTextProps extends Omit<ComponentProps<'div'>, 'children'> { value: RichTextValue; keywords?: readonly RichTextKeyword[] | undefined; accounts?: Readonly<Record<string, string>> | undefined; resolveReference?: | ((reference: MessageRichTextReference) => MessageRichTextResolvedReference | undefined) | undefined; renderNode?: ((node: RichTextValue) => ReactNode) | undefined; } export type MessageRichTextReference = ChatReferenceDetail; export type MessageRichTextResolvedReference = ChatReferenceResolved; function ChipHint({ hint, children }: { hint: ReactNode; children: ReactNode }) { return ( <Tooltip> <TooltipTrigger delay={150} tabIndex={0} render={<span className="sprout-message-rich-text-keyword" />}> {children} </TooltipTrigger> <TooltipContent className="sprout-message-rich-text-hint">{hint}</TooltipContent> </Tooltip> ); } type Mark = { type: string; attrs?: Record<string, unknown> }; const safeHref = (href: unknown) => typeof href === 'string' && /^(https?:|mailto:)/i.test(href.trim()) ? href.trim() : undefined; function withMarks(content: ReactNode, marks: readonly Mark[] | undefined, key: string): ReactNode { let node = content; for (const mark of marks ?? []) { if (mark.type === 'bold') node = <strong>{node}</strong>; else if (mark.type === 'italic') node = <em>{node}</em>; else if (mark.type === 'strike') node = <s>{node}</s>; else if (mark.type === 'underline') node = <u>{node}</u>; else if (mark.type === 'code') node = <code>{node}</code>; else if (mark.type === 'link') { const href = safeHref(mark.attrs?.href); if (href) node = ( <a href={href} target="_blank" rel="noopener noreferrer"> {node} </a> ); } } return <span key={key}>{node}</span>; } function Keyword({ keyword, children }: { keyword: RichTextKeyword; children: ReactNode }) { const connected = String(keyword.connected !== false); const style = keyword.color ? ({ '--sprout-rich-text-keyword-color': keyword.color } as CSSProperties) : undefined; const chip = ( <> {keyword.icon != null && ( <span className="sprout-rich-text-keyword-icon" data-keyword={keyword.id} data-connected={connected} aria-hidden="true" > <span className="sprout-rich-text-keyword-glyph">{keyword.icon}</span> </span> )} <span className="sprout-rich-text-keyword" data-keyword={keyword.id} data-connected={connected} data-icon={keyword.icon != null ? '' : undefined} data-tinted={keyword.color ? '' : undefined} style={style} > {children} </span> {keyword.connected === false && ( <span className="sprout-rich-text-keyword-status" data-keyword={keyword.id} data-connected="false" aria-hidden="true" > <span className="sprout-rich-text-keyword-glyph"> <LinkOffIcon /> </span> </span> )} </> ); if (!hasHint(keyword)) return chip; return <ChipHint hint={<KeywordHintBody keyword={keyword} />}>{chip}</ChipHint>; } function plainChild(child: RichTextValue) { return child.type === 'text' && !child.marks?.some(mark => mark.type === 'code' || mark.type === 'link'); } type Renderers = Pick<MessageRichTextProps, 'resolveReference' | 'renderNode'>; function Reference({ reference, resolved }: { reference: ChatReferenceDetail; resolved?: ChatReferenceResolved }) { const [open, setOpen] = useState(false); const chip = ( <span className="sprout-chat-input-reference" data-chat-reference={reference.kind} data-id={reference.id}> {`${chatReferenceChars[reference.kind] ?? ''}${reference.label}`} </span> ); const hover = referenceHover(reference, resolved); if (hover === 'hint') return <ChipHint hint={<ReferenceHintBody reference={reference} media={resolved?.media} />}>{chip}</ChipHint>; const file = resolved?.file; if (!file) return chip; const opener = { type: 'button' as const, className: 'sprout-message-rich-text-keyword sprout-message-rich-text-file', onClick: () => setOpen(true), }; return ( <> {hover === 'peek' ? ( <HoverCard> <HoverCardTrigger delay={300} closeDelay={150} render={<button {...opener} />}> {chip} </HoverCardTrigger> <ReferencePeek file={file} /> </HoverCard> ) : ( <button {...opener}>{chip}</button> )} <FileViewer file={file} open={open} onOpenChange={setOpen} /> </> ); } function renderInline( content: readonly RichTextValue[], pattern: KeywordPattern | null, renderers: Renderers, key: string, ) { const hits: KeywordHit[] = []; if (pattern) { const text = content.map(child => (plainChild(child) ? (child.text ?? '') : objectReplacement)).join(''); scanText(text, pattern, 0, hits); } const out: ReactNode[] = []; let offset = 0; content.forEach((child, index) => { const childKey = `${key}.${index}`; if (child.type === 'text') { const text = child.text ?? ''; const start = offset; const end = start + (plainChild(child) ? text.length : 1); offset = end; if (!plainChild(child)) return out.push(withMarks(text, child.marks as Mark[], childKey)); let cursor = start; for (const hit of hits) { if (hit.to <= start || hit.from >= end) continue; const from = Math.max(hit.from, start); const to = Math.min(hit.to, end); if (from > cursor) out.push(withMarks(text.slice(cursor - start, from - start), child.marks as Mark[], `${childKey}.${cursor}`)); out.push( <Keyword key={`${childKey}.${from}.k`} keyword={hit.keyword}> {withMarks(text.slice(from - start, to - start), child.marks as Mark[], 'text')} </Keyword>, ); cursor = to; } if (cursor < end) out.push(withMarks(text.slice(cursor - start), child.marks as Mark[], `${childKey}.${cursor}`)); return; } offset += 1; if (child.type === 'hardBreak') out.push(<br key={childKey} />); else if (child.type === 'chatReference') { const reference = referenceDetail(child.attrs); out.push(<Reference key={childKey} reference={reference} resolved={renderers.resolveReference?.(reference)} />); } else if (renderers.renderNode) out.push(<Fragment key={childKey}>{renderers.renderNode(child)}</Fragment>); }); return out; } function renderBlock( node: RichTextValue, pattern: KeywordPattern | null, renderers: Renderers, key: string, ): ReactNode { const children = (node.content ?? []).map((child, index) => renderBlock(child, pattern, renderers, `${key}.${index}`), ); const inline = () => renderInline(node.content ?? [], pattern, renderers, key); switch (node.type) { case 'doc': return children; case 'paragraph': return <p key={key}>{inline()}</p>; case 'heading': { const level = Math.min(Math.max(Number(node.attrs?.level) || 1, 1), 6); const Heading = `h${level}` as 'h1'; return <Heading key={key}>{inline()}</Heading>; } case 'bulletList': return <ul key={key}>{children}</ul>; case 'orderedList': return ( <ol key={key} start={Number(node.attrs?.start) || undefined}> {children} </ol> ); case 'listItem': return <li key={key}>{children}</li>; case 'blockquote': return <blockquote key={key}>{children}</blockquote>; case 'codeBlock': return ( <pre key={key}> <code>{(node.content ?? []).map(child => child.text ?? '').join('')}</code> </pre> ); case 'horizontalRule': return <hr key={key} />; default: return node.content?.some(child => child.type === 'text') ? <p key={key}>{inline()}</p> : children; } } export function MessageRichText({ value, keywords, accounts, resolveReference, renderNode, className, ...props }: MessageRichTextProps) { const sent = accounts ? keywords?.map(keyword => accounts[keyword.id] ? { ...keyword, connected: true, account: accounts[keyword.id] } : keyword, ) : keywords; const pattern = sent?.length ? keywordPattern(sent) : null; return ( <div data-slot="message-rich-text" className={cn('sprout-message-rich-text', className)} {...props}> {renderBlock(value, pattern, { resolveReference, renderNode }, 'n')} </div> ); }And the files it imports:
lib/sprout/icons/book-open-icon.tsxlib/sprout/icons/create-icon.tsxlib/sprout/icons/document-text-icon.tsxlib/sprout/icons/link-off-icon.tsxlib/sprout/icons/user-icon.tsxlib/sprout/chat-reference.tslib/sprout/keyword-hint.tsxlib/sprout/keywords.tslib/sprout/reference-hint.tsx
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-message-rich-text { overflow-wrap: anywhere; min-width: 0; } .sprout-message-rich-text-keyword { outline: none; } .sprout-message-rich-text-keyword:focus-visible > .sprout-rich-text-keyword { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 1px; } .sprout-message-rich-text-file { color: inherit; font: inherit; cursor: pointer; background: none; border: 0; padding: 0; } @media (hover: hover) { .sprout-message-rich-text-file:hover > .sprout-chat-input-reference { background-color: color-mix(in oklab, var(--sprout-text) 14%, transparent); } .sprout-chat-input-bar .sprout-input-group-button[data-variant="ghost"]:hover { color: var(--sprout-text); } .sprout-chat-input[data-mode-color] .sprout-chat-input-bar .sprout-chat-input-mode:hover { background: color-mix(in oklab, var(--sprout-chat-input-mode) 22%, transparent); } .sprout-attachment:has( > a, > button):hover { background: color-mix(in oklab, var(--sprout-hover) 50%, var(--sprout-surface)); } .sprout-file-attachment:has(button.sprout-file-attachment-open:not(:disabled)):hover { background: color-mix(in oklab, var(--sprout-hover) 50%, var(--sprout-surface)); } } .sprout-message-rich-text .sprout-rich-text-keyword-status { cursor: inherit; } .sprout-message-rich-text > * { margin-block: 0; } .sprout-message-rich-text > * + * { margin-top: .5em; } .sprout-message-rich-text :is(h1, h2, h3, h4, h5, h6) { font-size: 1em; font-weight: 600; } .sprout-message-rich-text :is(ul, ol) { padding-left: 1.25em; } .sprout-message-rich-text ul { list-style: outside; } .sprout-message-rich-text ol { list-style: decimal; } .sprout-message-rich-text li > p { margin: 0; } .sprout-message-rich-text blockquote { border-left: 2px solid var(--sprout-border); color: var(--sprout-muted); margin-inline: 0; padding-left: .75em; } .sprout-message-rich-text code { border-radius: var(--sprout-radius-control); background-color: color-mix(in oklab, var(--sprout-text) 8%, transparent); padding: .125em .3em; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .875em; } .sprout-message-rich-text pre { border-radius: var(--sprout-radius-control); background-color: color-mix(in oklab, var(--sprout-text) 8%, transparent); padding: .5em .75em; overflow-x: auto; } .sprout-message-rich-text pre code { white-space: pre; background-color: #0000; padding: 0; } .sprout-message-rich-text a { color: var(--sprout-info); text-underline-offset: 2px; text-decoration: underline; } .sprout-message-rich-text hr { border: 0; border-top: 1px solid var(--sprout-border); } .sprout-tooltip-content.sprout-message-rich-text-hint { border-radius: var(--sprout-radius-panel); box-shadow: var(--sprout-shadow-overlay); white-space: nowrap; gap: 6px; padding: 4px 10px; } .sprout-message-rich-text-hint > .sprout-tooltip-arrow { display: none; } .sprout-chat-input { box-sizing: border-box; width: 100%; min-width: 0; font-family: var(--sprout-font); flex-direction: column; gap: 12px; display: flex; } .sprout-chat-input-form { min-width: 0; position: relative; } .sprout-chat-input-attachments.sprout-input-group-addon { overscroll-behavior-x: contain; scrollbar-width: none; cursor: default; flex-wrap: nowrap; justify-content: flex-start; gap: 6px; padding: 4px 4px 0; overflow-x: auto; } .sprout-chat-input-attachments::-webkit-scrollbar { display: none; } .sprout-chat-input-attachments > * { flex-shrink: 0; } .sprout-chat-input-editor .sprout-rich-text-editor-body { min-height: 4.5rem; } .sprout-chat-input-editor .sprout-rich-text-editor-content { font-size: var(--sprout-text-base); line-height: 1.625; } .sprout-chat-input-reference { border-radius: var(--sprout-radius-control); background: color-mix(in oklab, var(--sprout-text) 8%, transparent); color: var(--sprout-text); white-space: nowrap; -webkit-box-decoration-break: clone; box-decoration-break: clone; padding: 0 .4375em; font-weight: 500; } .sprout-chat-input-reference[data-openable] { cursor: pointer; } .sprout-chat-input-reference[data-chat-reference="agent"] { background: var(--sprout-info-subtle); color: var(--sprout-info); } .sprout-chat-input-reference.ProseMirror-selectednode { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-chat-input-editor .sprout-rich-text-editor-content a { color: var(--sprout-info); text-underline-offset: 2px; text-decoration: underline; } .sprout-chat-input-bar { flex: 1; justify-content: space-between; align-items: center; gap: 8px; min-width: 0; display: flex; } .sprout-chat-input-bar-start, .sprout-chat-input-bar-end { align-items: center; gap: 2px; min-width: 0; display: flex; } .sprout-chat-input-bar-end { flex-shrink: 0; gap: 6px; } .sprout-chat-input-divider { background: var(--sprout-line); align-self: center; width: 1px; height: 20px; margin-inline: 6px; } .sprout-chat-input-bar .sprout-input-group-button[data-variant="ghost"] { color: var(--sprout-muted); } .sprout-chat-input-picker-label { text-overflow: ellipsis; white-space: nowrap; min-width: 0; max-width: 10rem; overflow: hidden; } .sprout-chat-input-bar .sprout-chat-input-mode { color: var(--sprout-text); } .sprout-chat-input[data-mode-color] .sprout-input-group.sprout-chat-input-box { border-color: var(--sprout-chat-input-mode); box-shadow: 0 0 0 3px color-mix(in oklab, var(--sprout-chat-input-mode) 14%, transparent); } .sprout-chat-input[data-mode-color] .sprout-chat-input-bar .sprout-chat-input-mode { background: color-mix(in oklab, var(--sprout-chat-input-mode) 14%, transparent); color: var(--sprout-chat-input-mode); } .sprout-chat-input-mode-option > :is(svg, .sprout-brand-icon) { color: var(--sprout-chat-input-mode); } .sprout-dropdown-menu-content.sprout-chat-input-menu { width: 18rem; } .sprout-chat-input-menu :is(.sprout-dropdown-menu-item, .sprout-command-item):has(.sprout-chat-input-option-text) { align-items: flex-start; padding-block: 6px; } .sprout-chat-input-menu :is(.sprout-dropdown-menu-item, .sprout-command-item):has(.sprout-chat-input-option-text) > :is(svg, .sprout-brand-icon) { margin-top: 2px; } .sprout-chat-input-option-text { flex-direction: column; gap: 2px; min-width: 0; display: flex; } .sprout-chat-input-option-description { color: var(--sprout-muted); font-size: var(--sprout-text-xs); line-height: 1rem; } .sprout-chat-input-panel { inset-inline: 0; z-index: 20; position: absolute; bottom: calc(100% + 8px); } .sprout-chat-input-panel[data-side="bottom"] { top: calc(100% + 8px); bottom: auto; } .sprout-chat-input-prompts { flex-wrap: wrap; gap: 8px; display: flex; } .sprout-chat-input-stop { background: currentColor; border-radius: 2px; width: 10px; height: 10px; } .sprout-button.sprout-chat-input-prompt { font-weight: 400; } @media (pointer: coarse) { .sprout-chat-input-picker-label { max-width: 6rem; } .sprout-chat-input .sprout-button.sprout-chat-input-send { border-radius: calc(var(--sprout-radius-control) + 8px); background-clip: padding-box; border: 8px solid #0000; } .sprout-attachment-carousel-arrow { opacity: 1; } } @media (width <= 480px) { .sprout-chat-input-bar .sprout-chat-input-picker-label { display: none; } } @media (prefers-reduced-motion: no-preference) { .sprout-chat-input[data-status="streaming"] .sprout-rich-text-editor-content .is-editor-empty:first-child:before { animation: 1.6s ease-in-out infinite alternate sprout-chat-input-pulse; } .sprout-input-group.sprout-chat-input-box { transition: border-color var(--sprout-motion-normal) var(--sprout-motion-ease), box-shadow var(--sprout-motion-normal) var(--sprout-motion-ease); } .sprout-attachment, .sprout-file-attachment, .sprout-attachment-dropzone { transition: background-color var(--sprout-motion-normal) var(--sprout-motion-ease), border-color var(--sprout-motion-normal) var(--sprout-motion-ease); } .sprout-attachment-media[data-variant="image"] { transition: opacity var(--sprout-motion-normal) var(--sprout-motion-ease); } .sprout-attachment-carousel-arrow { transition: opacity var(--sprout-motion-fast) var(--sprout-motion-ease), background-color var(--sprout-motion-fast) var(--sprout-motion-ease), color var(--sprout-motion-fast) var(--sprout-motion-ease); } .sprout-attachment-carousel-viewport { scroll-behavior: smooth; } .sprout-attachment:is([data-state="uploading"], [data-state="processing"]) .sprout-attachment-title, .sprout-file-attachment:is([data-state="uploading"], [data-state="processing"]) .sprout-file-attachment-name { background: linear-gradient(90deg, var(--sprout-muted) 0%, var(--sprout-muted) 40%, var(--sprout-text) 50%, var(--sprout-muted) 60%, var(--sprout-muted) 100%) 0 0 / 250% 100%; -webkit-text-fill-color: transparent; -webkit-background-clip: text; background-clip: text; animation: 1.8s linear infinite sprout-attachment-shimmer; } } @keyframes sprout-chat-input-pulse { to { opacity: .4; } } @media (forced-colors: active) { .sprout-chat-input[data-status="streaming"] .sprout-rich-text-editor-content .is-editor-empty:first-child:before { color: graytext; } .sprout-attachment-title, .sprout-file-attachment-name { -webkit-text-fill-color: currentColor !important; background: none !important; } } .sprout-attachment { box-sizing: border-box; border: 1px solid var(--sprout-line); background: var(--sprout-surface); width: fit-content; min-width: 0; max-width: 100%; color: var(--sprout-text); border-radius: 12px; flex-wrap: wrap; flex-shrink: 0; font-family: inherit; display: flex; position: relative; } .sprout-attachment:has( > .sprout-attachment-trigger:focus-visible) { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-attachment[data-state="idle"] { border-style: dashed; } .sprout-attachment[data-state="error"] { border-color: color-mix(in oklab, var(--sprout-danger) 35%, transparent); } .sprout-attachment-size-lg { font-size: var(--sprout-text-sm); gap: 8px; } .sprout-attachment-size-lg:has([data-slot="attachment-content"]) { padding: 8px 10px; } .sprout-attachment-size-lg:has([data-slot="attachment-media"]) { padding: 8px; } .sprout-attachment-size-md { font-size: var(--sprout-text-xs); gap: 10px; } .sprout-attachment-size-md:has([data-slot="attachment-content"]) { padding: 6px 8px; } .sprout-attachment-size-md:has([data-slot="attachment-media"]) { padding: 6px; } .sprout-attachment-size-sm { font-size: var(--sprout-text-xs); border-radius: 8px; gap: 6px; } .sprout-attachment-size-sm:has([data-slot="attachment-content"]) { padding: 4px 6px; } .sprout-attachment-size-sm:has([data-slot="attachment-media"]) { padding: 4px; } .sprout-attachment-horizontal { align-items: center; min-width: 10rem; } .sprout-attachment-vertical { flex-direction: column; width: 6rem; } .sprout-attachment-vertical:has([data-slot="attachment-content"]) { width: 7.5rem; } .sprout-attachment-media { aspect-ratio: 1; background: var(--sprout-hover); width: 40px; color: var(--sprout-text); border-radius: 8px; flex-shrink: 0; justify-content: center; align-items: center; display: flex; position: relative; overflow: hidden; } .sprout-attachment-size-md .sprout-attachment-media { width: 32px; } .sprout-attachment-size-sm .sprout-attachment-media { border-radius: 6px; width: 28px; } .sprout-attachment-vertical .sprout-attachment-media { width: 100%; } .sprout-attachment[data-state="error"] .sprout-attachment-media { background: var(--sprout-danger-subtle); color: var(--sprout-danger); } .sprout-attachment-media svg { pointer-events: none; } .sprout-attachment-media svg:not([class*="size-"]) { width: 16px; height: 16px; } .sprout-attachment-vertical .sprout-attachment-media svg:not([class*="size-"]), .sprout-attachment-vertical .sprout-attachment-media > [data-slot="spinner"] { width: 24px; height: 24px; } .sprout-attachment-size-sm .sprout-attachment-media svg:not([class*="size-"]) { width: 14px; height: 14px; } .sprout-attachment-media[data-variant="image"] { opacity: .6; } .sprout-attachment:is([data-state="done"], [data-state="idle"]) .sprout-attachment-media[data-variant="image"] { opacity: 1; } .sprout-attachment-media[data-variant="image"] > img { aspect-ratio: 1; object-fit: cover; width: 100%; } .sprout-attachment-content { flex: 1; min-width: 0; max-width: 100%; line-height: 1.25; } .sprout-attachment-vertical .sprout-attachment-content { padding-inline: 4px; } .sprout-attachment-title { text-overflow: ellipsis; white-space: nowrap; min-width: 0; max-width: 100%; font-weight: 500; display: block; overflow: hidden; } .sprout-attachment-description { min-width: 0; max-width: 100%; color: var(--sprout-muted); font-size: var(--sprout-text-xs); text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; display: block; overflow: hidden; } .sprout-attachment[data-state="error"] .sprout-attachment-description { color: var(--sprout-danger); } .sprout-attachment-actions { z-index: 2; flex-shrink: 0; align-items: center; display: flex; position: relative; } .sprout-attachment-vertical .sprout-attachment-actions { gap: 4px; position: absolute; top: 12px; right: 12px; } .sprout-attachment-vertical .sprout-attachment-actions .sprout-button { background: color-mix(in oklab, var(--sprout-surface) 88%, transparent); } .sprout-attachment-size-sm.sprout-attachment-vertical .sprout-attachment-actions { top: 6px; right: 6px; } .sprout-attachment-trigger { z-index: 1; border-radius: inherit; cursor: pointer; background: none; border: 0; outline: none; margin: 0; padding: 0; position: absolute; inset: 0; } .sprout-attachment:is([data-state="uploading"], [data-state="processing"]) .sprout-attachment-title, .sprout-file-attachment:is([data-state="uploading"], [data-state="processing"]) .sprout-file-attachment-name { color: var(--sprout-muted); } .sprout-attachment-group { min-width: 0; } .sprout-attachment-group-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; display: grid; } @media (width >= 40rem) { .sprout-attachment-group-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } } .sprout-attachment-group-list { flex-direction: column; gap: 8px; display: flex; } .sprout-attachment-group-inline { flex-wrap: wrap; gap: 8px; display: flex; } .sprout-attachment-group-carousel { --sprout-attachment-fade: var(--sprout-surface); position: relative; } .sprout-attachment-carousel-viewport { overscroll-behavior-x: contain; scrollbar-width: none; scroll-padding-inline: 32px; overflow: auto hidden; } .sprout-attachment-carousel-viewport::-webkit-scrollbar { display: none; } .sprout-attachment-carousel-track { gap: 8px; width: max-content; padding: 4px 3px; display: flex; } .sprout-attachment-carousel-item { flex-shrink: 0; min-width: 0; } .sprout-attachment-carousel-edge { pointer-events: none; align-items: center; display: flex; position: absolute; inset-block: 0; } .sprout-attachment-carousel-edge[data-side="start"] { background: linear-gradient(to right, var(--sprout-attachment-fade), transparent); padding-right: 32px; left: 0; } .sprout-attachment-carousel-edge[data-side="end"] { background: linear-gradient(to left, var(--sprout-attachment-fade), transparent); padding-left: 32px; right: 0; } .sprout-attachment-carousel-arrow { opacity: 0; pointer-events: auto; } .sprout-attachment-group-carousel:hover .sprout-attachment-carousel-arrow, .sprout-attachment-group-carousel:focus-within .sprout-attachment-carousel-arrow, .sprout-attachment-carousel-arrow:focus-visible { opacity: 1; } .sprout-file-type-icon { flex-shrink: 0; } .sprout-file-type-icon-size-sm { width: 20px; height: 24px; } .sprout-file-type-icon-size-lg { width: 30px; height: 36px; } .sprout-file-type-icon-paper { fill: var(--sprout-file-paper); stroke: var(--sprout-file-edge); } .sprout-file-type-icon-fold { fill: none; stroke: var(--sprout-file-edge); } .sprout-file-type-icon-lines { stroke: var(--sprout-file-edge); } .sprout-file-type-icon-badge { fill: var(--sprout-file-generic); } .sprout-file-type-icon[data-kind="pdf"] .sprout-file-type-icon-badge { fill: var(--sprout-file-pdf); } .sprout-file-type-icon[data-kind="sheet"] .sprout-file-type-icon-badge { fill: var(--sprout-file-sheet); } .sprout-file-type-icon:is([data-kind="document"], [data-kind="google"]) .sprout-file-type-icon-badge { fill: var(--sprout-file-document); } .sprout-file-type-icon[data-kind="slides"] .sprout-file-type-icon-badge { fill: var(--sprout-file-slides); } .sprout-file-type-icon[data-kind="image"] .sprout-file-type-icon-badge { fill: var(--sprout-file-image); } .sprout-file-type-icon-label { fill: var(--sprout-file-label); font-family: inherit; } .sprout-file-attachment { box-sizing: border-box; border: 1px solid var(--sprout-line); background: var(--sprout-surface); min-width: 0; color: var(--sprout-text); border-radius: 12px; font-family: inherit; position: relative; } .sprout-file-attachment[data-state="error"] { border-color: var(--sprout-danger); } .sprout-file-attachment[data-variant="list"] { align-items: center; gap: 8px; padding: 8px; display: flex; } .sprout-file-attachment[data-variant="inline"] { align-items: center; gap: 4px; max-width: 100%; padding-left: 8px; display: flex; } .sprout-file-attachment[data-variant="inline"]:not(:has(.sprout-file-attachment-actions)) { padding-right: 10px; } .sprout-file-attachment[data-variant="grid"] { flex-direction: column; display: flex; } .sprout-file-attachment[data-variant="chip"] { max-width: 12rem; height: 36px; box-shadow: var(--sprout-shadow-control); flex-shrink: 0; align-items: center; gap: 4px; padding: 0 12px 0 10px; display: flex; } .sprout-file-attachment[data-variant="chip"][data-removable] { padding-right: 4px; } .sprout-file-attachment-open { min-width: 0; color: inherit; font: inherit; text-align: left; background: none; border: 0; border-radius: 6px; flex: 1; margin: 0; padding: 4px 0; display: block; } button.sprout-file-attachment-open { cursor: pointer; } button.sprout-file-attachment-open:disabled { cursor: default; } .sprout-file-attachment[data-variant="grid"] .sprout-file-attachment-open { padding: 12px; } .sprout-file-attachment[data-variant="chip"] .sprout-file-attachment-open { align-items: center; gap: 8px; padding: 0; display: flex; } .sprout-hover-card-content.sprout-file-attachment-peek { width: 22rem; padding: 4px; } .sprout-file-attachment-open:focus-visible, .sprout-file-attachment-preview:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-file-attachment[data-variant="chip"] button.sprout-file-attachment-open:after { content: ""; border-radius: inherit; position: absolute; inset: 0; } .sprout-file-attachment[data-variant="chip"] > :not(.sprout-file-attachment-open) { z-index: 1; position: relative; } .sprout-file-attachment[data-variant="chip"] .sprout-file-attachment-open:focus-visible { outline: none; } .sprout-file-attachment[data-variant="chip"]:has( > .sprout-file-attachment-open:focus-visible) { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-file-attachment-name { min-width: 0; font-size: var(--sprout-text-sm); text-overflow: ellipsis; white-space: nowrap; font-weight: 500; line-height: 1.25rem; display: block; overflow: hidden; } .sprout-file-attachment[data-variant="chip"] .sprout-file-attachment-name { font-size: var(--sprout-text-xs); line-height: 1rem; } .sprout-file-attachment-status { color: var(--sprout-muted); font-size: var(--sprout-text-xs); align-items: center; gap: 4px; margin-top: 2px; line-height: 1rem; display: flex; } .sprout-file-attachment[data-state="error"] .sprout-file-attachment-status, .sprout-file-attachment-failed { color: var(--sprout-danger); } .sprout-file-attachment-failed { font-size: var(--sprout-text-xs); flex-shrink: 0; } .sprout-file-attachment-media { color: var(--sprout-muted); flex-shrink: 0; display: flex; } .sprout-file-attachment[data-variant="list"] .sprout-file-attachment-media { background: var(--sprout-hover); border-radius: 6px; justify-content: center; align-items: center; width: 40px; height: 40px; overflow: hidden; } .sprout-file-attachment-media > .sprout-attachment-image { object-fit: cover; width: 100%; height: 100%; } .sprout-file-attachment-preview { aspect-ratio: 16 / 9; background: var(--sprout-hover); width: 100%; color: var(--sprout-muted); font: inherit; border: 0; border-radius: 11px 11px 0 0; justify-content: center; align-items: center; margin: 0; padding: 0; display: flex; overflow: hidden; } button.sprout-file-attachment-preview { cursor: pointer; } .sprout-file-attachment-preview > .sprout-attachment-image { object-fit: cover; width: 100%; height: 100%; } .sprout-file-attachment-thumb { background: var(--sprout-file-image); border-radius: 6px; flex-shrink: 0; justify-content: center; align-items: center; width: 22px; height: 22px; display: flex; overflow: hidden; } .sprout-file-attachment-thumb > .sprout-attachment-image { object-fit: cover; width: 100%; height: 100%; } .sprout-file-attachment-thumb > .sprout-attachment-image-failed { padding: 0; font-size: 0; } .sprout-file-attachment-thumb > .sprout-attachment-image-failed svg { width: 12px; height: 12px; } .sprout-file-attachment-actions { flex-shrink: 0; align-items: center; display: flex; } .sprout-file-attachment[data-variant="grid"] .sprout-file-attachment-actions { background: var(--sprout-surface); border-radius: 6px; position: absolute; top: 4px; right: 4px; } .sprout-attachment-image-failed { color: var(--sprout-muted); font-size: var(--sprout-text-xs); align-items: center; gap: 8px; padding: 8px; display: flex; } .sprout-attachment-dropzone { box-sizing: border-box; border: 1px dashed var(--sprout-input); background: color-mix(in oklab, var(--sprout-hover) 30%, var(--sprout-surface)); color: var(--sprout-text); text-align: center; border-radius: 12px; padding: 20px; } .sprout-attachment-dropzone[data-dragging] { border-color: var(--sprout-focus); background: var(--sprout-hover); } .sprout-attachment-dropzone[data-disabled] { cursor: not-allowed; } .sprout-attachment-dropzone[data-disabled] .sprout-attachment-dropzone-icon { opacity: .5; } .sprout-attachment-dropzone-icon { color: var(--sprout-muted); margin: 0 auto 8px; display: block; } .sprout-attachment-dropzone-text { color: var(--sprout-muted); font-size: var(--sprout-text-sm); margin: 0 0 12px; line-height: 1.25rem; } @keyframes sprout-attachment-shimmer { from { background-position: 100% 0; } to { background-position: 0 0; } } } @layer base { :root, .light { --sprout-file-paper: oklch(100% 0 0); --sprout-file-edge: oklch(87.17% .0082 286.23); --sprout-file-generic: oklch(65.18% .0118 280.36); --sprout-file-pdf: oklch(61.65% .1939 27.02); --sprout-file-sheet: oklch(57.34% .1422 151.05); --sprout-file-document: oklch(53.55% .1755 260.83); --sprout-file-slides: oklch(63.49% .1638 42.8); --sprout-file-image: oklch(85.06% .0674 242.56); --sprout-file-label: oklch(100% 0 0); } .dark { --sprout-file-paper: oklch(29.01% .0092 276.8); --sprout-file-edge: oklch(41.45% .0116 279.39); --sprout-file-image: oklch(57.61% .0841 245.81); } @media (prefers-color-scheme: dark) { :root:not(.light):not(.dark) { --sprout-file-paper: oklch(29.01% .0092 276.8); --sprout-file-edge: oklch(41.45% .0116 279.39); --sprout-file-image: oklch(57.61% .0841 245.81); } } }Import it from
@/components/ui/message-rich-text. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/message-rich-text.
Usage
import { ChatInput } from "@tulipfarm/sprout/chat-input";
import { MessageRichText } from "@tulipfarm/sprout/message-rich-text";
import { keywordAccounts } from "@tulipfarm/sprout/rich-text-editor";
function send() {
addMessage({ value, accounts: keywordAccounts(value, integrations) });
}
<MessageBubble variant="secondary">
<MessageRichText
value={message.value}
keywords={integrations}
accounts={message.accounts}
resolveReference={({ kind, id }) => lookup(kind, id)}
/>
</MessageBubble>value is the Chat Input value. keywords takes the same list as Chat Input’sintegrations, so app names become the same chips. Paragraphs, headings, lists, quotes, code blocks, bold, italics, underline, strikethrough, inline code, and links are kept; a link is shown only when it is http, https, ormailto.
Accounts
An app chip names the account it used. Call keywordAccounts(value, integrations) when the message is sent and store the result with it. Pass it back as accounts and the chip keeps that account, even after the person switches accounts or disconnects the app. Without it, chips show the current account, and a disconnected app shows the same warning as the composer.
Agents, skills, and files
Tags keep their @, /, or # prefix. resolveReference returns the tagged item, the same shape as a Chat Input suggestion: media becomes the chip icon, andfile makes a file chip a button that opens the File Viewer. Hovering a CSV, TSV, text, or image file shows a preview. Return nothing for a file that no longer exists and its chip stays plain text. Agents and skills show their description on hover or focus.
Accessibility
Hints open on hover and on keyboard focus, and every chip with a hint can be reached with Tab. A file chip is a button named after the file and opens the viewer with Enter or Space. Chips use the composer’s colours, so their contrast is the same in every theme.