one
Version:
One is a new React Framework that makes Vite serve both native and web.
192 lines (191 loc) • 6.46 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 { useContextKey } from "../router/Route.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",
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(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 () {
return slotStateListeners.add(forceUpdate), function () {
slotStateListeners.delete(forceUpdate);
};
}, []);
}
var NavigatorContext = /* @__PURE__ */React.createContext(null);
process.env.NODE_ENV !== "production" && (NavigatorContext.displayName = "NavigatorContext");
function Navigator(param) {
var {
initialRouteName,
screenOptions,
children,
router
} = param,
contextKey = useContextKey(),
{
screens,
children: otherSlot,
protectedScreens
} = useFilterScreenChildren(children, {
isCustomNavigator: !0,
contextKey
});
registerProtectedRoutes(contextKey, protectedScreens), React.useEffect(function () {
return registerProtectedRoutes(contextKey, protectedScreens), function () {
unregisterProtectedRoutes(contextKey);
};
}, [contextKey, protectedScreens]);
var sorted = useSortedScreens(screens ?? [], {
protectedScreens
});
return sorted.length ? /* @__PURE__ */_jsx(QualifiedNavigator, {
initialRouteName,
screenOptions,
screens: sorted,
contextKey,
router,
children: otherSlot
}) : (console.warn(`Navigator at "${contextKey}" has no children.`), null);
}
function QualifiedNavigator(param) {
var {
initialRouteName,
screenOptions,
children,
screens,
contextKey,
router = StackRouter
} = param,
{
state,
navigation,
descriptors,
NavigationContent
} = 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;
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,
_descriptorsRef_current_current_key,
context = useNavigatorContext(),
{
state,
descriptorsRef
} = context,
current = state.routes.find(function (route, i) {
return state.index === i;
});
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;
return renderedElement !== null ? /* @__PURE__ */React.cloneElement(renderedElement, {
key: SLOT_STATIC_KEY
}) : renderedElement;
}
var Slot = /* @__PURE__ */React.memo(function (props) {
var contextKey = useContextKey(),
context = React.useContext(NavigatorContext);
return context?.contextKey !== contextKey ? /* @__PURE__ */_jsx(Navigator, {
...props,
children: /* @__PURE__ */_jsx(QualifiedSlot, {})
}) : /* @__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) {
return layoutContextKey ? `${layoutContextKey}:${slotName}` : slotName;
}
function useNamedSlot(slotName, layoutContextKey) {
useSlotStateSubscription();
var scopedKey = getScopedSlotKey(slotName, layoutContextKey),
slotState = getSlotState(scopedKey);
if (!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,
slotContent = useNamedSlot(name, layoutContextKey);
return slotContent ? /* @__PURE__ */_jsx(_Fragment, {
children: slotContent
}) : /* @__PURE__ */_jsx(_Fragment, {
children
});
}
export { DefaultNavigator, NamedSlot, Navigator, NavigatorContext, QualifiedSlot, Slot, clearAllSlotStates, getScopedSlotKey, getSlotState, setSlotState, useNamedSlot, useNavigatorContext, useSlot };
//# sourceMappingURL=Navigator.native.js.map