Message Response
Turns the Markdown an agent writes into a readable reply. It supports GitHub tables and task lists, shows fenced code in a Code Block, drops raw HTML and images, and lets you replace links with citations or downloads.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/message-response.jsonCopies Message Response and the components it uses (Code Block) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/message-response. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge react-markdown remark-gfmAdd 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: Code Block.
Copy and paste the following code into your project.
'use client'; import { createContext, useContext, useMemo, type ComponentProps, type HTMLAttributes, type ReactNode } from 'react'; import Markdown, { type Components } from 'react-markdown'; import remarkGfm from 'remark-gfm'; import { cn } from '@/lib/sprout/classes'; import { CodeBlock } from '@/components/ui/code-block'; export type MessageResponseRenderLink = (href: string, children: ReactNode) => ReactNode; const LinkContext = createContext<MessageResponseRenderLink | undefined>(undefined); const TableContext = createContext(false); export interface MessageTableViewportProps { bounded: boolean; children?: ReactNode; } export function MessageTableViewport({ bounded, children }: MessageTableViewportProps) { return <TableContext value={bounded}>{children}</TableContext>; } const ResponseTable: NonNullable<Components['table']> = ({ children }) => { const bounded = useContext(TableContext); return ( <div className="sprout-message-response-table" data-bounded={bounded ? '' : undefined} tabIndex={0} role="region" aria-label="Response table" > <table>{children}</table> </div> ); }; function ResponseLink({ href, children }: ComponentProps<'a'>) { const render = useContext(LinkContext); return render ? ( render(href ?? '', children) ) : ( <a href={href} target="_blank" rel="noopener noreferrer"> {children} </a> ); } const components: Components = { pre({ node, children }) { const code = node?.children[0]; if (code?.type !== 'element' || code.tagName !== 'code') return <pre>{children}</pre>; const classes = code.properties.className; const language = Array.isArray(classes) ? classes.find((value): value is string => typeof value === 'string' && value.startsWith('language-')) : undefined; const text = code.children .map(child => (child.type === 'text' ? child.value : '')) .join('') .replace(/\n$/, ''); return <CodeBlock code={text} language={language ? language.slice(9) : 'text'} />; }, a: ResponseLink, table: ResponseTable, input: ({ node: _node, ...props }) => props.type === 'checkbox' ? <input {...props} aria-label="Task" /> : <input {...props} />, }; export interface MessageResponseProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> { children: string; streaming?: boolean | undefined; renderLink?: MessageResponseRenderLink | undefined; } export function MessageResponse({ children, streaming = false, renderLink, className, ...props }: MessageResponseProps) { const markdown = useMemo( () => ( <Markdown remarkPlugins={[remarkGfm]} components={components} skipHtml disallowedElements={['img']}> {children} </Markdown> ), [children], ); return ( <div data-slot="message-response" data-streaming={streaming ? '' : undefined} aria-busy={streaming} {...props} className={cn('sprout-message-response', className)} > <LinkContext value={renderLink}>{markdown}</LinkContext> {streaming && <span aria-hidden="true" className="sprout-message-response-caret" />} </div> ); }Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-message-response { min-width: 0; color: var(--sprout-text); font-family: var(--sprout-font); font-size: var(--sprout-text-base); overflow-wrap: anywhere; line-height: 1.625; } .sprout-message-response > * { margin-block: 0; } .sprout-message-response > * + * { margin-top: 1rem; } .sprout-message-response :is(h1, h2, h3, h4, h5, h6) { color: var(--sprout-text); letter-spacing: normal; margin-bottom: 0; font-weight: 600; line-height: 1.5; } .sprout-message-response h1 { font-size: 1.25em; } .sprout-message-response h2 { font-size: 1.125em; } .sprout-message-response :is(h3, h4, h5, h6) { font-size: 1em; } .sprout-message-response p { margin-block: 0; } .sprout-message-response :is(ul, ol) { margin-block: 0; padding-left: 1.5rem; } .sprout-message-response li > :is(ul, ol) { margin-top: .25rem; } .sprout-message-response ul { list-style: outside; } .sprout-message-response ol { list-style: decimal; } .sprout-message-response li + li { margin-top: .25rem; } .sprout-message-response li > p + p { margin-top: .5rem; } .sprout-message-response blockquote { border-left: 2px solid var(--sprout-border); color: var(--sprout-muted); margin-inline: 0; padding-left: 1rem; } .sprout-message-response hr { border: 0; border-top: 1px solid var(--sprout-line); } .sprout-message-response a { color: var(--sprout-primary); text-underline-offset: 3px; transition: text-decoration-thickness var(--sprout-motion-fast) var(--sprout-motion-ease); border-radius: 2px; text-decoration: underline 1px; } @media (hover: hover) and (pointer: fine) { .sprout-message-response a:hover { text-decoration-thickness: 2px; } } .sprout-message-response a:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-message-response :not(pre) > code { background: var(--sprout-hover); border-radius: 4px; padding: 2px 4px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8125rem; } .sprout-message-response > pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: var(--sprout-text-sm); overflow-x: auto; } .sprout-message-response del { color: var(--sprout-muted); } .sprout-message-response-table { border-radius: 2px; max-width: 100%; overflow-x: auto; } .sprout-message-response-table[data-bounded] { max-height: 12rem; overflow: auto; } .sprout-message-response-table:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-message-response table { border-collapse: collapse; width: 100%; font-size: .8125rem; line-height: 1.25rem; } .sprout-message-response :is(th, td) { border-bottom: 1px solid var(--sprout-line); text-align: left; padding: .5rem .75rem; } .sprout-message-response th { background: var(--sprout-hover); font-weight: 500; } .sprout-message-response-table[data-bounded] th { background: linear-gradient(var(--sprout-hover) 0 0) var(--sprout-canvas); position: sticky; top: 0; } .sprout-message-response .contains-task-list { padding-left: 0; list-style: none; } .sprout-message-response .contains-task-list .contains-task-list { padding-left: 1.5rem; } .sprout-message-response input[type="checkbox"] { vertical-align: -1px; accent-color: var(--sprout-primary); margin: 0 .5rem 0 0; } .sprout-message-response-caret { background: var(--sprout-muted); vertical-align: middle; border-radius: 2px; width: 6px; height: 1rem; margin-left: 4px; display: inline-block; } .sprout-message-response > * + .sprout-message-response-caret { margin-top: 0; } @media (prefers-reduced-motion: no-preference) { .sprout-message-response-caret { animation: 2s cubic-bezier(.4, 0, .6, 1) infinite sprout-message-response-pulse; } } @media (prefers-reduced-motion: reduce) { .sprout-message-response a { transition: none; } } @media (forced-colors: active) { .sprout-message-response-caret { background: canvastext; } } @keyframes sprout-message-response-pulse { 50% { opacity: .5; } } }Import it from
@/components/ui/message-response. 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-response.
Usage
import { MessageResponse } from "@tulipfarm/sprout/message-response";
<MessageResponse streaming={message.status === "streaming"}>
{message.text}
</MessageResponse>children is the Markdown string. There are no variants or sizes: the reply uses the body text size and fills its column. Long words and URLs wrap, so it never pushes a chat column wider.
Markdown
Headings, emphasis, strikethrough, nested and numbered lists, task lists, quotes, rules, aligned tables, autolinks, and fenced code. A fence with a language gets syntax colors; a fence without one shows astext. Raw HTML is dropped and images are not shown.
Streaming
Set streaming while tokens arrive. A pulsing caret follows the text and the reply is marked busy. An empty reply that is streaming shows only the caret, which is the waiting state.
Table viewport
import { MessageTableViewport } from "@tulipfarm/sprout/message-response";
<MessageTableViewport bounded={rowViews.length > 0}>
<MessageResponse>{text}</MessageResponse>
</MessageTableViewport>Every table scrolls in its own focusable region. Inside abounded viewport a table stops at 12rem and scrolls both ways with a sticky header, so row cards below it stay in view. Without one, tables grow to their full height and scroll sideways only when their columns cannot fit.
Custom links
<MessageResponse
renderLink={(href, label) => {
const citation = citationId(href);
if (citation) return <Citation id={citation}>{label}</Citation>;
if (isWorkspaceDownload(href)) return <DownloadLink path={href}>{label}</DownloadLink>;
return <a href={href} target="_blank" rel="noopener noreferrer">{label}</a>;
}}
>
{text}
</MessageResponse>Without renderLink, links open in a new tab withnoopener. With it, every link goes through your function, so return a plain link for the ones you do not handle. The examples use sample adapters: citations check access, then show the passage, a hidden notice, or a retry; downloads report progress, success, or failure.
Result
You
Agent
2 explicit source references. Access is checked on each read.
Markdown files
<FileRenderersProvider
renderers={{ markdown: ({ text }) => <MessageResponse>{text}</MessageResponse> }}
>
<FileViewer file={file} open={open} onOpenChange={setOpen} />
</FileRenderersProvider>Opening file
Preparing the document viewer.
In a panel
Put a reply in a bordered panel when it sits among other details, such as a chat handoff.
Instructions: Keep order numbers and the open question.
Accessibility
Headings, lists, quotes, and tables keep their native roles. Each table scroll region is named “Response table” and takes focus, so keyboard users can scroll it with the arrow keys. The reply setsaria-busy while it streams and the caret is hidden from screen readers. Links and table regions show a focus ring. The caret stops pulsing with reduced motion and uses the system text color in forced colors mode.