one
Version:
One is a new React Framework that makes Vite serve both native and web.
171 lines • 7.68 kB
JavaScript
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 withLayoutContext_exports = {};
__export(withLayoutContext_exports, {
useFilterScreenChildren: () => useFilterScreenChildren,
useResolvedGuardedRedirects: () => useResolvedGuardedRedirects,
withLayoutContext: () => withLayoutContext
});
module.exports = __toCommonJS(withLayoutContext_exports);
var import_react = __toESM(require("react"), 1);
var import_matchers = require("../router/matchers.cjs");
var import_Route = require("../router/Route.cjs");
var import_router = require("../router/router.cjs");
var import_useScreens = require("../router/useScreens.cjs");
var import_sortRoutes = require("../router/sortRoutes.cjs");
var import_withStaticProperties = require("../utils/withStaticProperties.cjs");
var import_Protected = require("../views/Protected.cjs");
var import_Screen = require("../views/Screen.cjs");
var import_StackScreen = require("./stack-utils/StackScreen.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function useFilterScreenChildren(children, {
isCustomNavigator,
contextKey
} = {}) {
return import_react.default.useMemo(() => {
const customChildren = [];
const screens = [];
const protectedScreens = /* @__PURE__ */new Set();
const guardedRedirects = /* @__PURE__ */new Map();
function flattenChild(child, exclude = false, redirectTo) {
if (import_react.default.isValidElement(child) && (child.type === import_Screen.Screen || child.type === import_StackScreen.StackScreen)) {
if (typeof child.props === "object" && child.props && "name" in child.props && !child.props.name) {
throw new Error(`<Screen /> component in \`default export\` at \`app${contextKey}/_layout\` must have a \`name\` prop when used as a child of a Layout Route.`);
}
if (process.env.NODE_ENV !== "production") {
if (["children", "component", "getComponent"].some(key => child.props && typeof child.props === "object" && key in child.props)) {
throw new Error(`<Screen /> component in \`default export\` at \`app${contextKey}/_layout\` must not have a \`children\`, \`component\`, or \`getComponent\` prop when used as a child of a Layout Route`);
}
}
const screenProps = child.props;
if (exclude && screenProps.name) {
protectedScreens.add(screenProps.name);
guardedRedirects.set(screenProps.name, redirectTo);
} else {
screens.push(screenProps);
}
return;
}
if ((0, import_Protected.isProtectedElement)(child)) {
const guardFails = !child.props.guard;
const excludeChildren = exclude || guardFails;
const childRedirectTo = guardFails ? child.props.redirectTo : redirectTo;
import_react.default.Children.forEach(child.props.children, nested => {
flattenChild(nested, excludeChildren, childRedirectTo);
});
return;
}
if (isCustomNavigator) {
customChildren.push(child);
} else if (child != null && process.env.NODE_ENV === "development") {
console.warn(`[one] a non-<Screen> child of the navigator at "app${contextKey}/_layout" renders on web only. On native this navigator is drawn by react-navigation and the child is ignored.`);
}
}
import_react.default.Children.forEach(children, child => flattenChild(child));
if (process.env.NODE_ENV !== "production") {
const names = screens.map(screen => screen.name);
if (names && new Set(names).size !== names.length) {
throw new Error("Screen names must be unique: " + names);
}
}
return {
screens,
children: customChildren,
protectedScreens,
guardedRedirects
};
}, [children, contextKey, isCustomNavigator]);
}
function useResolvedGuardedRedirects(guardedRedirects) {
const node = (0, import_Route.useRouteNode)();
return import_react.default.useMemo(() => {
if (!node) {
return guardedRedirects;
}
const defaultRoute = [...node.children].sort((0, import_sortRoutes.sortRoutesWithInitial)(node.initialRouteName)).find(child => {
const normalized = child.route.replace(/\/index$/, "");
return !(guardedRedirects.has(child.route) || guardedRedirects.has(normalized));
});
const defaultHref = defaultRoute ? (0, import_matchers.stripInvisibleSegmentsFromPath)((0, import_matchers.getContextKey)(defaultRoute.contextKey)) || "/" : void 0;
return new Map(Array.from(guardedRedirects, ([name, redirectTo]) => [name, redirectTo ?? defaultHref]));
}, [guardedRedirects, node]);
}
function withLayoutContext(Nav, processor, options) {
return (0, import_withStaticProperties.withStaticProperties)(import_react.default.forwardRef((propsIn, ref) => {
const {
children,
...props
} = propsIn;
const contextKey = (0, import_Route.useContextKey)();
const {
screens,
protectedScreens,
guardedRedirects
} = useFilterScreenChildren(children, {
contextKey
});
const resolvedGuardedRedirects = useResolvedGuardedRedirects(guardedRedirects);
(0, import_router.registerProtectedRoutes)(contextKey, resolvedGuardedRedirects);
const currentPathname = import_router.routeInfo?.pathname;
const protectedHref = currentPathname ? (0, import_router.resolveProtectedHref)(currentPathname) : currentPathname;
import_react.default.useEffect(() => {
(0, import_router.registerProtectedRoutes)(contextKey, resolvedGuardedRedirects);
return () => {
(0, import_router.unregisterProtectedRoutes)(contextKey);
};
}, [contextKey, resolvedGuardedRedirects]);
import_react.default.useEffect(() => {
if (currentPathname && protectedHref && protectedHref !== currentPathname) {
(0, import_router.replace)(protectedHref);
}
}, [currentPathname, protectedHref]);
const processed = processor ? processor(screens ?? []) : screens;
const sorted = (0, import_useScreens.useSortedScreens)(processed ?? [], {
onlyMatching: true,
protectedScreens
});
if (!sorted.length) {
return null;
}
return /* @__PURE__ */(0, import_jsx_runtime.jsx)(Nav, {
...options?.props,
...props,
id: contextKey,
ref,
children: sorted
});
}), {
Screen: import_Screen.Screen
});
}