Skip to content
TypeScript

fix(mobile): disabled iOS header controls now show as disabled (#24088)

Victor Vogel59bb4e83bcf7
diff --git a/apps/mobile/src/native/V5StackHeader.ios.test.tsx b/apps/mobile/src/native/V5StackHeader.ios.test.tsx
new file mode 100644
index 00000000000..7f794e3f220
--- /dev/null
+++ b/apps/mobile/src/native/V5StackHeader.ios.test.tsx
@@ -0,0 +1,76 @@
+import type { NativeStackHeaderItem } from "@react-navigation/native-stack";
+import { act } from "react";
+import { createRoot } from "react-dom/client";
+import type { StackHeaderConfigProps } from "react-native-screens";
+import { expect, it, vi } from "vite-plus/test";
+
+const bridge = vi.hoisted(() => ({ ios: undefined as StackHeaderConfigProps["ios"] }));
+
+vi.mock("react-native", () => ({ StyleSheet: { flatten: (style: unknown) => style } }));
+vi.mock("react-native-keyboard-controller", () => ({
+ KeyboardController: { dismiss: vi.fn() },
+ useKeyboardState: () => false,
+}));
+vi.mock("../lib/useMobileNavigationTheme", () => ({
+ useMobileNavigationTheme: () => ({ colors: { card: "white" } }),
+}));
+vi.mock("./native-layout-metrics", () => ({
+ useDeviceType: () => "phone",
+ useNativeLayoutMetrics: () => null,
+}));
+vi.mock("react-native-screens", () => ({
+ Stack: {
+ HeaderConfig: ({ ios }: StackHeaderConfigProps) => {
+ bridge.ios = ios;
+ return null;
+ },
+ },
+}));
+
+import { V5StackHeader } from "./V5StackHeader.ios";
+
+it.each(["leadingItems", "trailingItems", "toolbarItems"] as const)(
+ "updates native enabled state after permissions change in %s",
+ (placement) => {
+ bridge.ios = undefined;
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ const root = createRoot(document.createElement("div"));
+ const onPress = () => {};
+ try {
+ for (const disabled of [true, false, undefined, true]) {
+ const items: NativeStackHeaderItem[] = [
+ { type: "button", label: "Files", disabled, onPress },
+ {
+ type: "menu",
+ label: "Terminal",
+ disabled,
+ menu: { items: [{ type: "action", label: "Open terminal", onPress }] },
+ },
+ ];
+ act(() =>
+ root.render(
+ <V5StackHeader
+ canGoBack={false}
+ options={{
+ unstable_headerLeftItems: () => (placement === "leadingItems" ? items : []),
+ unstable_headerRightItems: () => (placement === "trailingItems" ? items : []),
+ unstable_headerToolbarItems: () => (placement === "toolbarItems" ? items : []),
+ }}
+ />,
+ ),
+ );
+ const nativeItems = (bridge.ios as StackHeaderConfigProps["ios"])?.[placement];
+ expect(nativeItems).toHaveLength(2);
+ for (const item of nativeItems ?? []) {
+ if (item.type !== "item" || "render" in item)
+ throw new Error("Expected a native bar item");
+ // RNSHeaderContentFactory sets UIBarButtonItem.enabled to !item.disabled.
+ expect(!item.disabled).toBe(!disabled);
+ }
+ }
+ } finally {
+ act(() => root.unmount());
+ vi.unstubAllGlobals();
+ }
+ },
+);
diff --git a/apps/mobile/src/native/V5StackHeader.ios.tsx b/apps/mobile/src/native/V5StackHeader.ios.tsx
index 9caf92d5834..f6bbefce285 100644
--- a/apps/mobile/src/native/V5StackHeader.ios.tsx
+++ b/apps/mobile/src/native/V5StackHeader.ios.tsx
@@ -124,6 +124,7 @@ function convertItems(items: NativeStackHeaderItem[], prefix: string): HeaderIte
axisBehavior: item.axisBehavior,
icon: convertIcon(item.icon),
menu: item.disabled ? undefined : convertMenu(item.menu, `${id}:menu`),
+ disabled: item.disabled,
variant: item.variant,
tintColor: item.tintColor,
hidesSharedBackground: item.hidesSharedBackground,
@@ -137,6 +138,7 @@ function convertItems(items: NativeStackHeaderItem[], prefix: string): HeaderIte
axisBehavior: item.axisBehavior,
icon: convertIcon(item.icon),
onPress: item.disabled ? undefined : item.onPress,
+ disabled: item.disabled,
visibilityPriority:
item.visibilityPriority === "default" ? "standard" : item.visibilityPriority,
variant: item.variant,
diff --git a/apps/mobile/vite.config.ts b/apps/mobile/vite.config.ts
index 9097287717f..6e246366716 100644
--- a/apps/mobile/vite.config.ts
+++ b/apps/mobile/vite.config.ts
@@ -27,6 +27,7 @@ const reactTests = [
"src/features/threads/UsageLimitRecoveryCard.test.tsx",
"src/features/threads/NewTaskDraftRouteScreen.test.tsx",
"src/native/NativeHeaderToolbar.test.tsx",
+ "src/native/V5StackHeader.ios.test.tsx",
"src/native/useRestingTopOffset.test.tsx",
"src/native/useRetryingImageSource.test.tsx",
"src/state/queries.debounce.test.tsx",