Last active
March 20, 2023 18:12
-
-
Save Yassir4/416618daeb09b8b9b3bffec80bf283c2 to your computer and use it in GitHub Desktop.
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
import { useState, useEffect } from "react"; | |
import { Keyboard, Platform } from "react-native"; | |
const isIOS = Platform.OS === "ios"; | |
const useKeyboardHeight = () => { | |
const [keyboardHeight, setKeyboardHeight] = useState(0); | |
const handleKeyboardDidShow = (e) => { | |
setKeyboardHeight(e.endCoordinates.height); | |
}; | |
const handleKeyboardDidHide = () => { | |
setKeyboardHeight(0); | |
}; | |
useEffect(() => { | |
// keyboardWillShow is not supported on android | |
const showEvent = isIOS ? "keyboardWillShow" : "keyboardDidShow"; | |
const hideEvent = isIOS ? "keyboardWillHide" : "keyboardDidHide"; | |
const showSubscription = Keyboard.addListener(showEvent, handleKeyboardDidShow); | |
const hideSubscription = Keyboard.addListener(hideEvent, handleKeyboardDidHide); | |
return () => { | |
showSubscription.remove() | |
hideSubscription.remove() | |
}; | |
}, []); | |
return { keyboardHeight }; | |
}; | |
export default useKeyboardHeight; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment