Examples
Chat thread
A conversation with a friend: bubbles that group by sender and show the time once per run, the other side typing, quick replies, and a composer with an emoji button beside it. The emojis open in a sheet with search, recently used and skin tones. Send a message and Maya answers.
Loading…
import { useRef, useState } from 'react'; import type { ComponentRef } from 'react'; import { ScrollView, StyleSheet, View } from 'react-native'; import { Avatar, Button, ChatComposer, EmojiPickerSheet, Header, MessageBubble, SuggestionChips, TypingIndicator, useTheme, } from 'patine-ui'; import type { SkinTone } from 'patine-ui'; import { Icon } from '@patine-ui/icons'; type Message = { id: number; from: 'me' | 'them'; text: string; time: string }; const FIRST: Message[] = [ { id: 1, from: 'them', text: 'Landed! The light here is unreal ☀️', time: '18:02' }, { id: 2, from: 'them', text: 'Are we still on for dinner?', time: '18:02' }, { id: 3, from: 'me', text: 'Yes! I booked the place by the river for 8', time: '18:04' }, { id: 4, from: 'them', text: 'Perfect. Bring the guidebook, I want to plan Sintra 🏰', time: '18:05' }, ]; const REPLIES = ['On my way', 'Running 10 min late', 'See you there 👋']; const ANSWERS = ['Ha, love it 😄', 'Sounds good to me', 'Can’t wait!']; const now = () => new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', hour12: false }); export default function Chat() { const { colors } = useTheme(); const [messages, setMessages] = useState(FIRST); const [text, setText] = useState(''); const [typing, setTyping] = useState(false); const [showEmoji, setShowEmoji] = useState(false); const [tone, setTone] = useState<SkinTone>(0); const [recent, setRecent] = useState(['😂', '❤️', '👍']); const list = useRef<ComponentRef<typeof ScrollView>>(null); const send = (body: string) => { if (!body.trim()) return; setMessages((all) => [...all, { id: Date.now(), from: 'me', text: body.trim(), time: now() }]); setText(''); setTyping(true); setTimeout(() => { setTyping(false); setMessages((all) => [ ...all, { id: Date.now(), from: 'them', text: ANSWERS[all.length % ANSWERS.length] ?? '👍', time: now() }, ]); }, 1600); }; return ( <View style={[styles.screen, { backgroundColor: colors.background }]}> <Header title="Maya Levi" subtitle={typing ? 'typing…' : 'online'} onBack={() => {}} actions={ <Button variant="ghost" size="icon" accessibilityLabel="Call Maya"> <Icon name="phone" color="primary" /> </Button> } /> <ScrollView ref={list} style={styles.flex} contentContainerStyle={styles.messages} onContentSizeChange={() => list.current?.scrollToEnd()} > {messages.map((m, i) => { const next = messages[i + 1]; const lastInRun = !next || next.from !== m.from; const firstInRun = messages[i - 1]?.from !== m.from; return ( <View key={m.id} style={[styles.row, m.from === 'me' && styles.mine, !firstInRun && styles.tight]}> {m.from === 'them' ? ( lastInRun ? ( <Avatar name="Maya Levi" src="https://picsum.photos/id/64/120/120" size="sm" /> ) : ( <View style={styles.avatarSpace} /> ) ) : null} <MessageBubble from={m.from} meta={lastInRun ? m.time : undefined} style={styles.bubble}> {m.text} </MessageBubble> </View> ); })} {typing ? <TypingIndicator name="Maya" style={styles.typing} /> : null} </ScrollView> <View style={styles.replies}> <SuggestionChips suggestions={REPLIES} onPress={send} disabled={typing} accessibilityLabel="Quick replies" /> </View> <ChatComposer value={text} onValueChange={setText} onSend={send} placeholder="Message" leading={ <Button variant="ghost" size="icon" accessibilityLabel="Emoji" onPress={() => setShowEmoji(true)}> <Icon name="smile" /> </Button> } /> <EmojiPickerSheet open={showEmoji} onOpenChange={setShowEmoji} onSelect={(emoji) => setText((current) => current + emoji)} skinTone={tone} onSkinToneChange={setTone} recent={recent} onRecentChange={setRecent} /> </View> ); } const styles = StyleSheet.create({ screen: { flex: 1 }, flex: { flex: 1 }, messages: { padding: 12, gap: 10 }, row: { flexDirection: 'row', alignItems: 'flex-end', gap: 8 }, mine: { justifyContent: 'flex-end' }, tight: { marginTop: -6 }, avatarSpace: { width: 32 }, bubble: { maxWidth: '78%' }, typing: { marginStart: 40 }, replies: { flexGrow: 0, paddingVertical: 6 }, });
Components used
- MessageBubble:
from="me"or"them", with the time asmetaon the last bubble of each run - ChatComposer: the growing text box and the send button, with the emoji button in
leading - EmojiPickerSheet: search, recently used and skin tones in a bottom sheet
- TypingIndicator, SuggestionChips: the other side typing, and quick replies that send in one tap
- Avatar, Header, Button, Patine Icons
In production
- Persist
recentandtone(AsyncStorage, MMKV) so they survive restarts; see Recently used. - Picking an emoji adds it and closes the sheet. For several at once, pass
closeOnSelect={false}; the sheet then closes on a drag down or a tap on the backdrop. - At the cursor. With your own
TextInput, keeponSelectionChange's selection in a ref and splice the emoji in atselection.startinstead of appending it. - Dismiss the keyboard with
Keyboard.dismiss()when the sheet opens, so the two don't stack. To show the emojis in the keyboard's place instead, render an inlineEmojiPickerunder the composer at the last keyboard height. - On older Android, set
maxEmojiVersionto skip emojis the device can't draw. - Long threads belong in an inverted
FlatListrather than aScrollView, so the newest message renders first.