Skip to content

Commit 571961d

Browse files
committed
fix(mobile): skip native header options outside route screens
1 parent f7fa9e7 commit 571961d

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎apps/mobile/src/native/NativeHeaderToolbar.test.tsx‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
2+
import { NavigationContext } from "@react-navigation/native";
23
import { act, createContext, startTransition, Suspense, type ReactElement } from "react";
34
import { createRoot, type Root } from "react-dom/client";
45
import { afterEach, expect, it, vi } from "vite-plus/test";
@@ -20,7 +21,7 @@ const harness = vi.hoisted(() => ({
2021
// Navigation, the header renders again only when the options are no longer shallowly equal.
2122
vi.mock("@react-navigation/native", async () => {
2223
// vi.mock factories run before the file's imports.
23-
const { createContext } = await vi.importActual<typeof import("react")>("react");
24+
const { createContext, use } = await vi.importActual<typeof import("react")>("react");
2425
const navigation = {
2526
setOptions(options: HeaderOptions) {
2627
harness.setOptionsCalls += 1;
@@ -39,15 +40,23 @@ vi.mock("@react-navigation/native", async () => {
3940
[];
4041
},
4142
};
43+
const navigationContext = createContext<typeof navigation | undefined>(navigation);
4244
return {
4345
// Outside a workspace tab slot the header writes to the route's navigation.
4446
NavigationRouteContext: createContext(undefined),
45-
useNavigation: () => navigation,
47+
NavigationContext: navigationContext,
48+
useNavigation: () =>
49+
use(navigationContext) ?? {
50+
setOptions() {
51+
throw new Error("Cannot call setOptions outside a screen");
52+
},
53+
},
4654
useRoute: () => ({ key: "file", name: "ThreadFile", params: harness.params }),
4755
};
4856
});
4957

5058
import { NativeHeaderToolbar } from "./NativeHeaderToolbar";
59+
import { NativeStackScreenOptions } from "./StackHeader";
5160

5261
let root: Root | null = null;
5362

@@ -97,6 +106,19 @@ afterEach(() => {
97106
vi.unstubAllGlobals();
98107
});
99108

109+
it("does not configure a native screen from an embedded pane", () => {
110+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
111+
root = createRoot(document.createElement("div"));
112+
act(() =>
113+
root!.render(
114+
<NavigationContext value={undefined}>
115+
<NativeStackScreenOptions options={{ headerShown: false }} />
116+
</NavigationContext>,
117+
),
118+
);
119+
expect(harness.setOptionsCalls).toBe(0);
120+
});
121+
100122
it.each(["left", "right", "bottom"] as const)(
101123
"dispatches retained %s actions to the latest committed handlers without registering again",
102124
(placement) => {

‎apps/mobile/src/native/StackHeader.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2+
NavigationContext,
23
NavigationRouteContext,
3-
useNavigation,
44
type ParamListBase,
55
} from "@react-navigation/native";
66
import type {
@@ -37,7 +37,8 @@ export type AppNativeStackNavigationOptions = Omit<
3737
};
3838

3939
function useNativeStackNavigation(): NativeStackNavigationProp<ParamListBase> | null {
40-
return useNavigation<NativeStackNavigationProp<ParamListBase>>();
40+
const navigation = use(NavigationContext);
41+
return navigation ? (navigation as NativeStackNavigationProp<ParamListBase>) : null;
4142
}
4243

4344
function normalizeScreenOptions(

0 commit comments

Comments
 (0)