@@ -2,7 +2,7 @@ import { Toolbar } from "./Toolbar";
22import { AppActivityIndicator } from "./AppActivityIndicator" ;
33import type { HeaderBarButtonMailSearchToolbarItem } from "react-native-screens" ;
44import { NavigationContext } from "@react-navigation/native" ;
5- import { use , useId , useLayoutEffect } from "react" ;
5+ import { use , useEffect , useId , useLayoutEffect , useState } from "react" ;
66import { View } from "react-native" ;
77import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios" ;
88import { ScreenHeaderButton } from "./ScreenHeaderButton" ;
@@ -82,23 +82,71 @@ export function AppBar(props: AppBarProps) {
8282 const usesNativeMailSearchToolbar = useNativeMailSearchToolbar ( ) ;
8383 const { search, menus } = props ;
8484 const menu = menus ?. [ 0 ] ;
85- const compactSearch =
85+ // Reserve mail-toolbar chrome from the first frame even before search attaches, so menus[0]
86+ // never flashes as a trailing bar button during the push.
87+ const wantsCompactSearch =
8688 search !== undefined &&
8789 ( search . compactToolbar ?? ! layout . usesSplitView ) &&
8890 usesNativeMailSearchToolbar ;
89- const visibleMenus = compactSearch ? ( menus ?. slice ( 1 ) ?? [ ] ) : ( menus ?? [ ] ) ;
91+ // Token identity: search stays detached until transitionEnd marks this key ready.
92+ const searchAttachKey = wantsCompactSearch ? `${ headerId } :compact` : null ;
93+ const [ attachedSearchKey , setAttachedSearchKey ] = useState < string | null > ( null ) ;
94+ const searchAttached = searchAttachKey === null || attachedSearchKey === searchAttachKey ;
95+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches native search after the push so UIKit does not flash its trailing bar-button form
96+ useEffect ( ( ) => {
97+ if ( searchAttachKey === null ) return ;
98+ let done = false ;
99+ let opening = false ;
100+ const finish = ( ) => {
101+ if ( done ) return ;
102+ done = true ;
103+ setAttachedSearchKey ( searchAttachKey ) ;
104+ } ;
105+ const stackNavigation = navigation as unknown as {
106+ readonly addListener : (
107+ type : "transitionStart" | "transitionEnd" ,
108+ listener : ( event : { readonly data : { readonly closing : boolean } } ) => void ,
109+ ) => ( ) => void ;
110+ } | null ;
111+ const removeStart = stackNavigation ?. addListener ( "transitionStart" , ( { data } ) => {
112+ if ( ! data . closing ) opening = true ;
113+ } ) ;
114+ const removeEnd = stackNavigation ?. addListener ( "transitionEnd" , ( { data } ) => {
115+ if ( ! data . closing ) finish ( ) ;
116+ } ) ;
117+ let frame = requestAnimationFrame ( ( ) => {
118+ frame = requestAnimationFrame ( ( ) => {
119+ if ( ! opening ) finish ( ) ;
120+ } ) ;
121+ } ) ;
122+ const fallback = setTimeout ( finish , 1_000 ) ;
123+ return ( ) => {
124+ done = true ;
125+ removeStart ?.( ) ;
126+ removeEnd ?.( ) ;
127+ cancelAnimationFrame ( frame ) ;
128+ clearTimeout ( fallback ) ;
129+ } ;
130+ } , [ navigation , searchAttachKey ] ) ;
131+ const compactSearch = wantsCompactSearch && searchAttached ;
132+ const activeSearch = wantsCompactSearch && ! searchAttached ? undefined : search ;
133+ const visibleMenus = wantsCompactSearch ? ( menus ?. slice ( 1 ) ?? [ ] ) : ( menus ?? [ ] ) ;
90134 // Where the mail toolbar isn't used, the Duo keeps search at the bottom of the pane like its
91135 // workspace lists; elsewhere inline search is a button in the bar.
92136 const duoBottomSearch =
93- search !== undefined &&
137+ activeSearch !== undefined &&
94138 ! compactSearch &&
95139 NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED &&
96140 isDuoDevice ( deviceType ) ;
97141 if ( props . embedded || props . sheet ) return null ;
98142 return (
99143 < >
100144 < NativeStackScreenOptions
101- optionsVersion = { [ props . optionsVersion , compactSearch ? menu : undefined ] }
145+ optionsVersion = { [
146+ props . optionsVersion ,
147+ wantsCompactSearch ? menu : undefined ,
148+ searchAttached ,
149+ ] }
102150 options = { {
103151 headerShown : true ,
104152 headerBackVisible :
@@ -112,14 +160,14 @@ export function AppBar(props: AppBarProps) {
112160 ...( props . matchSearchSurface
113161 ? { contentStyle : { backgroundColor : themeVariables [ "--color-sheet-solid" ] } }
114162 : undefined ) ,
115- ...( search
163+ ...( activeSearch
116164 ? {
117165 unstable_headerToolbarItems : compactSearch
118166 ? ( ) => [
119167 createNativeMailSearchToolbarItem ( {
120168 showsSearchDismissButton : true ,
121- placeholder : search . compactPlaceholder ?? search . placeholder ,
122- onSearchTextChange : search . onChangeText ,
169+ placeholder : activeSearch . compactPlaceholder ?? activeSearch . placeholder ,
170+ onSearchTextChange : activeSearch . onChangeText ,
123171 searchTextChangeId : `${ headerId } -search-text` ,
124172 ...( menu
125173 ? {
@@ -137,20 +185,25 @@ export function AppBar(props: AppBarProps) {
137185 allowToolbarIntegration : true ,
138186 ...( duoBottomSearch
139187 ? { placement : "integrated" as const }
140- : NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED && search . mode === "inline"
188+ : NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED && activeSearch . mode === "inline"
141189 ? { placement : "integratedButton" as const }
142190 : undefined ) ,
143191 autoCapitalize : "none" ,
144192 hideNavigationBar : false ,
145- ...( search . mode === "inline" || duoBottomSearch
193+ ...( activeSearch . mode === "inline" || duoBottomSearch
146194 ? { obscureBackground : false }
147195 : undefined ) ,
148- placeholder : search . placeholder ,
149- onChangeText : ( event ) => search . onChangeText ( event . nativeEvent . text ) ,
150- onCancelButtonPress : ( ) => search . onChangeText ( "" ) ,
196+ placeholder : activeSearch . placeholder ,
197+ onChangeText : ( event ) => activeSearch . onChangeText ( event . nativeEvent . text ) ,
198+ onCancelButtonPress : ( ) => activeSearch . onChangeText ( "" ) ,
151199 } ,
152200 }
153- : undefined ) ,
201+ : wantsCompactSearch
202+ ? {
203+ // Keep the mail-toolbar filter slot reserved before search attaches.
204+ unstable_headerToolbarItems : ( ) => [ ] ,
205+ }
206+ : undefined ) ,
154207 } }
155208 />
156209 { props . onClose ? (
0 commit comments