Open History to see the last commit
1import {
2 createNavigatorFactory,
3 StackRouter,
4 useNavigationBuilder,
5 type NavigatorTypeBagBase,
6 type ParamListBase,
7 type StackActionHelpers,
8 type StackNavigationState,
9 type StackRouterOptions,
10 type StaticConfig,
11 type TypedNavigator,
12} from "@react-navigation/native";
13import {
14 NativeStackView,
15 type NativeStackNavigationEventMap,
16 type NativeStackNavigationOptions,
17 type NativeStackNavigatorProps,
18 type NativeStackTypeBag,
19} from "@react-navigation/native-stack";
20import { use, type ComponentProps } from "react";
21
22import { RoutedFormSheetContext } from "../../components/ui/RoutedFormSheetContext";
23
24import {
25 isPresentedRoute,
26 projectStackPresentation,
27} from "../../native/workspace-stack-projection";
28import {
29 WorkspaceColumnsRendererContext,
30 WorkspacePresentationHostContext,
31} from "./workspace-presentation-context";
32
33type ViewProps = ComponentProps<typeof NativeStackView>;
34type Descriptor = ViewProps["descriptors"][string];
35type Route = ViewProps["state"]["routes"][number];
36
37function WorkspaceStackView(props: ViewProps) {
38 const renderColumns = use(WorkspaceColumnsRendererContext);
39 if (!renderColumns) return <NativeStackView {...props} />;
40
41 const isOverlay = (route: Route) => {
42 const descriptor = props.descriptors[route.key] ?? props.describe(route, true);
43 return isPresentedRoute(descriptor.options);
44 };
45 const projection = projectStackPresentation(props.state, isOverlay);
46 const workspaceRoutes = projection.workspace;
47 const visibleRoute = workspaceRoutes.at(-1);
48 if (!visibleRoute) return <NativeStackView {...props} />;
49
50 const visibleDescriptor = props.descriptors[visibleRoute.key]!;
51 // Home is the sidebar beside the pages, and under them the page shown when none is selected.
52 const home = workspaceRoutes.find((route) => route.name === "Home");
53 const baseRoute = { key: `${props.state.key}:workspace`, name: "Workspace" };
54 const baseDescriptor: Descriptor = {
55 ...visibleDescriptor,
56 options: { headerShown: false, freezeOnBlur: false },
57 render: () =>
58 renderColumns(
59 <NativeStackView
60 {...props}
61 state={{
62 ...props.state,
63 routes: workspaceRoutes,
64 index: workspaceRoutes.length - 1,
65 preloadedRoutes: props.state.preloadedRoutes.filter((route) => !isOverlay(route)),
66 }}
67 />,
68 home ? props.descriptors[home.key]?.render() : null,
69 ),
70 };
71 const routes = [baseRoute, ...projection.overlays];
72 return (
73 <NativeStackView
74 {...props}
75 presentationEnvelope
76 state={{
77 ...props.state,
78 routes,
79 index: routes.length - 1,
80 preloadedRoutes: props.state.preloadedRoutes.filter(isOverlay),
81 }}
82 descriptors={{ ...props.descriptors, [baseRoute.key]: baseDescriptor }}
83 />
84 );
85}
86
87function WorkspaceStackNavigator({
88 id,
89 initialRouteName,
90 UNSTABLE_routeNamesChangeBehavior,
91 children,
92 layout,
93 screenListeners,
94 screenOptions,
95 screenLayout,
96 UNSTABLE_router,
97 ...rest
98}: NativeStackNavigatorProps) {
99 const { state, describe, descriptors, navigation, NavigationContent } = useNavigationBuilder<
100 StackNavigationState<ParamListBase>,
101 StackRouterOptions,
102 StackActionHelpers<ParamListBase>,
103 NativeStackNavigationOptions,
104 NativeStackNavigationEventMap
105 >(StackRouter, {
106 id,
107 initialRouteName,
108 UNSTABLE_routeNamesChangeBehavior,
109 children,
110 layout,
111 screenListeners,
112 screenOptions,
113 screenLayout,
114 UNSTABLE_router,
115 });
116 return (
117 <WorkspacePresentationHostContext value>
118 <NavigationContent>
119 <WorkspaceStackView
120 {...rest}
121 state={state}
122 describe={describe}
123 descriptors={Object.fromEntries(
124 Object.entries(descriptors).map(([key, descriptor]) => [
125 key,
126 {
127 ...descriptor,
128 // Screens draw their bar in-tree; hiding the native one only after mount
129 // shows it for the first frame and leaves the page measured short.
130 options: { ...descriptor.options, headerShown: false },
131 ...(descriptor.options.presentation === "formSheet"
132 ? {
133 render: () => (
134 <RoutedFormSheetContext value>{descriptor.render()}</RoutedFormSheetContext>
135 ),
136 }
137 : undefined),
138 },
139 ]),
140 )}
141 navigation={navigation}
142 />
143 </NavigationContent>
144 </WorkspacePresentationHostContext>
145 );
146}
147
148type WorkspaceTypeBag<
149 ParamList extends ParamListBase,
150 NavigatorID extends string | undefined,
151> = Omit<NativeStackTypeBag<ParamList, NavigatorID>, "Navigator"> & {
152 Navigator: typeof WorkspaceStackNavigator;
153};
154
155export function createWorkspaceStackNavigator<
156 const ParamList extends ParamListBase,
157 const NavigatorID extends string | undefined = string | undefined,
158 const TypeBag extends NavigatorTypeBagBase = WorkspaceTypeBag<ParamList, NavigatorID>,
159 const Config extends StaticConfig<TypeBag> = StaticConfig<TypeBag>,
160>(config?: Config): TypedNavigator<TypeBag, Config> {
161 return createNavigatorFactory(WorkspaceStackNavigator)(config);
162}