one
Version:
One is a new React Framework that makes Vite serve both native and web.
60 lines (59 loc) • 2.68 kB
JavaScript
import React from "react";
import { useContextKey } from "../router/Route.mjs";
import { useSortedScreens } from "../router/useScreens.mjs";
import { withStaticProperties } from "../utils/withStaticProperties.mjs";
import { Screen } from "../views/Screen.mjs";
import { jsx } from "react/jsx-runtime";
function useFilterScreenChildren(children, {
isCustomNavigator,
contextKey
} = {}) {
return React.useMemo(() => {
const customChildren = [],
screens = React.Children.map(children, child => {
if (React.isValidElement(child) && child && child.type === Screen) {
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" && ["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`);
return child.props;
}
return isCustomNavigator ? customChildren.push(child) : 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"`), null;
})?.filter(Boolean);
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
};
}, [children, contextKey, isCustomNavigator]);
}
function withLayoutContext(Nav, processor, options) {
return withStaticProperties(React.forwardRef((propsIn, ref) => {
const {
children,
...props
} = propsIn,
contextKey = useContextKey(),
{
screens
} = useFilterScreenChildren(children, {
contextKey
}),
processed = processor ? processor(screens ?? []) : screens,
sorted = useSortedScreens(processed ?? [], {
onlyMatching: !0
});
return sorted.length ? /* @__PURE__ */jsx(Nav, {
...options?.props,
...props,
id: contextKey,
ref,
children: sorted
}) : null;
}), {
Screen
});
}
export { useFilterScreenChildren, withLayoutContext };
//# sourceMappingURL=withLayoutContext.mjs.map