Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion apps/mobile/src/native/NativeHeaderToolbar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @vitest-environment jsdom
import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
import { act } from "react";
import { act, createContext, use } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, expect, it, vi } from "vite-plus/test";

Expand Down Expand Up @@ -99,3 +99,29 @@ it.each(["left", "right", "bottom"] as const)(
expect(harness.openedDocuments).toEqual(["first/guide.pdf"]);
},
);

// React Navigation's context defaults throw from getters, and dev elements keep their owner fiber.
const ThrowingDefaultContext = createContext({
get value(): never {
throw new Error("Couldn't find a navigation context.");
},
});

function CustomViewToolbar() {
use(ThrowingDefaultContext);
return (
<NativeHeaderToolbar placement="right">
<NativeHeaderToolbar.Custom>
<span>Status</span>
</NativeHeaderToolbar.Custom>
</NativeHeaderToolbar>
);
}

it("applies custom header views created under contexts whose defaults throw", () => {
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
root ??= createRoot(document.createElement("div"));
act(() => root!.render(<CustomViewToolbar />));

expect(harness.renderedItems.map((item) => item.type)).toEqual(["custom"]);
});
9 changes: 8 additions & 1 deletion apps/mobile/src/native/StackHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
NativeStackNavigationOptions,
NativeStackNavigationProp,
} from "@react-navigation/native-stack";
import { use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
import { isValidElement, use, useEffect, useLayoutEffect, useMemo, useRef } from "react";
import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context";
import type { ColorValue } from "react-native";

Expand Down Expand Up @@ -81,6 +81,13 @@ function optionsSignature(value: unknown, seen = new WeakSet<object>()): string
// React refs carry mutable native instances that must not make static
// screen options appear different after every render.
if ("current" in object) return "[ref]";
// Custom header views are elements. Only their type, key, and props describe them;
// dev-only internals such as the owner fiber reach context defaults that throw.
if (isValidElement(object)) {
const type = object.type as { displayName?: string; name?: string } | string;
const typeName = typeof type === "string" ? type : (type.displayName ?? type.name);
return `element:${typeName}:${String(object.key)}:${optionsSignature(object.props, seen)}`;
}
return `{${Object.keys(value as Record<string, unknown>)
.sort()
.map(
Expand Down
Loading