@crossed/sheet
Version:
A Cross Platform(Android & iOS) ActionSheet with a robust and flexible api, native performance and zero dependency code for react native. Create anything you want inside ActionSheet.
110 lines (109 loc) • 4.08 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useKeyboard_exports = {};
__export(useKeyboard_exports, {
useKeyboard: () => useKeyboard
});
module.exports = __toCommonJS(useKeyboard_exports);
var import_react = __toESM(require("react"));
var import_react_native = require("react-native");
const emptyCoordinates = Object.freeze({
screenX: 0,
screenY: 0,
width: 0,
height: 0
});
const initialValue = {
start: emptyCoordinates,
end: emptyCoordinates
};
function useKeyboard(enabled) {
const pauseKeyboardHandler = (0, import_react.useRef)(false);
const [shown, setShown] = (0, import_react.useState)(false);
const [coordinates, setCoordinates] = (0, import_react.useState)(initialValue);
const [keyboardHeight, setKeyboardHeight] = (0, import_react.useState)(0);
const handleKeyboardDidShow = import_react.default.useCallback(
(e) => {
if (pauseKeyboardHandler.current)
return;
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
setKeyboardHeight(e.endCoordinates.height);
setShown(true);
},
[]
);
const handleKeyboardDidHide = import_react.default.useCallback(
(e) => {
setShown(false);
if (e) {
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
} else {
setCoordinates(initialValue);
setKeyboardHeight(0);
}
},
[]
);
(0, import_react.useEffect)(() => {
let subscriptions = [];
if (enabled) {
subscriptions = [
import_react_native.Keyboard.addListener("keyboardDidChangeFrame", handleKeyboardDidShow),
import_react_native.Keyboard.addListener("keyboardDidHide", handleKeyboardDidHide)
];
if (import_react_native.Platform.OS == "android") {
subscriptions.push(
import_react_native.Keyboard.addListener("keyboardDidShow", handleKeyboardDidShow)
);
} else {
subscriptions.push(
import_react_native.Keyboard.addListener("keyboardWillShow", handleKeyboardDidShow),
import_react_native.Keyboard.addListener("keyboardWillHide", handleKeyboardDidHide)
);
}
}
return () => {
subscriptions.forEach((subscription) => subscription.remove());
};
}, [enabled, handleKeyboardDidHide, handleKeyboardDidShow]);
return {
keyboardShown: !enabled ? false : shown,
coordinates: !enabled || !shown ? emptyCoordinates : coordinates,
keyboardHeight: !enabled || !shown ? 0 : keyboardHeight,
pauseKeyboardHandler,
reset: () => {
setShown(false);
setKeyboardHeight(0);
}
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
useKeyboard
});
//# sourceMappingURL=useKeyboard.js.map