UNPKG

one

Version:

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

115 lines (109 loc) 4.54 kB
import { DarkTheme, DefaultTheme } from "@react-navigation/native"; import { useUserScheme } from "@vxrn/color-scheme"; import { createContext, StrictMode, useContext, useId, useLayoutEffect, useState } from "react"; import { SERVER_CONTEXT_KEY } from "./constants.mjs"; import { NavigationContainer as UpstreamNavigationContainer } from "./fork/NavigationContainer.mjs"; import { getURL } from "./getURL.mjs"; import { FlagsContext } from "./router/FlagsContext.mjs"; import { getLinking } from "./router/linkingConfig.mjs"; import { ServerLocationContext } from "./router/serverLocationContext.mjs"; import { useInitializeOneRouter } from "./router/useInitializeOneRouter.mjs"; import { useViteRoutes } from "./router/useViteRoutes.mjs"; import { ServerRenderID } from "./useServerHeadInsertion.mjs"; import { PreloadLinks } from "./views/PreloadLinks.mjs"; import { RootErrorBoundary } from "./views/RootErrorBoundary.mjs"; import { ScrollBehavior } from "./views/ScrollBehavior.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; const ServerAsyncLocalIDContext = createContext(null); globalThis.__vxrnGetContextFromReactContext = () => useContext(ServerAsyncLocalIDContext); function Root(props) { const { path, routes, routeOptions, isClient, navigationContainerProps, onRenderId } = props, context = useViteRoutes(routes, props.routerRoot, routeOptions, globalThis.__vxrnVersion), location = typeof window < "u" && window.location ? new URL(path || window.location.href || "/", window.location.href) : new URL(path || "/", getURL()), store = useInitializeOneRouter(context, location), userScheme = useUserScheme(), Component = store.rootComponent; if (!Component) throw new Error("No root component found"); const id = useId(); onRenderId?.(id); const value = process.env.VERCEL ? globalThis.__oneGlobalContextId : globalThis.__vxrnrequestAsyncLocalStore?.getStore() || null; let contents = /* @__PURE__ */jsx(ServerAsyncLocalIDContext.Provider, { value, children: /* @__PURE__ */jsxs(ServerRenderID.Provider, { value: id, children: [/* @__PURE__ */jsx(UpstreamNavigationContainer, { ref: store.navigationRef, initialState: store.initialState, linking: getLinking(), onUnhandledAction, theme: userScheme.value === "dark" ? DarkTheme : DefaultTheme, documentTitle: { enabled: !1 }, ...navigationContainerProps, children: /* @__PURE__ */jsx(ServerLocationContext.Provider, { value: location, children: /* @__PURE__ */jsxs(Fragment, { children: [/* @__PURE__ */jsx(ScrollBehavior, {}), /* @__PURE__ */jsx(RootErrorBoundary, { children: /* @__PURE__ */jsx(Component, {}) })] }) }) }), /* @__PURE__ */jsx(PreloadLinks, {}, "preload-links")] }) }); if (props.flags && (contents = /* @__PURE__ */jsx(FlagsContext.Provider, { value: props.flags, children: contents })), process.env.ONE_DISABLE_STRICT_MODE || (contents = /* @__PURE__ */jsx(StrictMode, { children: contents })), isClient) { if (globalThis[SERVER_CONTEXT_KEY]?.mode === "spa") { const [show, setShow] = useState(!1); return useLayoutEffect(() => { setShow(!0); }, []), show ? contents : null; } return contents; } return contents; } let onUnhandledAction; process.env.NODE_ENV !== "production" ? onUnhandledAction = action => { const payload = action.payload; let message = `The action '${action.type}'${payload ? ` with payload ${JSON.stringify(action.payload)}` : ""} was not handled by any navigator.`; switch (action.type) { case "NAVIGATE": case "PUSH": case "REPLACE": case "JUMP_TO": payload?.name ? message += ` Do you have a route named '${payload.name}'?` : message += ` You need to pass the name of the screen to navigate to. This may be a bug.`; break; case "GO_BACK": case "POP": case "POP_TO_TOP": message += ` Is there any screen to go back to?`; break; case "OPEN_DRAWER": case "CLOSE_DRAWER": case "TOGGLE_DRAWER": message += ` Is your screen inside a Drawer navigator?`; break; } if (message += ` This is a development-only warning and won't be shown in production.`, process.env.NODE_ENV === "test") throw new Error(message); console.error(message); } : onUnhandledAction = () => {}; export { Root }; //# sourceMappingURL=Root.mjs.map