UNPKG

one

Version:

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

344 lines (341 loc) 13.4 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Navigator_exports = {}; __export(Navigator_exports, { DefaultNavigator: () => DefaultNavigator, NamedSlot: () => NamedSlot, Navigator: () => Navigator, NavigatorContext: () => NavigatorContext, QualifiedSlot: () => QualifiedSlot, Slot: () => Slot, clearAllSlotStates: () => clearAllSlotStates, getScopedSlotKey: () => getScopedSlotKey, getSlotState: () => getSlotState, setSlotState: () => setSlotState, useNamedSlot: () => useNamedSlot, useNavigatorContext: () => useNavigatorContext, useSlot: () => useSlot }); module.exports = __toCommonJS(Navigator_exports); var import_jsx_runtime = require("react/jsx-runtime"); var import_native = require("@react-navigation/native"); var import_core = require("@react-navigation/core"); var React = __toESM(require("react"), 1); var import_react_native_safe_area_context = require("react-native-safe-area-context"); var import_withLayoutContext = require("../layouts/withLayoutContext.native.js"); var import_notFoundState = require("../notFoundState.native.js"); var import_Route = require("../router/Route.native.js"); var import_linkingConfig = require("../router/linkingConfig.native.js"); var import_router = require("../router/router.native.js"); var import_router2 = require("../router/router.native.js"); var import_useScreens = require("../router/useScreens.native.js"); var import_Screen = require("./Screen.native.js"); var import_interceptRoutes = require("../router/interceptRoutes.native.js"); function _type_of(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; } var SLOT_STATIC_KEY = "one-slot-static-key"; function resolveInitialRouteFromLinking(contextKey, browserPath) { var _current_index; var _current_routes, _current_routes_idx; var linking = (0, import_linkingConfig.getResolvedLinking)(); if (!(linking === null || linking === void 0 ? void 0 : linking.getStateFromPath)) return void 0; var current = linking.getStateFromPath(browserPath, linking.config); var segments = contextKey.split("/").filter(Boolean); var i = 0; while (i < segments.length) { var _current_index1; var _current_routes1; if (!(current === null || current === void 0 ? void 0 : (_current_routes1 = current.routes) === null || _current_routes1 === void 0 ? void 0 : _current_routes1.length)) return void 0; var idx = (_current_index1 = current.index) !== null && _current_index1 !== void 0 ? _current_index1 : current.routes.length - 1; var focused = current.routes[idx]; if (!(focused === null || focused === void 0 ? void 0 : focused.name) || !focused.state) return void 0; var nameSegments = focused.name.split("/").filter(Boolean); var expected = segments.slice(i, i + nameSegments.length).join("/"); if (focused.name !== expected) return void 0; current = focused.state; i += nameSegments.length; } if (!(current === null || current === void 0 ? void 0 : (_current_routes = current.routes) === null || _current_routes === void 0 ? void 0 : _current_routes.length)) return void 0; var idx1 = (_current_index = current.index) !== null && _current_index !== void 0 ? _current_index : current.routes.length - 1; return (_current_routes_idx = current.routes[idx1]) === null || _current_routes_idx === void 0 ? void 0 : _current_routes_idx.name; } 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(); }); } (0, import_interceptRoutes.registerClearSlotStates)(clearAllSlotStates); (0, import_interceptRoutes.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 = (0, import_Route.useContextKey)(); var { screens, children: otherSlot, protectedScreens } = (0, import_withLayoutContext.useFilterScreenChildren)(children, { isCustomNavigator: true, contextKey }); (0, import_router2.registerProtectedRoutes)(contextKey, protectedScreens); React.useEffect(function () { (0, import_router2.registerProtectedRoutes)(contextKey, protectedScreens); return function () { (0, import_router2.unregisterProtectedRoutes)(contextKey); }; }, [contextKey, protectedScreens]); var sorted = (0, import_useScreens.useSortedScreens)(screens !== null && screens !== void 0 ? screens : [], { protectedScreens }); if (!sorted.length) { console.warn(`Navigator at "${contextKey}" has no children.`); return null; } return /* @__PURE__ */(0, import_jsx_runtime.jsx)(QualifiedNavigator, { initialRouteName, screenOptions, screens: sorted, contextKey, router, children: otherSlot }); } function QualifiedNavigator(param) { var { initialRouteName, screenOptions, children, screens, contextKey, router = import_native.StackRouter } = param; var parentRoute = React.useContext(import_core.NavigationRouteContext); var screenFromParent = parentRoute && _type_of(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 = import_router.lastIntendedPathname !== null && import_router.lastIntendedPathname !== void 0 ? import_router.lastIntendedPathname : import_router.initialPathname) !== null && _ref !== void 0 ? _ref : (0, import_router.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, NavigationContent } = (0, import_native.useNavigationBuilder)(router, { // Used for getting the parent with navigation.getParent('/normalized/path') id: contextKey, 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__ */(0, import_jsx_runtime.jsx)(NavigatorContext.Provider, { value, children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(NavigationContent, { 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 = (0, import_notFoundState.useNotFoundState)(); if (notFoundState) { var notFoundRouteNode = notFoundState.notFoundRouteNode || (0, import_notFoundState.findRouteNodeByPath)(notFoundState.notFoundPath, import_router.routeNode) || (0, import_notFoundState.findNearestNotFoundRoute)(notFoundState.originalPath, import_router.routeNode); if (notFoundRouteNode) { var NotFoundComponent = (0, import_useScreens.getQualifiedRouteComponent)(notFoundRouteNode); return /* @__PURE__ */(0, import_jsx_runtime.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 = (0, import_Route.useContextKey)(); var context = React.useContext(NavigatorContext); if ((context === null || context === void 0 ? void 0 : context.contextKey) !== contextKey) { return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Navigator, { ...props, children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(QualifiedSlot, {}) }); } return /* @__PURE__ */(0, import_jsx_runtime.jsx)(QualifiedSlot, {}); }); function QualifiedSlot() { return useSlot(); } function DefaultNavigator() { return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_react_native_safe_area_context.SafeAreaView, { style: { flex: 1 }, children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(Navigator, { children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(QualifiedSlot, {}) }) }); } Navigator.Slot = Slot; Navigator.useContext = useNavigatorContext; Navigator.Screen = import_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 = (0, import_useScreens.getQualifiedRouteComponent)(slotState.activeRouteNode); return /* @__PURE__ */(0, import_jsx_runtime.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__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: slotContent }); } return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children }); } //# sourceMappingURL=Navigator.native.js.map