UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

90 lines (88 loc) 3.77 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0 }); }, __copyProps = (to, from, except, desc) => { if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__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: !0 }) : target, mod)), __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: !0 }), 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"), React = __toESM(require("react"), 1); function useBlocker(shouldBlock) { var navigation = (0, import_native.useNavigation)(), [state, setState] = React.useState("unblocked"), [pendingEvent, setPendingEvent] = React.useState(null), [blockedLocation, setBlockedLocation] = React.useState(null), shouldBlockRef = React.useRef(shouldBlock); shouldBlockRef.current = shouldBlock, React.useEffect(function () { var unsubscribe = navigation.addListener("beforeRemove", function (e) { var _e_data_action, _e_data, currentShouldBlock = shouldBlockRef.current, payload = (_e_data = e.data) === null || _e_data === void 0 || (_e_data_action = _e_data.action) === null || _e_data_action === void 0 ? void 0 : _e_data_action.payload, nextLocation = payload?.name || "previous screen", block = typeof currentShouldBlock == "function" ? currentShouldBlock({ currentLocation: "", // Not easily available on native nextLocation, historyAction: "pop" }) : currentShouldBlock; block && (e.preventDefault(), setPendingEvent(e), setBlockedLocation(nextLocation), setState("blocked")); }); return unsubscribe; }, [navigation]); var reset = React.useCallback(function () { setPendingEvent(null), setBlockedLocation(null), setState("unblocked"); }, []), proceed = React.useCallback(function () { pendingEvent && (setState("proceeding"), navigation.dispatch(pendingEvent.data.action), setTimeout(function () { setPendingEvent(null), setBlockedLocation(null), setState("unblocked"); }, 100)); }, [navigation, pendingEvent]); return state === "unblocked" ? { state: "unblocked" } : state === "proceeding" ? { state: "proceeding", location: blockedLocation } : { state: "blocked", reset, proceed, location: blockedLocation }; } function checkBlocker(_nextLocation) { var _historyAction = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : "push"; return !1; } //# sourceMappingURL=useBlocker.native.js.map