Skip to content

Instantly share code, notes, and snippets.

@pavermakov
Last active September 11, 2020 07:27
Show Gist options
  • Save pavermakov/cc386bd0a2300dc2b475afb974087aae to your computer and use it in GitHub Desktop.
Save pavermakov/cc386bd0a2300dc2b475afb974087aae to your computer and use it in GitHub Desktop.
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