UNPKG

one

Version:

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

78 lines (75 loc) • 3.29 kB
import { jsx } from "react/jsx-runtime"; import React, { createContext, useContext } from "react"; import { isNative } from "../constants.native.js"; import { findFocusedRoute } from "../fork/findFocusedRoute.native.js"; import { getResolvedLinking } from "./linkingConfig.native.js"; import { getContextKey } from "./matchers.native.js"; import { mergeDynamicParams } from "./params.native.js"; import { getPathWithRecoveredDynamicSegment } from "./path.native.js"; import { routeInfo } from "./router.native.js"; import { RouteInfoContextProvider } from "./RouteInfoContext.native.js"; var RouteParamsContext = /* @__PURE__ */ createContext({}); var CurrentRouteContext = /* @__PURE__ */ React.createContext([]); var SuspenseFallbackContext = /* @__PURE__ */ createContext(void 0); if (process.env.NODE_ENV !== "production") { CurrentRouteContext.displayName = "RouteNode"; SuspenseFallbackContext.displayName = "SuspenseFallback"; } function useRouteNode() { var _nodes_; var nodes = useContext(CurrentRouteContext); return (_nodes_ = nodes[nodes.length - 1]) !== null && _nodes_ !== void 0 ? _nodes_ : null; } function useRouteNodes() { return useContext(CurrentRouteContext); } function useContextKey() { var node = useRouteNode(); if (node == null) { throw new Error("No filename found. This is likely a bug in router."); } return getContextKey(node.contextKey); } function getParamsFromCurrentUrl(route) { var linking = getResolvedLinking(); if (!(linking === null || linking === void 0 ? void 0 : linking.getStateFromPath)) return void 0; var path = getPathWithRecoveredDynamicSegment([routeInfo === null || routeInfo === void 0 ? void 0 : routeInfo.unstable_globalHref, route === null || route === void 0 ? void 0 : route.path]); if (!path) return void 0; var state = linking.getStateFromPath(path, linking.config); if (!state) return void 0; var focused = findFocusedRoute(state); return focused === null || focused === void 0 ? void 0 : focused.params; } function Route(param) { var { children, node, route } = param; var parentParams = useContext(RouteParamsContext); var parentRouteNodes = useContext(CurrentRouteContext); var routeNodes = React.useMemo(function() { return [...parentRouteNodes, node]; }, [node, parentRouteNodes]); var resolvedParams = React.useMemo(function() { var _node_dynamic; var rp = route === null || route === void 0 ? void 0 : route.params; var ownParams = !isNative && ((_node_dynamic = node.dynamic) === null || _node_dynamic === void 0 ? void 0 : _node_dynamic.length) ? mergeDynamicParams(rp, node.dynamic, getParamsFromCurrentUrl(route)) : rp; if (!parentParams) return ownParams; if (!ownParams) return parentParams; return { ...parentParams, ...ownParams }; }, [ node, parentParams, route, routeInfo === null || routeInfo === void 0 ? void 0 : routeInfo.unstable_globalHref ]); return /* @__PURE__ */ jsx(RouteParamsContext.Provider, { value: resolvedParams, children: /* @__PURE__ */ jsx(CurrentRouteContext.Provider, { value: routeNodes, children: /* @__PURE__ */ jsx(RouteInfoContextProvider, { children }) }) }); } export { Route, RouteParamsContext, SuspenseFallbackContext, useContextKey, useRouteNode, useRouteNodes }; //# sourceMappingURL=Route.native.js.map