Skip to content

Commit 5cf4995

Browse files
feat(mobile): the folded Duo opens search from a search tab below the tab bar (#24360)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5848bc2 commit 5cf4995

5 files changed

Lines changed: 78 additions & 30 deletions

File tree

‎apps/mobile/src/features/workspace/WorkspaceNativeTabs.ios.tsx‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
} from "./navigation-tabs";
66
import { useCallback, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react";
77
import { View, useWindowDimensions, type NativeSyntheticEvent } from "react-native";
8-
import { Tabs, type TabSelectedEvent, type TabSelectionRejectedEvent } from "react-native-screens";
8+
import {
9+
Tabs,
10+
type TabSelectedEvent,
11+
type TabSelectionPreventedEvent,
12+
type TabSelectionRejectedEvent,
13+
} from "react-native-screens";
914

1015
import { WorkspaceTabsContext, useWorkspaceTabs } from "./WorkspaceTabsContext";
1116
import {
@@ -26,6 +31,7 @@ import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";
2631
import { useSelectWorkspaceTab } from "./useSelectWorkspaceTab";
2732

2833
const PULL_REQUEST_ICON = require("../../../assets/icons/pull-request.png");
34+
const SEARCH_SLOT = "search";
2935

3036
/** Retain each native controller tree; removing its root can leave RNS's stack empty. */
3137
function RetainedWorkspaceTab(props: {
@@ -93,6 +99,17 @@ export function WorkspaceNativeTabs(props: {
9399
const { colors } = useControlTheme();
94100
const selectedTab = workspace.state.selectedTab;
95101
const selectedSlot = workspace.settingsOpen ? -1 : primaryTabs.indexOf(selectedTab);
102+
// The folded Duo opens list search from a search tab UIKit sets apart below the others.
103+
const searchTab = deviceType === "duo-folded";
104+
// Searching is list state: the list's toolbar mounts its field and focuses it.
105+
const openSearch = () => {
106+
const tab =
107+
!workspace.settingsOpen && workspaceUsesColumns(selectedTab) ? selectedTab : "threads";
108+
// Back on the tab's list first: folded, a chat open on top would hide the field.
109+
selectTab(tab);
110+
workspace.dispatch({ type: "scroll", tab, offset: workspace.getScrollOffset(tab) });
111+
workspace.dispatch({ type: "search", tab });
112+
};
96113
const request = useMemo(
97114
() => ({
98115
selectedScreenKey:
@@ -116,9 +133,19 @@ export function WorkspaceNativeTabs(props: {
116133
}: NativeSyntheticEvent<TabSelectionRejectedEvent>) =>
117134
setProvenance(nativeEvent.provenance)
118135
}
136+
onTabSelectionPrevented={({
137+
nativeEvent,
138+
}: NativeSyntheticEvent<TabSelectionPreventedEvent>) => {
139+
if (nativeEvent.preventedScreenKey === SEARCH_SLOT) openSearch();
140+
}}
119141
onTabSelected={({ nativeEvent }: NativeSyntheticEvent<TabSelectedEvent>) => {
120142
setProvenance(nativeEvent.provenance);
121143
if (nativeEvent.actionOrigin === "programmatic-js") return;
144+
// UIKit reports reselecting a tab here even when its selection is prevented.
145+
if (nativeEvent.selectedScreenKey === SEARCH_SLOT) {
146+
openSearch();
147+
return;
148+
}
122149
const tab = workspaceTabForNativeSlot(primaryTabs, nativeEvent.selectedScreenKey);
123150
if (tab) selectTab(tab);
124151
}}
@@ -156,6 +183,24 @@ export function WorkspaceNativeTabs(props: {
156183
</Tabs.Screen>
157184
);
158185
})}
186+
{searchTab ? (
187+
// It never becomes the selected tab; pressing it opens the current list's search.
188+
<Tabs.Screen
189+
key={SEARCH_SLOT}
190+
screenKey={SEARCH_SLOT}
191+
title=""
192+
tabBarItemAccessibilityLabel="Search"
193+
preventNativeSelection
194+
ios={{
195+
role: "search",
196+
systemItem: "search",
197+
// iOS 27 only sets the search tab apart while it activates search.
198+
automaticallyActivatesSearch: true,
199+
}}
200+
>
201+
<View />
202+
</Tabs.Screen>
203+
) : null}
159204
</Tabs.Host>
160205
{(deviceType === "pad" ||
161206
compactTabs ||

‎apps/mobile/src/features/workspace/WorkspaceToolbar.ios.tsx‎

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -121,32 +121,19 @@ export function WorkspaceToolbar(props: {
121121
separateBackground
122122
/>
123123
) : null;
124-
const searchButton = foldedSearch ? (
125-
<Toolbar.Button
126-
identifier="workspace-search"
127-
accessibilityLabel={
128-
searchVisible
129-
? "Close search"
130-
: selectedTab === "pullRequests"
131-
? "Search pull requests"
132-
: "Search threads"
133-
}
134-
icon={searchVisible ? "xmark" : "magnifyingglass"}
135-
onPress={searchVisible ? () => searchRef.current?.cancelSearch() : focusSearch}
136-
separateBackground
137-
/>
138-
) : null;
139124
return (
140125
<>
141126
<NativeStackScreenOptions
142-
optionsVersion={[selectedTab, tab.searchMode]}
127+
optionsVersion={[selectedTab, tab.searchMode, tab.searchFocusRequest]}
143128
listeners={{ transitionEnd: restoreSearch }}
144129
options={{
130+
// Each search request focuses the field once it exists, after it mounts if need be.
131+
unstable_headerSearchFocusRequest:
132+
tab.searchMode === "active" ? tab.searchFocusRequest : undefined,
145133
headerSearchBarOptions:
146134
(NATIVE_LIQUID_GLASS_SUPPORTED || tab.searchMode !== "resting") && searchVisible
147135
? {
148136
ref: searchRef,
149-
autoFocus: tab.searchMode === "active",
150137
autoCapitalize: "none",
151138
hideNavigationBar: false,
152139
hideWhenScrolling: false,
@@ -162,7 +149,6 @@ export function WorkspaceToolbar(props: {
162149
sidebarTabs !== "picker",
163150
placeholder:
164151
selectedTab === "pullRequests" ? "Search pull requests" : "Search threads",
165-
cancelButtonHidden: foldedSearch,
166152
onFocus: searchFocused,
167153
onBlur: () => workspace.dispatch({ type: "expand", tab: selectedTab }),
168154
onSearchButtonPress: () => searchRef.current?.blur(),
@@ -179,8 +165,6 @@ export function WorkspaceToolbar(props: {
179165
/>
180166
{/* UIKit lays trailing items out from the edge inward. */}
181167
<Toolbar placement="right">
182-
{searchButton}
183-
{nativeToolbarCompose && foldedSearch ? <Toolbar.Spacer width={8} /> : null}
184168
{composeButton}
185169
{selectedTab !== "threads" ||
186170
isDuoDevice(deviceType) ||

‎apps/mobile/src/features/workspace/workspace-tabs-state.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export interface WorkspaceTabState {
2424
readonly searchMode: WorkspaceSearchMode;
2525
readonly scrollOffset: number;
2626
readonly searchEntryOffset: number;
27+
/** Counts requests to focus the search field; the header focuses once per request. */
28+
readonly searchFocusRequest: number;
2729
}
2830

2931
export interface WorkspaceTabsState {
@@ -33,7 +35,13 @@ export interface WorkspaceTabsState {
3335
}
3436

3537
function emptyTab(): WorkspaceTabState {
36-
return { query: "", searchMode: "resting", scrollOffset: 0, searchEntryOffset: 0 };
38+
return {
39+
query: "",
40+
searchMode: "resting",
41+
scrollOffset: 0,
42+
searchEntryOffset: 0,
43+
searchFocusRequest: 0,
44+
};
3745
}
3846

3947
export function initialWorkspaceTabsState(): WorkspaceTabsState {
@@ -79,6 +87,7 @@ export function workspaceTabsReducer(
7987
searchMode: "active",
8088
searchEntryOffset:
8189
current.searchMode === "resting" ? current.scrollOffset : current.searchEntryOffset,
90+
searchFocusRequest: current.searchFocusRequest + 1,
8291
};
8392
break;
8493
case "query":

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export type AppNativeStackNavigationOptions = Omit<
3434
readonly unstable_headerToolbarItems?: unknown;
3535
readonly unstable_navigationItemStyle?: unknown;
3636
readonly unstable_headerMinimizationBehavior?: unknown;
37+
/** Focuses the header search field once for each new value, as soon as the field exists. */
38+
readonly unstable_headerSearchFocusRequest?: number;
3739
};
3840

3941
function useNativeStackNavigation(): NativeStackNavigationProp<ParamListBase> | null {

‎apps/mobile/src/native/V5StackHeader.ios.tsx‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
NativeStackHeaderItemMenu,
44
NativeStackNavigationOptions,
55
} from "@react-navigation/native-stack";
6-
import { isValidElement, useEffect, useRef } from "react";
6+
import { isValidElement, useEffect, useRef, type RefObject } from "react";
77
import { StyleSheet } from "react-native";
88
import { isDuoDevice } from "../lib/device-type";
99
import { useMobileNavigationTheme } from "../lib/useMobileNavigationTheme";
@@ -29,6 +29,7 @@ type ToolbarItem =
2929
type Options = NativeStackNavigationOptions & {
3030
readonly unstable_navigationItemStyle?: "editor" | "navigator";
3131
readonly unstable_headerMinimizationBehavior?: StackHeaderMinimizationBehaviorIOS;
32+
readonly unstable_headerSearchFocusRequest?: number;
3233
readonly headerSubtitle?: string;
3334
readonly unstable_headerSubtitle?: string;
3435
readonly headerSubtitleStyle?: { color?: string; fontSize?: number; fontFamily?: string };
@@ -243,12 +244,18 @@ export function V5StackHeader(props: {
243244
? options.headerTitle({ children: title ?? "", tintColor: options.headerTintColor })
244245
: undefined;
245246
const searchOptions = options.headerSearchBarOptions;
246-
const autofocusRef = searchOptions?.ref ?? searchRef;
247-
useEffect(() => {
248-
if (!searchOptions?.autoFocus) return;
249-
const frame = requestAnimationFrame(() => autofocusRef.current?.focus());
250-
return () => cancelAnimationFrame(frame);
251-
}, [searchOptions?.autoFocus, autofocusRef]);
247+
const searchBarRef = searchOptions?.ref ?? searchRef;
248+
const focusRequest = options.unstable_headerSearchFocusRequest;
249+
const focusedRequest = useRef<number | undefined>(undefined);
250+
// The field can attach a render after the request, since it mounts through these options, and
251+
// Screens re-attaches its commands on every render; so focus once per request, on attach.
252+
const attachSearchBar = (instance: SearchBarCommands | null) => {
253+
searchBarRef.current = instance;
254+
if (instance && focusRequest !== undefined && focusedRequest.current !== focusRequest) {
255+
focusedRequest.current = focusRequest;
256+
instance.focus();
257+
}
258+
};
252259
const titleStyle = StyleSheet.flatten(options.headerTitleStyle);
253260
const headerStyle = StyleSheet.flatten(options.headerStyle);
254261
const appearance = {
@@ -288,7 +295,8 @@ export function V5StackHeader(props: {
288295
mailSearch || searchOptions ? (
289296
<SearchBar
290297
{...searchOptions}
291-
ref={searchOptions?.ref ?? searchRef}
298+
// React accepts callback refs here; Screens only types the object form.
299+
ref={attachSearchBar as unknown as RefObject<SearchBarCommands | null>}
292300
hideNavigationBar={false}
293301
hideWhenScrolling={false}
294302
allowToolbarIntegration={searchOptions?.allowToolbarIntegration ?? true}

0 commit comments

Comments
 (0)