Skip to content

Commit b43750c

Browse files
fix(android): sheet headers render outside navigation again
AppBar.android always registered for hardware back, and that hook checks screen focus, which needs a navigation context. Arrange threads renders its SheetHeader beside the navigator, so opening it would throw. Only a bar with search now owns search state and the back handler. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2a98091 commit b43750c

3 files changed

Lines changed: 113 additions & 30 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { act, type ReactNode } from "react";
2+
import { createRoot, type Root } from "react-dom/client";
3+
import { afterEach, expect, it, vi } from "vite-plus/test";
4+
5+
vi.mock("react-native", () => ({
6+
View: (props: { readonly children?: ReactNode }) => <div>{props.children}</div>,
7+
Keyboard: { dismiss: () => {} },
8+
BackHandler: { addEventListener: () => ({ remove: () => {} }) },
9+
}));
10+
// React Navigation throws this outside a navigator, as for the Arrange threads sheet.
11+
vi.mock("@react-navigation/native", () => ({
12+
useIsFocused: () => {
13+
throw new Error("Couldn't find a navigation object.");
14+
},
15+
}));
16+
vi.mock("./AndroidScreenHeader", () => ({
17+
AndroidScreenHeader: (props: { readonly title: string }) => <h1>{props.title}</h1>,
18+
}));
19+
vi.mock("./WorkspaceAppBar.android", () => ({
20+
useHasWorkspaceAppBar: () => false,
21+
WorkspaceAppBar: () => null,
22+
}));
23+
vi.mock("./Menu", () => ({ Menu: (props: { readonly children?: ReactNode }) => props.children }));
24+
vi.mock("./ScreenHeaderButton.android", () => ({ ScreenHeaderButton: () => null }));
25+
vi.mock("./SearchField", () => ({ SearchField: () => null }));
26+
vi.mock("./useToolbarLayout", () => ({
27+
useToolbarLayout: () => ({ height: 48, paddingTop: 0, paddingBottom: 0 }),
28+
}));
29+
vi.mock("../../native/StackHeader", () => ({ NativeStackScreenOptions: () => null }));
30+
vi.mock("../../features/layout/workspace-sidebar-toolbar", () => ({
31+
AndroidWorkspaceSidebarButton: () => null,
32+
}));
33+
vi.mock("../../features/settings/appearance/AppearancePreferencesProvider", () => ({
34+
useAppearancePreferences: () => ({ themeVariables: {} }),
35+
}));
36+
37+
import { AppBar } from "./AppBar.android";
38+
39+
const roots: Root[] = [];
40+
41+
afterEach(() => {
42+
for (const root of roots.splice(0)) act(() => root.unmount());
43+
document.body.innerHTML = "";
44+
});
45+
46+
it("renders a sheet bar without search outside a navigation container", () => {
47+
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
48+
const container = document.createElement("div");
49+
document.body.append(container);
50+
const root = createRoot(container);
51+
roots.push(root);
52+
53+
act(() => root.render(<AppBar sheet sidebar={false} title="Arrange threads" />));
54+
55+
expect(container.textContent).toBe("Arrange threads");
56+
});

‎apps/mobile/src/components/ui/AppBar.android.tsx‎

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useRef, useState } from "react";
1+
import { type RefObject, useRef, useState } from "react";
22
import { Keyboard, type TextInputInstance, View } from "react-native";
33
import { useHardwareBack } from "../../lib/backHandler";
44
import { useToolbarLayout } from "./useToolbarLayout";
@@ -10,60 +10,86 @@ import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
1010
import { Menu } from "./Menu";
1111
import { SearchField } from "./SearchField";
1212
import { androidHeaderMenuActions, findHeaderMenuAction } from "./headerMenu.android";
13-
import type { AppBarMenuItem, AppBarProps } from "./AppBar.types";
13+
import type { AppBarMenuItem, AppBarProps, AppBarSearch } from "./AppBar.types";
1414
import { useHasWorkspaceAppBar, WorkspaceAppBar } from "./WorkspaceAppBar.android";
1515

1616
function selectMenuItem(items: ReadonlyArray<AppBarMenuItem>, id: string) {
1717
const action = findHeaderMenuAction(items, id);
1818
if (action && !action.disabled) action.onPress();
1919
}
2020

