one
Version:
One is a new React Framework that makes Vite serve both native and web.
64 lines (63 loc) • 2.79 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React, { createContext, useContext } from "react";
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 { routeInfo } from "./router.native.js";
import { RouteInfoContextProvider } from "./RouteInfoContext.native.js";
var RouteParamsContext = /* @__PURE__ */createContext({});
var CurrentRouteContext = /* @__PURE__ */React.createContext(null);
if (process.env.NODE_ENV !== "production") {
CurrentRouteContext.displayName = "RouteNode";
}
function useRouteNode() {
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 = (routeInfo === null || routeInfo === void 0 ? void 0 : routeInfo.unstable_globalHref) || (route === null || route === void 0 ? void 0 : route.path) || (typeof window !== "undefined" && window.location ? window.location.pathname + window.location.search : void 0);
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 resolvedParams = React.useMemo(function () {
var _node_dynamic;
var rp = route === null || route === void 0 ? void 0 : route.params;
var ownParams = ((_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: node,
children: /* @__PURE__ */_jsx(RouteInfoContextProvider, {
children
})
})
});
}
export { Route, RouteParamsContext, useContextKey, useRouteNode };
//# sourceMappingURL=Route.native.js.map