TypeScript
feat(mobile): the folded Duo opens search from a search tab below the tab bar (#24360)
Julius Marminge5cf4995ce151
diff --git a/apps/mobile/src/features/workspace/WorkspaceNativeTabs.ios.tsx b/apps/mobile/src/features/workspace/WorkspaceNativeTabs.ios.tsxindex 344e787d044..bcdc835366a 100644--- a/apps/mobile/src/features/workspace/WorkspaceNativeTabs.ios.tsx+++ b/apps/mobile/src/features/workspace/WorkspaceNativeTabs.ios.tsx@@ -5,7 +5,12 @@ import {} from "./navigation-tabs";import { useCallback, useLayoutEffect, useMemo, useRef, useState, type ReactNode } from "react";import { View, useWindowDimensions, type NativeSyntheticEvent } from "react-native";-import { Tabs, type TabSelectedEvent, type TabSelectionRejectedEvent } from "react-native-screens";+import {+ Tabs,+ type TabSelectedEvent,+ type TabSelectionPreventedEvent,+ type TabSelectionRejectedEvent,+} from "react-native-screens";import { WorkspaceTabsContext, useWorkspaceTabs } from "./WorkspaceTabsContext";import {@@ -26,6 +31,7 @@ import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout";import { useSelectWorkspaceTab } from "./useSelectWorkspaceTab";const PULL_REQUEST_ICON = require("../../../assets/icons/pull-request.png");+const SEARCH_SLOT = "search";/** Retain each native controller tree; removing its root can leave RNS's stack empty. */function RetainedWorkspaceTab(props: {@@ -93,6 +99,17 @@ export function WorkspaceNativeTabs(props: {const { colors } = useControlTheme();const selectedTab = workspace.state.selectedTab;const selectedSlot = workspace.settingsOpen ? -1 : primaryTabs.indexOf(selectedTab);+ // The folded Duo opens list search from a search tab UIKit sets apart below the others.+ const searchTab = deviceType === "duo-folded";+ // Searching is list state: the list's toolbar mounts its field and focuses it.+ const openSearch = () => {+ const tab =+ !workspace.settingsOpen && workspaceUsesColumns(selectedTab) ? selectedTab : "threads";+ // Back on the tab's list first: folded, a chat open on top would hide the field.+ selectTab(tab);+ workspace.dispatch({ type: "scroll", tab, offset: workspace.getScrollOffset(tab) });+ workspace.dispatch({ type: "search", tab });+ };const request = useMemo(() => ({selectedScreenKey:@@ -116,9 +133,19 @@ export function WorkspaceNativeTabs(props: {}: NativeSyntheticEvent<TabSelectionRejectedEvent>) =>setProvenance(nativeEvent.provenance)}+ onTabSelectionPrevented={({+ nativeEvent,+ }: NativeSyntheticEvent<TabSelectionPreventedEvent>) => {+ if (nativeEvent.preventedScreenKey === SEARCH_SLOT) openSearch();+ }}onTabSelected={({ nativeEvent }: NativeSyntheticEvent<TabSelectedEvent>) => {setProvenance(nativeEvent.provenance);if (nativeEvent.actionOrigin === "programmatic-js") return;+ // UIKit reports reselecting a tab here even when its selection is prevented.+ if (nativeEvent.selectedScreenKey === SEARCH_SLOT) {+ openSearch();+ return;+ }const tab = workspaceTabForNativeSlot(primaryTabs, nativeEvent.selectedScreenKey);if (tab) selectTab(tab);}}@@ -156,6 +183,24 @@ export function WorkspaceNativeTabs(props: {</Tabs.Screen>);})}+ {searchTab ? (+ // It never becomes the selected tab; pressing it opens the current list's search.+ <Tabs.Screen+ key={SEARCH_SLOT}+ screenKey={SEARCH_SLOT}+ title=""+ tabBarItemAccessibilityLabel="Search"+ preventNativeSelection+ ios={{+ role: "search",+ systemItem: "search",+ // iOS 27 only sets the search tab apart while it activates search.+ automaticallyActivatesSearch: true,+ }}+ >+ <View />+ </Tabs.Screen>+ ) : null}</Tabs.Host>{(deviceType === "pad" ||compactTabs ||diff --git a/apps/mobile/src/features/workspace/WorkspaceToolbar.ios.tsx b/apps/mobile/src/features/workspace/WorkspaceToolbar.ios.tsxindex 17fff053258..877c4b7e8db 100644--- a/apps/mobile/src/features/workspace/WorkspaceToolbar.ios.tsx+++ b/apps/mobile/src/features/workspace/WorkspaceToolbar.ios.tsx@@ -121,32 +121,19 @@ export function WorkspaceToolbar(props: {separateBackground/>) : null;- const searchButton = foldedSearch ? (- <Toolbar.Button- identifier="workspace-search"- accessibilityLabel={- searchVisible- ? "Close search"- : selectedTab === "pullRequests"- ? "Search pull requests"- : "Search threads"- }- icon={searchVisible ? "xmark" : "magnifyingglass"}- onPress={searchVisible ? () => searchRef.current?.cancelSearch() : focusSearch}- separateBackground- />- ) : null;return (<><NativeStackScreenOptions- optionsVersion={[selectedTab, tab.searchMode]}+ optionsVersion={[selectedTab, tab.searchMode, tab.searchFocusRequest]}listeners={{ transitionEnd: restoreSearch }}options={{+ // Each search request focuses the field once it exists, after it mounts if need be.+ unstable_headerSearchFocusRequest:+ tab.searchMode === "active" ? tab.searchFocusRequest : undefined,headerSearchBarOptions:(NATIVE_LIQUID_GLASS_SUPPORTED || tab.searchMode !== "resting") && searchVisible? {ref: searchRef,- autoFocus: tab.searchMode === "active",autoCapitalize: "none",hideNavigationBar: false,hideWhenScrolling: false,@@ -162,7 +149,6 @@ export function WorkspaceToolbar(props: {sidebarTabs !== "picker",placeholder:selectedTab === "pullRequests" ? "Search pull requests" : "Search threads",- cancelButtonHidden: foldedSearch,onFocus: searchFocused,onBlur: () => workspace.dispatch({ type: "expand", tab: selectedTab }),onSearchButtonPress: () => searchRef.current?.blur(),@@ -179,8 +165,6 @@ export function WorkspaceToolbar(props: {/>{/* UIKit lays trailing items out from the edge inward. */}<Toolbar placement="right">- {searchButton}- {nativeToolbarCompose && foldedSearch ? <Toolbar.Spacer width={8} /> : null}{composeButton}{selectedTab !== "threads" ||isDuoDevice(deviceType) ||diff --git a/apps/mobile/src/features/workspace/workspace-tabs-state.ts b/apps/mobile/src/features/workspace/workspace-tabs-state.tsindex 6355796c84e..3afbcea4f6e 100644--- a/apps/mobile/src/features/workspace/workspace-tabs-state.ts+++ b/apps/mobile/src/features/workspace/workspace-tabs-state.ts@@ -24,6 +24,8 @@ export interface WorkspaceTabState {readonly searchMode: WorkspaceSearchMode;readonly scrollOffset: number;readonly searchEntryOffset: number;+ /** Counts requests to focus the search field; the header focuses once per request. */+ readonly searchFocusRequest: number;}export interface WorkspaceTabsState {@@ -33,7 +35,13 @@ export interface WorkspaceTabsState {}function emptyTab(): WorkspaceTabState {- return { query: "", searchMode: "resting", scrollOffset: 0, searchEntryOffset: 0 };+ return {+ query: "",+ searchMode: "resting",+ scrollOffset: 0,+ searchEntryOffset: 0,+ searchFocusRequest: 0,+ };}export function initialWorkspaceTabsState(): WorkspaceTabsState {@@ -79,6 +87,7 @@ export function workspaceTabsReducer(searchMode: "active",searchEntryOffset:current.searchMode === "resting" ? current.scrollOffset : current.searchEntryOffset,+ searchFocusRequest: current.searchFocusRequest + 1,};break;case "query":diff --git a/apps/mobile/src/native/StackHeader.tsx b/apps/mobile/src/native/StackHeader.tsxindex a206864d4d4..55652b64503 100644--- a/apps/mobile/src/native/StackHeader.tsx+++ b/apps/mobile/src/native/StackHeader.tsx@@ -34,6 +34,8 @@ export type AppNativeStackNavigationOptions = Omit<readonly unstable_headerToolbarItems?: unknown;readonly unstable_navigationItemStyle?: unknown;readonly unstable_headerMinimizationBehavior?: unknown;+ /** Focuses the header search field once for each new value, as soon as the field exists. */+ readonly unstable_headerSearchFocusRequest?: number;};function useNativeStackNavigation(): NativeStackNavigationProp<ParamListBase> | null {diff --git a/apps/mobile/src/native/V5StackHeader.ios.tsx b/apps/mobile/src/native/V5StackHeader.ios.tsxindex afcb5795fce..01629857100 100644--- a/apps/mobile/src/native/V5StackHeader.ios.tsx+++ b/apps/mobile/src/native/V5StackHeader.ios.tsx@@ -3,7 +3,7 @@ import type {NativeStackHeaderItemMenu,NativeStackNavigationOptions,} from "@react-navigation/native-stack";-import { isValidElement, useEffect, useRef } from "react";+import { isValidElement, useEffect, useRef, type RefObject } from "react";import { StyleSheet } from "react-native";import { isDuoDevice } from "../lib/device-type";import { useMobileNavigationTheme } from "../lib/useMobileNavigationTheme";@@ -29,6 +29,7 @@ type ToolbarItem =type Options = NativeStackNavigationOptions & {readonly unstable_navigationItemStyle?: "editor" | "navigator";readonly unstable_headerMinimizationBehavior?: StackHeaderMinimizationBehaviorIOS;+ readonly unstable_headerSearchFocusRequest?: number;readonly headerSubtitle?: string;readonly unstable_headerSubtitle?: string;readonly headerSubtitleStyle?: { color?: string; fontSize?: number; fontFamily?: string };@@ -243,12 +244,18 @@ export function V5StackHeader(props: {? options.headerTitle({ children: title ?? "", tintColor: options.headerTintColor }): undefined;const searchOptions = options.headerSearchBarOptions;- const autofocusRef = searchOptions?.ref ?? searchRef;- useEffect(() => {- if (!searchOptions?.autoFocus) return;- const frame = requestAnimationFrame(() => autofocusRef.current?.focus());- return () => cancelAnimationFrame(frame);- }, [searchOptions?.autoFocus, autofocusRef]);+ const searchBarRef = searchOptions?.ref ?? searchRef;+ const focusRequest = options.unstable_headerSearchFocusRequest;+ const focusedRequest = useRef<number | undefined>(undefined);+ // The field can attach a render after the request, since it mounts through these options, and+ // Screens re-attaches its commands on every render; so focus once per request, on attach.+ const attachSearchBar = (instance: SearchBarCommands | null) => {+ searchBarRef.current = instance;+ if (instance && focusRequest !== undefined && focusedRequest.current !== focusRequest) {+ focusedRequest.current = focusRequest;+ instance.focus();+ }+ };const titleStyle = StyleSheet.flatten(options.headerTitleStyle);const headerStyle = StyleSheet.flatten(options.headerStyle);const appearance = {@@ -288,7 +295,8 @@ export function V5StackHeader(props: {mailSearch || searchOptions ? (<SearchBar{...searchOptions}- ref={searchOptions?.ref ?? searchRef}+ // React accepts callback refs here; Screens only types the object form.+ ref={attachSearchBar as unknown as RefObject<SearchBarCommands | null>}hideNavigationBar={false}hideWhenScrolling={false}allowToolbarIntegration={searchOptions?.allowToolbarIntegration ?? true}