UNPKG

one

Version:

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

215 lines (210 loc) 8.69 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Root_exports = {}; __export(Root_exports, { Root: () => Root }); module.exports = __toCommonJS(Root_exports); var import_native = require("@react-navigation/native"); var import_color_scheme = require("@vxrn/color-scheme"); var import_react = require("react"); var import_constants = require("./constants.cjs"); var import_screensFeatureFlags = require("./screensFeatureFlags.cjs"); var import_SpaShellContext = require("./router/SpaShellContext.cjs"); var import_NavigationContainer = require("./fork/NavigationContainer.cjs"); var import_getURL = require("./getURL.cjs"); var import_FlagsContext = require("./router/FlagsContext.cjs"); var import_linkingConfig = require("./router/linkingConfig.cjs"); var import_router = require("./router/router.cjs"); var import_serverLocationContext = require("./router/serverLocationContext.cjs"); var import_useInitializeOneRouter = require("./router/useInitializeOneRouter.cjs"); var import_useViteRoutes = require("./router/useViteRoutes.cjs"); var import_evictOldest = require("./utils/evictOldest.cjs"); var import_useServerHeadInsertion = require("./useServerHeadInsertion.cjs"); var import_PreloadLinks = require("./views/PreloadLinks.cjs"); var import_RootErrorBoundary = require("./views/RootErrorBoundary.cjs"); var import_ScrollBehavior = require("./views/ScrollBehavior.cjs"); var import_jsx_runtime = require("react/jsx-runtime"); const ssrLocationCache = /* @__PURE__ */new Map(); function getCachedSSRLocation(path) { let url = ssrLocationCache.get(path); if (url) return url; url = new URL(path, (0, import_getURL.getURL)()); (0, import_evictOldest.evictOldest)(ssrLocationCache, 5e3, 1e3); ssrLocationCache.set(path, url); return url; } const ServerAsyncLocalIDContext = (0, import_react.createContext)(null); globalThis["__vxrnGetContextFromReactContext"] = () => (0, import_react.useContext)(ServerAsyncLocalIDContext); function Root(props) { if (import_constants.isNative) { (0, import_screensFeatureFlags.initScreensFeatureFlags)(); } const { path, routes, routeOptions, isClient, navigationContainerProps, onRenderId } = props; const context = (0, import_useViteRoutes.useViteRoutes)(routes, props.routerRoot, routeOptions, globalThis["__vxrnVersion"]); const location = typeof window !== "undefined" && window.location ? new URL(path || window.location.href || "/", window.location.href) : getCachedSSRLocation(path || "/"); const store = (0, import_useInitializeOneRouter.useInitializeOneRouter)(context, location); const userScheme = (0, import_color_scheme.useUserScheme)(); const Component = store.rootComponent; if (!Component) { throw new Error(`No root component found`); } const id = (0, import_react.useId)(); onRenderId?.(id); const value = process.env.VERCEL ? globalThis["__oneGlobalContextId"] : globalThis["__vxrnrequestAsyncLocalStore"]?.getStore() || null; const deferredPreloads = props.deferredPreloads; let contents = /* @__PURE__ */(0, import_jsx_runtime.jsx)(ServerAsyncLocalIDContext.Provider, { value, children: /* @__PURE__ */(0, import_jsx_runtime.jsxs)(import_useServerHeadInsertion.ServerRenderID.Provider, { value: id, children: [typeof window === "undefined" && deferredPreloads?.map(src => /* @__PURE__ */(0, import_jsx_runtime.jsx)("link", { rel: "modulepreload", fetchPriority: "low", href: src }, src)), /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_NavigationContainer.NavigationContainer, { ref: store.navigationRef, initialState: store.initialState, linking: (0, import_linkingConfig.getLinking)(), onUnhandledAction, onStateChange: import_router.handleNavigationContainerStateChange, theme: userScheme.value === "dark" ? import_native.DarkTheme : import_native.DefaultTheme, documentTitle: { enabled: false }, ...navigationContainerProps, children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_serverLocationContext.ServerLocationContext.Provider, { value: location, children: /* @__PURE__ */(0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [/* @__PURE__ */(0, import_jsx_runtime.jsx)(import_ScrollBehavior.ScrollBehavior, {}), /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_RootErrorBoundary.RootErrorBoundary, { children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(Component, {}) })] }) }) }), typeof window !== "undefined" && /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_PreloadLinks.PreloadLinks, {}, "preload-links")] }) }); if (props.flags) { contents = /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_FlagsContext.FlagsContext.Provider, { value: props.flags, children: contents }); } if (!process.env.ONE_DISABLE_STRICT_MODE) { contents = /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_react.StrictMode, { children: contents }); } if (isClient) { const serverMode = globalThis[import_constants.SERVER_CONTEXT_KEY]?.mode; if (serverMode === "spa") { const [show, setShow] = (0, import_react.useState)(false); (0, import_react.useLayoutEffect)(() => { setShow(true); }, []); return show ? contents : null; } if (serverMode === "spa-shell") { const [isSpaShell, setIsSpaShell] = (0, import_react.useState)(true); (0, import_react.useLayoutEffect)(() => { setIsSpaShell(false); }, []); (0, import_react.useLayoutEffect)(() => { if (!isSpaShell) { const initialPath = window.location.pathname + window.location.search; requestAnimationFrame(() => { if (window.location.pathname + window.location.search !== initialPath) return; const nav = store.navigationRef?.current; if (!nav) return; const linking = (0, import_linkingConfig.getLinking)(); if (linking?.getStateFromPath) { const freshState = linking.getStateFromPath(initialPath, linking.config); if (freshState) { nav.resetRoot(freshState); } } }); } }, [isSpaShell]); return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_SpaShellContext.SpaShellContext.Provider, { value: isSpaShell, children: contents }); } return contents; } if (props.mode === "spa-shell") { return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_SpaShellContext.SpaShellContext.Provider, { value: true, children: contents }); } return contents; } let onUnhandledAction; if (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": if (payload?.name) { message += ` Do you have a route named '${payload.name}'?`; } else { 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; } message += ` This is a development-only warning and won't be shown in production.`; if (process.env.NODE_ENV === "test") { throw new Error(message); } console.error(message); }; } else { onUnhandledAction = () => {}; }