one
Version:
One is a React framework for web, iOS, and Android apps.
90 lines (88 loc) • 3.1 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(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var useBlocker_native_exports = {};
__export(useBlocker_native_exports, {
checkBlocker: () => checkBlocker,
useBlocker: () => useBlocker
});
module.exports = __toCommonJS(useBlocker_native_exports);
var import_native = require("@react-navigation/native");
var React = __toESM(require("react"), 1);
function useBlocker(shouldBlock) {
var navigation = (0, import_native.useNavigation)();
var [state, setState] = React.useState("unblocked");
var [pendingAction, setPendingAction] = React.useState(null);
var [blockedLocation, setBlockedLocation] = React.useState(null);
var block = typeof shouldBlock === "function" ? shouldBlock({
currentLocation: "",
nextLocation: "previous screen",
historyAction: "pop"
}) : shouldBlock;
(0, import_native.usePreventRemove)(block, function(param) {
var { data } = param;
var _data_action;
var payload = data === null || data === void 0 ? void 0 : (_data_action = data.action) === null || _data_action === void 0 ? void 0 : _data_action.payload;
setPendingAction(data.action);
setBlockedLocation((payload === null || payload === void 0 ? void 0 : payload.name) || "previous screen");
setState("blocked");
});
var reset = React.useCallback(function() {
setPendingAction(null);
setBlockedLocation(null);
setState("unblocked");
}, []);
var proceed = React.useCallback(function() {
if (!pendingAction) return;
setState("proceeding");
navigation.dispatch(pendingAction);
setTimeout(function() {
setPendingAction(null);
setBlockedLocation(null);
setState("unblocked");
}, 100);
}, [navigation, pendingAction]);
if (state === "unblocked") {
return { state: "unblocked" };
}
if (state === "proceeding") {
return {
state: "proceeding",
location: blockedLocation
};
}
return {
state: "blocked",
reset,
proceed,
location: blockedLocation
};
}
function checkBlocker(_nextLocation) {
var _historyAction = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "push";
return false;
}