UNPKG

one

Version:

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

1 lines • 13.3 kB
{"version":3,"file":"Navigator.js","names":["globalRouteNode"],"sources":["esm/views/Navigator.js"],"sourcesContent":["import {\n StackRouter,\n useNavigationBuilder\n} from \"@react-navigation/native\";\nimport { NavigationRouteContext } from \"@react-navigation/core\";\nimport * as React from \"react\";\nimport { SafeAreaView } from \"../safe-area-context\";\nimport { resolveInitialRouteNameFromState } from \"../getReactNavigationConfig\";\nimport {\n useFilterScreenChildren,\n useResolvedGuardedRedirects\n} from \"../layouts/withLayoutContext\";\nimport {\n findNearestNotFoundRoute,\n findRouteNodeByPath,\n useNotFoundState\n} from \"../notFoundState\";\nimport { useContextKey } from \"../router/Route\";\nimport { getResolvedLinking } from \"../router/linkingConfig\";\nimport {\n routeNode as globalRouteNode,\n getSafeWindowPathname,\n initialPathname,\n lastIntendedPathname,\n replace,\n registerProtectedRoutes,\n resolveProtectedHref,\n routeInfo,\n unregisterProtectedRoutes\n} from \"../router/router\";\nimport { useSortedScreens, getQualifiedRouteComponent } from \"../router/useScreens\";\nimport { Screen } from \"./Screen\";\nimport { registerClearSlotStates, registerSetSlotState } from \"../router/interceptRoutes\";\nimport { Fragment, jsx } from \"react/jsx-runtime\";\nconst SLOT_STATIC_KEY = \"one-slot-static-key\";\nfunction resolveInitialRouteFromLinking(contextKey, browserPath) {\n const linking = getResolvedLinking();\n if (!linking?.getStateFromPath) return void 0;\n return resolveInitialRouteNameFromState(\n contextKey,\n linking.getStateFromPath(browserPath, linking.config)\n );\n}\nconst globalSlotState = /* @__PURE__ */ new Map();\nconst slotStateListeners = /* @__PURE__ */ new Set();\nfunction getSlotState(slotName) {\n return globalSlotState.get(slotName);\n}\nfunction setSlotState(slotName, state) {\n if (state === null) {\n globalSlotState.delete(slotName);\n } else {\n globalSlotState.set(slotName, state);\n }\n slotStateListeners.forEach((listener) => listener());\n}\nfunction clearAllSlotStates() {\n globalSlotState.clear();\n slotStateListeners.forEach((listener) => listener());\n}\nregisterClearSlotStates(clearAllSlotStates);\nregisterSetSlotState(setSlotState);\nfunction useSlotStateSubscription() {\n const [, forceUpdate] = React.useReducer((x) => x + 1, 0);\n React.useEffect(() => {\n slotStateListeners.add(forceUpdate);\n return () => {\n slotStateListeners.delete(forceUpdate);\n };\n }, []);\n}\nconst NavigatorContext = React.createContext(null);\nif (process.env.NODE_ENV !== \"production\") {\n NavigatorContext.displayName = \"NavigatorContext\";\n}\nfunction Navigator({\n initialRouteName,\n screenOptions,\n children,\n router\n}) {\n const contextKey = useContextKey();\n const {\n screens,\n children: otherSlot,\n protectedScreens,\n guardedRedirects\n } = useFilterScreenChildren(children, {\n isCustomNavigator: true,\n contextKey\n });\n const resolvedGuardedRedirects = useResolvedGuardedRedirects(guardedRedirects);\n registerProtectedRoutes(contextKey, resolvedGuardedRedirects);\n const currentPathname = routeInfo?.pathname;\n const protectedHref = currentPathname ? resolveProtectedHref(currentPathname) : currentPathname;\n React.useEffect(() => {\n registerProtectedRoutes(contextKey, resolvedGuardedRedirects);\n return () => {\n unregisterProtectedRoutes(contextKey);\n };\n }, [contextKey, resolvedGuardedRedirects]);\n React.useEffect(() => {\n if (currentPathname && protectedHref && protectedHref !== currentPathname) {\n replace(protectedHref);\n }\n }, [currentPathname, protectedHref]);\n const sorted = useSortedScreens(screens ?? [], { protectedScreens });\n if (!sorted.length) {\n console.warn(`Navigator at \"${contextKey}\" has no children.`);\n return null;\n }\n return /* @__PURE__ */ jsx(\n QualifiedNavigator,\n {\n initialRouteName,\n screenOptions,\n screens: sorted,\n contextKey,\n router,\n children: otherSlot\n }\n );\n}\nfunction QualifiedNavigator({\n initialRouteName,\n screenOptions,\n children,\n screens,\n contextKey,\n router = StackRouter\n}) {\n const parentRoute = React.useContext(NavigationRouteContext);\n const screenFromParent = parentRoute && typeof parentRoute.params === \"object\" && parentRoute.params !== null && typeof parentRoute.params.screen === \"string\" ? parentRoute.params.screen : void 0;\n const resolvedInitialRouteName = React.useMemo(() => {\n if (initialRouteName) return initialRouteName;\n if (screenFromParent) {\n const hasScreen2 = screens.some((s) => s?.props?.name === screenFromParent);\n if (hasScreen2) return screenFromParent;\n }\n const browserPath = lastIntendedPathname ?? initialPathname ?? getSafeWindowPathname();\n if (!browserPath) return void 0;\n const resolved = resolveInitialRouteFromLinking(contextKey, browserPath);\n if (!resolved) return void 0;\n const hasScreen = screens.some((s) => s?.props?.name === resolved);\n return hasScreen ? resolved : void 0;\n }, [initialRouteName, screens, contextKey, screenFromParent]);\n const { state, navigation, descriptors, render } = useNavigationBuilder(router, {\n children: screens,\n screenOptions,\n initialRouteName: resolvedInitialRouteName\n });\n const descriptorsRef = React.useRef(descriptors);\n descriptorsRef.current = descriptors;\n const value = React.useMemo(() => {\n return {\n contextKey,\n state,\n navigation,\n descriptorsRef,\n router\n };\n }, [contextKey, state, navigation, router]);\n return /* @__PURE__ */ jsx(NavigatorContext.Provider, { value, children: render(children) });\n}\nfunction useNavigatorContext() {\n const context = React.useContext(NavigatorContext);\n if (!context) {\n throw new Error(\"useNavigatorContext must be used within a <Navigator />\");\n }\n return context;\n}\nfunction useSlot() {\n const context = useNavigatorContext();\n const { state, descriptorsRef } = context;\n const notFoundState = useNotFoundState();\n if (notFoundState) {\n const notFoundRouteNode = notFoundState.notFoundRouteNode || findRouteNodeByPath(notFoundState.notFoundPath, globalRouteNode) || findNearestNotFoundRoute(notFoundState.originalPath, globalRouteNode);\n if (notFoundRouteNode) {\n const NotFoundComponent = getQualifiedRouteComponent(notFoundRouteNode);\n return /* @__PURE__ */ jsx(NotFoundComponent, { route: { params: {} } }, \"one-not-found-inline\");\n }\n return null;\n }\n if (!state.routes) {\n return null;\n }\n const current = state.routes[state.index];\n if (!current) {\n return null;\n }\n const renderedElement = descriptorsRef.current[current.key]?.render() ?? null;\n if (renderedElement !== null) {\n return React.cloneElement(renderedElement, {\n key: `${SLOT_STATIC_KEY}-${current.name}`\n });\n }\n return renderedElement;\n}\nconst Slot = React.memo(function Slot2(props) {\n const contextKey = useContextKey();\n const context = React.useContext(NavigatorContext);\n if (context?.contextKey !== contextKey) {\n return /* @__PURE__ */ jsx(Navigator, { ...props, children: /* @__PURE__ */ jsx(QualifiedSlot, {}) });\n }\n return /* @__PURE__ */ jsx(QualifiedSlot, {});\n});\nfunction QualifiedSlot() {\n return useSlot();\n}\nfunction DefaultNavigator() {\n return /* @__PURE__ */ jsx(SafeAreaView, { style: { flex: 1 }, children: /* @__PURE__ */ jsx(Navigator, { children: /* @__PURE__ */ jsx(QualifiedSlot, {}) }) });\n}\nNavigator.Slot = Slot;\nNavigator.useContext = useNavigatorContext;\nNavigator.Screen = Screen;\nfunction getScopedSlotKey(slotName, layoutContextKey) {\n if (!layoutContextKey) return slotName;\n return `${layoutContextKey}:${slotName}`;\n}\nfunction useNamedSlot(slotName, layoutContextKey) {\n useSlotStateSubscription();\n const scopedKey = getScopedSlotKey(slotName, layoutContextKey);\n const slotState = getSlotState(scopedKey);\n if (!slotState?.activeRouteKey || !slotState.isIntercepted) {\n return null;\n }\n if (slotState.activeRouteNode) {\n const Component = getQualifiedRouteComponent(slotState.activeRouteNode);\n return /* @__PURE__ */ jsx(\n Component,\n {\n route: { params: slotState.params || {} }\n },\n slotState.activeRouteKey\n );\n }\n return null;\n}\nfunction NamedSlot({\n name,\n layoutContextKey,\n children\n}) {\n const slotContent = useNamedSlot(name, layoutContextKey);\n if (slotContent) {\n return /* @__PURE__ */ jsx(Fragment, { children: slotContent });\n }\n return /* @__PURE__ */ jsx(Fragment, { children });\n}\nexport {\n DefaultNavigator,\n NamedSlot,\n Navigator,\n NavigatorContext,\n QualifiedSlot,\n Slot,\n clearAllSlotStates,\n getScopedSlotKey,\n getSlotState,\n setSlotState,\n useNamedSlot,\n useNavigatorContext,\n useSlot\n};\n//# sourceMappingURL=Navigator.js.map\n"],"mappings":";;;;;;;;;;;;;;;;AAkCA,MAAM,kBAAkB;AACxB,SAAS,+BAA+B,YAAY,aAAa;CAC/D,MAAM,UAAU,mBAAmB;CACnC,IAAI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