Skip to content

Hold to talk

Hold to speak, release to send, slide left to cancel.

Live demo, needs a microphone.Open full screen ↗
  1. Spread holdProps on any View: the hook brings the gesture and the session, you bring the looks.
  2. onSend gets the whole message. Pauses do not cut it, and it keeps listening for a moment after the release, so the last word is not lost.
  3. isCancelling is true once the finger is far enough: turn the button red. Sliding back undoes it.
  4. onCancel says why there is nothing to send, so a short tap can explain itself.
  5. The space and enter keys hold the button on web. Screen readers tap to start and tap again to send.

Uses

This file is what runs in the phone: the page reads it from the repository.

example/scenes/chat.tsx
import { Ionicons } from '@expo/vector-icons';
import { useHoldToTalk, type HoldCancelReason } from 'expo-stt';
import { useState } from 'react';
import { FlatList, StyleSheet, Text, View, type ViewStyle } from 'react-native';
import { ErrorNote, Screen, useDemo, Waveform } from './ui';
const BLUE = '#2f5bea';
// Web only: otherwise phones scroll the page instead of sliding to cancel.
const NO_SCROLL = { touchAction: 'none' } as ViewStyle;
type Message = { id: number; text: string; isMine: boolean };
const HELLO: Message[] = [
{ id: 2, text: 'Hold the button and talk to me.', isMine: false },
{ id: 1, text: 'Too busy to type?', isMine: false },
];
const HINTS: Record<HoldCancelReason, string> = {
cancelled: 'Message discarded.',
'too-short': 'Hold the button while you talk.',
'not-ready': 'Ready. Hold the button to talk.',
empty: 'I did not hear anything.',
};
/** Hold to speak, release to send, slide left to cancel. */
export function Chat() {
const { locale } = useDemo();
const [messages, setMessages] = useState(HELLO);
const [hint, setHint] = useState('');
const talk = useHoldToTalk({
locale,
onSend: (text) => {
setHint('');
setMessages((previous) => [{ id: previous.length + 1, text, isMine: true }, ...previous]);
},
onCancel: (reason) => setHint(HINTS[reason]),
onError: () => setHint(''),
});
const label = talk.isCancelling
? 'Release to cancel'
: talk.isHolding
? 'Release to send'
: talk.isActive
? 'Sending…'
: 'Hold to talk';
return (
<Screen title="Hold to talk" color="#14213d" background="#f2f5ff">
<FlatList
inverted
contentContainerStyle={styles.list}
data={messages}
keyExtractor={({ id }) => String(id)}
renderItem={({ item }) => (
<View style={[styles.bubble, item.isMine ? styles.mine : styles.theirs]}>
<Text style={item.isMine ? styles.mineText : styles.theirsText}>{item.text}</Text>
</View>
)}
/>
<ErrorNote error={talk.error} />
{talk.isActive ? (
<View style={styles.preview}>
<Waveform color={talk.isCancelling ? '#d6503f' : BLUE} height={24} />
<Text numberOfLines={3} style={[styles.previewText, talk.isCancelling && styles.cancelling]}>
{talk.transcript || '‹ Slide left to cancel'}
</Text>
</View>
) : (
<Text accessibilityLiveRegion="polite" style={styles.hint}>
{hint}
</Text>
)}
{/* The hook brings the gesture, this view the looks. */}
<View
{...talk.holdProps}
accessibilityLabel={talk.isHolding ? 'Send message' : 'Record message'}
style={[
styles.button,
NO_SCROLL,
talk.isHolding && styles.pressed,
talk.isCancelling && styles.buttonCancel,
]}
>
<Ionicons name={talk.isCancelling ? 'trash' : 'mic'} size={24} color="#fff" />
<Text selectable={false} style={styles.buttonText}>
{label}
</Text>
</View>
</Screen>
);
}
const styles = StyleSheet.create({
list: { padding: 16, gap: 8 },
bubble: { maxWidth: '80%', paddingHorizontal: 16, paddingVertical: 11, borderRadius: 20 },
mine: { alignSelf: 'flex-end', backgroundColor: BLUE, borderBottomRightRadius: 6 },
theirs: { alignSelf: 'flex-start', backgroundColor: '#fff', borderBottomLeftRadius: 6 },
mineText: { color: '#fff', fontSize: 16, lineHeight: 22 },
theirsText: { color: '#14213d', fontSize: 16, lineHeight: 22 },
preview: {
flexDirection: 'row',
alignItems: 'center',
gap: 14,
marginHorizontal: 16,
padding: 16,
borderRadius: 20,
backgroundColor: '#fff',
},
previewText: { flex: 1, color: '#14213d', fontSize: 16 },
hint: { minHeight: 22, paddingHorizontal: 16, textAlign: 'center', color: '#4a5578', fontSize: 15 },
cancelling: { color: '#b53a2b', fontWeight: '700' },
button: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
gap: 10,
margin: 16,
height: 64,
borderRadius: 32,
backgroundColor: BLUE,
userSelect: 'none',
},
pressed: { backgroundColor: '#1c3fb8' },
buttonCancel: { backgroundColor: '#c8412b' },
buttonText: { color: '#fff', fontSize: 17, fontWeight: '700', userSelect: 'none' },
});