21+
interface SearchState {
22+
readonly searching: boolean;
23+
readonly open: () => void;
24+
readonly close: () => void;
25+
readonly inputRef: RefObject<TextInputInstance | null>;
26+
}
27+
2128
export function AppBar(props: AppBarProps) {
29+
return props.search ? (
30+
<SearchableAppBar {...props} search={props.search} />
31+
) : (
32+
<AppBarView {...props} />
33+
);
34+
}
35+
36+
// Only a bar with search registers for hardware back: its focus check needs a navigation
37+
// context, and sheet headers such as Arrange threads render outside one.
38+
function SearchableAppBar(props: AppBarProps & { readonly search: AppBarSearch }) {
2239
const { search } = props;
23-
const { paddingTop, paddingBottom } = useToolbarLayout(props.sheet);
24-
const { themeVariables } = useAppearancePreferences();
25-
const inWorkspace = useHasWorkspaceAppBar();
2640
const [searchOpen, setSearchOpen] = useState(false);
2741
const inputRef = useRef<TextInputInstance>(null);
28-
const inline = search?.mode === "inline";
29-
const controlled = search?.active !== undefined;
42+
const inline = search.mode === "inline";
43+
const controlled = search.active !== undefined;
3044
const searching =
31-
search !== undefined &&
32-
(inline || (controlled ? search.active === true : searchOpen || search.value.length > 0));
33-
const openSearch = () => {
45+
inline || (controlled ? search.active === true : searchOpen || search.value.length > 0);
46+
const open = () => {
3447
if (!controlled) setSearchOpen(true);
35-
search?.onOpen?.();
48+
search.onOpen?.();
3649
inputRef.current?.focus();
3750
};
38-
const closeSearch = () => {
51+
const close = () => {
3952
if (!controlled) {
40-
search?.onChangeText("");
53+
search.onChangeText("");
4154
setSearchOpen(false);
4255
Keyboard.dismiss();
4356
}
44-
search?.onClose?.();
57+
search.onClose?.();
4558
};
4659
useHardwareBack(searching && !inline && !controlled, () => {
47-
closeSearch();
60+
close();
4861
return true;
4962
});
63+
return <AppBarView {...props} searchState={{ searching, open, close, inputRef }} />;
64+
}
65+
66+
function AppBarView(props: AppBarProps & { readonly searchState?: SearchState }) {
67+
const { search, searchState } = props;
68+
const { paddingTop, paddingBottom } = useToolbarLayout(props.sheet);
69+
const { themeVariables } = useAppearancePreferences();
70+
const inWorkspace = useHasWorkspaceAppBar();
71+
const inline = search?.mode === "inline";
72+
const searching = searchState?.searching === true;
73+
const openSearch = () => searchState?.open();
74+
const closeSearch = () => searchState?.close();
5075

5176
const searchLabel = search?.accessibilityLabel ?? search?.placeholder ?? "";
52-
const searchField = search ? (
53-
<SearchField
54-
inputRef={inputRef}
55-
accessibilityLabel={searchLabel}
56-
clearAccessibilityLabel={search.clearAccessibilityLabel ?? "Clear search"}
57-
placeholder={
58-
search.compactToolbar
59-
? (search.compactPlaceholder ?? search.placeholder)
60-
: search.placeholder
61-
}
62-
value={search.value}
63-
onChangeText={search.onChangeText}
64-
autoFocus={!inline}
65-
/>
66-
) : null;
77+
const searchField =
78+
search && searchState ? (
79+
<SearchField
80+
inputRef={searchState.inputRef}
81+
accessibilityLabel={searchLabel}
82+
clearAccessibilityLabel={search.clearAccessibilityLabel ?? "Clear search"}
83+
placeholder={
84+
search.compactToolbar
85+
? (search.compactPlaceholder ?? search.placeholder)
86+
: search.placeholder
87+
}
88+
value={search.value}
89+
onChangeText={search.onChangeText}
90+
autoFocus={!inline}
91+
/>
92+
) : null;
6793

6894
if (inWorkspace) {
6995
const menu = props.menus?.[0];

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const reactTests = [
2020
"src/components/ProjectFavicon.test.tsx",
2121
"src/components/ui/Dialog.android.test.tsx",
2222
"src/features/pull-requests/usePullRequestList.test.tsx",
23+
"src/components/ui/AppBar.android.test.tsx",
2324
"src/features/files/ZipArchiveView.test.tsx",
2425
"src/features/pull-requests/guide/useReviewGuide.test.tsx",
2526
"src/features/settings/SettingsServerControlsRouteScreen.test.tsx",

0 commit comments

Comments
 (0)