UNPKG

one

Version:

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

59 lines (58 loc) 2.54 kB
import React from "react"; import { useContextKey } from "../router/Route"; import { useSortedScreens } from "../router/useScreens"; import { Screen } from "../views/Screen"; import { withStaticProperties } from "../utils/withStaticProperties"; 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 (!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) => 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"` ); })?.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.js.map