Skip to content
TypeScript

fix(mobile): sidebar connection status uses the header's free width (#26266)

Julius Marminge28da8c84ff46
diff --git a/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx b/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx
index fd382d1de0a..5f78aba0832 100644
--- a/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx
+++ b/apps/mobile/src/features/workspace/WorkspaceRootScreen.tsx
@@ -11,6 +11,7 @@ import { SafeAreaInsetsContext, useSafeAreaInsets } from "react-native-safe-area
import { CompactBrandTitle } from "../../components/CompactBrandTitle";
import { AndroidScreenHeader } from "../../components/ui/AndroidScreenHeader";
+import { HeaderItemsWidthContext } from "../../native/header-items-width";
import { NativeStackScreenOptions } from "../../native/StackHeader";
import { NativePrimaryColumnContext } from "../../native/v5-workspace-context";
import { isDuoDevice } from "../../lib/device-type";
@@ -49,10 +50,11 @@ import { useSelectWorkspaceTab } from "./useSelectWorkspaceTab";
const TAB_TITLES = WORKSPACE_TAB_LABELS;
function WorkspaceRootTitle(props: {
- readonly maxWidth: number;
+ readonly columnWidth: number;
readonly onOpenEnvironments: () => void;
}) {
const { state, settingsOpen } = useWorkspaceTabs()!;
+ const maxWidth = Math.max(0, props.columnWidth - (use(HeaderItemsWidthContext) ?? 160));
const enabled = useEnabledNavigationTabs();
const tabTitle = settingsOpen
? "Settings"
@@ -60,10 +62,10 @@ function WorkspaceRootTitle(props: {
? TAB_TITLES[state.selectedTab]
: "Navigation";
return (
- <View style={{ width: props.maxWidth, minHeight: 32, justifyContent: "center" }}>
+ <View style={{ width: maxWidth, minHeight: 32, justifyContent: "center" }}>
<WorkspaceConnectionTitle
brand={<CompactBrandTitle accessibilityLabel={`T3 Code, ${tabTitle}`} fit />}
- maxWidth={props.maxWidth}
+ maxWidth={maxWidth}
onPress={props.onOpenEnvironments}
/>
</View>
@@ -99,8 +101,7 @@ function WorkspaceRootContent() {
const settingsOpen = workspace.settingsOpen;
const renderHeaderTitle = () => (
<WorkspaceRootTitle
- // Settings adds its scope filter beside search.
- maxWidth={Math.max(0, width - 160 - (settingsOpen ? 44 : 0))}
+ columnWidth={width}
onOpenEnvironments={() => openSettings(navigation, "SettingsEnvironments")}
/>
);
diff --git a/apps/mobile/src/native/V5StackHeader.ios.tsx b/apps/mobile/src/native/V5StackHeader.ios.tsx
index 21c242ba91f..9caf92d5834 100644
--- a/apps/mobile/src/native/V5StackHeader.ios.tsx
+++ b/apps/mobile/src/native/V5StackHeader.ios.tsx
@@ -9,6 +9,7 @@ import { KeyboardController, useKeyboardState } from "react-native-keyboard-cont
import { isDuoDevice } from "../lib/device-type";
import { useMobileNavigationTheme } from "../lib/useMobileNavigationTheme";
import { useDeviceType, useNativeLayoutMetrics } from "../native/native-layout-metrics";
+import { HeaderItemsWidthContext } from "./header-items-width";
import {
SearchBar,
Stack,
@@ -22,6 +23,10 @@ import {
type HeaderBarButtonSearchBarPlacementItem,
} from "react-native-screens";
+// UIKit's bar margins, and the width of a glass bar button.
+const BAR_MARGINS = 32;
+const BAR_BUTTON_WIDTH = 44;
+
type HeaderItem = NonNullable<NonNullable<StackHeaderConfigProps["ios"]>["leadingItems"]>[number];
type ToolbarItem =
| NativeStackHeaderItem
@@ -276,6 +281,24 @@ export function V5StackHeader(props: {
const frame = requestAnimationFrame(() => autofocusRef.current?.focus());
return () => cancelAnimationFrame(frame);
}, [searchOptions?.autoFocus, autofocusRef]);
+ const searchButton = !bottomSearch && searchOptions?.placement === "integratedButton";
+ const itemsWidth =
+ BAR_MARGINS +
+ (props.canGoBack && options.headerBackVisible !== false ? BAR_BUTTON_WIDTH : 0) +
+ (searchButton ? BAR_BUTTON_WIDTH : 0) +
+ [...leading, ...trailing].reduce(
+ (width, item) =>
+ width +
+ (item.type === "spacer"
+ ? item.sizing === "fixed"
+ ? item.width
+ : 0
+ : // The keyboard dismiss rides the vertical bar, not beside the title.
+ item.id === "dismiss-keyboard"
+ ? 0
+ : BAR_BUTTON_WIDTH),
+ 0,
+ );
const titleStyle = StyleSheet.flatten(options.headerTitleStyle);
const headerStyle = StyleSheet.flatten(options.headerStyle);
const appearance = {
@@ -292,56 +315,58 @@ export function V5StackHeader(props: {
subtitleFontColor: options.headerSubtitleStyle?.color,
};
return (
- <Stack.HeaderConfig
- hidden={options.headerShown === false}
- transparent={options.headerTransparent}
- title={title}
- subtitle={options.unstable_headerSubtitle ?? options.headerSubtitle}
- backButtonHidden={options.headerBackVisible === false}
- ios={{
- navigationItemStyle: options.unstable_navigationItemStyle,
- // Opt-ins apply only where the Duo is short: folded, or held landscape. The Duo
- // resolves `automatic` to minimize on scroll, so the tall unfolded display opts out.
- minimizationBehavior: !isDuoDevice(deviceType)
- ? undefined
- : deviceType === "duo-unfolded" &&
- nativeMetrics &&
- nativeMetrics.height > nativeMetrics.width
- ? "never"
- : options.unstable_headerMinimizationBehavior,
- tintColor: options.headerTintColor,
- searchBar:
- mailSearch || searchOptions ? (
- <SearchBar
- {...searchOptions}
- ref={searchOptions?.ref ?? searchRef}
- hideNavigationBar={false}
- hideWhenScrolling={false}
- allowToolbarIntegration={searchOptions?.allowToolbarIntegration ?? true}
- placement={bottomSearch ? "integrated" : (searchOptions?.placement ?? "automatic")}
- placeholder={mailSearch?.placeholder ?? searchOptions?.placeholder ?? "Search"}
- onFocus={(event) => {
- searchOptions?.onFocus?.(event);
- }}
- onChangeText={(event) => {
- mailSearch?.onSearchTextChange?.(event.nativeEvent.text);
- searchOptions?.onChangeText?.(event);
- }}
- />
- ) : undefined,
- toolbarItems: bottom,
- leadingItems: leading,
- trailingItems: trailing,
- titleItem: isValidElement(titleElement)
- ? { id: "title", render: () => titleElement }
- : undefined,
- largeTitleEnabled: options.headerLargeTitle,
- backButtonDisplayMode: options.headerBackButtonDisplayMode ?? "minimal",
- backButtonMenuEnabled: options.headerBackButtonMenuEnabled ?? true,
- backButtonTitle: options.headerBackTitle,
- standardAppearance: appearance,
- scrollEdgeAppearance: appearance,
- }}
- />
+ <HeaderItemsWidthContext value={itemsWidth}>
+ <Stack.HeaderConfig
+ hidden={options.headerShown === false}
+ transparent={options.headerTransparent}
+ title={title}
+ subtitle={options.unstable_headerSubtitle ?? options.headerSubtitle}
+ backButtonHidden={options.headerBackVisible === false}
+ ios={{
+ navigationItemStyle: options.unstable_navigationItemStyle,
+ // Opt-ins apply only where the Duo is short: folded, or held landscape. The Duo
+ // resolves `automatic` to minimize on scroll, so the tall unfolded display opts out.
+ minimizationBehavior: !isDuoDevice(deviceType)
+ ? undefined
+ : deviceType === "duo-unfolded" &&
+ nativeMetrics &&
+ nativeMetrics.height > nativeMetrics.width
+ ? "never"
+ : options.unstable_headerMinimizationBehavior,
+ tintColor: options.headerTintColor,
+ searchBar:
+ mailSearch || searchOptions ? (
+ <SearchBar
+ {...searchOptions}
+ ref={searchOptions?.ref ?? searchRef}
+ hideNavigationBar={false}
+ hideWhenScrolling={false}
+ allowToolbarIntegration={searchOptions?.allowToolbarIntegration ?? true}
+ placement={bottomSearch ? "integrated" : (searchOptions?.placement ?? "automatic")}
+ placeholder={mailSearch?.placeholder ?? searchOptions?.placeholder ?? "Search"}
+ onFocus={(event) => {
+ searchOptions?.onFocus?.(event);
+ }}
+ onChangeText={(event) => {
+ mailSearch?.onSearchTextChange?.(event.nativeEvent.text);
+ searchOptions?.onChangeText?.(event);
+ }}
+ />
+ ) : undefined,
+ toolbarItems: bottom,
+ leadingItems: leading,
+ trailingItems: trailing,
+ titleItem: isValidElement(titleElement)
+ ? { id: "title", render: () => titleElement }
+ : undefined,
+ largeTitleEnabled: options.headerLargeTitle,
+ backButtonDisplayMode: options.headerBackButtonDisplayMode ?? "minimal",
+ backButtonMenuEnabled: options.headerBackButtonMenuEnabled ?? true,
+ backButtonTitle: options.headerBackTitle,
+ standardAppearance: appearance,
+ scrollEdgeAppearance: appearance,
+ }}
+ />
+ </HeaderItemsWidthContext>
);
}
diff --git a/apps/mobile/src/native/header-items-width.ts b/apps/mobile/src/native/header-items-width.ts
new file mode 100644
index 00000000000..30d6b2882f2
--- /dev/null
+++ b/apps/mobile/src/native/header-items-width.ts
@@ -0,0 +1,4 @@
+import { createContext } from "react";
+
+/** Width a header's own buttons and margins take beside a custom title, which RN sizes itself. */
+export const HeaderItemsWidthContext = createContext<number | null>(null);