UNPKG

one

Version:

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

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