UNPKG

@nayan-ui/react-native

Version:

React Native Component Library for smooth and faster mobile application development.

52 lines (51 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useNKeyboard = useNKeyboard; var React = _interopRequireWildcard(require("react")); var _reactNative = require("react-native"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const EVENT_TYPE = { // Only keyboardDidShow and keyboardDidHide events are available on Android with 1 exception: https://reactnative.dev/docs/keyboard#addlistener didShow: { show: 'keyboardDidShow', hide: 'keyboardDidHide' }, willShow: { show: 'keyboardWillShow', hide: 'keyboardWillHide' } }; function useNKeyboard({ eventType = 'didShow' } = { eventType: 'didShow' }) { const [isKeyboardVisible, setKeyboardVisible] = React.useState(false); const [keyboardHeight, setKeyboardHeight] = React.useState(0); React.useEffect(() => { const showListener = _reactNative.Keyboard.addListener(EVENT_TYPE[eventType].show, e => { setKeyboardVisible(true); setKeyboardHeight(e.endCoordinates.height); }); const hideListener = _reactNative.Keyboard.addListener(EVENT_TYPE[eventType].hide, () => { setKeyboardVisible(false); setKeyboardHeight(0); }); return () => { showListener?.remove(); hideListener?.remove(); }; }, []); function dismissKeyboard() { _reactNative.Keyboard.dismiss(); setKeyboardVisible(false); } return { isKeyboardVisible, keyboardHeight, dismissKeyboard }; } //# sourceMappingURL=useNKeyboard.js.map