one
Version:
One is a new React Framework that makes Vite serve both native and web.
95 lines (93 loc) • 4.19 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import React from "react";
import { useContextKey } from "../router/Route.native.js";
import { registerProtectedRoutes, unregisterProtectedRoutes } from "../router/router.native.js";
import { useSortedScreens } from "../router/useScreens.native.js";
import { withStaticProperties } from "../utils/withStaticProperties.native.js";
import { isProtectedElement } from "../views/Protected.native.js";
import { Screen } from "../views/Screen.native.js";
function _type_of(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol < "u" && obj.constructor === Symbol ? "symbol" : typeof obj;
}
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 customChildren = [],
screens = [],
protectedScreens = /* @__PURE__ */new Set();
function flattenChild(child) {
var exclude = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : !1;
if (/* @__PURE__ */React.isValidElement(child) && child.type === Screen) {
if (_type_of(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(function (key) {
return child.props && _type_of(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`);
var screenProps = child.props;
exclude && screenProps.name ? protectedScreens.add(screenProps.name) : screens.push(screenProps);
return;
}
if (isProtectedElement(child)) {
var excludeChildren = exclude || !child.props.guard;
React.Children.forEach(child.props.children, function (nested) {
flattenChild(nested, excludeChildren);
});
return;
}
isCustomNavigator ? customChildren.push(child) : 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"`);
}
if (React.Children.forEach(children, function (child) {
return flattenChild(child);
}), 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,
protectedScreens
};
}, [children, contextKey, isCustomNavigator]);
}
function withLayoutContext(Nav, processor, options) {
return withStaticProperties(/* @__PURE__ */React.forwardRef(function (propsIn, ref) {
var {
children,
...props
} = propsIn,
contextKey = useContextKey(),
{
screens,
protectedScreens
} = useFilterScreenChildren(children, {
contextKey
});
registerProtectedRoutes(contextKey, protectedScreens), React.useEffect(function () {
return registerProtectedRoutes(contextKey, protectedScreens), function () {
unregisterProtectedRoutes(contextKey);
};
}, [contextKey, protectedScreens]);
var processed = processor ? processor(screens ?? []) : screens,
sorted = useSortedScreens(processed ?? [], {
onlyMatching: !0,
protectedScreens
});
return sorted.length ? /* @__PURE__ */_jsx(Nav, {
...options?.props,
...props,
id: contextKey,
ref,
children: sorted
}) : null;
}), {
Screen
});
}
export { useFilterScreenChildren, withLayoutContext };
//# sourceMappingURL=withLayoutContext.native.js.map