@@ -9,6 +9,7 @@ import { KeyboardController, useKeyboardState } from "react-native-keyboard-cont |
9 | 9 | import { isDuoDevice } from "../lib/device-type"; |
10 | 10 | import { useMobileNavigationTheme } from "../lib/useMobileNavigationTheme"; |
11 | 11 | import { useDeviceType, useNativeLayoutMetrics } from "../native/native-layout-metrics"; |
| 12 | +import { HeaderItemsWidthContext } from "./header-items-width"; |
12 | 13 | import { |
13 | 14 | SearchBar, |
14 | 15 | Stack, |
@@ -22,6 +23,10 @@ import { |
22 | 23 | type HeaderBarButtonSearchBarPlacementItem, |
23 | 24 | } from "react-native-screens"; |
24 | 25 |
|
| 26 | +// UIKit's bar margins, and the width of a glass bar button. |
| 27 | +const BAR_MARGINS = 32; |
| 28 | +const BAR_BUTTON_WIDTH = 44; |
| 29 | + |
25 | 30 | type HeaderItem = NonNullable<NonNullable<StackHeaderConfigProps["ios"]>["leadingItems"]>[number]; |
26 | 31 | type ToolbarItem = |
27 | 32 | | NativeStackHeaderItem |
@@ -276,6 +281,24 @@ export function V5StackHeader(props: { |
276 | 281 | const frame = requestAnimationFrame(() => autofocusRef.current?.focus()); |
277 | 282 | return () => cancelAnimationFrame(frame); |
278 | 283 | }, [searchOptions?.autoFocus, autofocusRef]); |
| 284 | + const searchButton = !bottomSearch && searchOptions?.placement === "integratedButton"; |
| 285 | + const itemsWidth = |
| 286 | + BAR_MARGINS + |
| 287 | + (props.canGoBack && options.headerBackVisible !== false ? BAR_BUTTON_WIDTH : 0) + |
| 288 | + (searchButton ? BAR_BUTTON_WIDTH : 0) + |
| 289 | + [...leading, ...trailing].reduce( |
| 290 | + (width, item) => |
| 291 | + width + |
| 292 | + (item.type === "spacer" |
| 293 | + ? item.sizing === "fixed" |
| 294 | + ? item.width |
| 295 | + : 0 |
| 296 | + : // The keyboard dismiss rides the vertical bar, not beside the title. |
| 297 | + item.id === "dismiss-keyboard" |
| 298 | + ? 0 |
| 299 | + : BAR_BUTTON_WIDTH), |
| 300 | + 0, |
| 301 | + ); |
279 | 302 | const titleStyle = StyleSheet.flatten(options.headerTitleStyle); |
280 | 303 | const headerStyle = StyleSheet.flatten(options.headerStyle); |
281 | 304 | const appearance = { |
@@ -292,56 +315,58 @@ export function V5StackHeader(props: { |
292 | 315 | subtitleFontColor: options.headerSubtitleStyle?.color, |
293 | 316 | }; |
294 | 317 | return ( |
295 | | - <Stack.HeaderConfig |
296 | | - hidden={options.headerShown === false} |
297 | | - transparent={options.headerTransparent} |
298 | | - title={title} |
299 | | - subtitle={options.unstable_headerSubtitle ?? options.headerSubtitle} |
300 | | - backButtonHidden={options.headerBackVisible === false} |
301 | | - ios={{ |
302 | | - navigationItemStyle: options.unstable_navigationItemStyle, |
303 | | - // Opt-ins apply only where the Duo is short: folded, or held landscape. The Duo |
304 | | - // resolves `automatic` to minimize on scroll, so the tall unfolded display opts out. |
305 | | - minimizationBehavior: !isDuoDevice(deviceType) |
306 | | - ? undefined |
307 | | - : deviceType === "duo-unfolded" && |
308 | | - nativeMetrics && |
309 | | - nativeMetrics.height > nativeMetrics.width |
310 | | - ? "never" |
311 | | - : options.unstable_headerMinimizationBehavior, |
312 | | - tintColor: options.headerTintColor, |
313 | | - searchBar: |
314 | | - mailSearch || searchOptions ? ( |
315 | | - <SearchBar |
316 | | - {...searchOptions} |
317 | | - ref={searchOptions?.ref ?? searchRef} |
318 | | - hideNavigationBar={false} |
319 | | - hideWhenScrolling={false} |
320 | | - allowToolbarIntegration={searchOptions?.allowToolbarIntegration ?? true} |
321 | | - placement={bottomSearch ? "integrated" : (searchOptions?.placement ?? "automatic")} |
322 | | - placeholder={mailSearch?.placeholder ?? searchOptions?.placeholder ?? "Search"} |
323 | | - onFocus={(event) => { |
324 | | - searchOptions?.onFocus?.(event); |
325 | | - }} |
326 | | - onChangeText={(event) => { |
327 | | - mailSearch?.onSearchTextChange?.(event.nativeEvent.text); |
328 | | - searchOptions?.onChangeText?.(event); |
329 | | - }} |
330 | | - /> |
331 | | - ) : undefined, |
332 | | - toolbarItems: bottom, |
333 | | - leadingItems: leading, |
334 | | - trailingItems: trailing, |
335 | | - titleItem: isValidElement(titleElement) |
336 | | - ? { id: "title", render: () => titleElement } |
337 | | - : undefined, |
338 | | - largeTitleEnabled: options.headerLargeTitle, |
339 | | - backButtonDisplayMode: options.headerBackButtonDisplayMode ?? "minimal", |
340 | | - backButtonMenuEnabled: options.headerBackButtonMenuEnabled ?? true, |
341 | | - backButtonTitle: options.headerBackTitle, |
342 | | - standardAppearance: appearance, |
343 | | - scrollEdgeAppearance: appearance, |
344 | | - }} |
345 | | - /> |
| 318 | + <HeaderItemsWidthContext value={itemsWidth}> |
| 319 | + <Stack.HeaderConfig |
| 320 | + hidden={options.headerShown === false} |
| 321 | + transparent={options.headerTransparent} |
| 322 | + title={title} |
| 323 | + subtitle={options.unstable_headerSubtitle ?? options.headerSubtitle} |
| 324 | + backButtonHidden={options.headerBackVisible === false} |
| 325 | + ios={{ |
| 326 | + navigationItemStyle: options.unstable_navigationItemStyle, |
| 327 | + // Opt-ins apply only where the Duo is short: folded, or held landscape. The Duo |
| 328 | + // resolves `automatic` to minimize on scroll, so the tall unfolded display opts out. |
| 329 | + minimizationBehavior: !isDuoDevice(deviceType) |
| 330 | + ? undefined |
| 331 | + : deviceType === "duo-unfolded" && |
| 332 | + nativeMetrics && |
| 333 | + nativeMetrics.height > nativeMetrics.width |
| 334 | + ? "never" |
| 335 | + : options.unstable_headerMinimizationBehavior, |
| 336 | + tintColor: options.headerTintColor, |
| 337 | + searchBar: |
| 338 | + mailSearch || searchOptions ? ( |
| 339 | + <SearchBar |
| 340 | + {...searchOptions} |
| 341 | + ref={searchOptions?.ref ?? searchRef} |
| 342 | + hideNavigationBar={false} |
| 343 | + hideWhenScrolling={false} |
| 344 | + allowToolbarIntegration={searchOptions?.allowToolbarIntegration ?? true} |
| 345 | + placement={bottomSearch ? "integrated" : (searchOptions?.placement ?? "automatic")} |
| 346 | + placeholder={mailSearch?.placeholder ?? searchOptions?.placeholder ?? "Search"} |
| 347 | + onFocus={(event) => { |
| 348 | + searchOptions?.onFocus?.(event); |
| 349 | + }} |
| 350 | + onChangeText={(event) => { |
| 351 | + mailSearch?.onSearchTextChange?.(event.nativeEvent.text); |
| 352 | + searchOptions?.onChangeText?.(event); |
| 353 | + }} |
| 354 | + /> |
| 355 | + ) : undefined, |
| 356 | + toolbarItems: bottom, |
| 357 | + leadingItems: leading, |
| 358 | + trailingItems: trailing, |
| 359 | + titleItem: isValidElement(titleElement) |
| 360 | + ? { id: "title", render: () => titleElement } |
| 361 | + : undefined, |
| 362 | + largeTitleEnabled: options.headerLargeTitle, |
| 363 | + backButtonDisplayMode: options.headerBackButtonDisplayMode ?? "minimal", |
| 364 | + backButtonMenuEnabled: options.headerBackButtonMenuEnabled ?? true, |
| 365 | + backButtonTitle: options.headerBackTitle, |
| 366 | + standardAppearance: appearance, |
| 367 | + scrollEdgeAppearance: appearance, |
| 368 | + }} |
| 369 | + /> |
| 370 | + </HeaderItemsWidthContext> |
346 | 371 | ); |
347 | 372 | } |