Last active
September 11, 2020 07:27
-
-
Save pavermakov/cc386bd0a2300dc2b475afb974087aae 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 React, { useEffect, useState } from "react"; | |
import { Keyboard } from "react-native"; | |
import { c } from "~/helpers/rootHelper"; | |
const useKeyboard = () => { | |
const [indent, setIndent] = useState(0); | |
const isVisible = indent > 0; | |
const dismiss = () => { | |
Keyboard.dismiss(); | |
}; | |
useEffect(() => { | |
const onKeyboardShow = ({ endCoordinates }) => { | |
setIndent(endCoordinates.height); | |
}; | |
const onKeyboardHide = () => { | |
setIndent(0); | |
}; | |
const event = c.IS_IOS ? "Will" : "Did"; | |
Keyboard.addListener(`keyboard${event}Show`, onKeyboardShow); | |
Keyboard.addListener(`keyboard${event}Hide`, onKeyboardHide); | |
return () => { | |
Keyboard.removeListener(`keyboard${event}Show`, onKeyboardShow); | |
Keyboard.removeListener(`keyboard${event}Hide`, onKeyboardHide); | |
}; | |
}, []); | |
return { | |
isVisible, | |
indent, | |
dismiss | |
}; | |
}; | |
export default useKeyboard; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment