UNPKG

keyboard-height

Version:

calaculate the height of soft keyboard in mobile phone and emit on changes

38 lines (32 loc) 894 B
import { isAndroid, isIos } from "./uitls"; import initIos, { getIosHeight, getIosInnerHeight } from "./ios"; import initAndroid, { getAndroidHeight, getAndroidInnerHight } from "./android"; class KeyboardObserver { constructor() { this.cbArr_ = []; if (isAndroid) initAndroid(this.cbArr_); else if (isIos) initIos(this.cbArr_); } on(cb) { if (this.cbArr_.indexOf(cb) === -1 && typeof cb === "function") { this.cbArr_.push(cb); } return this; } off(cb) { var index = this.cbArr_.indexOf(cb); if (index !== -1) { this.cbArr_.splice(index, 1); } return this; } getKeyboardHeight() { if (isAndroid) return getAndroidHeight(); else if (isIos) return getIosHeight(); return 0; } getInnerHeight() { return isAndroid ? getAndroidInnerHight() : getIosInnerHeight(); } } export default new KeyboardObserver();