UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

79 lines (78 loc) 3.62 kB
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 { jsx } from "react/jsx-runtime"; function useFilterScreenChildren(children, { isCustomNavigator, contextKey } = {}) { return React.useMemo(() => { const customChildren = [], screens = [], protectedScreens = /* @__PURE__ */new Set(); function flattenChild(child, exclude = !1) { if (React.isValidElement(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`); const screenProps = child.props; exclude && screenProps.name ? protectedScreens.add(screenProps.name) : screens.push(screenProps); return; } if (isProtectedElement(child)) { const excludeChildren = exclude || !child.props.guard; React.Children.forEach(child.props.children, 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, child => flattenChild(child)), 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, contextKey = useContextKey(), { screens, protectedScreens } = useFilterScreenChildren(children, { contextKey }); registerProtectedRoutes(contextKey, protectedScreens), React.useEffect(() => (registerProtectedRoutes(contextKey, protectedScreens), () => { unregisterProtectedRoutes(contextKey); }), [contextKey, protectedScreens]); const 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.mjs.map