one
Version:
One is a new React Framework that makes Vite serve both native and web.
104 lines (103 loc) • 3.97 kB
JavaScript
import React from "react";
import { useContextKey } from "../router/Route.mjs";
import { registerProtectedRoutes, unregisterProtectedRoutes } from "../router/router.mjs";
import { useSortedScreens } from "../router/useScreens.mjs";
import { withStaticProperties } from "../utils/withStaticProperties.mjs";
import { isProtectedElement } from "../views/Protected.mjs";
import { Screen } from "../views/Screen.mjs";
import { StackScreen } from "./stack-utils/StackScreen.mjs";
import { jsx } from "react/jsx-runtime";
function useFilterScreenChildren(children, {
isCustomNavigator,
contextKey
} = {}) {
return React.useMemo(() => {
const customChildren = [];
const screens = [];
const protectedScreens = /* @__PURE__ */new Set();
function flattenChild(child, exclude = false) {
if (React.isValidElement(child) && (child.type === Screen || child.type === 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);
} else {
screens.push(screenProps);
}
return;
}
if (isProtectedElement(child)) {
const excludeChildren = exclude || !child.props.guard;
React.Children.forEach(child.props.children, nested => {
flattenChild(nested, excludeChildren);
});
return;
}
if (isCustomNavigator) {
customChildren.push(child);
} else if (child != null) {
console.warn(`Layout children must be of type Screen, all other children are ignored. To use custom children, create a custom <Layout />. Update Layout Route at: "app${contextKey}/_layout"`);
}
}
React.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
};
}, [children, contextKey, isCustomNavigator]);
}
function withLayoutContext(Nav, processor, options) {
return withStaticProperties(React.forwardRef((propsIn, ref) => {
const {
children,
...props
} = propsIn;
const contextKey = useContextKey();
const {
screens,
protectedScreens
} = useFilterScreenChildren(children, {
contextKey
});
registerProtectedRoutes(contextKey, protectedScreens);
React.useEffect(() => {
registerProtectedRoutes(contextKey, protectedScreens);
return () => {
unregisterProtectedRoutes(contextKey);
};
}, [contextKey, protectedScreens]);
const processed = processor ? processor(screens ?? []) : screens;
const sorted = useSortedScreens(processed ?? [], {
onlyMatching: true,
protectedScreens
});
if (!sorted.length) {
return null;
}
return /* @__PURE__ */jsx(Nav, {
...options?.props,
...props,
id: contextKey,
ref,
children: sorted
});
}), {
Screen
});
}
export { useFilterScreenChildren, withLayoutContext };
//# sourceMappingURL=withLayoutContext.mjs.map