$ git show expand-search
Expanding search
A round search button stretches into a full search field and focuses it; cancel shrinks it back.
// Reanimated 4: width and opacity transitions on state, the input fades in after the stretch
const [open, setOpen] = useState(false);
const input = useRef(null);
const { width } = useWindowDimensions();
useEffect(() => { open ? input.current?.focus() : input.current?.blur(); }, [open]);
<View style={{ flexDirection: 'row', justifyContent: 'flex-end' }}>
<Animated.View style={[styles.field, {
width: open ? width - 32 : 40,
transitionProperty: 'width',
transitionDuration: open ? '280ms' : '220ms',
transitionTimingFunction: open ? cubicBezier(0.3, 1.2, 0.5, 1) : 'ease-in',
}]}>
<Pressable onPress={() => setOpen(true)}><SearchIcon /></Pressable>
<Animated.View style={{
flex: 1, flexDirection: 'row',
opacity: open ? 1 : 0,
transitionProperty: 'opacity',
transitionDuration: '150ms',
transitionDelay: open ? '140ms' : '0ms',
}}>
<TextInput ref={input} placeholder="Search recipes…" style={{ flex: 1 }} />
<Pressable onPress={() => setOpen(false)}><Text>Cancel</Text></Pressable>
</Animated.View>
</Animated.View>
</View>