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 { projectStackPresentation } from "../../native/workspace-stack-projection";
25import {
26 WorkspaceColumnsRendererContext,
27 WorkspacePresentationHostContext,
28} from "./workspace-presentation-context";
29
30type ViewProps = ComponentProps<typeof NativeStackView>;
31type Descriptor = ViewProps["descriptors"][string];
32type Route = ViewProps["state"]["routes"][number];
33
34function WorkspaceStackView(props: ViewProps) {
35 const renderColumns = use(WorkspaceColumnsRendererContext);
36 if (!renderColumns) return <NativeStackView {...props} />;
37
38 const isOverlay = (route: Route) => {
39 const descriptor = props.descriptors[route.key] ?? props.describe(route, true);
40 const presentation = descriptor.options.presentation;
41 return presentation !== undefined && presentation !== "card";
42 };
43 const projection = projectStackPresentation(props.state, isOverlay);
44 const workspaceRoutes = projection.workspace;
45 const visibleRoute = workspaceRoutes.at(-1);
46 if (!visibleRoute) return <NativeStackView {...props} />;
47
48 const visibleDescriptor = props.descriptors[visibleRoute.key]!;
49 const baseRoute = { key: `${props.state.key}:workspace`, name: "Workspace" };
50 const baseDescriptor: Descriptor = {
51 ...visibleDescriptor,
52 options: { headerShown: false, freezeOnBlur: false },
53 render: () =>
54 renderColumns(
55 <NativeStackView
56 {...props}
57 state={{
58 ...props.state,
59 routes: workspaceRoutes,
60 index: workspaceRoutes.length - 1,
61 preloadedRoutes: props.state.preloadedRoutes.filter((route) => !isOverlay(route)),
62 }}
63 />,
64 ),
65 };
66 const routes = [baseRoute, ...projection.overlays];
67 return (
68 <NativeStackView
69 {...props}
70 presentationEnvelope
71 state={{
72 ...props.state,
73 routes,
74 index: routes.length - 1,
75 preloadedRoutes: props.state.preloadedRoutes.filter(isOverlay),
76 }}
77 descriptors={{ ...props.descriptors, [baseRoute.key]: baseDescriptor }}
78 />
79 );
80}
81
82function WorkspaceStackNavigator({
83 id,
84 initialRouteName,
85 UNSTABLE_routeNamesChangeBehavior,
86 children,
87 layout,
88 screenListeners,
89 screenOptions,
90 screenLayout,
91 UNSTABLE_router,
92 ...rest
93}: NativeStackNavigatorProps) {
94 const { state, describe, descriptors, navigation, NavigationContent } = useNavigationBuilder<
95 StackNavigationState<ParamListBase>,
96 StackRouterOptions,
97 StackActionHelpers<ParamListBase>,
98 NativeStackNavigationOptions,
99 NativeStackNavigationEventMap
100 >(StackRouter, {
101 id,
102 initialRouteName,
103 UNSTABLE_routeNamesChangeBehavior,
104 children,
105 layout,
106 screenListeners,
107 screenOptions,
108 screenLayout,
109 UNSTABLE_router,
110 });
111 return (
112 <WorkspacePresentationHostContext value>
113 <NavigationContent>
114 <WorkspaceStackView
115 {...rest}
116 state={state}
117 describe={describe}
118 descriptors={Object.fromEntries(
119 Object.entries(descriptors).map(([key, descriptor]) => [
120 key,
121 descriptor.options.presentation === "formSheet"
122 ? {
123 ...descriptor,
124 render: () => (
125 <RoutedFormSheetContext value>{descriptor.render()}</RoutedFormSheetContext>
126 ),
127 }
128 : descriptor,
129 ]),
130 )}
131 navigation={navigation}
132 />
133 </NavigationContent>
134 </WorkspacePresentationHostContext>
135 );
136}
137
138type WorkspaceTypeBag<
139 ParamList extends ParamListBase,
140 NavigatorID extends string | undefined,
141> = Omit<NativeStackTypeBag<ParamList, NavigatorID>, "Navigator"> & {
142 Navigator: typeof WorkspaceStackNavigator;
143};
144
145export function createWorkspaceStackNavigator<
146 const ParamList extends ParamListBase,
147 const NavigatorID extends string | undefined = string | undefined,
148 const TypeBag extends NavigatorTypeBagBase = WorkspaceTypeBag<ParamList, NavigatorID>,
149 const Config extends StaticConfig<TypeBag> = StaticConfig<TypeBag>,
150>(config?: Config): TypedNavigator<TypeBag, Config> {
151 return createNavigatorFactory(WorkspaceStackNavigator)(config);
152}