UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

132 lines (129 loc) • 4.76 kB
"use strict"; 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 useNavigation_exports = {}; __export(useNavigation_exports, { resolveParentId: () => resolveParentId, resolveParentRouteName: () => resolveParentRouteName, useNavigation: () => useNavigation }); module.exports = __toCommonJS(useNavigation_exports); var import_native = require("@react-navigation/native"); var import_react = __toESM(require("react"), 1); var import_getReactNavigationConfig = require("../getReactNavigationConfig.native.js"); var import_matchers = require("./matchers.native.js"); var import_Route = require("./Route.native.js"); function useNavigation(parent) { var navigation = (0, import_native.useNavigation)(); var contextKey = (0, import_Route.useContextKey)(); var routeNodes = (0, import_Route.useRouteNodes)(); var normalizedParent = import_react.default.useMemo(function() { if (!parent) { return null; } return resolveParentRouteName(routeNodes, contextKey, parent); }, [ contextKey, parent, routeNodes ]); if (parent && normalizedParent === void 0) { throw new Error(`Could not find parent navigation with route "${parent}". (normalized context: ${resolveParentId(contextKey, parent)})`); } if (normalizedParent != null) { if (normalizedParent === "") { var rootNavigation = navigation; var nextNavigation = rootNavigation.getParent(); while (nextNavigation) { rootNavigation = nextNavigation; nextNavigation = rootNavigation.getParent(); } return rootNavigation; } var parentNavigation = navigation.getParent(normalizedParent); if (!parentNavigation) { throw new Error(`Could not find parent navigation with route "${parent}".` + (normalizedParent !== parent ? ` (normalized: ${normalizedParent})` : "")); } return parentNavigation; } return navigation; } function resolveParentId(contextKey, parentId) { if (!parentId) { return null; } if (parentId.startsWith(".")) { return normalizeContextPath(relativePaths(contextKey, parentId)); } return normalizeContextPath(parentId); } function resolveParentRouteName(routeNodes, contextKey, parentId) { var parentContextKey = resolveParentId(contextKey, parentId); if (parentContextKey == null) return null; if (parentContextKey === "/") return ""; var parentRoute = routeNodes.find(function(node) { return (0, import_matchers.getContextKey)(node.contextKey) === parentContextKey; }); return parentRoute ? (0, import_getReactNavigationConfig.getReactNavigationRouteName)(parentRoute) : void 0; } function normalizeContextPath(path) { var normalized = (0, import_matchers.getNameFromFilePath)(path).replace(/^\/+|\/+$/g, "").replace(/\/_layout$/, ""); return normalized ? `/${normalized}` : "/"; } function relativePaths(from, to) { var fromParts = from.split("/").filter(Boolean); var toParts = to.split("/").filter(Boolean); var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0; try { for (var _iterator = toParts[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var part = _step.value; if (part === "..") { if (fromParts.length === 0) { throw new Error(`Cannot resolve path "${to}" relative to "${from}"`); } fromParts.pop(); } else if (part === ".") {} else { fromParts.push(part); } } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return != null) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } return "/" + fromParts.join("/"); } //# sourceMappingURL=useNavigation.native.js.map