Skip to content
8 changes: 8 additions & 0 deletions apps/mobile/src/components/ui/AppBar.android.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ import { afterEach, expect, it, vi } from "vite-plus/test";

vi.mock("react-native", () => ({
View: (props: { readonly children?: ReactNode }) => <div>{props.children}</div>,
Pressable: (props: { readonly children?: ReactNode }) => (
<button type="button">{props.children}</button>
),
Keyboard: { dismiss: () => {} },
BackHandler: { addEventListener: () => ({ remove: () => {} }) },
}));
vi.mock("./AppText", () => ({
AppText: (props: { readonly children?: ReactNode }) => <span>{props.children}</span>,
}));
vi.mock("./AppSymbol", () => ({ SymbolView: () => null }));
vi.mock("./AppActivityIndicator", () => ({ AppActivityIndicator: () => null }));
// React Navigation throws this outside a navigator, as for the Arrange threads sheet.
vi.mock("@react-navigation/native", () => ({
useIsFocused: () => {
Expand Down
83 changes: 71 additions & 12 deletions apps/mobile/src/components/ui/AppBar.android.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type RefObject, use, useRef, useState } from "react";
import { Keyboard, type TextInputInstance, View } from "react-native";
import { Keyboard, Pressable, type TextInputInstance, View } from "react-native";
import { useHardwareBack } from "../../lib/backHandler";
import { useToolbarLayout } from "./useToolbarLayout";
import { RoutedFormSheetContext } from "./RoutedFormSheetContext";
Expand Down Expand Up @@ -143,7 +143,11 @@ function AppBarView(props: AppBarProps & { readonly searchState?: SearchState })
);
}

const menuView = props.menus?.map((menu) => (
const confirmMenuTitle = props.confirm?.menu?.title;
const menusWithoutConfirm = confirmMenuTitle
? (props.menus?.filter((entry) => entry.title !== confirmMenuTitle) ?? [])
: (props.menus ?? []);
const menuView = menusWithoutConfirm.map((menu) => (
<Menu
key={menu.title}
actions={menuActionsFromAppBarItems(menu.items)}
Expand All @@ -154,17 +158,72 @@ function AppBarView(props: AppBarProps & { readonly searchState?: SearchState })
<ScreenHeaderButton accessibilityLabel={menu.title} icon={menu.icon} />
</Menu>
));
const confirmButton = props.confirm ? (
<View className="self-center">
<Button
label={props.confirm.label}
tone="text"
loading={props.confirm.loading}
disabled={props.confirm.disabled}
onPress={props.confirm.onPress}
/>
</View>
const confirm = props.confirm;
const menuConfirm = confirm?.menu ? (
<Menu
actions={menuActionsFromAppBarItems(confirm.menu.items)}
isAnchoredToRight
title={confirm.menu.status ?? confirm.menu.title}
shouldOpenOnLongPress
onPressAction={({ nativeEvent }) => selectMenuItem(confirm.menu!.items, nativeEvent.event)}
>
{/* Pressable owns tap + long-press; Menu clones onLongPress onto this child. */}
<Pressable
accessibilityRole="button"
accessibilityLabel={confirm.accessibilityLabel ?? confirm.label}
accessibilityHint="Shows more actions on long press"
accessibilityState={{ disabled: Boolean(confirm.disabled), busy: Boolean(confirm.loading) }}
disabled={confirm.disabled || confirm.loading}
onPress={confirm.onPress}
className="self-center"
>
<View pointerEvents="none">
{confirm.showLabel || !confirm.icon ? (
<Button
label={confirm.label}
icon={confirm.showLabel ? confirm.icon : undefined}
accessibilityLabel={confirm.accessibilityLabel ?? confirm.label}
tone="text"
loading={confirm.loading}
disabled={confirm.disabled}
onPress={confirm.onPress}
/>
) : (
<AndroidHeaderIconButton
accessibilityLabel={confirm.accessibilityLabel ?? confirm.label}
icon={confirm.icon}
disabled={confirm.disabled}
/>
)}
</View>
</Pressable>
</Menu>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
) : null;
const labeledConfirm =
confirm && !confirm.menu && (confirm.showLabel || !confirm.icon) ? (
<View className="self-center">
<Button
label={confirm.label}
icon={confirm.showLabel ? confirm.icon : undefined}
accessibilityLabel={confirm.accessibilityLabel ?? confirm.label}
tone="text"
loading={confirm.loading}
disabled={confirm.disabled}
onPress={confirm.onPress}
/>
</View>
) : null;
const confirmButton = confirm
? (menuConfirm ??
labeledConfirm ?? (
<AndroidHeaderIconButton
accessibilityLabel={confirm.accessibilityLabel ?? confirm.label}
icon={confirm.icon!}
disabled={confirm.disabled}
onPress={confirm.onPress}
/>
))
: null;
const options =
props.embedded || props.sheet ? null : (
<NativeStackScreenOptions
Expand Down
107 changes: 80 additions & 27 deletions apps/mobile/src/components/ui/AppBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { AppActivityIndicator } from "./AppActivityIndicator";
import type { HeaderBarButtonMailSearchToolbarItem } from "react-native-screens";
import { NavigationContext } from "@react-navigation/native";
import { use, useId, useLayoutEffect } from "react";
import { View } from "react-native";
import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios";
import { Pressable, View } from "react-native";
import { createNativeHeaderMenu, renderNativeHeaderMenuItems } from "./nativeHeaderMenu.ios";
import { ScreenHeaderButton } from "./ScreenHeaderButton";
import { NativeStackScreenOptions, useScreenOptionsWriter } from "../../native/StackHeader";
import { useNativeWorkspaceColumnsSupported } from "../../native/NativeWorkspaceColumns";
Expand All @@ -19,7 +19,7 @@ import {
} from "../../features/layout/native-mail-search-toolbar";
import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
import type { AppBarMenuItem, AppBarProps } from "./AppBar.types";
import type { AppSymbolName } from "./AppSymbol";
import { SymbolView, type AppSymbolName } from "./AppSymbol";
import { useNativeMailSearchToolbar } from "../../native/use-native-mail-search-toolbar";
import { useControlTheme } from "./useControlTheme";

Expand Down Expand Up @@ -77,12 +77,16 @@ export function AppBar(props: AppBarProps) {
const { colors } = useControlTheme();
const usesNativeMailSearchToolbar = useNativeMailSearchToolbar();
const { search, menus } = props;
const menu = menus?.[0];
const confirmMenuTitle = props.confirm?.menu?.title;
const menusWithoutConfirm = confirmMenuTitle
? (menus?.filter((entry) => entry.title !== confirmMenuTitle) ?? [])
: (menus ?? []);
const menu = menusWithoutConfirm[0];
const compactSearch =
search !== undefined &&
(search.compactToolbar ?? !layout.usesSplitView) &&
usesNativeMailSearchToolbar;
const visibleMenus = compactSearch ? (menus?.slice(1) ?? []) : (menus ?? []);
const visibleMenus = compactSearch ? menusWithoutConfirm.slice(1) : menusWithoutConfirm;
// Where the mail toolbar isn't used, the Duo keeps search at the bottom of the pane like its
// workspace lists; elsewhere inline search is a button in the bar.
const duoBottomSearch =
Expand Down Expand Up @@ -194,6 +198,77 @@ export function AppBar(props: AppBarProps) {
props.confirm ||
closeInspector ? (
<Toolbar placement="right">
{/* First child is trailing-edge on iOS; keep confirm outermost when labelled. */}
{props.confirm?.loading ? (
<Toolbar.Custom>
<View
className="size-11 items-center justify-center"
accessible
accessibilityLabel={props.confirm.accessibilityLabel ?? props.confirm.label}
accessibilityState={{ busy: true }}
>
<AppActivityIndicator colorClassName="accent-prominent-action" />
</View>
</Toolbar.Custom>
) : props.confirm?.menu ? (
// Edit-style glass text (or icon) + Safari-Back long-press: native primaryAction
// + menu, no chevron — the glass morphs into the menu on hold.
<Toolbar.Button
icon={
props.confirm.icon
? iosIcon(props.confirm.icon)
: props.confirm.showLabel
? undefined
: iosIcon(props.confirm.menu.icon)
}
label={props.confirm.showLabel ? props.confirm.label : undefined}
accessibilityLabel={props.confirm.accessibilityLabel ?? props.confirm.label}
accessibilityHint="Shows more actions on long press"
disabled={props.confirm.disabled}
onPress={props.confirm.onPress}
menuTitle={props.confirm.menu.status ?? props.confirm.menu.title}
visibilityPriority="high"
separateBackground
>
{renderNativeHeaderMenuItems(props.confirm.menu.items)}
</Toolbar.Button>
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
) : props.confirm?.icon && !props.confirm.showLabel ? (
// Custom AppSymbols (e.g. PR glyph) are not SF Symbols — render via SymbolView,
// same footprint as other trailing icons (no prominent pill).
<Toolbar.Custom>
<Pressable
accessibilityLabel={props.confirm.accessibilityLabel ?? props.confirm.label}
accessibilityRole="button"
accessibilityState={{ disabled: Boolean(props.confirm.disabled) }}
disabled={props.confirm.disabled}
onPress={props.confirm.onPress}
className="size-11 items-center justify-center"
>
<SymbolView
name={props.confirm.icon}
size={22}
tintColorClassName="accent-header-foreground"
/>
</Pressable>
</Toolbar.Custom>
) : props.confirm ? (
<Toolbar.Button
icon={
props.confirm.icon
? iosIcon(props.confirm.icon)
: props.confirm.showLabel
? undefined
: "checkmark"
}
label={props.confirm.showLabel ? props.confirm.label : undefined}
variant="prominent"
visibilityPriority="high"
tintColor={colors["--color-prominent-action"]}
accessibilityLabel={props.confirm.accessibilityLabel ?? props.confirm.label}
disabled={props.confirm.disabled}
onPress={props.confirm.onPress}
/>
) : null}
{props.actions?.map(({ loading, ...action }) =>
loading ? (
<Toolbar.Custom key={action.accessibilityLabel}>
Expand All @@ -215,28 +290,6 @@ export function AppBar(props: AppBarProps) {
)}
{visibleMenus.map(createNativeHeaderMenu)}
{props.trailing}
{props.confirm?.loading ? (
<Toolbar.Custom>
<View
className="size-11 items-center justify-center"
accessible
accessibilityLabel={props.confirm.label}
accessibilityState={{ busy: true }}
>
<AppActivityIndicator colorClassName="accent-prominent-action" />
</View>
</Toolbar.Custom>
) : props.confirm ? (
<Toolbar.Button
icon="checkmark"
variant="prominent"
visibilityPriority="high"
tintColor={colors["--color-prominent-action"]}
accessibilityLabel={props.confirm.label}
disabled={props.confirm.disabled}
onPress={props.confirm.onPress}
/>
) : null}
{closeInspector ? (
<ScreenHeaderButton
accessibilityLabel="Close inspector"
Expand Down
16 changes: 16 additions & 0 deletions apps/mobile/src/components/ui/AppBar.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ export interface AppBarConfirm {
readonly onPress: () => void;
readonly disabled?: boolean;
readonly loading?: boolean;
/**
* When true, shows `label` in the toolbar. Sheets keep the checkmark-only control by
* leaving this unset. With `icon` and no label, both platforms use an icon button.
* With both `showLabel` and `icon`, platforms that support it show the glyph beside the label
* (e.g. PR glyph + linked count).
*/
readonly showLabel?: boolean;
/** Custom glyph; without `showLabel`, replaces the default checkmark (iOS) / text (Android). */
readonly icon?: AppSymbolName;
/** Defaults to `label`. Use when the visible control is icon-only or shortened. */
readonly accessibilityLabel?: string;
/**
* Long-press menu on the confirm control (iOS: primaryAction + menu; Android: long-press).
* When set, AppBar does not also render this menu as a separate trailing icon.
*/
readonly menu?: AppBarMenu;
}

export interface AppBarSearch {
Expand Down
18 changes: 6 additions & 12 deletions apps/mobile/src/components/ui/headerMenu.android.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
export function splitAndroidHeaderActions<Action extends { readonly overflowOnly?: boolean }>(
actions: ReadonlyArray<Action>,
wide: boolean,
) {
const inline = actions.filter((action) => !action.overflowOnly);
const directCount = actions.length > 2 ? (wide ? 3 : 1) : inline.length;
const visibleActions = inline.slice(0, directCount);
return {
visibleActions,
overflowActions: actions.filter((action) => !visibleActions.includes(action)),
};
}
export {
findHeaderMenuAction,
headerMenuActions,
headerMenuActions as androidHeaderMenuActions,
splitAndroidHeaderActions,
} from "./headerMenu";
60 changes: 60 additions & 0 deletions apps/mobile/src/components/ui/headerMenu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { MenuAction } from "@react-native-menu/menu";
import type { AppBarMenuItem } from "./AppBar.types";

export function headerMenuActions(items: ReadonlyArray<AppBarMenuItem>): MenuAction[] {
return items.flatMap((item): MenuAction[] =>
"items" in item
? item.inline && !item.title
? headerMenuActions(item.items)
: [
{
id: item.id,
title: item.title ?? "",
image: item.icon,
subactions: headerMenuActions(item.items),
},
]
: [
{
id: item.id,
title: item.title,
subtitle: item.subtitle,
image: item.icon,
imageColor: item.iconColor,
state: item.selected ? "on" : undefined,
attributes:
item.disabled || item.destructive
? { disabled: item.disabled, destructive: item.destructive }
: undefined,
},
],
);
}

export function findHeaderMenuAction(
items: ReadonlyArray<AppBarMenuItem>,
id: string,
): Extract<AppBarMenuItem, { readonly onPress: () => void }> | undefined {
for (const item of items) {
if ("items" in item) {
const action = findHeaderMenuAction(item.items, id);
if (action) return action;
} else if (item.id === id) {
return item;
}
}
return undefined;
}

export function splitAndroidHeaderActions<Action extends { readonly overflowOnly?: boolean }>(
actions: ReadonlyArray<Action>,
wide: boolean,
) {
const inline = actions.filter((action) => !action.overflowOnly);
const directCount = actions.length > 2 ? (wide ? 3 : 1) : inline.length;
const visibleActions = inline.slice(0, directCount);
return {
visibleActions,
overflowActions: actions.filter((action) => !visibleActions.includes(action)),
};
}
6 changes: 3 additions & 3 deletions apps/mobile/src/components/ui/nativeHeaderMenu.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Toolbar } from "./Toolbar";

import type { AppBarMenu, AppBarMenuItem } from "./AppBar.types";

function renderMenuItems(items: ReadonlyArray<AppBarMenuItem>) {
export function renderNativeHeaderMenuItems(items: ReadonlyArray<AppBarMenuItem>) {
return items.map((item) =>
"items" in item ? (
<Toolbar.Menu key={item.id} title={item.title} icon={item.icon} inline={item.inline}>
{renderMenuItems(item.items)}
{renderNativeHeaderMenuItems(item.items)}
</Toolbar.Menu>
) : (
<Toolbar.MenuAction
Expand Down Expand Up @@ -34,7 +34,7 @@ export function createNativeHeaderMenu(menu: AppBarMenu) {
separateBackground={menu.separateBackground ?? true}
>
{menu.status ? <Toolbar.Label>{menu.status}</Toolbar.Label> : null}
{renderMenuItems(menu.items)}
{renderNativeHeaderMenuItems(menu.items)}
</Toolbar.Menu>
);
}
Loading
Loading