one
Version:
One is a React framework for web, iOS, and Android apps.
1 lines • 9.19 kB
Source Map (JSON)
{"version":3,"file":"withLayoutContext.js","names":[],"sources":["esm/layouts/withLayoutContext.js"],"sourcesContent":["import React from \"react\";\nimport { getContextKey, stripInvisibleSegmentsFromPath } from \"../router/matchers\";\nimport { useContextKey, useRouteNode } from \"../router/Route\";\nimport {\n registerProtectedRoutes,\n replace,\n resolveProtectedHref,\n routeInfo,\n unregisterProtectedRoutes\n} from \"../router/router\";\nimport { useSortedScreens } from \"../router/useScreens\";\nimport { sortRoutesWithInitial } from \"../router/sortRoutes\";\nimport { withStaticProperties } from \"../utils/withStaticProperties\";\nimport { isProtectedElement } from \"../views/Protected\";\nimport { Screen } from \"../views/Screen\";\nimport { StackScreen } from \"./stack-utils/StackScreen\";\nimport { IsWithinNativeNavigator } from \"./NativeNavigatorContext\";\nimport { jsx } from \"react/jsx-runtime\";\nfunction useFilterScreenChildren(children, {\n isCustomNavigator,\n contextKey\n} = {}) {\n return React.useMemo(() => {\n const customChildren = [];\n const screens = [];\n const protectedScreens = /* @__PURE__ */ new Set();\n const guardedRedirects = /* @__PURE__ */ new Map();\n function flattenChild(child, exclude = false, redirectTo) {\n if (React.isValidElement(child) && (child.type === Screen || child.type === StackScreen)) {\n if (typeof child.props === \"object\" && child.props && \"name\" in child.props && !child.props.name) {\n throw new Error(\n `<Screen /> component in \\`default export\\` at \\`app${contextKey}/_layout\\` must have a \\`name\\` prop when used as a child of a Layout Route.`\n );\n }\n if (process.env.NODE_ENV !== \"production\") {\n if ([\"children\", \"component\", \"getComponent\"].some(\n (key) => child.props && typeof child.props === \"object\" && key in child.props\n )) {\n throw new Error(\n `<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`\n );\n }\n }\n const screenProps = child.props;\n if (exclude && screenProps.name) {\n protectedScreens.add(screenProps.name);\n guardedRedirects.set(screenProps.name, redirectTo);\n } else {\n screens.push(screenProps);\n }\n return;\n }\n if (isProtectedElement(child)) {\n const guardFails = !child.props.guard;\n const excludeChildren = exclude || guardFails;\n const childRedirectTo = guardFails ? child.props.redirectTo : redirectTo;\n React.Children.forEach(child.props.children, (nested) => {\n flattenChild(nested, excludeChildren, childRedirectTo);\n });\n return;\n }\n if (isCustomNavigator) {\n customChildren.push(child);\n } else if (child != null && process.env.NODE_ENV === \"development\") {\n console.warn(\n `[one] a non-<Screen> child of the navigator at \"app${contextKey}/_layout\" renders on web only. On native this navigator is drawn by react-navigation and the child is ignored.`\n );\n }\n }\n React.Children.forEach(children, (child) => flattenChild(child));\n if (process.env.NODE_ENV !== \"production\") {\n const names = screens.map((screen) => screen.name);\n if (names && new Set(names).size !== names.length) {\n throw new Error(\"Screen names must be unique: \" + names);\n }\n }\n return {\n screens,\n children: customChildren,\n protectedScreens,\n guardedRedirects\n };\n }, [children, contextKey, isCustomNavigator]);\n}\nfunction useResolvedGuardedRedirects(guardedRedirects) {\n const node = useRouteNode();\n return React.useMemo(() => {\n if (!node) {\n return guardedRedirects;\n }\n const defaultRoute = [...node.children].sort(sortRoutesWithInitial(node.initialRouteName)).find((child) => {\n const normalized = child.route.replace(/\\/index$/, \"\");\n return !(guardedRedirects.has(child.route) || guardedRedirects.has(normalized));\n });\n const defaultHref = defaultRoute ? stripInvisibleSegmentsFromPath(getContextKey(defaultRoute.contextKey)) || \"/\" : void 0;\n return new Map(\n Array.from(guardedRedirects, ([name, redirectTo]) => [\n name,\n redirectTo ?? defaultHref\n ])\n );\n }, [guardedRedirects, node]);\n}\nfunction withLayoutContext(Nav, processor, options) {\n return withStaticProperties(\n React.forwardRef(\n (propsIn, ref) => {\n const { children, ...props } = propsIn;\n const contextKey = useContextKey();\n const { screens, protectedScreens, guardedRedirects } = useFilterScreenChildren(\n children,\n { contextKey }\n );\n const resolvedGuardedRedirects = useResolvedGuardedRedirects(guardedRedirects);\n registerProtectedRoutes(contextKey, resolvedGuardedRedirects);\n const currentPathname = routeInfo?.pathname;\n const protectedHref = currentPathname ? resolveProtectedHref(currentPathname) : currentPathname;\n React.useEffect(() => {\n registerProtectedRoutes(contextKey, resolvedGuardedRedirects);\n return () => {\n unregisterProtectedRoutes(contextKey);\n };\n }, [contextKey, resolvedGuardedRedirects]);\n React.useEffect(() => {\n if (currentPathname && protectedHref && protectedHref !== currentPathname) {\n replace(protectedHref);\n }\n }, [currentPathname, protectedHref]);\n const processed = processor ? processor(screens ?? []) : screens;\n const sorted = useSortedScreens(processed ?? [], {\n remaining: options?.undeclaredRoutes ? \"authored\" : \"none\",\n protectedScreens\n });\n if (!sorted.length) {\n return null;\n }\n return /* @__PURE__ */ jsx(IsWithinNativeNavigator.Provider, { value: true, children: /* @__PURE__ */ jsx(Nav, { ...options?.props, ...props, ref, children: sorted }) });\n }\n ),\n {\n Screen\n }\n );\n}\nexport {\n useFilterScreenChildren,\n useResolvedGuardedRedirects,\n withLayoutContext\n};\n//# sourceMappingURL=withLayoutContext.js.map\n"],"mappings":";;;;;;;;;;;;;;AAkBA,SAAS,wBAAwB,UAAU,EACzC,mBACA,eACE,CAAC,GAAG;CACN,OAAO,MAAM,cAAc;EACzB,MAAM,iBAAiB,CAAC;EACxB,MAAM,UAAU,CAAC;EACjB,MAAM,mCAAmC,IAAI,IAAI;EACjD,MAAM,mCAAmC,IAAI,IAAI;EACjD,SAAS,aAAa,OAAO,UAAU,OAAO,YAAY;GACxD,IAAI,MAAM,eAAe,KAAK,MAAM,MAAM,SAAS,UAAU,MAAM,SAAS,cAAc;IACxF,IAAI,OAAO,MAAM,UAAU,YAAY,MAAM,SAAS,UAAU,MAAM,SAAS,CAAC,MAAM,MAAM,MAAM;KAChG,MAAM,IAAI,MACR,sDAAsD,WAAW,6EACnE;IACF;IACA,IAAI,QAAQ,IAAI,aAAa,cAAc;KACzC,IAAI;MAAC;MAAY;MAAa;KAAc,CAAC,CAAC,MAC3C,QAAQ,MAAM,SAAS,OAAO,MAAM,UAAU,YAAY,OAAO,MAA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