expo-router 0.0.37 → 0.0.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Route.d.ts +5 -4
- package/build/Route.d.ts.map +1 -1
- package/build/Route.js +11 -4
- package/build/Route.js.map +1 -1
- package/build/getRoutes.d.ts +3 -5
- package/build/getRoutes.d.ts.map +1 -1
- package/build/getRoutes.js +9 -2
- package/build/getRoutes.js.map +1 -1
- package/build/useScreens.d.ts +1 -1
- package/build/useScreens.d.ts.map +1 -1
- package/build/useScreens.js +22 -16
- package/build/useScreens.js.map +1 -1
- package/package.json +1 -1
package/build/Route.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { ReactNode } from "react";
|
|
2
2
|
/** The list of input keys will become optional, everything else will remain the same. */
|
|
3
3
|
export declare type PickPartial<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
4
|
+
export declare type DynamicConvention = {
|
|
5
|
+
name: string;
|
|
6
|
+
deep: boolean;
|
|
7
|
+
};
|
|
4
8
|
export declare type RouteNode = {
|
|
5
9
|
/** Load a route into memory. Returns the exports from a route. */
|
|
6
10
|
loadRoute: () => any;
|
|
@@ -9,10 +13,7 @@ export declare type RouteNode = {
|
|
|
9
13
|
/** nested routes */
|
|
10
14
|
children: RouteNode[];
|
|
11
15
|
/** Is the route a dynamic path */
|
|
12
|
-
dynamic: null |
|
|
13
|
-
name: string;
|
|
14
|
-
deep: boolean;
|
|
15
|
-
};
|
|
16
|
+
dynamic: null | DynamicConvention[];
|
|
16
17
|
/** `index`, `error-boundary`, etc. */
|
|
17
18
|
route: string;
|
|
18
19
|
/** require.context key, used for matching children. */
|
package/build/Route.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Route.d.ts","sourceRoot":"","sources":["../src/Route.tsx"],"names":[],"mappings":"AAAA,OAAc,EAAE,SAAS,EAAc,MAAM,OAAO,CAAC;AAKr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,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAWF,+DAA+D;AAC/D,wBAAgB,YAAY,IAAI,SAAS,GAAG,IAAI,CAE/C;AAED,wBAAgB,aAAa,IAAI,MAAM,CAMtC;AAED,iEAAiE;AACjE,wBAAgB,KAAK,CAAC,EACpB,QAAQ,EACR,IAAI,GACL,EAAE;IACD,QAAQ,EAAE,SAAS,CAAC;IACpB,IAAI,EAAE,SAAS,CAAC;CACjB,eAkBA;AAED,wBAAgB,YAAY,IAAI,SAAS,GAAG,IAAI,CAE/C;AAED,wBAAgB,qBAAqB,CAAC,gBAAgB,CAAC,EAAE,MAAM,OAClD,SAAS,KAAK,SAAS,KAAG,MAAM,CAW5C;AAED,wBAAgB,UAAU,CAAC,CAAC,EAAE,SAAS,EAAE,CAAC,EAAE,SAAS,GAAG,MAAM,CAmC7D"}
|
package/build/Route.js
CHANGED
|
@@ -56,11 +56,18 @@ export function sortRoutes(a, b) {
|
|
|
56
56
|
return -1;
|
|
57
57
|
}
|
|
58
58
|
if (a.dynamic && b.dynamic) {
|
|
59
|
-
if (a.dynamic.
|
|
60
|
-
return
|
|
59
|
+
if (a.dynamic.length !== b.dynamic.length) {
|
|
60
|
+
return b.dynamic.length - a.dynamic.length;
|
|
61
61
|
}
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
for (let i = 0; i < a.dynamic.length; i++) {
|
|
63
|
+
const aDynamic = a.dynamic[i];
|
|
64
|
+
const bDynamic = b.dynamic[i];
|
|
65
|
+
if (aDynamic.deep && !bDynamic.deep) {
|
|
66
|
+
return 1;
|
|
67
|
+
}
|
|
68
|
+
if (!aDynamic.deep && bDynamic.deep) {
|
|
69
|
+
return -1;
|
|
70
|
+
}
|
|
64
71
|
}
|
|
65
72
|
return 0;
|
|
66
73
|
}
|
package/build/Route.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Route.js","sourceRoot":"","sources":["../src/Route.tsx"],"names":[],"mappings":"AAAA,OAA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KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE;YACzC,OAAO,CAAC,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC;SAC5C;QACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;YACzC,MAAM,QAAQ,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YAC9B,MAAM,QAAQ,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YAC9B,IAAI,QAAQ,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE;gBACnC,OAAO,CAAC,CAAC;aACV;YACD,IAAI,CAAC,QAAQ,CAAC,IAAI,IAAI,QAAQ,CAAC,IAAI,EAAE;gBACnC,OAAO,CAAC,CAAC,CAAC;aACX;SACF;QACD,OAAO,CAAC,CAAC;KACV;IAED,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,KAAK,OAAO,IAAI,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;IACzE,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,KAAK,OAAO,IAAI,iBAAiB,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;IAEzE,IAAI,MAAM,IAAI,CAAC,MAAM,EAAE;QACrB,OAAO,CAAC,CAAC,CAAC;KACX;IACD,IAAI,CAAC,MAAM,IAAI,MAAM,EAAE;QACrB,OAAO,CAAC,CAAC;KACV;IAED,OAAO,CAAC,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC;AACzC,CAAC","sourcesContent":["import React, { ReactNode, useContext } from \"react\";\n\nimport { RootRouteNodeContext } from \"./context\";\nimport { getNameFromFilePath, matchFragmentName } from \"./matchers\";\n\n/** The list of input keys will become optional, everything else will remain the same. */\nexport type PickPartial<T, K extends keyof T> = Omit<T, K> &\n Partial<Pick<T, K>>;\n\nexport type DynamicConvention = { name: string; deep: boolean };\n\nexport type RouteNode = {\n /** Load a route into memory. Returns the exports from a route. */\n loadRoute: () => any;\n\n /** Loaded initial route name. */\n initialRouteName?: string;\n /** nested routes */\n children: RouteNode[];\n /** Is the route a dynamic path */\n dynamic: null | DynamicConvention[];\n /** `index`, `error-boundary`, etc. */\n route: string;\n /** require.context key, used for matching children. */\n contextKey: string;\n /** Added in-memory */\n generated?: boolean;\n\n /** Internal screens like the directory or the auto 404 should be marked as internal. */\n internal?: boolean;\n};\n\nconst CurrentRoutePathContext = React.createContext<string | null>(null);\n\nconst CurrentRouteContext = React.createContext<RouteNode | null>(null);\n\nif (process.env.NODE_ENV !== \"production\") {\n CurrentRoutePathContext.displayName = \"RoutePath\";\n CurrentRouteContext.displayName = \"Route\";\n}\n\n/** Return the RouteNode at the current contextual boundary. */\nexport function useRouteNode(): RouteNode | null {\n return useContext(CurrentRouteContext);\n}\n\nexport function useContextKey(): string {\n const filename = useContext(CurrentRoutePathContext);\n if (filename == null) {\n throw new Error(\"No filename found. This is likely a bug in expo-router.\");\n }\n return filename;\n}\n\n/** Provides the matching routes and filename to the children. */\nexport function Route({\n children,\n node,\n}: {\n children: ReactNode;\n node: RouteNode;\n}) {\n const normalName = React.useMemo(() => {\n // The root path is `` (empty string) so always prepend `/` to ensure\n // there is some value.\n const normal = \"/\" + getNameFromFilePath(node.contextKey);\n if (!normal.endsWith(\"_layout\")) {\n return normal;\n }\n return normal.replace(/\\/?_layout$/, \"\");\n }, [node.contextKey]);\n\n return (\n <CurrentRoutePathContext.Provider value={normalName}>\n <CurrentRouteContext.Provider value={node}>\n {children}\n </CurrentRouteContext.Provider>\n </CurrentRoutePathContext.Provider>\n );\n}\n\nexport function useRootRoute(): RouteNode | null {\n return useContext(RootRouteNodeContext);\n}\n\nexport function sortRoutesWithInitial(initialRouteName?: string) {\n return (a: RouteNode, b: RouteNode): number => {\n if (initialRouteName) {\n if (a.route === initialRouteName) {\n return -1;\n }\n if (b.route === initialRouteName) {\n return 1;\n }\n }\n return sortRoutes(a, b);\n };\n}\n\nexport function sortRoutes(a: RouteNode, b: RouteNode): number {\n if (a.dynamic && !b.dynamic) {\n return 1;\n }\n if (!a.dynamic && b.dynamic) {\n return -1;\n }\n if (a.dynamic && b.dynamic) {\n if (a.dynamic.length !== b.dynamic.length) {\n return b.dynamic.length - a.dynamic.length;\n }\n for (let i = 0; i < a.dynamic.length; i++) {\n const aDynamic = a.dynamic[i];\n const bDynamic = b.dynamic[i];\n if (aDynamic.deep && !bDynamic.deep) {\n return 1;\n }\n if (!aDynamic.deep && bDynamic.deep) {\n return -1;\n }\n }\n return 0;\n }\n\n const aIndex = a.route === \"index\" || matchFragmentName(a.route) != null;\n const bIndex = b.route === \"index\" || matchFragmentName(b.route) != null;\n\n if (aIndex && !bIndex) {\n return -1;\n }\n if (!aIndex && bIndex) {\n return 1;\n }\n\n return a.route.length - b.route.length;\n}\n"]}
|
package/build/getRoutes.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RouteNode } from "./Route";
|
|
1
|
+
import { DynamicConvention, RouteNode } from "./Route";
|
|
2
2
|
import { RequireContext } from "./types";
|
|
3
3
|
export declare type FileNode = Pick<RouteNode, "contextKey" | "loadRoute"> & {
|
|
4
4
|
/** Like `(tab)/index` */
|
|
@@ -13,10 +13,8 @@ declare type TreeNode = {
|
|
|
13
13
|
};
|
|
14
14
|
/** Convert a flat map of file nodes into a nested tree of files. */
|
|
15
15
|
export declare function getRecursiveTree(files: FileNode[]): TreeNode;
|
|
16
|
-
export declare function
|
|
17
|
-
|
|
18
|
-
deep: boolean;
|
|
19
|
-
} | null;
|
|
16
|
+
export declare function generateDynamicFromSegment(name: string): DynamicConvention | null;
|
|
17
|
+
export declare function generateDynamic(name: string): RouteNode["dynamic"];
|
|
20
18
|
/** Given a Metro context module, return an array of nested routes. */
|
|
21
19
|
export declare function getRoutes(contextModule: RequireContext): RouteNode | null;
|
|
22
20
|
/**
|
package/build/getRoutes.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getRoutes.d.ts","sourceRoot":"","sources":["../src/getRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"getRoutes.d.ts","sourceRoot":"","sources":["../src/getRoutes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAQvD,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAGzC,oBAAY,QAAQ,GAAG,IAAI,CAAC,SAAS,EAAE,YAAY,GAAG,WAAW,CAAC,GAAG;IACnE,yBAAyB;IACzB,cAAc,EAAE,MAAM,CAAC;CACxB,CAAC;AAEF,aAAK,QAAQ,GAAG;IACd,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,QAAQ,EAAE,CAAC;IACrB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,8CAA8C;IAC9C,IAAI,EAAE,QAAQ,GAAG,IAAI,CAAC;CACvB,CAAC;AAEF,oEAAoE;AACpE,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,QAAQ,EAAE,GAAG,QAAQ,CA+C5D;AAsBD,wBAAgB,0BAA0B,CACxC,IAAI,EAAE,MAAM,GACX,iBAAiB,GAAG,IAAI,CAK1B;AAED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC,SAAS,CAAC,CAMlE;AA0LD,sEAAsE;AACtE,wBAAgB,SAAS,CAAC,aAAa,EAAE,cAAc,GAAG,SAAS,GAAG,IAAI,CAezE;AA4CD;;;GAGG;AACH,wBAAgB,8BAA8B,CAC5C,MAAM,EAAE,SAAS,GAChB,SAAS,GAAG,IAAI,CAgBlB"}
|
package/build/getRoutes.js
CHANGED
|
@@ -57,11 +57,18 @@ function assertDeprecatedFormat(tree) {
|
|
|
57
57
|
function getTreeNodesAsRouteNodes(nodes) {
|
|
58
58
|
return nodes.map(treeNodeToRouteNode).flat().filter(Boolean);
|
|
59
59
|
}
|
|
60
|
-
export function
|
|
60
|
+
export function generateDynamicFromSegment(name) {
|
|
61
61
|
const deepDynamicName = matchDeepDynamicRouteName(name);
|
|
62
62
|
const dynamicName = deepDynamicName ?? matchDynamicName(name);
|
|
63
63
|
return dynamicName ? { name: dynamicName, deep: !!deepDynamicName } : null;
|
|
64
64
|
}
|
|
65
|
+
export function generateDynamic(name) {
|
|
66
|
+
const description = name
|
|
67
|
+
.split("/")
|
|
68
|
+
.map((segment) => generateDynamicFromSegment(segment))
|
|
69
|
+
.filter(Boolean);
|
|
70
|
+
return description.length === 0 ? null : description;
|
|
71
|
+
}
|
|
65
72
|
function collapseRouteSegments(route) {
|
|
66
73
|
return stripFragmentSegmentsFromPath(route.replace(/\/index$/, ""));
|
|
67
74
|
}
|
|
@@ -250,7 +257,7 @@ function appendUnmatchedRoute(routes) {
|
|
|
250
257
|
},
|
|
251
258
|
route: "[...404]",
|
|
252
259
|
contextKey: "./[...404].tsx",
|
|
253
|
-
dynamic: { name: "404", deep: true },
|
|
260
|
+
dynamic: [{ name: "404", deep: true }],
|
|
254
261
|
children: [],
|
|
255
262
|
generated: true,
|
|
256
263
|
internal: true,
|
package/build/getRoutes.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getRoutes.js","sourceRoot":"","sources":["../src/getRoutes.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,yBAAyB,EACzB,gBAAgB,EAChB,iBAAiB,EACjB,6BAA6B,GAC9B,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAe/C,oEAAoE;AACpE,MAAM,UAAU,gBAAgB,CAAC,KAAiB;IAChD,MAAM,IAAI,GAAG;QACX,IAAI,EAAE,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MAAiB;IAEjB,+CAA+C;IAC/C,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,EAAE,EAAE;QACzC,MAAM,aAAa,GAAG,yBAAyB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC7D,IAAI,aAAa,EAAE;YACjB,OAAO,KAAK,CAAC;SACd;QACD,kCAAkC;QAClC,IAAI,iBAAiB,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;YAClC,MAAM,KAAK,GAAG,8BAA8B,CAAC,KAAK,CAAC,CAAC;YACpD,IAAI,KAAK,EAAE;gBACT,OAAO,KAAK,CAAC;aACd;SACF;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { RouteNode } from \"./Route\";\nimport {\n getNameFromFilePath,\n matchDeepDynamicRouteName,\n matchDynamicName,\n matchFragmentName,\n stripFragmentSegmentsFromPath,\n} from \"./matchers\";\nimport { RequireContext } from \"./types\";\nimport { DefaultLayout } from \"./views/Layout\";\n\nexport type FileNode = Pick<RouteNode, \"contextKey\" | \"loadRoute\"> & {\n /** Like `(tab)/index` */\n normalizedName: string;\n};\n\ntype TreeNode = {\n name: string;\n children: TreeNode[];\n parents: string[];\n /** null when there is no file in a folder. */\n node: FileNode | null;\n};\n\n/** Convert a flat map of file nodes into a nested tree of files. */\nexport function getRecursiveTree(files: FileNode[]): TreeNode {\n const tree = {\n name: \"\",\n children: [],\n parents: [],\n node: null,\n };\n\n for (const file of files) {\n // ['(tab)', 'settings', '[...another]']\n const parts = file.normalizedName.split(\"/\");\n let currentNode: TreeNode = tree;\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i];\n\n if (i === parts.length - 1 && part === \"_layout\") {\n if (currentNode.node) {\n const overwritten = currentNode.node.contextKey;\n throw new Error(\n `Higher priority Layout Route \"${file.contextKey}\" overriding redundant Layout Route \"${overwritten}\". Remove the Layout Route \"${overwritten}\" to fix this.`\n );\n }\n continue;\n }\n\n const existing = currentNode.children.find((item) => item.name === part);\n if (existing) {\n currentNode = existing;\n } else {\n const newNode: TreeNode = {\n name: part,\n children: [],\n parents: [...currentNode.parents, currentNode.name],\n node: null,\n };\n currentNode.children.push(newNode);\n currentNode = newNode;\n }\n }\n currentNode.node = file;\n }\n\n if (process.env.NODE_ENV !== \"production\") {\n assertDeprecatedFormat(tree);\n }\n\n return tree;\n}\n\nfunction assertDeprecatedFormat(tree: TreeNode) {\n for (const child of tree.children) {\n if (\n child.node &&\n child.children.length &&\n !child.node.normalizedName.endsWith(\"_layout\")\n ) {\n const ext = child.node.contextKey.split(\".\").pop();\n throw new Error(\n `Using deprecated Layout Route format: Move \\`./app/${child.node.normalizedName}.${ext}\\` to \\`./app/${child.node.normalizedName}/_layout.${ext}\\``\n );\n }\n assertDeprecatedFormat(child);\n }\n}\n\nfunction getTreeNodesAsRouteNodes(nodes: TreeNode[]): RouteNode[] {\n return nodes.map(treeNodeToRouteNode).flat().filter(Boolean) as RouteNode[];\n}\n\nexport function generateDynamic(name: string) {\n const deepDynamicName = matchDeepDynamicRouteName(name);\n const dynamicName = deepDynamicName ?? matchDynamicName(name);\n\n return dynamicName ? { name: dynamicName, deep: !!deepDynamicName } : null;\n}\n\nfunction collapseRouteSegments(route: string) {\n return stripFragmentSegmentsFromPath(route.replace(/\\/index$/, \"\"));\n}\n\n/**\n * Given a route node and a name representing the fragment name,\n * find the nearest child matching the name.\n *\n * Doesn't support slashes in the name.\n * Routes like `explore/(something)/index` will be matched against `explore`.\n *\n */\nfunction getDefaultInitialRoute(node: RouteNode, name: string) {\n return node.children.find(\n (node) => collapseRouteSegments(node.route) === name\n );\n}\n\nfunction applyDefaultInitialRouteName(node: RouteNode): RouteNode {\n const fragmentName = matchFragmentName(node.route);\n if (!node.children || !fragmentName) {\n return node;\n }\n\n // Guess at the initial route based on the fragment name.\n // TODO(EvanBacon): Perhaps we should attempt to warn when the fragment doesn't match any child routes.\n const initialRouteName = getDefaultInitialRoute(node, fragmentName)?.route;\n const route = {\n ...node,\n loadRoute() {\n const { unstable_settings, ...route } = node.loadRoute();\n return {\n ...route,\n unstable_settings: {\n initialRouteName:\n unstable_settings?.initialRouteName ?? initialRouteName,\n // Allow overriding the initial route name using the layout settings.\n ...unstable_settings,\n },\n };\n },\n };\n if (initialRouteName != null) {\n route.initialRouteName = initialRouteName;\n }\n return route;\n}\n\nfunction cloneFragmentRoute(\n node: RouteNode,\n { name: nextName }: { name: string }\n): RouteNode {\n const fragmentName = `(${nextName})`;\n const parts = node.contextKey.split(\"/\");\n parts[parts.length - 2] = fragmentName;\n\n return {\n ...node,\n route: fragmentName,\n contextKey: parts.join(\"/\"),\n };\n}\n\nfunction treeNodeToRouteNode({\n name,\n node,\n children,\n}: TreeNode): RouteNode[] | null {\n const dynamic = generateDynamic(name);\n\n if (node) {\n const fragmentName = matchFragmentName(name);\n const multiFragment = fragmentName?.includes(\",\");\n\n const clones = multiFragment\n ? fragmentName!.split(\",\").map((v) => ({ name: v.trim() }))\n : null;\n\n const output = {\n loadRoute: node.loadRoute,\n route: name,\n contextKey: node.contextKey,\n children: getTreeNodesAsRouteNodes(children),\n dynamic,\n };\n\n if (Array.isArray(clones)) {\n return clones.map((clone) =>\n applyDefaultInitialRouteName(cloneFragmentRoute({ ...output }, clone))\n );\n }\n\n return [\n applyDefaultInitialRouteName({\n loadRoute: node.loadRoute,\n route: name,\n contextKey: node.contextKey,\n children: getTreeNodesAsRouteNodes(children),\n dynamic,\n }),\n ];\n }\n\n // Empty folder, skip it.\n if (!children.length) {\n return null;\n }\n\n // When there's a directory, but no layout route file (with valid export), the child routes won't be grouped.\n // This pushes all children into the nearest layout route.\n return getTreeNodesAsRouteNodes(\n children.map((child) => {\n return {\n ...child,\n name: [name, child.name].filter(Boolean).join(\"/\"),\n };\n })\n );\n}\n\nfunction contextModuleToFileNodes(contextModule: RequireContext): FileNode[] {\n const nodes = contextModule.keys().map((key) => {\n // In development, check if the file exports a default component\n // this helps keep things snappy when creating files. In production we load all screens lazily.\n try {\n if (!contextModule(key)?.default) {\n return null;\n }\n } catch (error) {\n // Probably this won't stop metro from freaking out but it's worth a try.\n console.warn('Error loading route \"' + key + '\"', error);\n return null;\n }\n\n const node: FileNode = {\n loadRoute: () => contextModule(key),\n normalizedName: getNameFromFilePath(key),\n contextKey: key,\n };\n\n return node;\n });\n\n return nodes.filter(Boolean) as FileNode[];\n}\n\nfunction hasCustomRootLayoutNode(routes: RouteNode[]) {\n if (routes.length !== 1) {\n return false;\n }\n // This could either be the root _layout or an app with a single file.\n const route = routes[0];\n\n if (\n route.route === \"\" &&\n route.contextKey.match(/^\\.\\/_layout\\.([jt]sx?)$/)\n ) {\n return true;\n }\n return false;\n}\n\nfunction treeNodesToRootRoute(treeNode: TreeNode): RouteNode | null {\n const routes = treeNodeToRouteNode(treeNode);\n\n if (!routes?.length) {\n return null;\n }\n\n if (hasCustomRootLayoutNode(routes)) {\n return routes[0];\n }\n\n return {\n loadRoute: () => ({ default: DefaultLayout }),\n // Generate a fake file name for the directory\n contextKey: \"./_layout.tsx\",\n route: \"\",\n generated: true,\n dynamic: null,\n children: routes,\n };\n}\n\n/** Given a Metro context module, return an array of nested routes. */\nexport function getRoutes(contextModule: RequireContext): RouteNode | null {\n const files = contextModuleToFileNodes(contextModule);\n const treeNodes = getRecursiveTree(files);\n const route = treeNodesToRootRoute(treeNodes);\n\n if (!route) {\n return null;\n }\n\n appendSitemapRoute(route);\n\n // Auto add not found route if it doesn't exist\n appendUnmatchedRoute(route);\n\n return route;\n}\n\nfunction appendSitemapRoute(routes: RouteNode) {\n if (\n !routes.children.length ||\n // Allow overriding the sitemap route\n routes.children.some((route) => route.route === \"_sitemap\")\n ) {\n return routes;\n }\n const { Sitemap, getNavOptions } = require(\"./views/Sitemap\");\n routes.children.push({\n loadRoute() {\n return { default: Sitemap, getNavOptions };\n },\n route: \"_sitemap\",\n contextKey: \"./_sitemap.tsx\",\n generated: true,\n internal: true,\n dynamic: null,\n children: [],\n });\n return routes;\n}\n\nfunction appendUnmatchedRoute(routes: RouteNode) {\n // Auto add not found route if it doesn't exist\n const userDefinedDynamicRoute = getUserDefinedDeepDynamicRoute(routes);\n if (!userDefinedDynamicRoute) {\n routes.children.push({\n loadRoute() {\n return { default: require(\"./views/Unmatched\").Unmatched };\n },\n route: \"[...404]\",\n contextKey: \"./[...404].tsx\",\n dynamic: { name: \"404\", deep: true },\n children: [],\n generated: true,\n internal: true,\n });\n }\n return routes;\n}\n\n/**\n * Exposed for testing.\n * @returns a top-level deep dynamic route if it exists, otherwise null.\n */\nexport function getUserDefinedDeepDynamicRoute(\n routes: RouteNode\n): RouteNode | null {\n // Auto add not found route if it doesn't exist\n for (const route of routes.children ?? []) {\n const isDeepDynamic = matchDeepDynamicRouteName(route.route);\n if (isDeepDynamic) {\n return route;\n }\n // Recurse through fragment routes\n if (matchFragmentName(route.route)) {\n const child = getUserDefinedDeepDynamicRoute(route);\n if (child) {\n return child;\n }\n }\n }\n return null;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"getRoutes.js","sourceRoot":"","sources":["../src/getRoutes.ts"],"names":[],"mappings":"AACA,OAAO,EACL,mBAAmB,EACnB,yBAAyB,EACzB,gBAAgB,EAChB,iBAAiB,EACjB,6BAA6B,GAC9B,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAe/C,oEAAoE;AACpE,MAAM,UAAU,gBAAgB,CAAC,KAAiB;IAChD,MAAM,IAAI,GAAG;QACX,IAAI,EAAE,EAAE;QACR,QAAQ,EAAE,EAAE;QACZ,OAAO,EAAE,EAAE;QACX,IAAI,EAAE,IAAI;KACX,CAAC;IAEF,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE;QACxB,wCAAwC;QACxC,MAAM,KAAK,GAAG,IAAI,CAAC,cAAc,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAC7C,IAAI,WAAW,GAAa,IAAI,CAAC;QACjC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,K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UAAU,8BAA8B,CAC5C,MAAiB;IAEjB,+CAA+C;IAC/C,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,QAAQ,IAAI,EAAE,EAAE;QACzC,MAAM,aAAa,GAAG,yBAAyB,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC7D,IAAI,aAAa,EAAE;YACjB,OAAO,KAAK,CAAC;SACd;QACD,kCAAkC;QAClC,IAAI,iBAAiB,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;YAClC,MAAM,KAAK,GAAG,8BAA8B,CAAC,KAAK,CAAC,CAAC;YACpD,IAAI,KAAK,EAAE;gBACT,OAAO,KAAK,CAAC;aACd;SACF;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import { DynamicConvention, RouteNode } from \"./Route\";\nimport {\n getNameFromFilePath,\n matchDeepDynamicRouteName,\n matchDynamicName,\n matchFragmentName,\n stripFragmentSegmentsFromPath,\n} from \"./matchers\";\nimport { RequireContext } from \"./types\";\nimport { DefaultLayout } from \"./views/Layout\";\n\nexport type FileNode = Pick<RouteNode, \"contextKey\" | \"loadRoute\"> & {\n /** Like `(tab)/index` */\n normalizedName: string;\n};\n\ntype TreeNode = {\n name: string;\n children: TreeNode[];\n parents: string[];\n /** null when there is no file in a folder. */\n node: FileNode | null;\n};\n\n/** Convert a flat map of file nodes into a nested tree of files. */\nexport function getRecursiveTree(files: FileNode[]): TreeNode {\n const tree = {\n name: \"\",\n children: [],\n parents: [],\n node: null,\n };\n\n for (const file of files) {\n // ['(tab)', 'settings', '[...another]']\n const parts = file.normalizedName.split(\"/\");\n let currentNode: TreeNode = tree;\n for (let i = 0; i < parts.length; i++) {\n const part = parts[i];\n\n if (i === parts.length - 1 && part === \"_layout\") {\n if (currentNode.node) {\n const overwritten = currentNode.node.contextKey;\n throw new Error(\n `Higher priority Layout Route \"${file.contextKey}\" overriding redundant Layout Route \"${overwritten}\". Remove the Layout Route \"${overwritten}\" to fix this.`\n );\n }\n continue;\n }\n\n const existing = currentNode.children.find((item) => item.name === part);\n if (existing) {\n currentNode = existing;\n } else {\n const newNode: TreeNode = {\n name: part,\n children: [],\n parents: [...currentNode.parents, currentNode.name],\n node: null,\n };\n currentNode.children.push(newNode);\n currentNode = newNode;\n }\n }\n currentNode.node = file;\n }\n\n if (process.env.NODE_ENV !== \"production\") {\n assertDeprecatedFormat(tree);\n }\n\n return tree;\n}\n\nfunction assertDeprecatedFormat(tree: TreeNode) {\n for (const child of tree.children) {\n if (\n child.node &&\n child.children.length &&\n !child.node.normalizedName.endsWith(\"_layout\")\n ) {\n const ext = child.node.contextKey.split(\".\").pop();\n throw new Error(\n `Using deprecated Layout Route format: Move \\`./app/${child.node.normalizedName}.${ext}\\` to \\`./app/${child.node.normalizedName}/_layout.${ext}\\``\n );\n }\n assertDeprecatedFormat(child);\n }\n}\n\nfunction getTreeNodesAsRouteNodes(nodes: TreeNode[]): RouteNode[] {\n return nodes.map(treeNodeToRouteNode).flat().filter(Boolean) as RouteNode[];\n}\n\nexport function generateDynamicFromSegment(\n name: string\n): DynamicConvention | null {\n const deepDynamicName = matchDeepDynamicRouteName(name);\n const dynamicName = deepDynamicName ?? matchDynamicName(name);\n\n return dynamicName ? { name: dynamicName, deep: !!deepDynamicName } : null;\n}\n\nexport function generateDynamic(name: string): RouteNode[\"dynamic\"] {\n const description = name\n .split(\"/\")\n .map((segment) => generateDynamicFromSegment(segment))\n .filter(Boolean) as DynamicConvention[];\n return description.length === 0 ? null : description;\n}\n\nfunction collapseRouteSegments(route: string) {\n return stripFragmentSegmentsFromPath(route.replace(/\\/index$/, \"\"));\n}\n\n/**\n * Given a route node and a name representing the fragment name,\n * find the nearest child matching the name.\n *\n * Doesn't support slashes in the name.\n * Routes like `explore/(something)/index` will be matched against `explore`.\n *\n */\nfunction getDefaultInitialRoute(node: RouteNode, name: string) {\n return node.children.find(\n (node) => collapseRouteSegments(node.route) === name\n );\n}\n\nfunction applyDefaultInitialRouteName(node: RouteNode): RouteNode {\n const fragmentName = matchFragmentName(node.route);\n if (!node.children || !fragmentName) {\n return node;\n }\n\n // Guess at the initial route based on the fragment name.\n // TODO(EvanBacon): Perhaps we should attempt to warn when the fragment doesn't match any child routes.\n const initialRouteName = getDefaultInitialRoute(node, fragmentName)?.route;\n const route = {\n ...node,\n loadRoute() {\n const { unstable_settings, ...route } = node.loadRoute();\n return {\n ...route,\n unstable_settings: {\n initialRouteName:\n unstable_settings?.initialRouteName ?? initialRouteName,\n // Allow overriding the initial route name using the layout settings.\n ...unstable_settings,\n },\n };\n },\n };\n if (initialRouteName != null) {\n route.initialRouteName = initialRouteName;\n }\n return route;\n}\n\nfunction cloneFragmentRoute(\n node: RouteNode,\n { name: nextName }: { name: string }\n): RouteNode {\n const fragmentName = `(${nextName})`;\n const parts = node.contextKey.split(\"/\");\n parts[parts.length - 2] = fragmentName;\n\n return {\n ...node,\n route: fragmentName,\n contextKey: parts.join(\"/\"),\n };\n}\n\nfunction treeNodeToRouteNode({\n name,\n node,\n children,\n}: TreeNode): RouteNode[] | null {\n const dynamic = generateDynamic(name);\n\n if (node) {\n const fragmentName = matchFragmentName(name);\n const multiFragment = fragmentName?.includes(\",\");\n\n const clones = multiFragment\n ? fragmentName!.split(\",\").map((v) => ({ name: v.trim() }))\n : null;\n\n const output = {\n loadRoute: node.loadRoute,\n route: name,\n contextKey: node.contextKey,\n children: getTreeNodesAsRouteNodes(children),\n dynamic,\n };\n\n if (Array.isArray(clones)) {\n return clones.map((clone) =>\n applyDefaultInitialRouteName(cloneFragmentRoute({ ...output }, clone))\n );\n }\n\n return [\n applyDefaultInitialRouteName({\n loadRoute: node.loadRoute,\n route: name,\n contextKey: node.contextKey,\n children: getTreeNodesAsRouteNodes(children),\n dynamic,\n }),\n ];\n }\n\n // Empty folder, skip it.\n if (!children.length) {\n return null;\n }\n\n // When there's a directory, but no layout route file (with valid export), the child routes won't be grouped.\n // This pushes all children into the nearest layout route.\n return getTreeNodesAsRouteNodes(\n children.map((child) => {\n return {\n ...child,\n name: [name, child.name].filter(Boolean).join(\"/\"),\n };\n })\n );\n}\n\nfunction contextModuleToFileNodes(contextModule: RequireContext): FileNode[] {\n const nodes = contextModule.keys().map((key) => {\n // In development, check if the file exports a default component\n // this helps keep things snappy when creating files. In production we load all screens lazily.\n try {\n if (!contextModule(key)?.default) {\n return null;\n }\n } catch (error) {\n // Probably this won't stop metro from freaking out but it's worth a try.\n console.warn('Error loading route \"' + key + '\"', error);\n return null;\n }\n\n const node: FileNode = {\n loadRoute: () => contextModule(key),\n normalizedName: getNameFromFilePath(key),\n contextKey: key,\n };\n\n return node;\n });\n\n return nodes.filter(Boolean) as FileNode[];\n}\n\nfunction hasCustomRootLayoutNode(routes: RouteNode[]) {\n if (routes.length !== 1) {\n return false;\n }\n // This could either be the root _layout or an app with a single file.\n const route = routes[0];\n\n if (\n route.route === \"\" &&\n route.contextKey.match(/^\\.\\/_layout\\.([jt]sx?)$/)\n ) {\n return true;\n }\n return false;\n}\n\nfunction treeNodesToRootRoute(treeNode: TreeNode): RouteNode | null {\n const routes = treeNodeToRouteNode(treeNode);\n\n if (!routes?.length) {\n return null;\n }\n\n if (hasCustomRootLayoutNode(routes)) {\n return routes[0];\n }\n\n return {\n loadRoute: () => ({ default: DefaultLayout }),\n // Generate a fake file name for the directory\n contextKey: \"./_layout.tsx\",\n route: \"\",\n generated: true,\n dynamic: null,\n children: routes,\n };\n}\n\n/** Given a Metro context module, return an array of nested routes. */\nexport function getRoutes(contextModule: RequireContext): RouteNode | null {\n const files = contextModuleToFileNodes(contextModule);\n const treeNodes = getRecursiveTree(files);\n const route = treeNodesToRootRoute(treeNodes);\n\n if (!route) {\n return null;\n }\n\n appendSitemapRoute(route);\n\n // Auto add not found route if it doesn't exist\n appendUnmatchedRoute(route);\n\n return route;\n}\n\nfunction appendSitemapRoute(routes: RouteNode) {\n if (\n !routes.children.length ||\n // Allow overriding the sitemap route\n routes.children.some((route) => route.route === \"_sitemap\")\n ) {\n return routes;\n }\n const { Sitemap, getNavOptions } = require(\"./views/Sitemap\");\n routes.children.push({\n loadRoute() {\n return { default: Sitemap, getNavOptions };\n },\n route: \"_sitemap\",\n contextKey: \"./_sitemap.tsx\",\n generated: true,\n internal: true,\n dynamic: null,\n children: [],\n });\n return routes;\n}\n\nfunction appendUnmatchedRoute(routes: RouteNode) {\n // Auto add not found route if it doesn't exist\n const userDefinedDynamicRoute = getUserDefinedDeepDynamicRoute(routes);\n if (!userDefinedDynamicRoute) {\n routes.children.push({\n loadRoute() {\n return { default: require(\"./views/Unmatched\").Unmatched };\n },\n route: \"[...404]\",\n contextKey: \"./[...404].tsx\",\n dynamic: [{ name: \"404\", deep: true }],\n children: [],\n generated: true,\n internal: true,\n });\n }\n return routes;\n}\n\n/**\n * Exposed for testing.\n * @returns a top-level deep dynamic route if it exists, otherwise null.\n */\nexport function getUserDefinedDeepDynamicRoute(\n routes: RouteNode\n): RouteNode | null {\n // Auto add not found route if it doesn't exist\n for (const route of routes.children ?? []) {\n const isDeepDynamic = matchDeepDynamicRouteName(route.route);\n if (isDeepDynamic) {\n return route;\n }\n // Recurse through fragment routes\n if (matchFragmentName(route.route)) {\n const child = getUserDefinedDeepDynamicRoute(route);\n if (child) {\n return child;\n }\n }\n }\n return null;\n}\n"]}
|
package/build/useScreens.d.ts
CHANGED
|
@@ -25,5 +25,5 @@ export declare function getQualifiedRouteComponent(value: RouteNode): React.Comp
|
|
|
25
25
|
/** @returns a function which provides a screen id that matches the dynamic route name in params. */
|
|
26
26
|
export declare function createGetIdForRoute(route: Pick<RouteNode, "dynamic" | "route">): (({ params }: {
|
|
27
27
|
params: any;
|
|
28
|
-
}) =>
|
|
28
|
+
}) => string | undefined) | undefined;
|
|
29
29
|
//# sourceMappingURL=useScreens.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useScreens.d.ts","sourceRoot":"","sources":["../src/useScreens.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,
|
|
1
|
+
{"version":3,"file":"useScreens.d.ts","sourceRoot":"","sources":["../src/useScreens.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAGL,SAAS,EAGV,MAAM,SAAS,CAAC;AAIjB,oBAAY,WAAW,CACrB,QAAQ,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IACxD;IACF,4DAA4D;IAC5D,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,aAAa,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,CAAC;IACvC,OAAO,CAAC,EAAE,QAAQ,CAAC;CACpB,CAAC;AA8DF;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,WAAW,EAAE,GAAG,KAAK,CAAC,SAAS,EAAE,CAUxE;AAMD,mFAAmF;AACnF,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,SAAS;WAQtC,GAAG;gBAAc,GAAG;kCAwBxC;AAED,oGAAoG;AACpG,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,IAAI,CAAC,SAAS,EAAE,SAAS,GAAG,OAAO,CAAC;;sCA0B5C"}
|
package/build/useScreens.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
-
import { Route, sortRoutesWithInitial, useRouteNode } from "./Route";
|
|
2
|
+
import { Route, sortRoutesWithInitial, useRouteNode, } from "./Route";
|
|
3
3
|
import { Screen } from "./primitives";
|
|
4
4
|
import { Try } from "./views/Try";
|
|
5
5
|
function getSortedChildren(children, order, initialRouteName) {
|
|
@@ -77,24 +77,30 @@ export function getQualifiedRouteComponent(value) {
|
|
|
77
77
|
}
|
|
78
78
|
/** @returns a function which provides a screen id that matches the dynamic route name in params. */
|
|
79
79
|
export function createGetIdForRoute(route) {
|
|
80
|
-
if (!route.dynamic) {
|
|
80
|
+
if (!route.dynamic?.length) {
|
|
81
81
|
return undefined;
|
|
82
82
|
}
|
|
83
|
-
const dynamicName = route.dynamic.name;
|
|
84
|
-
const routeName = route.route;
|
|
85
83
|
return ({ params }) => {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
84
|
+
const getPreferredId = (segment) => {
|
|
85
|
+
// Params can be undefined when there are no params in the route.
|
|
86
|
+
const preferredId = params?.[segment.name];
|
|
87
|
+
// If the route has a dynamic segment, use the matching parameter
|
|
88
|
+
// as the screen id. This enables pushing a screen like `/[user]` multiple times
|
|
89
|
+
// when the user is different.
|
|
90
|
+
if (preferredId) {
|
|
91
|
+
if (!Array.isArray(preferredId)) {
|
|
92
|
+
return preferredId;
|
|
93
|
+
}
|
|
94
|
+
else if (preferredId.length) {
|
|
95
|
+
// Deep dynamic routes will return as an array, so we'll join them to create a
|
|
96
|
+
// fully qualified string.
|
|
97
|
+
return preferredId.join("/");
|
|
98
|
+
}
|
|
99
|
+
// Empty arrays...
|
|
100
|
+
}
|
|
101
|
+
return segment.deep ? `[...${segment.name}]` : `[${segment.name}]`;
|
|
102
|
+
};
|
|
103
|
+
return route.dynamic?.map((segment) => getPreferredId(segment)).join("/");
|
|
98
104
|
};
|
|
99
105
|
}
|
|
100
106
|
function routeToScreen(route, { options, ...props } = {}) {
|
package/build/useScreens.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useScreens.js","sourceRoot":"","sources":["../src/useScreens.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,KAAK,EAAa,qBAAqB,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAChF,OA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itial, useRouteNode } from \"./Route\";\nimport { Screen } from \"./primitives\";\nimport { Try } from \"./views/Try\";\n\nexport type ScreenProps<\n TOptions extends Record<string, any> = Record<string, any>\n> = {\n /** Name is required when used inside a Layout component. */\n name?: string;\n /**\n * Redirect to the nearest or provided sibling route.\n * If all children are redirect={true}, the layout will render `null` as there are no children to render.\n */\n redirect?: boolean | string;\n initialParams?: { [key: string]: any };\n options?: TOptions;\n};\n\nfunction getSortedChildren(\n children: RouteNode[],\n order?: ScreenProps[],\n initialRouteName?: string\n): { route: RouteNode; props: any }[] {\n if (!order?.length) {\n return children\n .sort(sortRoutesWithInitial(initialRouteName))\n .map((route) => ({ route, props: {} }));\n }\n const entries = [...children];\n\n const ordered = order\n .map(({ name, redirect, initialParams, options }) => {\n if (!entries.length) {\n console.warn(\n `[Layout children]: Too many screens defined. Route \"${name}\" is extraneous.`\n );\n return null;\n }\n const matchIndex = entries.findIndex((child) => child.route === name);\n if (matchIndex === -1) {\n console.warn(\n `[Layout children]: No route named \"${name}\" exists in nested children:`,\n children.map(({ route }) => route)\n );\n return null;\n } else {\n // Get match and remove from entries\n const match = entries[matchIndex];\n entries.splice(matchIndex, 1);\n\n // Ensure to return null after removing from entries.\n if (redirect) {\n if (typeof redirect === \"string\") {\n throw new Error(\n `Redirecting to a specific route is not supported yet.`\n );\n }\n return null;\n }\n\n return { route: match, props: { initialParams, options } };\n }\n })\n .filter(Boolean) as {\n route: RouteNode;\n props: Partial<ScreenProps>;\n }[];\n\n // Add any remaining children\n ordered.push(\n ...entries\n .sort(sortRoutesWithInitial(initialRouteName))\n .map((route) => ({ route, props: {} }))\n );\n\n return ordered;\n}\n\n/**\n * @returns React Navigation screens sorted by the `route` property.\n */\nexport function useSortedScreens(order: ScreenProps[]): React.ReactNode[] {\n const node = useRouteNode();\n\n const sorted = node?.children?.length\n ? getSortedChildren(node.children, order, node.initialRouteName)\n : [];\n return React.useMemo(\n () => sorted.map((value) => routeToScreen(value.route, value.props)),\n [sorted]\n );\n}\n\n// TODO: Maybe there's a more React-y way to do this?\n// Without this store, the process enters a recursive loop.\nconst qualifiedStore = new WeakMap<RouteNode, React.ComponentType<any>>();\n\n/** Wrap the component with various enhancements and add access to child routes. */\nexport function getQualifiedRouteComponent(value: RouteNode) {\n if (qualifiedStore.has(value)) {\n return qualifiedStore.get(value)!;\n }\n\n const { default: Component, ErrorBoundary } = value.loadRoute();\n\n const QualifiedRoute = React.forwardRef(\n (props: { route: any; navigation: any }, ref: any) => {\n const children = React.createElement(Component, {\n ...props,\n ref,\n // Expose the template segment path, e.g. `(home)`, `[foo]`, `index`\n // the intention is to make it possible to deduce shared routes.\n segment: value.route,\n });\n\n const errorBoundary = ErrorBoundary ? (\n <Try catch={ErrorBoundary}>{children}</Try>\n ) : (\n children\n );\n\n return <Route node={value}>{errorBoundary}</Route>;\n }\n );\n\n QualifiedRoute.displayName = `Route(${\n Component.displayName || Component.name || value.route\n })`;\n qualifiedStore.set(value, QualifiedRoute);\n return QualifiedRoute;\n}\n\n/** @returns a function which provides a screen id that matches the dynamic route name in params. */\nexport function createGetIdForRoute(\n route: Pick<RouteNode, \"dynamic\" | \"route\">\n) {\n if (!route.dynamic) {\n return undefined;\n }\n const dynamicName = route.dynamic.name;\n const routeName = route.route;\n return ({ params }) => {\n // Params can be undefined when there are no params in the route.\n const preferredId = params?.[dynamicName];\n // If the route has a dynamic segment, use the matching parameter\n // as the screen id. This enables pushing a screen like `/[user]` multiple times\n // when the user is different.\n if (preferredId) {\n // Deep dynamic routes will return as an array, so we'll join them to create a\n // fully qualified string.\n return (\n (Array.isArray(preferredId) ? preferredId.join(\"/\") : preferredId) ||\n routeName\n );\n }\n return routeName;\n };\n}\n\nfunction routeToScreen(\n route: RouteNode,\n { options, ...props }: Partial<ScreenProps> = {}\n) {\n return (\n <Screen\n // Users can override the screen getId function.\n getId={createGetIdForRoute(route)}\n {...props}\n name={route.route}\n key={route.route}\n options={(args) => {\n // Only eager load generated components\n const staticOptions = route.generated\n ? route.loadRoute()?.getNavOptions\n : null;\n const staticResult =\n typeof staticOptions === \"function\"\n ? staticOptions(args)\n : staticOptions;\n const dynamicResult =\n typeof options === \"function\" ? options?.(args) : options;\n return {\n ...staticResult,\n ...dynamicResult,\n };\n }}\n getComponent={() => getQualifiedRouteComponent(route)}\n />\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"useScreens.js","sourceRoot":"","sources":["../src/useScreens.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAEL,KAAK,EAEL,qBAAqB,EACrB,YAAY,GACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAgBlC,SAAS,iBAAiB,CACxB,QAAqB,EACrB,KAAqB,EACrB,gBAAyB;IAEzB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE;QAClB,OAAO,QAAQ;aACZ,IAAI,CAAC,qBAAqB,CAAC,gBAAgB,CAAC,CAAC;aAC7C,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CAAC,CAAC;KAC3C;IACD,MAAM,OAAO,GAAG,CAAC,GAAG,QAAQ,CAAC,CAAC;IAE9B,MAAM,OAAO,GAAG,KAAK;SAClB,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,EAAE,EAAE;QAClD,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YACnB,OAAO,CAAC,IAAI,CACV,uDAAuD,IAAI,kBAAkB,CAC9E,CAAC;YACF,OAAO,IAAI,CAAC;SACb;QACD,MAAM,UAAU,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,KAAK,IAAI,CAAC,CAAC;QACtE,IAAI,UAA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jB,GAAG,EAAE,KAAK,CAAC,KAAK,EAChB,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE;YAChB,uCAAuC;YACvC,MAAM,aAAa,GAAG,KAAK,CAAC,SAAS;gBACnC,CAAC,CAAC,KAAK,CAAC,SAAS,EAAE,EAAE,aAAa;gBAClC,CAAC,CAAC,IAAI,CAAC;YACT,MAAM,YAAY,GAChB,OAAO,aAAa,KAAK,UAAU;gBACjC,CAAC,CAAC,aAAa,CAAC,IAAI,CAAC;gBACrB,CAAC,CAAC,aAAa,CAAC;YACpB,MAAM,aAAa,GACjB,OAAO,OAAO,KAAK,UAAU,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;YAC5D,OAAO;gBACL,GAAG,YAAY;gBACf,GAAG,aAAa;aACjB,CAAC;QACJ,CAAC,EACD,YAAY,EAAE,GAAG,EAAE,CAAC,0BAA0B,CAAC,KAAK,CAAC,GACrD,CACH,CAAC;AACJ,CAAC","sourcesContent":["import React from \"react\";\n\nimport {\n DynamicConvention,\n Route,\n RouteNode,\n sortRoutesWithInitial,\n useRouteNode,\n} from \"./Route\";\nimport { Screen } from \"./primitives\";\nimport { Try } from \"./views/Try\";\n\nexport type ScreenProps<\n TOptions extends Record<string, any> = Record<string, any>\n> = {\n /** Name is required when used inside a Layout component. */\n name?: string;\n /**\n * Redirect to the nearest or provided sibling route.\n * If all children are redirect={true}, the layout will render `null` as there are no children to render.\n */\n redirect?: boolean | string;\n initialParams?: { [key: string]: any };\n options?: TOptions;\n};\n\nfunction getSortedChildren(\n children: RouteNode[],\n order?: ScreenProps[],\n initialRouteName?: string\n): { route: RouteNode; props: any }[] {\n if (!order?.length) {\n return children\n .sort(sortRoutesWithInitial(initialRouteName))\n .map((route) => ({ route, props: {} }));\n }\n const entries = [...children];\n\n const ordered = order\n .map(({ name, redirect, initialParams, options }) => {\n if (!entries.length) {\n console.warn(\n `[Layout children]: Too many screens defined. Route \"${name}\" is extraneous.`\n );\n return null;\n }\n const matchIndex = entries.findIndex((child) => child.route === name);\n if (matchIndex === -1) {\n console.warn(\n `[Layout children]: No route named \"${name}\" exists in nested children:`,\n children.map(({ route }) => route)\n );\n return null;\n } else {\n // Get match and remove from entries\n const match = entries[matchIndex];\n entries.splice(matchIndex, 1);\n\n // Ensure to return null after removing from entries.\n if (redirect) {\n if (typeof redirect === \"string\") {\n throw new Error(\n `Redirecting to a specific route is not supported yet.`\n );\n }\n return null;\n }\n\n return { route: match, props: { initialParams, options } };\n }\n })\n .filter(Boolean) as {\n route: RouteNode;\n props: Partial<ScreenProps>;\n }[];\n\n // Add any remaining children\n ordered.push(\n ...entries\n .sort(sortRoutesWithInitial(initialRouteName))\n .map((route) => ({ route, props: {} }))\n );\n\n return ordered;\n}\n\n/**\n * @returns React Navigation screens sorted by the `route` property.\n */\nexport function useSortedScreens(order: ScreenProps[]): React.ReactNode[] {\n const node = useRouteNode();\n\n const sorted = node?.children?.length\n ? getSortedChildren(node.children, order, node.initialRouteName)\n : [];\n return React.useMemo(\n () => sorted.map((value) => routeToScreen(value.route, value.props)),\n [sorted]\n );\n}\n\n// TODO: Maybe there's a more React-y way to do this?\n// Without this store, the process enters a recursive loop.\nconst qualifiedStore = new WeakMap<RouteNode, React.ComponentType<any>>();\n\n/** Wrap the component with various enhancements and add access to child routes. */\nexport function getQualifiedRouteComponent(value: RouteNode) {\n if (qualifiedStore.has(value)) {\n return qualifiedStore.get(value)!;\n }\n\n const { default: Component, ErrorBoundary } = value.loadRoute();\n\n const QualifiedRoute = React.forwardRef(\n (props: { route: any; navigation: any }, ref: any) => {\n const children = React.createElement(Component, {\n ...props,\n ref,\n // Expose the template segment path, e.g. `(home)`, `[foo]`, `index`\n // the intention is to make it possible to deduce shared routes.\n segment: value.route,\n });\n\n const errorBoundary = ErrorBoundary ? (\n <Try catch={ErrorBoundary}>{children}</Try>\n ) : (\n children\n );\n\n return <Route node={value}>{errorBoundary}</Route>;\n }\n );\n\n QualifiedRoute.displayName = `Route(${\n Component.displayName || Component.name || value.route\n })`;\n qualifiedStore.set(value, QualifiedRoute);\n return QualifiedRoute;\n}\n\n/** @returns a function which provides a screen id that matches the dynamic route name in params. */\nexport function createGetIdForRoute(\n route: Pick<RouteNode, \"dynamic\" | \"route\">\n) {\n if (!route.dynamic?.length) {\n return undefined;\n }\n return ({ params }) => {\n const getPreferredId = (segment: DynamicConvention) => {\n // Params can be undefined when there are no params in the route.\n const preferredId = params?.[segment.name];\n // If the route has a dynamic segment, use the matching parameter\n // as the screen id. This enables pushing a screen like `/[user]` multiple times\n // when the user is different.\n if (preferredId) {\n if (!Array.isArray(preferredId)) {\n return preferredId;\n } else if (preferredId.length) {\n // Deep dynamic routes will return as an array, so we'll join them to create a\n // fully qualified string.\n return preferredId.join(\"/\");\n }\n // Empty arrays...\n }\n return segment.deep ? `[...${segment.name}]` : `[${segment.name}]`;\n };\n return route.dynamic?.map((segment) => getPreferredId(segment)).join(\"/\");\n };\n}\n\nfunction routeToScreen(\n route: RouteNode,\n { options, ...props }: Partial<ScreenProps> = {}\n) {\n return (\n <Screen\n // Users can override the screen getId function.\n getId={createGetIdForRoute(route)}\n {...props}\n name={route.route}\n key={route.route}\n options={(args) => {\n // Only eager load generated components\n const staticOptions = route.generated\n ? route.loadRoute()?.getNavOptions\n : null;\n const staticResult =\n typeof staticOptions === \"function\"\n ? staticOptions(args)\n : staticOptions;\n const dynamicResult =\n typeof options === \"function\" ? options?.(args) : options;\n return {\n ...staticResult,\n ...dynamicResult,\n };\n }}\n getComponent={() => getQualifiedRouteComponent(route)}\n />\n );\n}\n"]}
|