one
Version:
One is a React framework for web, iOS, and Android apps.
109 lines (108 loc) • 4.14 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 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.cjs");
var import_matchers = require("./matchers.cjs");
var import_Route = require("./Route.cjs");
function useNavigation(parent) {
const navigation = (0, import_native.useNavigation)();
const contextKey = (0, import_Route.useContextKey)();
const routeNodes = (0, import_Route.useRouteNodes)();
const normalizedParent = import_react.default.useMemo(() => {
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 === "") {
let rootNavigation = navigation;
let nextNavigation = rootNavigation.getParent();
while (nextNavigation) {
rootNavigation = nextNavigation;
nextNavigation = rootNavigation.getParent();
}
return rootNavigation;
}
const 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) {
const parentContextKey = resolveParentId(contextKey, parentId);
if (parentContextKey == null) return null;
if (parentContextKey === "/") return "";
const parentRoute = routeNodes.find((node) => (0, import_matchers.getContextKey)(node.contextKey) === parentContextKey);
return parentRoute ? (0, import_getReactNavigationConfig.getReactNavigationRouteName)(parentRoute) : void 0;
}
function normalizeContextPath(path) {
const normalized = (0, import_matchers.getNameFromFilePath)(path).replace(/^\/+|\/+$/g, "").replace(/\/_layout$/, "");
return normalized ? `/${normalized}` : "/";
}
function relativePaths(from, to) {
const fromParts = from.split("/").filter(Boolean);
const toParts = to.split("/").filter(Boolean);
for (const part of toParts) {
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);
}
}
return "/" + fromParts.join("/");
}