UNPKG

@td-design/rn-hooks

Version:

从ahooks里面抽取的一些在rn项目里可以使用的hooks。提高开发效率

59 lines (55 loc) 2.14 kB
'use strict'; var _tslib = require('../_virtual/_tslib.js'); var react = require('react'); var reactNative = require('react-native'); var index = require('../useBoolean/index.js'); var index$1 = require('../useSafeState/index.js'); var emptyCoord = Object.freeze({ screenX: 0, screenY: 0, width: 0, height: 0, }); var initialValue = { start: emptyCoord, end: emptyCoord, }; function useKeyboard() { var _a = _tslib.__read(index(false), 2), shown = _a[0], _b = _a[1], setTrue = _b.setTrue, setFalse = _b.setFalse; var _c = _tslib.__read(index$1(initialValue), 2), coords = _c[0], setCoords = _c[1]; var _d = _tslib.__read(index$1(0), 2), keyboardHeight = _d[0], setKeyboardHeight = _d[1]; react.useEffect(function () { var subscriptions = [ reactNative.Keyboard.addListener('keyboardWillShow', function (e) { setCoords({ start: e.startCoordinates, end: e.endCoordinates }); }), reactNative.Keyboard.addListener('keyboardDidShow', function (e) { setTrue(); setCoords({ start: e.startCoordinates, end: e.endCoordinates }); setKeyboardHeight(e.endCoordinates.height); }), reactNative.Keyboard.addListener('keyboardWillHide', function (e) { setCoords({ start: e.startCoordinates, end: e.endCoordinates }); }), reactNative.Keyboard.addListener('keyboardDidHide', function (e) { setFalse(); if (e) { setCoords({ start: e.startCoordinates, end: e.endCoordinates }); } else { setCoords(initialValue); setKeyboardHeight(0); } }), ]; return function () { subscriptions.forEach(function (subscription) { return subscription.remove(); }); }; }, [setCoords, setFalse, setKeyboardHeight, setTrue]); return { keyboardShown: shown, keyboardHeight: keyboardHeight, coords: coords, }; } module.exports = useKeyboard;