Render AI responses as native rich text, with an optional streaming variant.
Render AI responses as native rich text, with an optional streaming variant.
import { Markdown, StreamMarkdown } from 'heroui-native-pro';Rendering runs through the optional react-native-enriched-markdown peer. Without it, the Markdown source renders as plain text, with a warning in development.
npx expo install react-native-enriched-markdownenriched-markdown block of your app's package.json (see CodeBlock).react-native-enriched-markdown to onlyBuiltDependencies. Otherwise its install script never downloads the highlighting grammars, and code renders uncolored (see CodeBlock).StreamMarkdown can repair half-written syntax (an unclosed **bold, a half-typed [link]() while a response streams. Repair is optional:
react-native-streamdown, repair runs on a worklet runtime, off the JS thread. It needs react-native-worklets in Bundle Mode (Babel plugin and Metro config).babel-preset-expo, pass { worklets: false, reanimated: false } to the preset when you register the worklets plugin yourself.withUniwindConfig, and resolve the Bundle Mode shim's own react-native import to React Native directly. Otherwise the app fails at startup with Cannot read property 'select' of undefined. The example app's metro.config.js shows the setup.npx expo start -c) after changing the config.react-native-streamdown, repair falls back to remend on the JS thread when it is installed (npm install remend). It needs no Babel or Metro setup.Pass the Markdown string as children. GitHub Flavored Markdown is on by default: headings, lists, links, inline code, fenced code blocks, blockquotes and alerts, tables, task lists, images, and math. Single newlines render as line breaks.
<ChatMessage.Assistant>
<ChatMessage.Avatar alt="Assistant" fallback="AI" />
<ChatMessage.Body>
<ChatMessage.Content>
<Markdown>{response}</Markdown>
</ChatMessage.Content>
</ChatMessage.Body>
</ChatMessage.Assistant>StreamMarkdown renders responses that stream in token by token. While isStreaming, new text is revealed at a steady pace, tables and code blocks stream progressively, and an optional caret trails the text.
<StreamMarkdown isStreaming={status === 'streaming'}>{text}</StreamMarkdown>reveal: "char" (default) types bursty text out at the pace it arrives, catching up within about 3 seconds. "word" reveals whole words. "raw" shows each chunk as it arrives, with a fade-in. Once the stream ends, the rest of the text keeps its pace, and the caret stays until it is all shown.caret: false (default), "block" (▋), or "circle" (●).streamingConfig: { tableMode, codeBlockMode }, each "progressive" (default) or "hidden" (the block appears once complete). Code highlighting applies once the fence closes.animation={false}: turns off the fade-in of new text with reveal="raw".remendConfig: options for the repair, with react-native-streamdown or remend.The fade-in only runs while isStreaming, so finished messages never fade in when they mount.
<StreamMarkdown
isStreaming={isStreaming}
reveal="word"
caret="block"
streamingConfig={{ tableMode: 'hidden', codeBlockMode: 'progressive' }}
>
{text}
</StreamMarkdown>Fenced code blocks render natively as the CodeBlock card: the surface-secondary surface, the GitHub Light / Dark syntax palette picked from the surface brightness, a header with the language and a copy button, and horizontal scrolling for wide code. Code always reads left-to-right. Restyle syntax colors with markdownStyle.
<Markdown
markdownStyle={{ codeBlock: { syntaxColors: { keyword: '#d73a49' } } }}
>
{text}
</Markdown>Links open with React Native Linking.openURL. Pass onLinkPress to route them elsewhere, for example to an in-app browser.
import * as WebBrowser from 'expo-web-browser';
<Markdown onLinkPress={({ url }) => WebBrowser.openBrowserAsync(url)}>
{text}
</Markdown>;One native view renders the whole document. The root view takes className, and each Markdown element has a slot in classNames. Slots are not views: their classes are resolved and fed to the native renderer, so each slot only honors the properties listed in the API reference. Theme tokens, color presets, and dark mode flow through them.
<Markdown
classNames={{
h3: 'text-accent',
link: 'text-success no-underline',
list: 'gap-2',
listMarker: 'text-accent',
blockquote: 'bg-accent-soft border-accent',
}}
>
{text}
</Markdown>The first block's top margin is cancelled, so a leading heading or blockquote sits flush with the top edge, level with a message avatar.
markdownStyle passes values the slots cannot express (syntaxColors, linkVariants, admonitions, blockquote.padding). It is deep-merged one level over the theme style, so { h1: { color } } keeps the theme h1 size.
<Markdown
classNames={{ blockquote: 'bg-accent-soft border-accent' }}
markdownStyle={{ blockquote: { padding: 8, borderRadius: 8 } }}
>
{text}
</Markdown>Every EnrichedMarkdownText prop is accepted, for example onImagePress, onTaskListItemPress, enableTaskListItemToggle, contextMenuItems, selectable, selectionColor, writingDirection, accessibilityLabels, and md4cFlags. Text is selectable by default. accessibilityLabels localizes the renderer's screen reader labels. flavor="commonmark" renders everything in one text view, so selection spans the whole message, but without tables, task lists, or the code block header.
<Markdown flavor="commonmark" md4cFlags={{ highlight: true }} selectable={false}>
{text}
</Markdown>import { Button } from 'heroui-native';
import { ChatMessage, StreamMarkdown } from 'heroui-native-pro';
import { useCallback, useEffect, useRef, useState } from 'react';
import { ScrollView, View } from 'react-native';
const FULL_TEXT = [
'# Streaming demo',
'Half-written syntax such as an unclosed **bold phrase** or a [link](https://heroui.com) is repaired while it streams.',
'1. First item\n2. Second item\n3. Third item',
[
'| Mode | Behavior |',
'| --- | --- |',
'| Progressive | Rows appear as they stream |',
'| Hidden | The table appears once complete |',
].join('\n'),
'```js\nconsole.log("streaming markdown");\n```',
].join('\n\n');
const STREAM_CHUNK_SIZE = 6;
const STREAM_INTERVAL = 50;
export default function MarkdownExample() {
const [length, setLength] = useState(0);
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const isStreaming = length < FULL_TEXT.length;
const startStream = useCallback(() => {
if (intervalRef.current) clearInterval(intervalRef.current);
setLength(0);
intervalRef.current = setInterval(() => {
setLength((current) => current + STREAM_CHUNK_SIZE);
}, STREAM_INTERVAL);
}, []);
useEffect(() => {
startStream();
return () => {
if (intervalRef.current) clearInterval(intervalRef.current);
};
}, [startStream]);
useEffect(() => {
if (!isStreaming && intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
}
}, [isStreaming]);
return (
<View className="flex-1 px-5 items-center justify-center gap-6">
<ScrollView className="w-full grow-0">
<ChatMessage.Assistant>
<ChatMessage.Avatar alt="Assistant" fallback="AI" />
<ChatMessage.Body>
<ChatMessage.Content>
<StreamMarkdown isStreaming={isStreaming}>
{FULL_TEXT.slice(0, length)}
</StreamMarkdown>
</ChatMessage.Content>
</ChatMessage.Body>
</ChatMessage.Assistant>
</ScrollView>
<Button
variant="secondary"
size="sm"
isDisabled={isStreaming}
onPress={startStream}
>
<Button.Label>Replay</Button.Label>
</Button>
</View>
);
}| prop | type | default | description |
|---|---|---|---|
children | string | - | Markdown source |
flavor | 'github' | 'commonmark' | 'github' | Markdown flavor. 'commonmark' renders everything in one text view |
className | string | - | Additional CSS classes for the root view |
classNames | ElementSlots<MarkdownSlots> | - | Additional CSS classes for the root view and each Markdown element |
style | StyleProp<ViewStyle> | - | Inline style of the root view |
markdownStyle | MarkdownStyle | - | Renderer style override, deep-merged one level over the theme style |
md4cFlags | Md4cFlags | { hardSoftBreaks: true } | Parser flags, merged over the default |
onLinkPress | (event: LinkPressEvent) => void | Linking.openURL | Called when a link is pressed |
animation | MarkdownRootAnimation | - | "disable-all" cascades the disabled state to animated descendants |
...EnrichedMarkdownTextProps | EnrichedMarkdownTextProps | - | Renderer props, except markdown, containerStyle, streamingAnimation, and streamingConfig |
ElementSlots<MarkdownSlots>Every slot except container feeds the native renderer and honors only the listed properties.
| slot | element | properties |
|---|---|---|
container | Root view | Any view style (merged with className) |
paragraph | Body text | Block typography. Its color also applies to strikethrough, underline, inline math, and checked task items |
h1–h6 | Headings | Block typography |
strong | Bold text | font-family, color |
link | Links | font-family, color, text-decoration-line |
inlineCode | Inline code | font-family, font-size, color, background-color, border-color |
codeBlock | Fenced code blocks | Block typography, background-color, border-color, border-width, border-radius, padding |
blockquote | Blockquotes and alerts | Block typography, border-color, border-inline-start-width, padding-inline-start, background-color, border-radius, padding-top |
list | Lists | Block typography, row-gap (item spacing), margin-inline-start (nesting indent) |
listMarker | Bullets | color |
table | Tables | Block typography, border-color, border-width, border-radius, padding-inline, padding-block, background-color |
tableHeader | Header row | background-color, color, font-family |
thematicBreak | Horizontal rules | background-color, height, margin-top, margin-bottom |
taskList | Task checkboxes | background-color (checked), border-color, color (checkmark), border-radius, width |
highlight | ==highlighted== text | color, background-color (needs md4cFlags.highlight) |
math | Block math | font-size, color, background-color, padding, margin-top, margin-bottom |
image | Images | border-radius, margin-top, margin-bottom |
Block typography is font-family, font-size, line-height, color, margin-top, and margin-bottom.
| type | description |
|---|---|
undefined | Default animations |
'disable-all' | Disables all animations, cascaded to animated descendants |
Accepts every Markdown prop, with its own animation type.
| prop | type | default | description |
|---|---|---|---|
children | string | - | Markdown source streamed so far |
isStreaming | boolean | false | Reveals new text, streams tables and code progressively, and shows the caret |
reveal | StreamMarkdownReveal | 'char' | How streamed text is released onto the screen |
caret | 'block' | 'circle' | false | false | Caret appended to the text while streaming |
streamingConfig | { tableMode?, codeBlockMode? } | 'progressive' | 'progressive' or 'hidden' for tables and code blocks that are still streaming |
remendConfig | RemendOptions | - | Repair options, used with react-native-streamdown or remend |
animation | StreamMarkdownRootAnimation | - | Fade-in of newly streamed text |
...MarkdownProps | MarkdownProps | - | All Markdown props except animation |
| value | description |
|---|---|
'char' | A few characters at a time, so words build up evenly |
'word' | Whole words. Each release re-parses the Markdown |
'raw' | Each update renders as it arrives, with the native fade-in |
Text that is complete when the component mounts is shown at once.
| type | description |
|---|---|
true | undefined | New text fades in while streaming (reveal="raw" only) |
false | 'disabled' | No fade-in |
'disable-all' | No fade-in, cascaded to animated descendants |