Skip to content

Commit

Permalink
create keyboardverticaloffset hook (#3823)
Browse files Browse the repository at this point in the history
  • Loading branch information
mozzius authored May 2, 2024
1 parent aca55cb commit 96c5db3
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/screens/Messages/Conversation/MessagesList.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import React, {useCallback, useRef} from 'react'
import React, {useCallback, useEffect, useRef} from 'react'
import {
Dimensions,
FlatList,
NativeScrollEvent,
NativeSyntheticEvent,
Platform,
View,
} from 'react-native'
import {KeyboardAvoidingView} from 'react-native-keyboard-controller'
Expand All @@ -11,7 +13,6 @@ import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {useFocusEffect} from '@react-navigation/native'

import {isIOS} from '#/platform/detection'
import {useChat} from '#/state/messages'
import {ConvoItem, ConvoStatus} from '#/state/messages/convo'
import {useSetMinimalShellMode} from '#/state/shell'
Expand Down Expand Up @@ -125,11 +126,12 @@ export function MessagesList() {
)

const {bottom: bottomInset} = useSafeAreaInsets()
const keyboardVerticalOffset = useKeyboardVerticalOffset()

return (
<KeyboardAvoidingView
style={[a.flex_1, {marginBottom: bottomInset}]}
keyboardVerticalOffset={isIOS ? 60 : 25}
keyboardVerticalOffset={keyboardVerticalOffset}
behavior="padding"
contentContainerStyle={a.flex_1}>
<FlatList
Expand Down Expand Up @@ -175,3 +177,26 @@ export function MessagesList() {
</KeyboardAvoidingView>
)
}

function useKeyboardVerticalOffset() {
const {top: topInset} = useSafeAreaInsets()
const [screenWindowDifference, setScreenWindowDifference] = React.useState(
() => Dimensions.get('screen').height - Dimensions.get('window').height,
)

useEffect(() => {
const subscription = Dimensions.addEventListener(
'change',
({screen, window}) => {
setScreenWindowDifference(screen.height - window.height)
},
)
return () => subscription.remove()
}, [])

return Platform.select({
ios: topInset,
android: screenWindowDifference,
default: 0,
})
}

0 comments on commit 96c5db3

Please sign in to comment.