A stick-to-bottom conversation viewport for streaming chat messages.
A stick-to-bottom conversation viewport for streaming chat messages.
import { ChatConversation, useChatConversation } from 'heroui-native-pro';The message list needs Legend List (@legendapp/list 3.7 or newer), an optional peer dependency. ChatConversation.Content throws an error naming the install command when it is missing. For keyboard handling, also install react-native-keyboard-controller (1.21.7 or newer). Without it, the composer sits below the list and your screen handles the keyboard (for example with a KeyboardAvoidingView).
npx expo install @legendapp/list react-native-keyboard-controller<ChatConversation>
<ChatConversation.Content data={messages} renderItem={renderMessage} />
<ChatConversation.Empty>...</ChatConversation.Empty>
<ChatConversation.ScrollButton />
<ChatConversation.Composer>...</ChatConversation.Composer>
</ChatConversation>PromptInput). With react-native-keyboard-controller it floats over the bottom of the list and rides on the keyboard. The list insets its end by the composer's height, so the last message stays visible above it.Messages are data, rendered with the ChatMessage building blocks. Every message renders as one row.
<ChatConversation>
<ChatConversation.Content
data={messages}
keyExtractor={(message) => message.id}
itemEntrance={(message) => (message.role === 'user' ? 'send' : 'reply')}
estimatedItemSize={120}
renderItem={({ item }) =>
item.role === 'user' ? (
<ChatMessage.User>
<ChatMessage.Bubble>
<ChatMessage.Content>{item.text}</ChatMessage.Content>
</ChatMessage.Bubble>
</ChatMessage.User>
) : (
<ChatMessage.Assistant>
<ChatMessage.Avatar alt="Assistant" fallback="AI" />
<ChatMessage.Body>
<ChatMessage.Content>
<StreamMarkdown isStreaming={item.isStreaming}>{item.text}</StreamMarkdown>
</ChatMessage.Content>
<ChatMessageActions>
<ChatMessageActions.Copy />
<ChatMessageActions.Regenerate />
</ChatMessageActions>
</ChatMessage.Body>
</ChatMessage.Assistant>
)
}
/>
<ChatConversation.ScrollButton />
<ChatConversation.Composer className="pb-safe-offset-3">
<PromptInput value={value} status={status} onValueChange={setValue} onSubmit={send} onStop={stop}>
<PromptInput.Shell>
<PromptInput.Content>
<PromptInput.TextArea />
</PromptInput.Content>
<PromptInput.Toolbar>
<PromptInput.ToolbarEnd>
<PromptInput.Send />
</PromptInput.ToolbarEnd>
</PromptInput.Toolbar>
</PromptInput.Shell>
</PromptInput>
</ChatConversation.Composer>
</ChatConversation>Update the last message's text in data as the reply streams. By default a sent message is pinned under the top of the list and the reply grows below it.
Place ChatConversation.Composer last. When the conversation reaches the bottom of the screen, pad the composer with pb-safe-offset-* for the home indicator. keyboardOffset is the space under the composer that the keyboard covers when it opens. It defaults to the safe-area bottom inset. If the conversation ends above other UI, such as a tab bar, add that gap.
<ChatConversation keyboardOffset={tabBarHeight} style={{ marginBottom: tabBarHeight }}>
...
</ChatConversation>followMode sets how the list moves when a message is sent and while its reply streams.
"anchor" (default): the sent message flies up from behind the composer and pins under the top of the list (anchorOffset). The list reserves blank space below it, and the reply fades in and grows into that space. The text the user is reading never moves. A reply that outgrows the space continues past the fold and the scroll button appears. Pressing it (or calling scrollToEnd()) follows the end until the user drags the list again. A long sent message keeps only the last lines of its text (anchorTailHeight) on screen, so its reply starts in view."tail": a send pins the message the same way. Once the reply outgrows the space, the list follows its end, so the newest lines stay just above the composer. Dragging the list stops following. Reaching the end again, the scroll button, or scrollToEnd() resumes it.While following the end, the list follows every change only while a reply generates. Otherwise it follows only new messages, so expanding a panel in a finished thread keeps the tapped header in place. A PromptInput in ChatConversation.Composer reports this from its status. Without one, pass isStreaming.
<ChatConversation followMode="tail" isStreaming={status === 'submitted' || status === 'streaming'}>
...
</ChatConversation>Mark sent messages with itemEntrance, since a send is what moves the list. Sending closes the keyboard as the message flies (isKeyboardDismissedOnSend). To send from anything else in the composer, such as a suggestion, call submit(text) from usePromptInput() (see PromptInput) so it takes the same path. A regenerated reply pins its message again with a scroll instead of the flight. Update the reply and its message ids in one state update.
resize sets how the list follows the end. "smooth" (default) eases toward the end on the UI thread, so each new line slides up from under the composer. "instant" jumps to the end on every change.
ChatConversation.Content sets chat defaults for some Legend List props, and you can override each: initialScrollAtEnd, maintainScrollAtEnd, maintainVisibleContentPosition, recycleItems, and keyboardDismissMode. Other Legend List props pass through, such as onStartReached to load older history and ListFooterComponent for a typing indicator.
Prepend older messages to data from onStartReached. The messages on screen stay in place while the new rows are measured, and older messages never play an entrance. A pinned message, its reply, and a send in flight are tracked by key, so they survive the shift.
<ChatConversation.Content
data={messages}
onStartReached={loadOlderMessages}
onStartReachedThreshold={0.5}
ListHeaderComponent={hasOlderMessages ? <OlderMessagesSpinnerRow /> : null}
...
/>Keep such a header the same height while it loads, so the spinner appearing does not shift the messages.
Reasoning, ChatTool, ChatToolGroup, and ChatSources detect that they render in a conversation row. There they change height in a single commit and fade their content in, instead of running a layout transition. Outside a conversation they keep their layout transitions.
Rows unmount while scrolled far out of view. Give each of these components a stateKey, unique within its message, and the conversation keeps its expanded state with the message. ChatTool uses its toolCallId when you don't pass one. Controlled isExpanded still works when you want the state in your own store.
<Reasoning stateKey={`reasoning-${index}`} status={isStreaming ? 'streaming' : 'complete'}>...</Reasoning>
<ChatTool stateKey={part.toolCallId} state={state}>...</ChatTool>
<ChatSources stateKey="sources">...</ChatSources>ChatConversation.Empty renders its children while Content has no messages. It is centered between the list's top padding and the composer. It fades out while the keyboard is open (isHiddenWithKeyboard) and as the first message arrives. Touches outside its content reach the list. The composer drops its fade while the list is empty.
<ChatConversation>
<ChatConversation.Content data={messages} renderItem={renderMessage} />
<ChatConversation.Empty>
<Text className="text-2xl font-semibold text-foreground">How can I help you today?</Text>
</ChatConversation.Empty>
<ChatConversation.Composer>...</ChatConversation.Composer>
</ChatConversation>itemEntrance says what each message is, for every message or per message: "send" (a message the user sent), "reply" (default), or "none". A sent message flies up from behind the composer, and its reply fades in after it lands. The initial messages, rows remounted while scrolling, and the messages of another conversation opened in the same list (change entranceKey) do not animate.
<ChatConversation.Content
data={messages}
keyExtractor={(message) => message.id}
itemEntrance={(message) => (message.role === 'user' ? 'send' : 'reply')}
entranceKey={conversationId}
renderItem={renderMessage}
/>With reduced motion or animation="disable-all" on the root, sends still pin their message, without the flight or the fade.
Messages scrolling under the composer fade into its background along its top edge. The fade slot is 32px tall and uses the composer's background color. Turn it off with fade={false}, or resize it with the fade slot. A composer with a transparent background gets no fade.
<ChatConversation.Composer fade={false}>...</ChatConversation.Composer>
<ChatConversation.Composer classNames={{ fade: 'h-12' }}>...</ChatConversation.Composer>useChatConversation() returns isAtEnd, scrollToEnd(), and listRef inside the conversation. scrollToEnd() also resumes following the end. Sends need no scrolling of your own.
const { scrollToEnd } = useChatConversation();
<Button onPress={() => scrollToEnd()}>Jump to latest</Button>The content slot holds the padding around the messages and the gap between them. Its row-gap is applied as the list's row gap.
<ChatConversation.Content classNames={{ content: 'px-5 gap-8' }} ... />import type { ChatStatus } from 'heroui-native-pro';
import {
ChatConversation,
ChatMessage,
ChatMessageActions,
PromptInput,
StreamMarkdown,
} from 'heroui-native-pro';
import { useState } from 'react';
type Message = {
id: string;
role: 'user' | 'assistant';
text: string;
isStreaming?: boolean;
};
const MessageRow = ({ message }: { message: Message }) => {
if (message.role === 'user') {
return (
<ChatMessage.User>
<ChatMessage.Bubble>
<ChatMessage.Content>{message.text}</ChatMessage.Content>
</ChatMessage.Bubble>
</ChatMessage.User>
);
}
return (
<ChatMessage.Assistant>
<ChatMessage.Avatar alt="Assistant" fallback="AI" />
<ChatMessage.Body>
<ChatMessage.Content>
<StreamMarkdown isStreaming={Boolean(message.isStreaming)}>
{message.text}
</StreamMarkdown>
</ChatMessage.Content>
{message.isStreaming ? null : (
<ChatMessageActions>
<ChatMessageActions.Copy accessibilityLabel="Copy" />
<ChatMessageActions.ThumbsUp accessibilityLabel="Good response" />
<ChatMessageActions.ThumbsDown accessibilityLabel="Bad response" />
</ChatMessageActions>
)}
</ChatMessage.Body>
</ChatMessage.Assistant>
);
};
type ChatScreenProps = {
messages: Message[];
status: ChatStatus;
onSend: (text: string) => void;
onStop: () => void;
};
export default function ChatScreen({
messages,
status,
onSend,
onStop,
}: ChatScreenProps) {
const [value, setValue] = useState('');
return (
<ChatConversation>
<ChatConversation.Content
data={messages}
keyExtractor={(message) => message.id}
itemEntrance={(message) => (message.role === 'user' ? 'send' : 'reply')}
renderItem={({ item }) => <MessageRow message={item} />}
estimatedItemSize={120}
/>
<ChatConversation.ScrollButton accessibilityLabel="Scroll to bottom" />
<ChatConversation.Composer className="pb-safe-offset-3">
<PromptInput
value={value}
status={status}
onValueChange={setValue}
onSubmit={(text) => {
onSend(text.trim());
setValue('');
}}
onStop={onStop}
>
<PromptInput.Shell>
<PromptInput.Content>
<PromptInput.TextArea placeholder="Ask anything" />
</PromptInput.Content>
<PromptInput.Toolbar>
<PromptInput.ToolbarEnd>
<PromptInput.Send />
</PromptInput.ToolbarEnd>
</PromptInput.Toolbar>
</PromptInput.Shell>
</PromptInput>
</ChatConversation.Composer>
</ChatConversation>
);
}| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Content, Empty, ScrollButton, and Composer |
keyboardOffset | number | Safe-area bottom inset | Space under the composer covered by the open keyboard. Only used with react-native-keyboard-controller |
followMode | 'anchor' | 'tail' | 'anchor' | Once a reply outgrows the screen, let it run past the fold (anchor) or follow its end (tail) |
resize | 'smooth' | 'instant' | 'smooth' | Glide or jump to the end as content grows while following it |
isStreaming | boolean | Composer PromptInput status | Whether a reply is generating. Sets which changes the list follows at the end |
className | string | - | Additional CSS classes for the root |
animation | ChatConversationRootAnimation | - | Animation configuration for the root |
...ViewProps | ViewProps | - | All standard React Native View props are supported |
The root animates nothing itself. Can be:
"disable-all": Disable all animations, including the scroll button and the messagesundefined: Use default animations| prop | type | default | description |
|---|---|---|---|
data | ItemT[] | - | Messages |
renderItem | (props) => ReactNode | - | Renders a message |
keyExtractor | (item: ItemT, index: number) => string | - | Stable message key |
anchorOffset | number | content slot top padding | Distance from the top of the list to a pinned (sent) message |
anchorTailHeight | number | 82 | Height of a long sent message's text kept on screen (three lines). Attachments stay in view |
className | string | - | Additional CSS classes for the list (container slot) |
classNames | ElementSlots<ContentSlots> | - | Additional CSS classes for the slots |
styles | Partial<Record<ContentSlots, ViewStyle>> | - | Styles for the slots |
initialScrollAtEnd | boolean | true | Opens at the latest message |
maintainScrollAtEnd | boolean | MaintainScrollAtEndOptions | false | Legend List's own follow. Setting it replaces the conversation's follow (resize, isStreaming) |
maintainVisibleContentPosition | boolean | MaintainVisibleContentPositionConfig | { size: false, data: true } | The offset holds when a row resizes, so panels grow downward from their header |
recycleItems | boolean | false | Whether rows are recycled. Off so rows keep per-message state |
keyboardDismissMode | 'none' | 'on-drag' | 'interactive' | 'interactive' | How dragging the list dismisses the keyboard |
isKeyboardDismissedOnSend | boolean | true | Closes the keyboard as a sent message flies up |
itemEntrance | ChatConversationItemEntrance | ((item: ItemT, index: number) => ChatConversationItemEntrance) | 'reply' | What a message is: a sent message pins and flies in, a reply fades in |
entranceKey | string | number | - | Identifies the conversation. When it changes, its messages do not animate in |
...LegendListProps | LegendListProps<ItemT> | - | Legend List props, except style, contentContainerStyle, contentInsetEndAdjustment, and refScrollView |
The ref is the Legend List ref (LegendListRef).
ElementSlots<ContentSlots>| prop | type | description |
|---|---|---|
container | string | Custom class name for the list |
content | string | Custom class name for the scroll content: padding and the gap between messages |
styles| prop | type | description |
|---|---|---|
container | ViewStyle | Styles for the list |
content | ViewStyle | Styles for the scroll content |
| value | description |
|---|---|
'send' | A message the user sent. It flies in from the composer and pins near the top |
'reply' | Fades in where it sits, once the message above it has landed |
'none' | Appears at once |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Content shown while there are no messages |
isHiddenWithKeyboard | boolean | true | Fades the content out while the keyboard is open. Needs react-native-keyboard-controller |
className | string | - | Additional CSS classes for the container slot |
classNames | ElementSlots<EmptySlots> | - | Additional CSS classes for the slots |
animation | ChatConversationEmptyAnimation | - | Entering, exiting, and layout animations |
...ViewProps | ViewProps | - | All standard React Native View props are supported |
ElementSlots<EmptySlots>| prop | type | description |
|---|---|---|
container | string | Custom class name for the layer between the list top and the composer |
content | string | Custom class name for the centered content |
Animation configuration for the empty state. Can be:
false or "disabled": Appears and leaves at oncetrue or undefined: Use default animationsobject: Custom animation configuration| prop | type | default | description |
|---|---|---|---|
state | 'disabled' | boolean | - | Disable animations while customizing properties |
entering.value | EntryOrExitLayoutType | FadeIn.duration(250) | Shown when a conversation opens empty or is cleared |
exiting.value | EntryOrExitLayoutType | FadeOut.duration(280) | Leaves as the first message arrives |
layout.value | LayoutTransition | LinearTransition.duration(250).easing(Easing.out(Easing.cubic)) | Re-centers the content when the composer resizes |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | Down chevron | Custom icon |
className | string | - | Additional CSS classes for the button |
classNames | ElementSlots<ScrollButtonSlots> | - | Additional CSS classes for the slots |
iconProps | ChatConversationScrollButtonIconProps | - | Props of the default chevron |
accessibilityLabel | string | 'Scroll to bottom' | Accessible label of the button. Set it to localize |
animation | ChatConversationScrollButtonAnimation | - | Show and hide animation |
isAnimatedStyleActive | boolean | true | Whether the animated opacity and scale apply to the body slot |
...ButtonRootProps | ButtonRootProps | - | heroui-native Button props, except animation and isAnimatedStyleActive |
ElementSlots<ScrollButtonSlots>| prop | type | description |
|---|---|---|
container | string | Custom class name for the full-width row floating above the composer |
body | string | Custom class name for the animated wrapper. opacity and transform are animated |
button | string | Custom class name for the heroui-native Button |
| prop | type | default | description |
|---|---|---|---|
size | number | 16 | Size of the chevron, in pixels |
color | string | foreground | Color of the chevron |
Animation configuration for showing and hiding the button. Can be:
false or "disabled": Shows and hides instantlytrue or undefined: Use default animationsobject: Custom animation configuration| prop | type | default | description |
|---|---|---|---|
state | 'disabled' | boolean | - | Disable animations while customizing properties |
opacity.value | [number, number] | [0, 1] | Opacity [hidden, visible] |
opacity.timingConfig | WithTimingConfig | { duration: 150, easing: Easing.out(Easing.ease) } | Timing of the opacity change |
scale.value | [number, number] | [0.85, 1] | Scale [hidden, visible] |
scale.timingConfig | WithTimingConfig | { duration: 150, easing: Easing.out(Easing.ease) } | Timing of the scale change |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | The prompt input |
className | string | - | Additional CSS classes for the composer. Add pb-safe-offset-* at the screen bottom |
classNames | ElementSlots<ComposerSlots> | - | Additional CSS classes for the slots |
fade | boolean | true | Fades messages into the composer background along its top edge. Not drawn while empty |
...ViewProps | ViewProps | - | All standard React Native View props are supported |
ElementSlots<ComposerSlots>| prop | type | description |
|---|---|---|
container | string | Custom class name for the floating wrapper (with react-native-keyboard-controller) |
composer | string | Custom class name for the measured view hosting the prompt input |
fade | string | Custom class name for the gradient band above the composer |
Returns the conversation state and actions inside a ChatConversation. It throws outside one.
| property | type | description |
|---|---|---|
isAtEnd | boolean | Whether the list is scrolled to the end (or has nothing to scroll) |
isAtBottom | boolean | Same as isAtEnd |
isEmpty | boolean | undefined | Whether the list has no messages. undefined before Content renders |
isStreaming | boolean | undefined | Whether a reply is being generated |
isScrollButtonVisible | boolean | Whether the scroll button is shown |
isKeyboardAware | boolean | Whether the keyboard-aware list and composer are used |
resize | ChatConversationScrollBehavior | Follow behavior while at the end |
followMode | ChatConversationFollowMode | How the list scrolls as the conversation grows |
keyboardOffset | number | Resolved keyboard offset |
composerHeight | number | Measured height of the composer (0 without one) |
contentPaddingTop | number | Top padding of the list content |
scrollToEnd | (options?: { animated?: boolean }) => void | Scrolls to the latest message and resumes following. Animated by default |
scrollToBottom | (behavior?: 'smooth' | 'instant') => void | Same as scrollToEnd. 'smooth' (default) animates, 'instant' jumps |
listRef | RefObject<LegendListRef | null> | Ref of the Legend List |
willSendRef | RefObject<(() => void) | null> | Prepares the list for a send. Call it from a custom composer before sending |