UNPKG

one

Version:

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

62 lines (61 loc) 2.29 kB
import React, { createContext, useContext } from "react"; import { findFocusedRoute } from "../fork/findFocusedRoute.mjs"; import { getResolvedLinking } from "./linkingConfig.mjs"; import { getContextKey } from "./matchers.mjs"; import { mergeDynamicParams } from "./params.mjs"; import { routeInfo } from "./router.mjs"; import { RouteInfoContextProvider } from "./RouteInfoContext.mjs"; import { jsx } from "react/jsx-runtime"; const RouteParamsContext = createContext({}); const CurrentRouteContext = React.createContext(null); if (process.env.NODE_ENV !== "production") { CurrentRouteContext.displayName = "RouteNode"; } function useRouteNode() { return useContext(CurrentRouteContext); } function useContextKey() { const 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) { const linking = getResolvedLinking(); if (!linking?.getStateFromPath) return void 0; const path = routeInfo?.unstable_globalHref || route?.path || (typeof window !== "undefined" && window.location ? window.location.pathname + window.location.search : void 0); if (!path) return void 0; const state = linking.getStateFromPath(path, linking.config); if (!state) return void 0; const focused = findFocusedRoute(state); return focused?.params; } function Route({ children, node, route }) { const parentParams = useContext(RouteParamsContext); const resolvedParams = React.useMemo(() => { const rp = route?.params; const ownParams = 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?.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.mjs.map