-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add KeyboardAvoidingView #4339
Draft
terrysahaidak
wants to merge
13
commits into
software-mansion:main
Choose a base branch
from
terrysahaidak:@terrysahaidak/keyboard-avoiding-view
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add KeyboardAvoidingView #4339
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1d56d1a
Add KeyboardAvoidingView
terrysahaidak 6b1ca2d
Merge remote-tracking branch 'upstream/main' into @terrysahaidak/keyb…
terrysahaidak 025577e
stash work
terrysahaidak 3828840
revert unnecessary changes
terrysahaidak 7490483
Add toggle to use RN's Keyboard avoiding view
terrysahaidak d019930
more fixes
terrysahaidak a17f1ed
Merge remote-tracking branch 'upstream/main' into @terrysahaidak/keyb…
terrysahaidak b384958
flicker reproducing
terrysahaidak 252f8b0
Merge remote-tracking branch 'upstream/main' into @terrysahaidak/keyb…
terrysahaidak 504714f
Merge remote-tracking branch 'upstream/main' into @terrysahaidak/keyb…
terrysahaidak 778a8ca
Fix initial render
terrysahaidak 3f1cd61
Merge remote-tracking branch 'upstream/main' into @terrysahaidak/keyb…
terrysahaidak 3529dc1
wip demo
terrysahaidak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
import Animated, { | ||
KeyboardState, | ||
// runOnJS, | ||
// runOnUI, | ||
useAnimatedKeyboard, | ||
useAnimatedStyle, | ||
useDerivedValue, | ||
useSharedValue, | ||
} from 'react-native-reanimated'; | ||
import { | ||
FlatList, | ||
Modal, | ||
Pressable, | ||
ScrollView, | ||
ScrollViewProps, | ||
StyleSheet, | ||
Switch, | ||
Text, | ||
TextInput, | ||
View, | ||
KeyboardAvoidingView as RNKeyboardAvoidingView, | ||
} from 'react-native'; | ||
import React, { useCallback, useState } from 'react'; | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
import { TouchableOpacity } from 'react-native-gesture-handler'; | ||
|
||
function ListItem({ | ||
title, | ||
onLongPress, | ||
}: { | ||
title: string; | ||
onLongPress: () => void; | ||
}) { | ||
return ( | ||
<TouchableOpacity onLongPress={onLongPress}> | ||
<View style={styles.item}> | ||
<Text>Open keyboard and long press on message number {title}</Text> | ||
</View> | ||
</TouchableOpacity> | ||
); | ||
} | ||
|
||
function KeyboardSpace({ | ||
// modalVisible, | ||
children, | ||
...props | ||
}: ScrollViewProps & { | ||
modalVisible?: Animated.SharedValue<boolean>; | ||
children: React.ReactNode; | ||
}) { | ||
const keyboard = useAnimatedKeyboard(); | ||
|
||
const [ctx] = useState({ | ||
lastHeight: 0, | ||
keyboardWasOpen: false, | ||
}); | ||
|
||
const translateY = useDerivedValue(() => { | ||
const { height, state } = keyboard; | ||
|
||
if (state.value === KeyboardState.OPEN) { | ||
ctx.lastHeight = height.value; | ||
} | ||
|
||
console.log( | ||
'height', | ||
height.value, | ||
'ctx.lastHeight', | ||
ctx.lastHeight, | ||
'state.value', | ||
state.value | ||
); | ||
|
||
if (state.value === KeyboardState.CLOSED) { | ||
return Math.max(ctx.lastHeight - 17, 0); | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
transform: [ | ||
{ | ||
translateY: translateY.value, | ||
}, | ||
], | ||
}; | ||
}); | ||
|
||
return ( | ||
<ScrollView {...props}> | ||
<Animated.View style={[{ flex: 1 }, animatedStyle]}> | ||
{children} | ||
</Animated.View> | ||
</ScrollView> | ||
); | ||
} | ||
|
||
export default function KeyboardAvoidingViewExample(): React.ReactElement { | ||
const insets = useSafeAreaInsets(); | ||
|
||
const [useReanimated, setUseReanimated] = useState(true); | ||
|
||
const [isModalVisible, setIsModalVisible] = useState(false); | ||
const modalVisible = useSharedValue(false); | ||
|
||
const showModal = useCallback(() => { | ||
// runOnUI(() => { | ||
// 'worklet'; | ||
|
||
// modalVisible.value = true; | ||
|
||
// runOnJS(setIsModalVisible)(true); | ||
// })(); | ||
|
||
setIsModalVisible(true); | ||
}, [setIsModalVisible]); | ||
|
||
const hideModal = useCallback(() => { | ||
// runOnUI(() => { | ||
// 'worklet'; | ||
|
||
// modalVisible.value = false; | ||
|
||
// runOnJS(setIsModalVisible)(false); | ||
// })(); | ||
|
||
setIsModalVisible(false); | ||
}, [setIsModalVisible]); | ||
|
||
const renderScrollComponent = useCallback( | ||
(props: ScrollViewProps) => { | ||
// @ts-expect-error | ||
return <KeyboardSpace {...props} modalVisible={modalVisible} />; | ||
}, | ||
[modalVisible] | ||
); | ||
|
||
const KeyboardAvoidingView = useReanimated | ||
? // @ts-expect-error | ||
Animated.KeyboardAvoidingView | ||
: RNKeyboardAvoidingView; | ||
|
||
return ( | ||
<View style={[styles.container]}> | ||
<View style={styles.header}> | ||
<Text>Use Reanimated KeyboardAvoidingView</Text> | ||
<Switch | ||
value={useReanimated} | ||
onChange={(evt) => setUseReanimated(evt.nativeEvent.value)} | ||
/> | ||
</View> | ||
|
||
<FlatList | ||
inverted | ||
renderScrollComponent={renderScrollComponent} | ||
keyboardDismissMode="on-drag" | ||
keyboardShouldPersistTaps="always" | ||
data={Array.from({ length: 20 }).map((_, i) => i)} | ||
renderItem={({ item }) => ( | ||
<ListItem onLongPress={showModal} title={`Item ${item}`} /> | ||
)} | ||
/> | ||
|
||
<KeyboardAvoidingView | ||
behavior="padding" | ||
keyboardVerticalOffset={40 + insets.bottom}> | ||
<View | ||
style={{ | ||
paddingBottom: insets.bottom, | ||
}}> | ||
<TextInput | ||
placeholder="Message here..." | ||
style={[styles.textInput]} | ||
autoCorrect | ||
/> | ||
</View> | ||
</KeyboardAvoidingView> | ||
|
||
<Modal | ||
style={styles.sheetContainer} | ||
visible={isModalVisible} | ||
transparent | ||
animationType="none"> | ||
<Pressable style={styles.sheetBackdrop} onPress={hideModal} /> | ||
|
||
<View style={styles.sheet}> | ||
<Text style={styles.sheetText}> | ||
This modal should cover the keyboard, but the latest message should | ||
be in the same place. | ||
</Text> | ||
</View> | ||
</Modal> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
}, | ||
header: { | ||
height: 40, | ||
flexDirection: 'row', | ||
justifyContent: 'space-between', | ||
alignItems: 'center', | ||
paddingHorizontal: 20, | ||
backgroundColor: 'rgba(29, 28, 29, 0.13)', | ||
}, | ||
item: { | ||
paddingHorizontal: 20, | ||
paddingVertical: 20, | ||
borderTopWidth: 1, | ||
marginHorizontal: 20, | ||
borderColor: 'rgba(29, 28, 29, 0.13)', | ||
}, | ||
textInput: { | ||
borderColor: 'rgba(29, 28, 29, 0.13)', | ||
borderStyle: 'solid', | ||
borderWidth: 1, | ||
height: 48, | ||
borderRadius: 20, | ||
marginHorizontal: 20, | ||
paddingHorizontal: 20, | ||
}, | ||
sheetContainer: { | ||
flex: 1, | ||
backgroundColor: 'transparent', | ||
}, | ||
sheet: { | ||
borderColor: 'rgba(29, 28, 29, 0.13)', | ||
borderWidth: 1, | ||
backgroundColor: 'white', | ||
height: 300, | ||
position: 'absolute', | ||
bottom: 0, | ||
left: 0, | ||
right: 0, | ||
borderTopLeftRadius: 20, | ||
borderTopRightRadius: 20, | ||
padding: 20, | ||
}, | ||
sheetBackdrop: { | ||
...StyleSheet.absoluteFillObject, | ||
backgroundColor: 'rgba(0, 0, 0, 0.2)', | ||
}, | ||
sheetText: { | ||
fontSize: 20, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @terrysahaidak, I've tested your example with the PR #4360.
If you look closely, there's still some flickering when opening the modal and it happens both with Reanimated's and React Native's KeyboardAvoidingView:
KeyboardAvoidingView from Reanimated
reanimated.mp4
KeyboardAvoidingView from React Native
react.native.mp4
I believe the root cause of this flicker is not the implementation of
useAnimatedKeyboard
hook but rather the fact that the visibility of<Modal>
component is controlled using React state whereas the other part of the logic uses a shared value. In particular, there are two sources of truth and it takes 1-2 frames to render the changes from the JS thread when the React state is updated:react-native-reanimated/app/src/examples/KeyboardAvoidingViewExample.tsx
Lines 109 to 110 in d019930
Instead, you should keep the state only in shared value and control the visibility of the modal via animated props on the UI thread:
Edit: looks like
visible
prop is available only on iOS, on Android it's implemented on JS side