one
Version:
One is a new React Framework that makes Vite serve both native and web.
281 lines (280 loc) • 9.36 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
import { StackRouter, useNavigationBuilder } from "@react-navigation/native";
import * as React from "react";
import { SafeAreaView } from "react-native-safe-area-context";
import { useFilterScreenChildren } from "../layouts/withLayoutContext.native.js";
import { findNearestNotFoundRoute, findRouteNodeByPath, useNotFoundState } from "../notFoundState.native.js";
import { useContextKey } from "../router/Route.native.js";
import { matchRoutePattern, stripGroupSegmentsFromPath } from "../router/matchers.native.js";
import { routeNode as globalRouteNode, initialPathname } from "../router/router.native.js";
import { registerProtectedRoutes, unregisterProtectedRoutes } from "../router/router.native.js";
import { useSortedScreens, getQualifiedRouteComponent } 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";
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
} = useFilterScreenChildren(children, {
isCustomNavigator: true,
contextKey
});
registerProtectedRoutes(contextKey, protectedScreens);
React.useEffect(function () {
registerProtectedRoutes(contextKey, protectedScreens);
return function () {
unregisterProtectedRoutes(contextKey);
};
}, [contextKey, protectedScreens]);
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 resolvedInitialRouteName = React.useMemo(function () {
if (initialRouteName) return initialRouteName;
var browserPath = initialPathname !== null && initialPathname !== void 0 ? initialPathname : typeof window !== "undefined" ? window.location.pathname : void 0;
if (!browserPath) return void 0;
var layoutUrlPrefix = stripGroupSegmentsFromPath(contextKey).replace(/\/+$/, "");
var best;
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = void 0;
try {
for (var _iterator = screens[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var screen = _step.value;
var _screen_props;
var name = screen === null || screen === void 0 ? void 0 : (_screen_props = screen.props) === null || _screen_props === void 0 ? void 0 : _screen_props.name;
if (!name) continue;
var fullPattern = layoutUrlPrefix ? `${layoutUrlPrefix}/${name}` : name;
var match = matchRoutePattern(fullPattern, browserPath);
if (!match) continue;
if (!best || match.specificity > best.specificity) {
best = {
name,
specificity: match.specificity
};
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return best === null || best === void 0 ? void 0 : best.name;
}, [initialRouteName, screens, contextKey]);
var {
state,
navigation,
descriptors,
NavigationContent
} = 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__ */_jsx(NavigatorContext.Provider, {
value,
children: /* @__PURE__ */_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 = useNotFoundState();
if (notFoundState) {
var notFoundRouteNode = notFoundState.notFoundRouteNode || findRouteNodeByPath(notFoundState.notFoundPath, globalRouteNode) || findNearestNotFoundRoute(notFoundState.originalPath, globalRouteNode);
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