react-native-keyboard-controller
Version:
Keyboard manager which works in identical way on both iOS and Android
34 lines (32 loc) • 1.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useKeyboardState = void 0;
var _react = require("react");
var _bindings = require("../../bindings");
var _module = require("../../module");
const EVENTS = ["keyboardDidShow", "keyboardDidHide"];
const getLatestState = () => ({
..._module.KeyboardController.state(),
isVisible: _module.KeyboardController.isVisible()
});
const useKeyboardState = () => {
const [state, setState] = (0, _react.useState)(getLatestState);
(0, _react.useEffect)(() => {
const subscriptions = EVENTS.map(event => _bindings.KeyboardEvents.addListener(event, () =>
// state will be updated by global listener first,
// so we simply read it and don't derive data from the event
setState(getLatestState)));
// we might have missed an update between reading a value in render and
// `addListener` in this handler, so we set it here. If there was
// no change, React will filter out this update as a no-op.
setState(getLatestState);
return () => {
subscriptions.forEach(subscription => subscription.remove());
};
}, []);
return state;
};
exports.useKeyboardState = useKeyboardState;
//# sourceMappingURL=index.js.map