refactor(mobile): centralize platform header rendering (#12388)
This commit is contained in:
1 parent
7230f6b1be
commit
4fb9dfc8b8
16 files changed
+1043
-383
No files matched your search
@@ -0,0 +1,203 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { BackHandler, Keyboard, Pressable, TextInput, View } from "react-native";
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
import { NativeStackScreenOptions } from "../native/StackHeader";
|
||||
import { AndroidWorkspaceSidebarButton } from "../features/layout/workspace-sidebar-toolbar";
|
||||
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
|
||||
import { AndroidScreenHeader } from "./AndroidScreenHeader";
|
||||
import { ScreenHeaderButton } from "./ScreenHeaderButton.android";
|
||||
import { SymbolView } from "./AppSymbol";
|
||||
import { ControlPillMenu } from "./ControlPill";
|
||||
import { MaterialSearchField } from "./MaterialSearchField";
|
||||
import { androidHeaderMenuActions, findHeaderMenuAction } from "./headerMenu.android";
|
||||
import type { ScreenHeaderProps } from "./ScreenHeader.types";
|
||||
|
||||
export function ScreenHeader(props: ScreenHeaderProps) {
|
||||
const { search } = props;
|
||||
const insets = useSafeAreaInsets();
|
||||
const { themeVariables } = useAppearancePreferences();
|
||||
const inputRef = useRef<TextInput>(null);
|
||||
const [searchOpen, setSearchOpen] = useState(false);
|
||||
const searching = search !== undefined && (searchOpen || search.value.length > 0);
|
||||
const onSearchChange = search?.onChangeText;
|
||||
const closeSearch = useCallback(() => {
|
||||
onSearchChange?.("");
|
||||
setSearchOpen(false);
|
||||
Keyboard.dismiss();
|
||||
}, [onSearchChange]);
|
||||
useEffect(() => {
|
||||
if (!searching || search?.mode === "inline") return;
|
||||
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
|
||||
closeSearch();
|
||||
return true;
|
||||
});
|
||||
return () => subscription.remove();
|
||||
}, [closeSearch, searching, search?.mode]);
|
||||
const menuView = props.menus?.map((menu) => (
|
||||
<ControlPillMenu
|
||||
key={menu.title}
|
||||
actions={androidHeaderMenuActions(menu.items)}
|
||||
isAnchoredToRight
|
||||
title={menu.status ?? menu.title}
|
||||
onPressAction={({ nativeEvent }) => {
|
||||
const action = findHeaderMenuAction(menu.items, nativeEvent.event);
|
||||
if (action && !action.disabled) action.onPress();
|
||||
}}
|
||||
>
|
||||
{search?.mode === "inline" ? (
|
||||
<Pressable
|
||||
accessibilityLabel={menu.title}
|
||||
accessibilityRole="button"
|
||||
className="size-11 items-center justify-center rounded-full bg-subtle"
|
||||
>
|
||||
<SymbolView
|
||||
name={menu.icon}
|
||||
size={16}
|
||||
tintColorClassName="accent-icon"
|
||||
type="monochrome"
|
||||
/>
|
||||
</Pressable>
|
||||
) : (
|
||||
<ScreenHeaderButton accessibilityLabel={menu.title} icon={menu.icon} />
|
||||
)}
|
||||
</ControlPillMenu>
|
||||
));
|
||||
const options = (
|
||||
<NativeStackScreenOptions
|
||||
options={{
|
||||
...props.options,
|
||||
...(props.matchSearchSurface
|
||||
? { contentStyle: { backgroundColor: themeVariables["--color-header"] } }
|
||||
: undefined),
|
||||
headerShown: false,
|
||||
title: props.title,
|
||||
}}
|
||||
optionsVersion={props.optionsVersion}
|
||||
/>
|
||||
);
|
||||
if (search?.mode === "inline") {
|
||||
return (
|
||||
<>
|
||||
{options}
|
||||
<View
|
||||
className="border-b border-header-border bg-header px-3 pb-2.5"
|
||||
style={{ paddingTop: Math.max(insets.top, 12), borderBottomWidth: 0 }}
|
||||
>
|
||||
<View className="min-h-12 flex-row items-center gap-2">
|
||||
{props.onBack ? (
|
||||
<Pressable
|
||||
accessibilityLabel="Navigate up"
|
||||
accessibilityRole="button"
|
||||
hitSlop={8}
|
||||
onPress={props.onBack}
|
||||
className="size-11 items-center justify-center"
|
||||
>
|
||||
<SymbolView
|
||||
name="chevron.left"
|
||||
size={24}
|
||||
tintColorClassName="accent-foreground"
|
||||
type="monochrome"
|
||||
/>
|
||||
</Pressable>
|
||||
) : null}
|
||||
<View className="min-h-11 flex-1 flex-row items-center gap-2.5 rounded-2xl bg-input px-3.5">
|
||||
<SymbolView
|
||||
name="magnifyingglass"
|
||||
size={17}
|
||||
tintColorClassName="accent-icon"
|
||||
type="monochrome"
|
||||
/>
|
||||
<TextInput
|
||||
accessibilityLabel={search.placeholder}
|
||||
autoCapitalize="none"
|
||||
onChangeText={search.onChangeText}
|
||||
value={search.value}
|
||||
placeholder={search.placeholder}
|
||||
placeholderTextColorClassName="accent-placeholder"
|
||||
className="flex-1 py-2 text-base font-sans text-foreground"
|
||||
/>
|
||||
</View>
|
||||
{menuView}
|
||||
</View>
|
||||
</View>
|
||||
</>
|
||||
);
|
||||
}
|
||||
const header = (
|
||||
<AndroidScreenHeader
|
||||
title={props.title}
|
||||
subtitle={props.subtitle}
|
||||
onBack={props.onBack}
|
||||
leading={props.sidebar !== false ? <AndroidWorkspaceSidebarButton /> : undefined}
|
||||
hideBottomBorder={props.hideBottomBorder}
|
||||
actions={
|
||||
search
|
||||
? [
|
||||
{
|
||||
accessibilityLabel: search.placeholder,
|
||||
icon: "magnifyingglass",
|
||||
onPress: () => setSearchOpen(true),
|
||||
},
|
||||
...(search.onRefresh
|
||||
? [
|
||||
{
|
||||
accessibilityLabel: search.refreshAccessibilityLabel ?? "Refresh",
|
||||
icon: "arrow.clockwise" as const,
|
||||
onPress: search.onRefresh,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(props.actions ?? []),
|
||||
]
|
||||
: props.actions
|
||||
}
|
||||
trailing={
|
||||
<>
|
||||
{menuView}
|
||||
{props.trailing}
|
||||
</>
|
||||
}
|
||||
/>
|
||||
);
|
||||
return (
|
||||
<>
|
||||
{options}
|
||||
{search ? (
|
||||
<View>
|
||||
<View
|
||||
pointerEvents={searching ? "none" : "auto"}
|
||||
accessibilityElementsHidden={searching}
|
||||
importantForAccessibility={searching ? "no-hide-descendants" : "auto"}
|
||||
style={{ opacity: searching ? 0 : 1 }}
|
||||
>
|
||||
{header}
|
||||
</View>
|
||||
{searching ? (
|
||||
<View
|
||||
className="absolute inset-0 bg-header px-2 pb-2"
|
||||
style={{ paddingTop: Math.max(insets.top, 12) }}
|
||||
>
|
||||
<View className="min-h-14 flex-1 flex-row items-center gap-1">
|
||||
<ScreenHeaderButton
|
||||
accessibilityLabel={search.closeAccessibilityLabel ?? "Close search"}
|
||||
icon="arrow.left"
|
||||
onPress={closeSearch}
|
||||
/>
|
||||
<MaterialSearchField
|
||||
inputRef={inputRef}
|
||||
accessibilityLabel={search.placeholder}
|
||||
clearAccessibilityLabel={search.clearAccessibilityLabel ?? "Clear search"}
|
||||
placeholder={search.placeholder}
|
||||
value={search.value}
|
||||
onChangeText={search.onChangeText}
|
||||
/>
|
||||
</View>
|
||||
</View>
|
||||
) : null}
|
||||
</View>
|
||||
) : (
|
||||
header
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
import type { HeaderBarButtonMailSearchToolbarItem } from "react-native-screens";
|
||||
import { useId } from "react";
|
||||
import { createNativeHeaderMenu } from "./nativeHeaderMenu.ios";
|
||||
import { ScreenHeaderButton } from "./ScreenHeaderButton";
|
||||
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../native/StackHeader";
|
||||
import { useAdaptiveWorkspaceLayout } from "../features/layout/AdaptiveWorkspaceLayout";
|
||||
import {
|
||||
createNativeMailSearchToolbarItem,
|
||||
NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED,
|
||||
} from "../features/layout/native-mail-search-toolbar";
|
||||
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
|
||||
import type { ScreenHeaderMenuItem, ScreenHeaderProps } from "./ScreenHeader.types";
|
||||
import type { AppSymbolName } from "./AppSymbol";
|
||||
|
||||
function iosIcon(icon: AppSymbolName) {
|
||||
return typeof icon === "string" ? icon : icon.ios;
|
||||
}
|
||||
|
||||
type MailMenu = NonNullable<HeaderBarButtonMailSearchToolbarItem["filterMenu"]>;
|
||||
function mailMenuItems(items: ReadonlyArray<ScreenHeaderMenuItem>): MailMenu["items"] {
|
||||
return items.map((item) =>
|
||||
"items" in item
|
||||
? { type: "submenu", title: item.title ?? "", items: mailMenuItems(item.items) }
|
||||
: {
|
||||
type: "action",
|
||||
title: item.title,
|
||||
state: item.selected ? "on" : "off",
|
||||
disabled: item.disabled,
|
||||
onPress: item.onPress,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export function ScreenHeader(props: ScreenHeaderProps) {
|
||||
const headerId = useId();
|
||||
const { layout, panes, togglePrimarySidebar } = useAdaptiveWorkspaceLayout();
|
||||
const { themeVariables } = useAppearancePreferences();
|
||||
const { search, menus } = props;
|
||||
const menu = menus?.[0];
|
||||
const compactSearch =
|
||||
search !== undefined &&
|
||||
(search.compactToolbar ?? !layout.usesSplitView) &&
|
||||
NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED;
|
||||
const visibleMenus = compactSearch ? (menus?.slice(1) ?? []) : (menus ?? []);
|
||||
const refresh = search?.refreshInToolbar ? search.onRefresh : undefined;
|
||||
return (
|
||||
<>
|
||||
<NativeStackScreenOptions
|
||||
optionsVersion={[
|
||||
props.optionsVersion,
|
||||
compactSearch ? menu : undefined,
|
||||
compactSearch ? refresh : undefined,
|
||||
]}
|
||||
options={{
|
||||
headerShown: true,
|
||||
title: props.title,
|
||||
unstable_headerSubtitle: props.subtitle || undefined,
|
||||
...(props.matchSearchSurface
|
||||
? { contentStyle: { backgroundColor: themeVariables["--color-sheet-solid"] } }
|
||||
: undefined),
|
||||
...(search
|
||||
? {
|
||||
unstable_headerToolbarItems: compactSearch
|
||||
? () => [
|
||||
createNativeMailSearchToolbarItem({
|
||||
showsSearchDismissButton: true,
|
||||
placeholder: search.compactPlaceholder ?? search.placeholder,
|
||||
onSearchTextChange: search.onChangeText,
|
||||
searchTextChangeId: `${headerId}-search-text`,
|
||||
...(refresh
|
||||
? {
|
||||
composeButtonId: `${headerId}-refresh`,
|
||||
composeSystemImageName: "arrow.clockwise",
|
||||
onComposePress: refresh,
|
||||
}
|
||||
: undefined),
|
||||
...(menu
|
||||
? {
|
||||
filterButtonId: `${headerId}-filter`,
|
||||
filterSystemImageName: iosIcon(menu.icon),
|
||||
filterMenu: { title: menu.title, items: mailMenuItems(menu.items) },
|
||||
}
|
||||
: undefined),
|
||||
}),
|
||||
]
|
||||
: undefined,
|
||||
headerSearchBarOptions: compactSearch
|
||||
? undefined
|
||||
: {
|
||||
allowToolbarIntegration: true,
|
||||
...(NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED && search.mode === "inline"
|
||||
? { placement: "integratedButton" as const }
|
||||
: undefined),
|
||||
autoCapitalize: "none",
|
||||
hideNavigationBar: false,
|
||||
...(search.mode === "inline" ? { obscureBackground: false } : undefined),
|
||||
placeholder: search.placeholder,
|
||||
onChangeText: (event) => search.onChangeText(event.nativeEvent.text),
|
||||
onCancelButtonPress: () => search.onChangeText(""),
|
||||
},
|
||||
}
|
||||
: undefined),
|
||||
...props.options,
|
||||
}}
|
||||
/>
|
||||
{props.sidebar !== false && layout.usesSplitView ? (
|
||||
<NativeHeaderToolbar placement="left">
|
||||
{props.backInSplitView && (props.backInSplitView.onPress || props.onBack) ? (
|
||||
<ScreenHeaderButton
|
||||
{...props.backInSplitView}
|
||||
onPress={props.backInSplitView.onPress ?? props.onBack}
|
||||
/>
|
||||
) : null}
|
||||
<ScreenHeaderButton
|
||||
accessibilityLabel={
|
||||
panes.primarySidebarVisible ? `Maximize ${props.title.toLowerCase()}` : "Show threads"
|
||||
}
|
||||
icon={
|
||||
panes.primarySidebarVisible ? "arrow.up.left.and.arrow.down.right" : "sidebar.left"
|
||||
}
|
||||
onPress={togglePrimarySidebar}
|
||||
separateBackground={
|
||||
props.backInSplitView ? props.backInSplitView.separateBackground : true
|
||||
}
|
||||
/>
|
||||
</NativeHeaderToolbar>
|
||||
) : null}
|
||||
{(props.actions?.length ||
|
||||
(!compactSearch && refresh) ||
|
||||
visibleMenus.length ||
|
||||
props.trailing) &&
|
||||
props.options?.unstable_headerRightItems === undefined ? (
|
||||
<NativeHeaderToolbar placement="right">
|
||||
{props.actions?.map((action) => (
|
||||
<ScreenHeaderButton
|
||||
key={action.accessibilityLabel}
|
||||
{...action}
|
||||
icon={iosIcon(action.icon)}
|
||||
separateBackground
|
||||
/>
|
||||
))}
|
||||
{refresh && !compactSearch ? (
|
||||
<ScreenHeaderButton
|
||||
accessibilityLabel={search?.refreshAccessibilityLabel}
|
||||
icon="arrow.clockwise"
|
||||
onPress={refresh}
|
||||
separateBackground
|
||||
/>
|
||||
) : null}
|
||||
{visibleMenus.map(createNativeHeaderMenu)}
|
||||
{props.trailing}
|
||||
</NativeHeaderToolbar>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export type {
|
||||
ScreenHeaderAction,
|
||||
ScreenHeaderMenu,
|
||||
ScreenHeaderMenuItem,
|
||||
ScreenHeaderProps,
|
||||
ScreenHeaderSearch,
|
||||
} from "./ScreenHeader.types";
|
||||
@@ -0,0 +1,76 @@
|
||||
import type { ReactNode } from "react";
|
||||
import type { ColorValue } from "react-native";
|
||||
import type { AppNativeStackNavigationOptions } from "../native/StackHeader";
|
||||
import type { AppSymbolName } from "./AppSymbol";
|
||||
|
||||
export interface ScreenHeaderAction {
|
||||
readonly accessibilityLabel: string;
|
||||
readonly icon: AppSymbolName;
|
||||
readonly onPress: () => void;
|
||||
readonly disabled?: boolean;
|
||||
readonly selected?: boolean;
|
||||
readonly tintColor?: ColorValue;
|
||||
}
|
||||
|
||||
export type ScreenHeaderMenuItem =
|
||||
| {
|
||||
readonly id: string;
|
||||
readonly title: string;
|
||||
readonly icon?: string;
|
||||
readonly subtitle?: string;
|
||||
readonly disabled?: boolean;
|
||||
readonly selected?: boolean;
|
||||
readonly onPress: () => void;
|
||||
}
|
||||
| {
|
||||
readonly id: string;
|
||||
readonly title?: string;
|
||||
readonly icon?: string;
|
||||
readonly inline?: boolean;
|
||||
readonly items: ReadonlyArray<ScreenHeaderMenuItem>;
|
||||
};
|
||||
|
||||
export interface ScreenHeaderMenu {
|
||||
readonly title: string;
|
||||
readonly icon: AppSymbolName;
|
||||
readonly status?: string;
|
||||
readonly separateBackground?: boolean;
|
||||
readonly items: ReadonlyArray<ScreenHeaderMenuItem>;
|
||||
}
|
||||
|
||||
export interface ScreenHeaderSearch {
|
||||
readonly value: string;
|
||||
readonly onChangeText: (value: string) => void;
|
||||
readonly placeholder: string;
|
||||
readonly mode?: "inline" | "collapsible";
|
||||
readonly compactToolbar?: boolean;
|
||||
readonly compactPlaceholder?: string;
|
||||
readonly closeAccessibilityLabel?: string;
|
||||
readonly clearAccessibilityLabel?: string;
|
||||
readonly onRefresh?: () => void;
|
||||
readonly refreshAccessibilityLabel?: string;
|
||||
readonly refreshInToolbar?: boolean;
|
||||
}
|
||||
|
||||
export interface ScreenHeaderProps {
|
||||
readonly title: string;
|
||||
readonly subtitle?: string;
|
||||
readonly actions?: ReadonlyArray<ScreenHeaderAction>;
|
||||
readonly menus?: ReadonlyArray<ScreenHeaderMenu>;
|
||||
readonly search?: ScreenHeaderSearch;
|
||||
readonly onBack?: () => void;
|
||||
readonly sidebar?: boolean;
|
||||
readonly backInSplitView?: {
|
||||
readonly accessibilityLabel: string;
|
||||
readonly icon: string;
|
||||
readonly separateBackground?: boolean;
|
||||
readonly onPress?: () => void;
|
||||
};
|
||||
readonly hideBottomBorder?: boolean;
|
||||
readonly trailing?: ReactNode;
|
||||
/** Search scenes use sheet colors on iOS and header colors on Android. */
|
||||
readonly matchSearchSurface?: boolean;
|
||||
/** Custom native layouts can override options without duplicating the header renderer. */
|
||||
readonly options?: AppNativeStackNavigationOptions;
|
||||
readonly optionsVersion?: unknown;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { AndroidHeaderIconButton as ScreenHeaderButton } from "./AndroidScreenHeader";
|
||||
@@ -0,0 +1,4 @@
|
||||
import { NativeHeaderToolbar } from "../native/StackHeader";
|
||||
export const ScreenHeaderButton: (
|
||||
props: Parameters<typeof NativeHeaderToolbar.Button>[0] & { readonly selected?: boolean },
|
||||
) => ReturnType<typeof NativeHeaderToolbar.Button> = NativeHeaderToolbar.Button;
|
||||
@@ -0,0 +1,43 @@
|
||||
import type { MenuAction } from "@react-native-menu/menu";
|
||||
import type { ScreenHeaderMenuItem } from "./ScreenHeader.types";
|
||||
|
||||
export function androidHeaderMenuActions(items: ReadonlyArray<ScreenHeaderMenuItem>): MenuAction[] {
|
||||
return items.flatMap((item): MenuAction[] =>
|
||||
"items" in item
|
||||
? item.inline && !item.title
|
||||
? androidHeaderMenuActions(item.items)
|
||||
: [
|
||||
{
|
||||
id: item.id,
|
||||
title: item.title ?? "",
|
||||
image: item.icon,
|
||||
subactions: androidHeaderMenuActions(item.items),
|
||||
},
|
||||
]
|
||||
: [
|
||||
{
|
||||
id: item.id,
|
||||
title: item.title,
|
||||
subtitle: item.subtitle,
|
||||
image: item.icon,
|
||||
state: item.selected ? "on" : undefined,
|
||||
attributes: item.disabled ? { disabled: true } : undefined,
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
export function findHeaderMenuAction(
|
||||
items: ReadonlyArray<ScreenHeaderMenuItem>,
|
||||
id: string,
|
||||
): Extract<ScreenHeaderMenuItem, { 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;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import { androidHeaderMenuActions, findHeaderMenuAction } from "./headerMenu.android";
|
||||
import type { ScreenHeaderMenuItem } from "./ScreenHeader.types";
|
||||
|
||||
const items: ReadonlyArray<ScreenHeaderMenuItem> = [
|
||||
{
|
||||
id: "sessions",
|
||||
title: "Sessions",
|
||||
items: [
|
||||
{
|
||||
id: "terminal-session:main",
|
||||
title: "Main shell",
|
||||
subtitle: "Running · workspace",
|
||||
selected: true,
|
||||
onPress: () => {},
|
||||
},
|
||||
{ id: "closed", title: "Closed shell", disabled: true, onPress: () => {} },
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
describe("header menus", () => {
|
||||
it("keeps named sections as Android submenus", () => {
|
||||
const actions = androidHeaderMenuActions([
|
||||
{
|
||||
id: "text-size",
|
||||
title: "Text size",
|
||||
inline: true,
|
||||
items: [{ id: "font-increase", title: "A+", onPress: () => {} }],
|
||||
},
|
||||
]);
|
||||
expect(actions[0]?.title).toBe("Text size");
|
||||
expect(actions[0]?.subactions?.[0]?.id).toBe("font-increase");
|
||||
});
|
||||
|
||||
it("flattens inline groups while keeping their action IDs", () => {
|
||||
const actions = androidHeaderMenuActions([
|
||||
{ id: "modes", inline: true, items: [{ id: "code", title: "Code", onPress: () => {} }] },
|
||||
]);
|
||||
expect(actions.map(({ id }) => id)).toEqual(["code"]);
|
||||
});
|
||||
|
||||
it("keeps stable action IDs, selection, and disabled state inside submenus", () => {
|
||||
const menu = androidHeaderMenuActions(items);
|
||||
expect(menu[0]?.id).toBe("sessions");
|
||||
expect(menu[0]?.subactions?.[0]).toMatchObject({
|
||||
id: "terminal-session:main",
|
||||
title: "Main shell",
|
||||
subtitle: "Running · workspace",
|
||||
state: "on",
|
||||
});
|
||||
expect(menu[0]?.subactions?.[1]).toMatchObject({
|
||||
id: "closed",
|
||||
attributes: { disabled: true },
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves nested action IDs without dispatching group or missing IDs", () => {
|
||||
expect(findHeaderMenuAction(items, "terminal-session:main")).toMatchObject({
|
||||
title: "Main shell",
|
||||
selected: true,
|
||||
});
|
||||
expect(findHeaderMenuAction(items, "sessions")).toBeUndefined();
|
||||
expect(findHeaderMenuAction(items, "removed-session")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { NativeHeaderToolbar } from "../native/StackHeader";
|
||||
import type { ScreenHeaderMenu, ScreenHeaderMenuItem } from "./ScreenHeader.types";
|
||||
|
||||
function renderMenuItems(items: ReadonlyArray<ScreenHeaderMenuItem>) {
|
||||
return items.map((item) =>
|
||||
"items" in item ? (
|
||||
<NativeHeaderToolbar.Menu
|
||||
key={item.id}
|
||||
title={item.title}
|
||||
icon={item.icon}
|
||||
inline={item.inline}
|
||||
>
|
||||
{renderMenuItems(item.items)}
|
||||
</NativeHeaderToolbar.Menu>
|
||||
) : (
|
||||
<NativeHeaderToolbar.MenuAction
|
||||
key={item.id}
|
||||
icon={item.icon}
|
||||
subtitle={item.subtitle}
|
||||
disabled={item.disabled}
|
||||
isOn={item.selected}
|
||||
onPress={item.onPress}
|
||||
>
|
||||
{item.title}
|
||||
</NativeHeaderToolbar.MenuAction>
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** Returns direct native items for toolbar serialization, including nested menu data. */
|
||||
export function createNativeHeaderMenu(menu: ScreenHeaderMenu) {
|
||||
return (
|
||||
<NativeHeaderToolbar.Menu
|
||||
key={menu.title}
|
||||
title={menu.title}
|
||||
accessibilityLabel={menu.title}
|
||||
icon={typeof menu.icon === "string" ? menu.icon : menu.icon.ios}
|
||||
separateBackground={menu.separateBackground ?? true}
|
||||
>
|
||||
{menu.status ? <NativeHeaderToolbar.Label>{menu.status}</NativeHeaderToolbar.Label> : null}
|
||||
{renderMenuItems(menu.items)}
|
||||
</NativeHeaderToolbar.Menu>
|
||||
);
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
|
||||
import { CameraView, useCameraPermissions } from "expo-camera";
|
||||
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
|
||||
import {
|
||||
StackActions,
|
||||
useNavigation,
|
||||
@@ -194,7 +193,8 @@ export function ConnectionsNewRouteScreen({
|
||||
actions={[
|
||||
{
|
||||
accessibilityLabel: showScanner ? "Close scanner" : "Scan QR code",
|
||||
icon: showScanner ? "xmark" : "camera",
|
||||
icon: showScanner ? "xmark" : Platform.OS === "ios" ? "qrcode.viewfinder" : "camera",
|
||||
tintColor: headerIconColor,
|
||||
onPress: () => {
|
||||
if (showScanner) {
|
||||
closeScanner();
|
||||
@@ -205,26 +205,6 @@ export function ConnectionsNewRouteScreen({
|
||||
},
|
||||
]}
|
||||
>
|
||||
<NativeStackScreenOptions
|
||||
options={{ title: showScanner ? "Scan QR Code" : "Add Environment" }}
|
||||
/>
|
||||
{Platform.OS !== "android" ? (
|
||||
<NativeHeaderToolbar placement="right">
|
||||
<NativeHeaderToolbar.Button
|
||||
icon={showScanner ? "xmark" : "qrcode.viewfinder"}
|
||||
onPress={() => {
|
||||
if (showScanner) {
|
||||
closeScanner();
|
||||
} else {
|
||||
void openScanner();
|
||||
}
|
||||
}}
|
||||
separateBackground
|
||||
tintColor={headerIconColor}
|
||||
/>
|
||||
</NativeHeaderToolbar>
|
||||
) : null}
|
||||
|
||||
<ScrollView
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
showsVerticalScrollIndicator={false}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView";
|
||||
import { NativeHeaderToolbar } from "../../native/StackHeader";
|
||||
import { useNavigation } from "@react-navigation/native";
|
||||
import type { EnvironmentId } from "@t3tools/contracts";
|
||||
import { useCallback, useState } from "react";
|
||||
import { Platform } from "react-native";
|
||||
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
||||
|
||||
import { SettingsScreen } from "./components/SettingsScreen";
|
||||
@@ -82,6 +80,7 @@ export function SettingsEnvironmentsRouteScreen() {
|
||||
{
|
||||
accessibilityLabel: "Add environment",
|
||||
icon: "plus",
|
||||
tintColor: headerIconColor,
|
||||
onPress: () =>
|
||||
navigation.navigate("SettingsSheet", {
|
||||
screen: "SettingsContent",
|
||||
@@ -90,21 +89,6 @@ export function SettingsEnvironmentsRouteScreen() {
|
||||
},
|
||||
]}
|
||||
>
|
||||
{Platform.OS !== "android" ? (
|
||||
<NativeHeaderToolbar placement="right">
|
||||
<NativeHeaderToolbar.Button
|
||||
icon="plus"
|
||||
onPress={() =>
|
||||
navigation.navigate("SettingsSheet", {
|
||||
screen: "SettingsContent",
|
||||
params: { screen: "SettingsEnvironmentNew" },
|
||||
})
|
||||
}
|
||||
separateBackground
|
||||
tintColor={headerIconColor}
|
||||
/>
|
||||
</NativeHeaderToolbar>
|
||||
) : null}
|
||||
<ScrollView
|
||||
contentInsetAdjustmentBehavior="automatic"
|
||||
showsVerticalScrollIndicator={false}
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import { useNavigation } from "@react-navigation/native";
|
||||
import type { ComponentProps, ReactNode } from "react";
|
||||
import type { ReactNode } from "react";
|
||||
import { Platform, View } from "react-native";
|
||||
|
||||
import { AndroidScreenHeader } from "../../../components/AndroidScreenHeader";
|
||||
import type { ScreenHeaderProps } from "../../../components/ScreenHeader.types";
|
||||
import { ScreenHeader } from "../../../components/ScreenHeader";
|
||||
import { MaterialScreenContent as SettingsScreenContent } from "../../../components/MaterialScreenContent";
|
||||
import { NativeStackScreenOptions } from "../../../native/StackHeader";
|
||||
import { AndroidWorkspaceSidebarButton } from "../../layout/workspace-sidebar-toolbar";
|
||||
|
||||
export { SettingsScreenContent };
|
||||
|
||||
export function SettingsScreen(
|
||||
props: Pick<ComponentProps<typeof AndroidScreenHeader>, "title" | "actions" | "trailing"> & {
|
||||
props: Pick<ScreenHeaderProps, "title" | "actions" | "trailing"> & {
|
||||
readonly children: ReactNode;
|
||||
/** A native form sheet already owns its rounded outer frame. */
|
||||
readonly formSheet?: boolean;
|
||||
@@ -20,19 +19,14 @@ export function SettingsScreen(
|
||||
|
||||
return (
|
||||
<View collapsable={false} className="flex-1 bg-sheet">
|
||||
{Platform.OS === "android" ? (
|
||||
<>
|
||||
<NativeStackScreenOptions options={{ headerShown: false }} />
|
||||
<AndroidScreenHeader
|
||||
title={props.title}
|
||||
leading={props.formSheet ? undefined : <AndroidWorkspaceSidebarButton />}
|
||||
actions={props.actions}
|
||||
trailing={props.trailing}
|
||||
onBack={() => navigation.goBack()}
|
||||
hideBottomBorder={!props.formSheet}
|
||||
/>
|
||||
</>
|
||||
) : null}
|
||||
<ScreenHeader
|
||||
title={props.title}
|
||||
actions={props.actions}
|
||||
trailing={Platform.OS === "android" ? props.trailing : undefined}
|
||||
sidebar={Platform.OS === "android" && !props.formSheet}
|
||||
onBack={() => navigation.goBack()}
|
||||
hideBottomBorder={!props.formSheet}
|
||||
/>
|
||||
{props.formSheet ? (
|
||||
props.children
|
||||
) : (
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type {
|
||||
ThreadGitControls as IosThreadGitControls,
|
||||
ThreadGitMenu as IosThreadGitMenu,
|
||||
useThreadGitRightHeaderItems as useIosThreadGitRightHeaderItems,
|
||||
} from "./ThreadGitControls";
|
||||
|
||||
export type { ThreadGitMenuProps } from "./ThreadGitControls";
|
||||
|
||||
type ThreadGitControlsProps = Parameters<typeof IosThreadGitControls>[0];
|
||||
const EMPTY_HEADER_ITEMS: ReturnType<typeof useIosThreadGitRightHeaderItems> = [];
|
||||
|
||||
export function useThreadGitRightHeaderItems(_props: ThreadGitControlsProps) {
|
||||
return EMPTY_HEADER_ITEMS;
|
||||
}
|
||||
|
||||
export function useThreadGitCenterHeaderItems(_props: ThreadGitControlsProps) {
|
||||
return EMPTY_HEADER_ITEMS;
|
||||
}
|
||||
|
||||
export function ThreadGitControls(_props: ThreadGitControlsProps) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function ThreadGitMenu(_props: Parameters<typeof IosThreadGitMenu>[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
export function useThreadGitMenuDefinition(_props: Parameters<typeof IosThreadGitMenu>[0]) {
|
||||
return null;
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
import { createNativeHeaderMenu } from "../../components/nativeHeaderMenu.ios";
|
||||
import type { ScreenHeaderMenu } from "../../components/ScreenHeader.types";
|
||||
import {
|
||||
EnvironmentId,
|
||||
type GitRunStackedActionResult,
|
||||
@@ -426,6 +428,7 @@ export function ThreadGitControls(props: ThreadGitControlsProps) {
|
||||
) : null}
|
||||
{showActionControls ? (
|
||||
<NativeHeaderToolbar.Menu
|
||||
accessibilityLabel="Open terminal"
|
||||
icon="terminal"
|
||||
disabled={!props.canOpenTerminal}
|
||||
separateBackground
|
||||
@@ -489,7 +492,7 @@ export function ThreadGitControls(props: ThreadGitControlsProps) {
|
||||
separateBackground
|
||||
/>
|
||||
) : null}
|
||||
{showActionControls ? <ThreadGitMenu {...props} /> : null}
|
||||
{showActionControls ? createNativeHeaderMenu(threadGitMenuDefinition(props, model)) : null}
|
||||
</NativeHeaderToolbar>
|
||||
);
|
||||
}
|
||||
@@ -500,43 +503,57 @@ export function ThreadGitControls(props: ThreadGitControlsProps) {
|
||||
* chat header and the review screen's toolbar.
|
||||
*/
|
||||
export function ThreadGitMenu(props: ThreadGitMenuProps) {
|
||||
const model = useThreadGitControlModel(props);
|
||||
|
||||
return (
|
||||
<NativeHeaderToolbar.Menu icon="point.topleft.down.curvedto.point.bottomright.up">
|
||||
<NativeHeaderToolbar.MenuAction
|
||||
icon="point.topleft.down.curvedto.point.bottomright.up"
|
||||
disabled
|
||||
onPress={() => {}}
|
||||
subtitle={compactMenuStatus(props.gitStatus)}
|
||||
>
|
||||
<NativeHeaderToolbar.Label>
|
||||
{compactMenuBranchLabel(model.currentBranchLabel)}
|
||||
</NativeHeaderToolbar.Label>
|
||||
</NativeHeaderToolbar.MenuAction>
|
||||
<NativeHeaderToolbar.MenuAction
|
||||
icon={model.quickActionIcon}
|
||||
disabled={model.quickAction.disabled}
|
||||
onPress={() => void model.runQuickAction()}
|
||||
subtitle={model.quickActionHint ?? undefined}
|
||||
>
|
||||
<NativeHeaderToolbar.Label>{model.quickAction.label}</NativeHeaderToolbar.Label>
|
||||
</NativeHeaderToolbar.MenuAction>
|
||||
<NativeHeaderToolbar.MenuAction
|
||||
icon="text.bubble"
|
||||
disabled={!model.isRepo}
|
||||
onPress={model.openReview}
|
||||
subtitle="Turn diffs and worktree changes"
|
||||
>
|
||||
<NativeHeaderToolbar.Label>Review changes</NativeHeaderToolbar.Label>
|
||||
</NativeHeaderToolbar.MenuAction>
|
||||
<NativeHeaderToolbar.MenuAction
|
||||
icon="ellipsis"
|
||||
onPress={model.openGitInspector}
|
||||
subtitle="Commit, files, branches"
|
||||
>
|
||||
<NativeHeaderToolbar.Label>More</NativeHeaderToolbar.Label>
|
||||
</NativeHeaderToolbar.MenuAction>
|
||||
</NativeHeaderToolbar.Menu>
|
||||
);
|
||||
const menu = useThreadGitMenuDefinition(props);
|
||||
return menu ? createNativeHeaderMenu(menu) : null;
|
||||
}
|
||||
|
||||
/** Returns menu data because native toolbars serialize direct items rather than rendering component children. */
|
||||
export function useThreadGitMenuDefinition(props: ThreadGitMenuProps): ScreenHeaderMenu | null {
|
||||
return threadGitMenuDefinition(props, useThreadGitControlModel(props));
|
||||
}
|
||||
|
||||
function threadGitMenuDefinition(
|
||||
props: ThreadGitMenuProps,
|
||||
model: ReturnType<typeof useThreadGitControlModel>,
|
||||
): ScreenHeaderMenu {
|
||||
return {
|
||||
title: "Git controls",
|
||||
icon: "point.topleft.down.curvedto.point.bottomright.up",
|
||||
separateBackground: false,
|
||||
items: [
|
||||
{
|
||||
id: "git-status",
|
||||
title: compactMenuBranchLabel(model.currentBranchLabel),
|
||||
icon: "point.topleft.down.curvedto.point.bottomright.up",
|
||||
disabled: true,
|
||||
subtitle: compactMenuStatus(props.gitStatus),
|
||||
onPress: () => {},
|
||||
},
|
||||
{
|
||||
id: "git-quick-action",
|
||||
title: model.quickAction.label,
|
||||
icon: model.quickActionIcon,
|
||||
disabled: model.quickAction.disabled,
|
||||
subtitle: model.quickActionHint ?? undefined,
|
||||
onPress: () => {
|
||||
void model.runQuickAction();
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "git-review",
|
||||
title: "Review changes",
|
||||
icon: "text.bubble",
|
||||
disabled: !model.isRepo,
|
||||
subtitle: "Turn diffs and worktree changes",
|
||||
onPress: model.openReview,
|
||||
},
|
||||
{
|
||||
id: "git-more",
|
||||
title: "More",
|
||||
icon: "ellipsis",
|
||||
subtitle: "Commit, files, branches",
|
||||
onPress: model.openGitInspector,
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import type { NativeHeaderToolbar as IosNativeHeaderToolbar } from "./NativeHeaderToolbar";
|
||||
|
||||
function NativeHeaderToolbarRoot(_props: Parameters<typeof IosNativeHeaderToolbar>[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarButton(_props: Parameters<typeof IosNativeHeaderToolbar.Button>[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarMenu(_props: Parameters<typeof IosNativeHeaderToolbar.Menu>[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarMenuAction(
|
||||
_props: Parameters<typeof IosNativeHeaderToolbar.MenuAction>[0],
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarLabel(_props: Parameters<typeof IosNativeHeaderToolbar.Label>[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarSpacer(_props: Parameters<typeof IosNativeHeaderToolbar.Spacer>[0]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarSearchBarSlot() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Native header item factories are iOS-only; Android owns its in-flow header.
|
||||
export const NativeHeaderToolbar = Object.assign(NativeHeaderToolbarRoot, {
|
||||
Button: NativeHeaderToolbarButton,
|
||||
Label: NativeHeaderToolbarLabel,
|
||||
Menu: Object.assign(NativeHeaderToolbarMenu, {
|
||||
Action: NativeHeaderToolbarMenuAction,
|
||||
}),
|
||||
MenuAction: NativeHeaderToolbarMenuAction,
|
||||
SearchBarSlot: NativeHeaderToolbarSearchBarSlot,
|
||||
Spacer: NativeHeaderToolbarSpacer,
|
||||
});
|
||||
@@ -0,0 +1,294 @@
|
||||
import { useNavigation, type ParamListBase } from "@react-navigation/native";
|
||||
import type {
|
||||
NativeStackHeaderItem,
|
||||
NativeStackHeaderItemMenu,
|
||||
NativeStackNavigationOptions,
|
||||
NativeStackNavigationProp,
|
||||
} from "@react-navigation/native-stack";
|
||||
import {
|
||||
Children,
|
||||
isValidElement,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
type ReactElement,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import type { ColorValue } from "react-native";
|
||||
|
||||
function useNativeStackNavigation(): NativeStackNavigationProp<ParamListBase> | null {
|
||||
return useNavigation<NativeStackNavigationProp<ParamListBase>>();
|
||||
}
|
||||
|
||||
function labelFromChildren(children: ReactNode): string {
|
||||
const parts: string[] = [];
|
||||
Children.forEach(children, (child) => {
|
||||
if (typeof child === "string" || typeof child === "number") {
|
||||
parts.push(String(child));
|
||||
} else if (isValidElement<{ children?: ReactNode }>(child)) {
|
||||
parts.push(labelFromChildren(child.props.children));
|
||||
}
|
||||
});
|
||||
return parts.join("");
|
||||
}
|
||||
|
||||
type NativeStackHeaderIcon = NonNullable<
|
||||
Extract<NativeStackHeaderItem, { type: "button" }>["icon"]
|
||||
>;
|
||||
type NativeStackOptionsWithToolbar = NativeStackNavigationOptions & {
|
||||
unstable_headerToolbarItems?: () => NativeStackHeaderItem[];
|
||||
};
|
||||
|
||||
function iconFromProp(icon: unknown): NativeStackHeaderIcon | undefined {
|
||||
if (typeof icon !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
return { type: "sfSymbol", name: icon as never };
|
||||
}
|
||||
|
||||
type ToolbarElementProps = Record<string, unknown> & { readonly children?: ReactNode };
|
||||
|
||||
function elementTypeName(element: ReactElement): string | undefined {
|
||||
const type = element.type;
|
||||
if (typeof type === "function") {
|
||||
return (type as { displayName?: string; name?: string }).displayName ?? type.name;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function convertMenuAction(
|
||||
element: ReactElement<ToolbarElementProps>,
|
||||
): NativeStackHeaderItemMenu["menu"]["items"][number] | null {
|
||||
const typeName = elementTypeName(element);
|
||||
if (typeName === "NativeHeaderToolbarMenuAction") {
|
||||
const label = labelFromChildren(element.props.children);
|
||||
return {
|
||||
type: "action",
|
||||
label,
|
||||
description: typeof element.props.subtitle === "string" ? element.props.subtitle : undefined,
|
||||
disabled: Boolean(element.props.disabled),
|
||||
icon: iconFromProp(element.props.icon),
|
||||
onPress:
|
||||
typeof element.props.onPress === "function"
|
||||
? (element.props.onPress as () => void)
|
||||
: () => undefined,
|
||||
state: element.props.isOn === true ? "on" : undefined,
|
||||
destructive: Boolean(element.props.destructive),
|
||||
discoverabilityLabel:
|
||||
typeof element.props.discoverabilityLabel === "string"
|
||||
? element.props.discoverabilityLabel
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
if (typeName === "NativeHeaderToolbarMenu") {
|
||||
return {
|
||||
type: "submenu",
|
||||
label:
|
||||
typeof element.props.title === "string"
|
||||
? element.props.title
|
||||
: labelFromChildren(element.props.children),
|
||||
icon: iconFromProp(element.props.icon),
|
||||
inline: Boolean(element.props.inline),
|
||||
items: collectMenuItems(element.props.children),
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function collectMenuItems(children: ReactNode): NativeStackHeaderItemMenu["menu"]["items"] {
|
||||
const items: NativeStackHeaderItemMenu["menu"]["items"] = [];
|
||||
Children.forEach(children, (child) => {
|
||||
if (!isValidElement<ToolbarElementProps>(child)) {
|
||||
return;
|
||||
}
|
||||
const item = convertMenuAction(child);
|
||||
if (item) {
|
||||
items.push(item);
|
||||
return;
|
||||
}
|
||||
items.push(...collectMenuItems(child.props.children));
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
function convertToolbarChild(child: ReactNode): NativeStackHeaderItem | null {
|
||||
if (!isValidElement<ToolbarElementProps>(child)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const typeName = elementTypeName(child);
|
||||
if (typeName === "NativeHeaderToolbarButton") {
|
||||
return {
|
||||
type: "button",
|
||||
label: typeof child.props.label === "string" ? child.props.label : "",
|
||||
accessibilityLabel:
|
||||
typeof child.props.accessibilityLabel === "string"
|
||||
? child.props.accessibilityLabel
|
||||
: undefined,
|
||||
disabled: Boolean(child.props.disabled),
|
||||
icon: iconFromProp(child.props.icon),
|
||||
onPress:
|
||||
typeof child.props.onPress === "function"
|
||||
? (child.props.onPress as () => void)
|
||||
: () => undefined,
|
||||
sharesBackground: !child.props.separateBackground,
|
||||
tintColor: child.props.tintColor as ColorValue | undefined,
|
||||
variant: "plain",
|
||||
};
|
||||
}
|
||||
|
||||
if (typeName === "NativeHeaderToolbarMenu") {
|
||||
return {
|
||||
type: "menu",
|
||||
label: typeof child.props.title === "string" ? child.props.title : "",
|
||||
accessibilityLabel:
|
||||
typeof child.props.accessibilityLabel === "string"
|
||||
? child.props.accessibilityLabel
|
||||
: undefined,
|
||||
disabled: Boolean(child.props.disabled),
|
||||
icon: iconFromProp(child.props.icon),
|
||||
menu: {
|
||||
title: typeof child.props.title === "string" ? child.props.title : undefined,
|
||||
items: collectMenuItems(child.props.children),
|
||||
},
|
||||
sharesBackground: !child.props.separateBackground,
|
||||
tintColor: child.props.tintColor as ColorValue | undefined,
|
||||
variant: "plain",
|
||||
};
|
||||
}
|
||||
|
||||
if (typeName === "NativeHeaderToolbarSpacer") {
|
||||
return {
|
||||
type: "spacing",
|
||||
spacing: typeof child.props.width === "number" ? child.props.width : 8,
|
||||
flexible: Boolean(child.props.flexible),
|
||||
} as NativeStackHeaderItem;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function collectToolbarItems(children: ReactNode): NativeStackHeaderItem[] {
|
||||
const items: NativeStackHeaderItem[] = [];
|
||||
Children.forEach(children, (child) => {
|
||||
const item = convertToolbarChild(child);
|
||||
if (item) {
|
||||
if (item.type === "spacing") {
|
||||
// Native inserts spacing items at `index`, treating a missing index
|
||||
// as 0 — which would move the spacer in front of earlier siblings.
|
||||
(item as { index?: number }).index = items.length;
|
||||
}
|
||||
items.push(item);
|
||||
}
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarRoot(props: {
|
||||
readonly placement?: "left" | "right" | "bottom";
|
||||
readonly children?: ReactNode;
|
||||
}) {
|
||||
const navigation = useNativeStackNavigation();
|
||||
const items = useMemo(() => collectToolbarItems(props.children), [props.children]);
|
||||
|
||||
// Swap toolbar owners before paint so split and compact headers cannot clear each other.
|
||||
useLayoutEffect(() => {
|
||||
if (!navigation) {
|
||||
return;
|
||||
}
|
||||
if (props.placement === "bottom") {
|
||||
navigation.setOptions({
|
||||
unstable_headerToolbarItems: () => items,
|
||||
} as NativeStackOptionsWithToolbar);
|
||||
return () => {
|
||||
navigation.setOptions({
|
||||
unstable_headerToolbarItems: () => [],
|
||||
} as NativeStackOptionsWithToolbar);
|
||||
};
|
||||
}
|
||||
if (props.placement === "left") {
|
||||
navigation.setOptions({ unstable_headerLeftItems: () => items });
|
||||
return () => {
|
||||
navigation.setOptions({ unstable_headerLeftItems: () => [] });
|
||||
};
|
||||
}
|
||||
navigation.setOptions({ unstable_headerRightItems: () => items });
|
||||
return () => {
|
||||
navigation.setOptions({ unstable_headerRightItems: () => [] });
|
||||
};
|
||||
}, [items, navigation, props.placement]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarButton(_props: {
|
||||
readonly accessibilityLabel?: string;
|
||||
readonly disabled?: boolean;
|
||||
readonly icon?: string;
|
||||
readonly label?: string;
|
||||
readonly onPress?: () => void;
|
||||
readonly separateBackground?: boolean;
|
||||
readonly tintColor?: ColorValue;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarButton.displayName = "NativeHeaderToolbarButton";
|
||||
|
||||
function NativeHeaderToolbarMenu(_props: {
|
||||
readonly accessibilityLabel?: string;
|
||||
readonly children?: ReactNode;
|
||||
readonly disabled?: boolean;
|
||||
readonly icon?: string;
|
||||
readonly inline?: boolean;
|
||||
readonly separateBackground?: boolean;
|
||||
readonly tintColor?: ColorValue;
|
||||
readonly title?: string;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarMenu.displayName = "NativeHeaderToolbarMenu";
|
||||
|
||||
function NativeHeaderToolbarMenuAction(_props: {
|
||||
readonly children?: ReactNode;
|
||||
readonly destructive?: boolean;
|
||||
readonly disabled?: boolean;
|
||||
readonly discoverabilityLabel?: string;
|
||||
readonly icon?: string;
|
||||
readonly isOn?: boolean;
|
||||
readonly onPress?: () => void;
|
||||
readonly subtitle?: string;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarMenuAction.displayName = "NativeHeaderToolbarMenuAction";
|
||||
|
||||
function NativeHeaderToolbarLabel(_props: { readonly children?: ReactNode }) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarLabel.displayName = "NativeHeaderToolbarLabel";
|
||||
|
||||
function NativeHeaderToolbarSpacer(_props: {
|
||||
readonly flexible?: boolean;
|
||||
readonly sharesBackground?: boolean;
|
||||
readonly width?: number;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarSpacer.displayName = "NativeHeaderToolbarSpacer";
|
||||
|
||||
function NativeHeaderToolbarSearchBarSlot() {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarSearchBarSlot.displayName = "NativeHeaderToolbarSearchBarSlot";
|
||||
|
||||
export const NativeHeaderToolbar = Object.assign(NativeHeaderToolbarRoot, {
|
||||
Button: NativeHeaderToolbarButton,
|
||||
Label: NativeHeaderToolbarLabel,
|
||||
Menu: Object.assign(NativeHeaderToolbarMenu, {
|
||||
Action: NativeHeaderToolbarMenuAction,
|
||||
}),
|
||||
MenuAction: NativeHeaderToolbarMenuAction,
|
||||
SearchBarSlot: NativeHeaderToolbarSearchBarSlot,
|
||||
Spacer: NativeHeaderToolbarSpacer,
|
||||
});
|
||||
@@ -1,22 +1,13 @@
|
||||
import { useNavigation, type ParamListBase } from "@react-navigation/native";
|
||||
import type {
|
||||
NativeStackHeaderItem,
|
||||
NativeStackHeaderItemMenu,
|
||||
NativeStackNavigationOptions,
|
||||
NativeStackNavigationProp,
|
||||
} from "@react-navigation/native-stack";
|
||||
import {
|
||||
Children,
|
||||
isValidElement,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
type ReactElement,
|
||||
type ReactNode,
|
||||
} from "react";
|
||||
import { useEffect, useLayoutEffect, useMemo, useRef } from "react";
|
||||
import type { ColorValue } from "react-native";
|
||||
|
||||
export { NativeHeaderToolbar } from "./NativeHeaderToolbar";
|
||||
|
||||
export {
|
||||
nativeHeaderScrollEdgeEffects,
|
||||
nativeTopScrollEdgeEffect,
|
||||
@@ -196,277 +187,3 @@ export function NativeStackScreenOptions(props: {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function labelFromChildren(children: ReactNode): string {
|
||||
const parts: string[] = [];
|
||||
Children.forEach(children, (child) => {
|
||||
if (typeof child === "string" || typeof child === "number") {
|
||||
parts.push(String(child));
|
||||
} else if (isValidElement<{ children?: ReactNode }>(child)) {
|
||||
parts.push(labelFromChildren(child.props.children));
|
||||
}
|
||||
});
|
||||
return parts.join("");
|
||||
}
|
||||
|
||||
type NativeStackHeaderIcon = NonNullable<
|
||||
Extract<NativeStackHeaderItem, { type: "button" }>["icon"]
|
||||
>;
|
||||
type NativeStackOptionsWithToolbar = NativeStackNavigationOptions & {
|
||||
unstable_headerToolbarItems?: () => NativeStackHeaderItem[];
|
||||
};
|
||||
|
||||
function iconFromProp(icon: unknown): NativeStackHeaderIcon | undefined {
|
||||
if (typeof icon !== "string") {
|
||||
return undefined;
|
||||
}
|
||||
return { type: "sfSymbol", name: icon as never };
|
||||
}
|
||||
|
||||
type ToolbarElementProps = Record<string, unknown> & { readonly children?: ReactNode };
|
||||
|
||||
function elementTypeName(element: ReactElement): string | undefined {
|
||||
const type = element.type;
|
||||
if (typeof type === "function") {
|
||||
return (type as { displayName?: string; name?: string }).displayName ?? type.name;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function convertMenuAction(
|
||||
element: ReactElement<ToolbarElementProps>,
|
||||
): NativeStackHeaderItemMenu["menu"]["items"][number] | null {
|
||||
const typeName = elementTypeName(element);
|
||||
if (typeName === "NativeHeaderToolbarMenuAction") {
|
||||
const label = labelFromChildren(element.props.children);
|
||||
return {
|
||||
type: "action",
|
||||
label,
|
||||
description: typeof element.props.subtitle === "string" ? element.props.subtitle : undefined,
|
||||
disabled: Boolean(element.props.disabled),
|
||||
icon: iconFromProp(element.props.icon),
|
||||
onPress:
|
||||
typeof element.props.onPress === "function"
|
||||
? (element.props.onPress as () => void)
|
||||
: () => undefined,
|
||||
state: element.props.isOn === true ? "on" : undefined,
|
||||
destructive: Boolean(element.props.destructive),
|
||||
discoverabilityLabel:
|
||||
typeof element.props.discoverabilityLabel === "string"
|
||||
? element.props.discoverabilityLabel
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
if (typeName === "NativeHeaderToolbarMenu") {
|
||||
return {
|
||||
type: "submenu",
|
||||
label:
|
||||
typeof element.props.title === "string"
|
||||
? element.props.title
|
||||
: labelFromChildren(element.props.children),
|
||||
icon: iconFromProp(element.props.icon),
|
||||
inline: Boolean(element.props.inline),
|
||||
items: collectMenuItems(element.props.children),
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function collectMenuItems(children: ReactNode): NativeStackHeaderItemMenu["menu"]["items"] {
|
||||
const items: NativeStackHeaderItemMenu["menu"]["items"] = [];
|
||||
Children.forEach(children, (child) => {
|
||||
if (!isValidElement<ToolbarElementProps>(child)) {
|
||||
return;
|
||||
}
|
||||
const item = convertMenuAction(child);
|
||||
if (item) {
|
||||
items.push(item);
|
||||
return;
|
||||
}
|
||||
items.push(...collectMenuItems(child.props.children));
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
function convertToolbarChild(child: ReactNode): NativeStackHeaderItem | null {
|
||||
if (!isValidElement<ToolbarElementProps>(child)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const typeName = elementTypeName(child);
|
||||
if (typeName === "NativeHeaderToolbarButton") {
|
||||
return {
|
||||
type: "button",
|
||||
label: typeof child.props.label === "string" ? child.props.label : "",
|
||||
accessibilityLabel:
|
||||
typeof child.props.accessibilityLabel === "string"
|
||||
? child.props.accessibilityLabel
|
||||
: undefined,
|
||||
disabled: Boolean(child.props.disabled),
|
||||
icon: iconFromProp(child.props.icon),
|
||||
onPress:
|
||||
typeof child.props.onPress === "function"
|
||||
? (child.props.onPress as () => void)
|
||||
: () => undefined,
|
||||
sharesBackground: !child.props.separateBackground,
|
||||
tintColor: child.props.tintColor as ColorValue | undefined,
|
||||
variant: "plain",
|
||||
};
|
||||
}
|
||||
|
||||
if (typeName === "NativeHeaderToolbarMenu") {
|
||||
return {
|
||||
type: "menu",
|
||||
label: typeof child.props.title === "string" ? child.props.title : "",
|
||||
accessibilityLabel:
|
||||
typeof child.props.accessibilityLabel === "string"
|
||||
? child.props.accessibilityLabel
|
||||
: undefined,
|
||||
disabled: Boolean(child.props.disabled),
|
||||
icon: iconFromProp(child.props.icon),
|
||||
menu: {
|
||||
title: typeof child.props.title === "string" ? child.props.title : undefined,
|
||||
items: collectMenuItems(child.props.children),
|
||||
},
|
||||
sharesBackground: !child.props.separateBackground,
|
||||
tintColor: child.props.tintColor as ColorValue | undefined,
|
||||
variant: "plain",
|
||||
};
|
||||
}
|
||||
|
||||
if (typeName === "NativeHeaderToolbarSpacer") {
|
||||
return {
|
||||
type: "spacing",
|
||||
spacing: typeof child.props.width === "number" ? child.props.width : 8,
|
||||
flexible: Boolean(child.props.flexible),
|
||||
} as NativeStackHeaderItem;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function collectToolbarItems(children: ReactNode): NativeStackHeaderItem[] {
|
||||
const items: NativeStackHeaderItem[] = [];
|
||||
Children.forEach(children, (child) => {
|
||||
const item = convertToolbarChild(child);
|
||||
if (item) {
|
||||
if (item.type === "spacing") {
|
||||
// Native inserts spacing items at `index`, treating a missing index
|
||||
// as 0 — which would move the spacer in front of earlier siblings.
|
||||
(item as { index?: number }).index = items.length;
|
||||
}
|
||||
items.push(item);
|
||||
}
|
||||
});
|
||||
return items;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarRoot(props: {
|
||||
readonly placement?: "left" | "right" | "bottom";
|
||||
readonly children?: ReactNode;
|
||||
}) {
|
||||
const navigation = useNativeStackNavigation();
|
||||
const items = useMemo(() => collectToolbarItems(props.children), [props.children]);
|
||||
|
||||
// Swap toolbar owners before paint so split and compact headers cannot clear each other.
|
||||
useLayoutEffect(() => {
|
||||
if (!navigation) {
|
||||
return;
|
||||
}
|
||||
if (props.placement === "bottom") {
|
||||
navigation.setOptions({
|
||||
unstable_headerToolbarItems: () => items,
|
||||
} as NativeStackOptionsWithToolbar);
|
||||
return () => {
|
||||
navigation.setOptions({
|
||||
unstable_headerToolbarItems: () => [],
|
||||
} as NativeStackOptionsWithToolbar);
|
||||
};
|
||||
}
|
||||
if (props.placement === "left") {
|
||||
navigation.setOptions({ unstable_headerLeftItems: () => items });
|
||||
return () => {
|
||||
navigation.setOptions({ unstable_headerLeftItems: () => [] });
|
||||
};
|
||||
}
|
||||
navigation.setOptions({ unstable_headerRightItems: () => items });
|
||||
return () => {
|
||||
navigation.setOptions({ unstable_headerRightItems: () => [] });
|
||||
};
|
||||
}, [items, navigation, props.placement]);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function NativeHeaderToolbarButton(_props: {
|
||||
readonly accessibilityLabel?: string;
|
||||
readonly disabled?: boolean;
|
||||
readonly icon?: string;
|
||||
readonly label?: string;
|
||||
readonly onPress?: () => void;
|
||||
readonly separateBackground?: boolean;
|
||||
readonly tintColor?: ColorValue;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarButton.displayName = "NativeHeaderToolbarButton";
|
||||
|
||||
function NativeHeaderToolbarMenu(_props: {
|
||||
readonly accessibilityLabel?: string;
|
||||
readonly children?: ReactNode;
|
||||
readonly disabled?: boolean;
|
||||
readonly icon?: string;
|
||||
readonly inline?: boolean;
|
||||
readonly separateBackground?: boolean;
|
||||
readonly tintColor?: ColorValue;
|
||||
readonly title?: string;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarMenu.displayName = "NativeHeaderToolbarMenu";
|
||||
|
||||
function NativeHeaderToolbarMenuAction(_props: {
|
||||
readonly children?: ReactNode;
|
||||
readonly destructive?: boolean;
|
||||
readonly disabled?: boolean;
|
||||
readonly discoverabilityLabel?: string;
|
||||
readonly icon?: string;
|
||||
readonly isOn?: boolean;
|
||||
readonly onPress?: () => void;
|
||||
readonly subtitle?: string;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarMenuAction.displayName = "NativeHeaderToolbarMenuAction";
|
||||
|
||||
function NativeHeaderToolbarLabel(_props: { readonly children?: ReactNode }) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarLabel.displayName = "NativeHeaderToolbarLabel";
|
||||
|
||||
function NativeHeaderToolbarSpacer(_props: {
|
||||
readonly flexible?: boolean;
|
||||
readonly sharesBackground?: boolean;
|
||||
readonly width?: number;
|
||||
}) {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarSpacer.displayName = "NativeHeaderToolbarSpacer";
|
||||
|
||||
function NativeHeaderToolbarSearchBarSlot() {
|
||||
return null;
|
||||
}
|
||||
NativeHeaderToolbarSearchBarSlot.displayName = "NativeHeaderToolbarSearchBarSlot";
|
||||
|
||||
export const NativeHeaderToolbar = Object.assign(NativeHeaderToolbarRoot, {
|
||||
Button: NativeHeaderToolbarButton,
|
||||
Label: NativeHeaderToolbarLabel,
|
||||
Menu: Object.assign(NativeHeaderToolbarMenu, {
|
||||
Action: NativeHeaderToolbarMenuAction,
|
||||
}),
|
||||
MenuAction: NativeHeaderToolbarMenuAction,
|
||||
SearchBarSlot: NativeHeaderToolbarSearchBarSlot,
|
||||
Spacer: NativeHeaderToolbarSpacer,
|
||||
});
|
||||
Reference in new issue
Block a user