Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

workspace-stack-projection.ts

146 lines4.9 KB

1import type {
2 NavigationState,
3 ParamListBase,
4 StackNavigationState,
5} from "@react-navigation/native";
6​
7type Route = StackNavigationState<ParamListBase>["routes"][number];
8​
9export function projectWorkspaceStack(
10 state: StackNavigationState<ParamListBase>,
11 isOverlay: (route: Route) => boolean,
12) {
13 const firstOverlay = state.routes.findIndex(isOverlay);
14 const workspaceRoutes = state.routes.slice(0, firstOverlay < 0 ? undefined : firstOverlay);
15 const primary = workspaceRoutes.find((route) => route.name === "Home");
16 const detail = workspaceRoutes.filter((route) => route.name !== "Home");
17 const overlays = firstOverlay < 0 ? [] : state.routes.slice(firstOverlay);
18 return { primary, detail, overlays };
20​
21/** Native callbacks can arrive after a JS pop or replace; those must not pop the next screen. */
22export function nativeWorkspacePopCount(
23 state: Pick<NavigationState, "index" | "routes">,
24 dismissedKey: string,
25): number {
26 const index = state.routes.findIndex((route) => route.key === dismissedKey);
27 return index <= 0 || index > state.index ? 0 : state.index - index + 1;
29​
30/** POP counts backwards from its source, so dismiss a group from the active descendant. */
31export function nativeWorkspacePopAction(
32 state: Pick<NavigationState, "key" | "index" | "routes">,
33 dismissedKey: string,
34) {
35 const count = nativeWorkspacePopCount(state, dismissedKey);
36 return count
37 ? {
38 type: "POP" as const,
39 payload: { count },
40 source: state.routes[state.index]!.key,
41 target: state.key,
42 }
43 : null;
45​
46/** A native card pop must preserve pushes made before its completion event reaches JS. */
47export function nativeStackPopAction(
48 state: Pick<NavigationState, "key" | "index" | "routes">,
49 dismissedKey: string,
50 destinationKey?: string,
51) {
52 const sourceIndex = state.routes.findIndex((route) => route.key === dismissedKey);
53 const destinationIndex =
54 destinationKey === undefined
55 ? sourceIndex - 1
56 : state.routes.findIndex((route) => route.key === destinationKey);
57 if (
58 sourceIndex <= 0 ||
59 sourceIndex > state.index ||
60 destinationIndex < 0 ||
61 destinationIndex >= sourceIndex
62 ) {
63 return null;
64 }
65 return {
66 type: "POP" as const,
67 payload: { count: sourceIndex - destinationIndex },
68 source: dismissedKey,
69 target: state.key,
70 };
72​
73/** The column UIKit shows while a split is collapsed, and the active detail it began removing. */
74export interface CollapsedSplitColumn {
75 readonly visible: "primary" | "secondary";
76 readonly leaving: string | null;
78​
79/**
80 * A collapsed split nests the detail column's navigation controller in the list column's. Back
81 * from the detail root pops that controller whole, so no screen leaves its own stack or reports a
82 * dismissal: the list appearing after the active detail began leaving is the only sign of it.
83 */
84export function collapsedSplitTransition(
85 column: CollapsedSplitColumn,
86 event: { readonly type: "willAppear" | "didAppear" | "willDisappear"; readonly key: string },
87 state: Pick<NavigationState, "key" | "index" | "routes">,
88 primaryKey: string,
89): {
90 readonly column: CollapsedSplitColumn;
91 readonly pop: ReturnType<typeof nativeStackPopAction>;
92} {
93 switch (event.type) {
94 case "willDisappear":
95 // JS pushes, pops, and presentations have already moved the router past this screen.
96 return {
97 column:
98 state.index > 0 && state.routes[state.index]?.key === event.key
99 ? { ...column, leaving: event.key }
100 : column,
101 pop: null,
102 };
103 case "willAppear":
104 // A cancelled swipe, or an expansion that keeps the detail on screen.
105 return {
106 column: column.leaving === event.key ? { ...column, leaving: null } : column,
107 pop: null,
108 };
109 case "didAppear":
110 return {
111 column: { visible: event.key === primaryKey ? "primary" : "secondary", leaving: null },
112 pop:
113 event.key === primaryKey && column.leaving
114 ? nativeStackPopAction(state, column.leaving, primaryKey)
115 : null,
116 };
117 }
119​
120export function partitionStackPresentations<T>(
121 routes: readonly T[],
122 isModal: (route: T) => boolean,
123): T[][] {
124 const groups: T[][] = [];
125 for (const route of routes) {
126 if (groups.length === 0 || isModal(route)) groups.push([]);
127 groups.at(-1)!.push(route);
128 }
129 return groups;
131​
132/** Keep outgoing screens in place until dismissal; Fabric moves enqueue native pop/push operations. */
133export function reconcileStackScreens<T extends { readonly key: string }>(
134 previous: readonly T[],
135 current: readonly T[],
136 completedNativeDismissals: ReadonlySet<string> = new Set(),
137): T[] {
138 const active = new Set(current.map((route) => route.key));
139 const screens = [...current];
140 previous.forEach((route, index) => {
141 if (!active.has(route.key) && !completedNativeDismissals.has(route.key)) {
142 screens.splice(Math.min(index, screens.length), 0, route);
143 }
144 });
145 return screens;