one
Version:
One is a new React Framework that makes Vite serve both native and web.
111 lines (109 loc) • 3.96 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 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 [pendingEvent, setPendingEvent] = React.useState(null);
var [blockedLocation, setBlockedLocation] = React.useState(null);
var shouldBlockRef = React.useRef(shouldBlock);
shouldBlockRef.current = shouldBlock;
React.useEffect(function () {
var unsubscribe = navigation.addListener("beforeRemove", function (e) {
var _e_data_action, _e_data;
var currentShouldBlock = shouldBlockRef.current;
var payload = (_e_data = e.data) === null || _e_data === void 0 ? void 0 : (_e_data_action = _e_data.action) === null || _e_data_action === void 0 ? void 0 : _e_data_action.payload;
var nextLocation = (payload === null || payload === void 0 ? void 0 : payload.name) || "previous screen";
var block = typeof currentShouldBlock === "function" ? currentShouldBlock({
currentLocation: "",
// Not easily available on native
nextLocation,
historyAction: "pop"
}) : currentShouldBlock;
if (!block) {
return;
}
e.preventDefault();
setPendingEvent(e);
setBlockedLocation(nextLocation);
setState("blocked");
});
return unsubscribe;
}, [navigation]);
var reset = React.useCallback(function () {
setPendingEvent(null);
setBlockedLocation(null);
setState("unblocked");
}, []);
var proceed = React.useCallback(function () {
if (!pendingEvent) return;
setState("proceeding");
navigation.dispatch(pendingEvent.data.action);
setTimeout(function () {
setPendingEvent(null);
setBlockedLocation(null);
setState("unblocked");
}, 100);
}, [navigation, pendingEvent]);
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;
}
//# sourceMappingURL=useBlocker.native.js.map