Skip to content

Instantly share code, notes, and snippets.

@Yassir4
Last active March 20, 2023 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yassir4/416618daeb09b8b9b3bffec80bf283c2 to your computer and use it in GitHub Desktop.
Save Yassir4/416618daeb09b8b9b3bffec80bf283c2 to your computer and use it in GitHub Desktop.
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