$ git show typewriter
Typewriter text
Text types itself out one character at a time while a caret blinks at the end.
// Reanimated 4: the caret is a CSS keyframe animation, the text still reveals on a timer
const blink = { '0%': { opacity: 1 }, '50%': { opacity: 0 }, '100%': { opacity: 1 } };
const [shown, setShown] = useState(0);
useEffect(() => {
setShown(0);
const id = setInterval(() => {
setShown((n) => {
if (n >= text.length) { clearInterval(id); return n; }
return n + 1;
});
}, 45);
return () => clearInterval(id);
}, [text]);
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<Text style={styles.mono}>{text.slice(0, shown)}</Text>
<Animated.View
style={{
width: 2, height: 20, marginLeft: 2, backgroundColor: '#2F54EB',
animationName: blink,
animationDuration: '900ms',
animationIterationCount: 'infinite',
animationTimingFunction: 'steps(2)', // hard on/off instead of a fade
}}
/>
</View>