one
Version:
One is a new React Framework that makes Vite serve both native and web.
183 lines (182 loc) • 8.7 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf, __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: !0 });
}, __copyProps = (to, from, except, desc) => {
if (from && typeof from == "object" || typeof from == "function")
for (let key of __getOwnPropNames(from))
!__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: !0 }) : target,
mod
)), __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: !0 }), 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_native = require("@react-navigation/native"), React = __toESM(require("react"), 1), import_react_native_safe_area_context = require("react-native-safe-area-context"), import_withLayoutContext = require("../layouts/withLayoutContext"), import_notFoundState = require("../notFoundState"), import_Route = require("../router/Route"), import_router = require("../router/router"), import_router2 = require("../router/router"), import_useScreens = require("../router/useScreens"), import_Screen = require("./Screen"), import_interceptRoutes = require("../router/interceptRoutes"), import_jsx_runtime = require("react/jsx-runtime");
const SLOT_STATIC_KEY = "one-slot-static-key", globalSlotState = /* @__PURE__ */ new Map(), slotStateListeners = /* @__PURE__ */ new Set();
function getSlotState(slotName) {
return globalSlotState.get(slotName);
}
function setSlotState(slotName, state) {
state === null ? globalSlotState.delete(slotName) : globalSlotState.set(slotName, state), slotStateListeners.forEach((listener) => listener());
}
function clearAllSlotStates() {
globalSlotState.clear(), slotStateListeners.forEach((listener) => listener());
}
(0, import_interceptRoutes.registerClearSlotStates)(clearAllSlotStates);
(0, import_interceptRoutes.registerSetSlotState)(setSlotState);
function useSlotStateSubscription() {
const [, forceUpdate] = React.useReducer((x) => x + 1, 0);
React.useEffect(() => (slotStateListeners.add(forceUpdate), () => {
slotStateListeners.delete(forceUpdate);
}), []);
}
const NavigatorContext = React.createContext(null);
process.env.NODE_ENV !== "production" && (NavigatorContext.displayName = "NavigatorContext");
function Navigator({
initialRouteName,
screenOptions,
children,
router
}) {
const contextKey = (0, import_Route.useContextKey)(), {
screens,
children: otherSlot,
protectedScreens
} = (0, import_withLayoutContext.useFilterScreenChildren)(children, {
isCustomNavigator: !0,
contextKey
});
(0, import_router2.registerProtectedRoutes)(contextKey, protectedScreens), React.useEffect(() => ((0, import_router2.registerProtectedRoutes)(contextKey, protectedScreens), () => {
(0, import_router2.unregisterProtectedRoutes)(contextKey);
}), [contextKey, protectedScreens]);
const sorted = (0, import_useScreens.useSortedScreens)(screens ?? [], { protectedScreens });
return sorted.length ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
QualifiedNavigator,
{
initialRouteName,
screenOptions,
screens: sorted,
contextKey,
router,
children: otherSlot
}
) : (console.warn(`Navigator at "${contextKey}" has no children.`), null);
}
function QualifiedNavigator({
initialRouteName,
screenOptions,
children,
screens,
contextKey,
router = import_native.StackRouter
}) {
const { 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
}
), descriptorsRef = React.useRef(descriptors);
descriptorsRef.current = descriptors;
const value = React.useMemo(() => ({
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() {
const context = React.useContext(NavigatorContext);
if (!context)
throw new Error("useNavigatorContext must be used within a <Navigator />");
return context;
}
function useSlot() {
const context = useNavigatorContext(), { state, descriptorsRef } = context, notFoundState = (0, import_notFoundState.useNotFoundState)();
if (notFoundState) {
const notFoundRouteNode = notFoundState.notFoundRouteNode || (0, import_notFoundState.findRouteNodeByPath)(notFoundState.notFoundPath, import_router.routeNode) || (0, import_notFoundState.findNearestNotFoundRoute)(notFoundState.originalPath, import_router.routeNode);
if (notFoundRouteNode) {
const NotFoundComponent = (0, import_useScreens.getQualifiedRouteComponent)(notFoundRouteNode);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(NotFoundComponent, { route: { params: {} } }, "one-not-found-inline");
}
return null;
}
const current = state.routes.find((route, i) => state.index === i);
if (!current)
return null;
const renderedElement = descriptorsRef.current[current.key]?.render() ?? null;
return renderedElement !== null ? React.cloneElement(renderedElement, { key: SLOT_STATIC_KEY }) : renderedElement;
}
const Slot = React.memo(function(props) {
const contextKey = (0, import_Route.useContextKey)();
return React.useContext(NavigatorContext)?.contextKey !== contextKey ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Navigator, { ...props, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(QualifiedSlot, {}) }) : /* @__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) {
return layoutContextKey ? `${layoutContextKey}:${slotName}` : slotName;
}
function useNamedSlot(slotName, layoutContextKey) {
useSlotStateSubscription();
const scopedKey = getScopedSlotKey(slotName, layoutContextKey), slotState = getSlotState(scopedKey);
if (!slotState?.activeRouteKey || !slotState.isIntercepted)
return null;
if (slotState.activeRouteNode) {
const 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({
name,
layoutContextKey,
children
}) {
const slotContent = useNamedSlot(name, layoutContextKey);
return slotContent ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: slotContent }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
}
//# sourceMappingURL=Navigator.js.map