one
Version:
One is a new React Framework that makes Vite serve both native and web.
67 lines (66 loc) • 2.95 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import { useContextKey } from "../router/Route.native.js";
import { useSortedScreens } from "../router/useScreens.native.js";
import { Screen } from "../views/Screen.native.js";
import { withStaticProperties } from "../utils/withStaticProperties.native.js";
function useFilterScreenChildren(children) {
var {
isCustomNavigator,
contextKey
} = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : (/** Used for sending developer hints */
{});
return React.useMemo(function () {
var _React_Children_map,
customChildren = [],
screens = (_React_Children_map = React.Children.map(children, function (child) {
if (/* @__PURE__ */React.isValidElement(child) && child && child.type === Screen) {
if (!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(function (key) {
return 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;
}
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 || _React_Children_map === void 0 ? void 0 : _React_Children_map.filter(Boolean);
if (process.env.NODE_ENV !== "production") {
var names = screens?.map(function (screen) {
return 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(/* @__PURE__ */React.forwardRef(function (propsIn, ref) {
var {
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.native.js.map