The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import type { |
| 2 | NativeStackHeaderItem, |
| 3 | NativeStackHeaderItemMenu, |
| 4 | NativeStackNavigationOptions, |
| 5 | } from "@react-navigation/native-stack"; |
| 6 | import { useGT } from "gt-react-native"; |
| 7 | import { isValidElement, useEffect, useRef } from "react"; |
| 8 | import { StyleSheet } from "react-native"; |
| 9 | import { KeyboardController, useKeyboardState } from "react-native-keyboard-controller"; |
| 10 | import { isDuoDevice } from "../lib/device-type"; |
| 11 | import { useMobileNavigationTheme } from "../lib/useMobileNavigationTheme"; |
| 12 | import { useDeviceType, useNativeLayoutMetrics } from "./native-layout-metrics"; |
| 13 | import { HeaderItemsWidthContext } from "./header-items-width"; |
| 14 | import { |
| 15 | SearchBar, |
| 16 | Stack, |
| 17 | type HeaderBarButtonMailSearchToolbarItem, |
| 18 | type SearchBarCommands, |
| 19 | type StackHeaderConfigProps, |
| 20 | type StackHeaderMinimizationBehaviorIOS, |
| 21 | type StackHeaderMenuElementIOS, |
| 22 | type StackHeaderMenuIOS, |
| 23 | type HeaderBarButtonItemWithMenu, |
| 24 | type HeaderBarButtonSearchBarPlacementItem, |
| 25 | } from "react-native-screens"; |
| 26 | |
| 27 | // UIKit's bar margins, and the width of a glass bar button. |
| 28 | const BAR_MARGINS = 32; |
| 29 | const BAR_BUTTON_WIDTH = 44; |
| 30 | |
| 31 | type HeaderItem = NonNullable<NonNullable<StackHeaderConfigProps["ios"]>["leadingItems"]>[number]; |
| 32 | type ToolbarItem = |
| 33 | | NativeStackHeaderItem |
| 34 | | HeaderBarButtonMailSearchToolbarItem |
| 35 | | HeaderBarButtonSearchBarPlacementItem; |
| 36 | type Options = NativeStackNavigationOptions & { |
| 37 | readonly unstable_navigationItemStyle?: "editor" | "navigator"; |
| 38 | readonly unstable_headerMinimizationBehavior?: StackHeaderMinimizationBehaviorIOS; |
| 39 | readonly headerSubtitle?: string; |
| 40 | readonly unstable_headerSubtitle?: string; |
| 41 | readonly headerSubtitleStyle?: { color?: string; fontSize?: number; fontFamily?: string }; |
| 42 | readonly unstable_headerCenterItems?: () => NativeStackHeaderItem[]; |
| 43 | readonly unstable_headerToolbarItems?: () => ToolbarItem[]; |
| 44 | }; |
| 45 | |
| 46 | type MenuElementInput = |
| 47 | | NativeStackHeaderItemMenu["menu"]["items"][number] |
| 48 | | HeaderBarButtonItemWithMenu["menu"]["items"][number]; |
| 49 | type MenuInput = { readonly title?: string; readonly items: readonly MenuElementInput[] }; |
| 50 | |
| 51 | function convertIcon( |
| 52 | icon: |
| 53 | | Extract<NativeStackHeaderItem, { type: "button" }>["icon"] |
| 54 | | HeaderBarButtonItemWithMenu["icon"], |
| 55 | ) { |
| 56 | if (!icon) return undefined; |
| 57 | if (icon.type === "image") |
| 58 | return icon.tinted === false |
| 59 | ? { type: "imageSource" as const, imageSource: icon.source } |
| 60 | : { type: "templateSource" as const, templateSource: icon.source }; |
| 61 | return icon; |
| 62 | } |
| 63 | |
| 64 | function convertMenu(menu: MenuInput, id: string): StackHeaderMenuIOS { |
| 65 | const children = menu.items.flatMap<StackHeaderMenuElementIOS>((item, index) => { |
| 66 | if ("hidden" in item && item.hidden) return []; |
| 67 | const childId = `${id}:${index}`; |
| 68 | if (item.type === "submenu") { |
| 69 | return [ |
| 70 | { |
| 71 | ...convertMenu({ items: item.items }, childId), |
| 72 | title: "label" in item ? item.label : item.title, |
| 73 | displayInline: |
| 74 | "inline" in item |
| 75 | ? item.inline |
| 76 | : "displayInline" in item |
| 77 | ? item.displayInline |
| 78 | : undefined, |
| 79 | }, |
| 80 | ]; |
| 81 | } |
| 82 | return [ |
| 83 | { |
| 84 | id: childId, |
| 85 | type: "menuItem" as const, |
| 86 | itemType: "action" as const, |
| 87 | title: "label" in item ? item.label : item.title, |
| 88 | icon: convertIcon(item.icon), |
| 89 | onPress: item.disabled ? undefined : item.onPress, |
| 90 | keepsMenuPresented: item.keepsMenuPresented, |
| 91 | disabled: item.disabled, |
| 92 | destructive: item.destructive, |
| 93 | subtitle: |
| 94 | ("subtitle" in item ? item.subtitle : undefined) ?? |
| 95 | ("description" in item ? item.description : undefined), |
| 96 | state: item.state, |
| 97 | }, |
| 98 | ]; |
| 99 | }); |
| 100 | return { id, type: "menu", title: menu.title, children }; |
| 101 | } |
| 102 | |
| 103 | function convertItems(items: NativeStackHeaderItem[], prefix: string): HeaderItem[] { |
| 104 | return items.map((item, index): HeaderItem => { |
| 105 | const id = |
| 106 | item.type === "spacing" ? `${prefix}:${index}` : (item.identifier ?? `${prefix}:${index}`); |
| 107 | switch (item.type) { |
| 108 | case "spacing": |
| 109 | return "flexible" in item && item.flexible === true |
| 110 | ? { type: "spacer", id, sizing: "flexible" } |
| 111 | : { type: "spacer", id, sizing: "fixed", width: item.spacing }; |
| 112 | case "custom": |
| 113 | return { |
| 114 | type: "item", |
| 115 | id, |
| 116 | render: () => item.element, |
| 117 | hidesSharedBackground: item.hidesSharedBackground, |
| 118 | }; |
| 119 | case "menu": |
| 120 | return { |
| 121 | type: "item", |
| 122 | id, |
| 123 | identifier: item.identifier, |
| 124 | title: item.label || item.accessibilityLabel || undefined, |
| 125 | axisBehavior: item.axisBehavior, |
| 126 | icon: convertIcon(item.icon), |
| 127 | menu: item.disabled ? undefined : convertMenu(item.menu, `${id}:menu`), |
| 128 | disabled: item.disabled, |
| 129 | variant: item.variant, |
| 130 | tintColor: item.tintColor, |
| 131 | hidesSharedBackground: item.hidesSharedBackground, |
| 132 | }; |
| 133 | case "button": |
| 134 | return { |
| 135 | type: "item", |
| 136 | id, |
| 137 | identifier: item.identifier, |
| 138 | title: item.label || item.accessibilityLabel || undefined, |
| 139 | axisBehavior: item.axisBehavior, |
| 140 | icon: convertIcon(item.icon), |
| 141 | onPress: item.disabled ? undefined : item.onPress, |
| 142 | disabled: item.disabled, |
| 143 | visibilityPriority: |
| 144 | item.visibilityPriority === "default" ? "standard" : item.visibilityPriority, |
| 145 | variant: item.variant, |
| 146 | tintColor: item.tintColor, |
| 147 | hidesSharedBackground: item.hidesSharedBackground, |
| 148 | }; |
| 149 | } |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | export function V5StackHeader(props: { |
| 154 | readonly options: Options; |
| 155 | readonly canGoBack: boolean; |
| 156 | /** Workspace split columns share the vertical bar with the workspace tab bar. */ |
| 157 | readonly column?: "primary" | "detail"; |
| 158 | /** The workspace's compose action, shown as its own group in this column's bar. */ |
| 159 | readonly onCompose?: () => void; |
| 160 | /** UIKit morphs search items that share an identifier between bars as columns change. */ |
| 161 | readonly searchIdentifier?: string; |
| 162 | }) { |
| 163 | const { options } = props; |
| 164 | const gt = useGT(); |
| 165 | const theme = useMobileNavigationTheme(); |
| 166 | const nativeMetrics = useNativeLayoutMetrics(); |
| 167 | const deviceType = useDeviceType(); |
| 168 | const keyboardVisible = useKeyboardState((state) => state.isVisible); |
| 169 | const searchRef = useRef<SearchBarCommands>(null); |
| 170 | const itemProps = { tintColor: options.headerTintColor, canGoBack: props.canGoBack }; |
| 171 | const leading = convertItems(options.unstable_headerLeftItems?.(itemProps) ?? [], "leading"); |
| 172 | const trailing = convertItems( |
| 173 | [ |
| 174 | ...(options.unstable_headerCenterItems?.() ?? []), |
| 175 | ...(options.unstable_headerRightItems?.(itemProps) ?? []), |
| 176 | ], |
| 177 | "trailing", |
| 178 | ); |
| 179 | const toolbar = options.unstable_headerToolbarItems?.() ?? []; |
| 180 | const mailSearch = toolbar.find( |
| 181 | (item): item is HeaderBarButtonMailSearchToolbarItem => item.type === "mailSearchToolbar", |
| 182 | ); |
| 183 | const bottomSearch = Boolean(mailSearch) && deviceType !== "pad"; |
| 184 | // A vertical bar keeps fixed spaces at full length and separates items itself. |
| 185 | const spacesSearch = bottomSearch && (nativeMetrics?.verticalBarEdge ?? "none") === "none"; |
| 186 | const bottom = toolbar.flatMap<HeaderItem>((item, index) => { |
| 187 | if (item.type === "mailSearchToolbar") return []; |
| 188 | if (item.type === "searchBarPlacement") |
| 189 | return [ |
| 190 | { |
| 191 | type: "item", |
| 192 | id: `toolbar-search:${index}`, |
| 193 | searchBarPlacement: true, |
| 194 | ...(props.searchIdentifier ? { identifier: props.searchIdentifier } : {}), |
| 195 | }, |
| 196 | ]; |
| 197 | return convertItems([item], `toolbar:${index}`); |
| 198 | }); |
| 199 | if (mailSearch) { |
| 200 | if (mailSearch.filterMenu) |
| 201 | bottom.push({ |
| 202 | type: "item", |
| 203 | id: mailSearch.filterButtonId ?? "filter", |
| 204 | icon: { |
| 205 | type: "sfSymbol", |
| 206 | name: mailSearch.filterSystemImageName ?? "line.3.horizontal.decrease", |
| 207 | }, |
| 208 | menu: convertMenu(mailSearch.filterMenu, "filter-menu"), |
| 209 | }); |
| 210 | if (bottomSearch) { |
| 211 | if (spacesSearch && bottom.length) |
| 212 | bottom.push({ type: "spacer", id: "before-search", sizing: "fixed", width: 8 }); |
| 213 | bottom.push({ |
| 214 | type: "item", |
| 215 | id: "home-search", |
| 216 | searchBarPlacement: true, |
| 217 | ...(props.searchIdentifier ? { identifier: props.searchIdentifier } : {}), |
| 218 | }); |
| 219 | if (spacesSearch && mailSearch.onComposePress) |
| 220 | bottom.push({ type: "spacer", id: "after-search", sizing: "fixed", width: 8 }); |
| 221 | } |
| 222 | if (mailSearch.onComposePress) |
| 223 | bottom.push({ |
| 224 | type: "item", |
| 225 | id: mailSearch.composeButtonId ?? "compose", |
| 226 | identifier: "workspace-trailing-action", |
| 227 | icon: { type: "sfSymbol", name: mailSearch.composeSystemImageName ?? "square.and.pencil" }, |
| 228 | onPress: mailSearch.onComposePress, |
| 229 | }); |
| 230 | } |
| 231 | // UIKit doesn't stack a column's toolbar against the workspace tab bar, which owns the |
| 232 | // bottom of the vertical bar and draws over it. Those columns keep the group in their |
| 233 | // navigation items. Integrated search stays in the column's horizontal bottom toolbar. |
| 234 | if ( |
| 235 | props.column && |
| 236 | nativeMetrics && |
| 237 | nativeMetrics.verticalBarEdge !== "none" && |
| 238 | (props.column === "detail" || |
| 239 | (nativeMetrics.horizontalSizeClass === "compact" && |
| 240 | !toolbar.some((item) => item.type === "searchBarPlacement"))) |
| 241 | ) { |
| 242 | if (trailing.length && bottom.length) |
| 243 | trailing.push({ type: "spacer", id: "toolbar-group", sizing: "fixed", width: 8 }); |
| 244 | trailing.push(...bottom); |
| 245 | bottom.length = 0; |
| 246 | } |
| 247 | if (props.onCompose) { |
| 248 | const compose: HeaderItem = { |
| 249 | type: "item", |
| 250 | id: "workspace-compose", |
| 251 | title: gt("New task"), |
| 252 | icon: { type: "sfSymbol", name: "square.and.pencil" }, |
| 253 | onPress: props.onCompose, |
| 254 | }; |
| 255 | if (trailing.length) |
| 256 | trailing.push({ type: "spacer", id: "compose-group", sizing: "fixed", width: 8 }); |
| 257 | trailing.push(compose); |
| 258 | } |
| 259 | // The vertical bar has no keyboard accessory row, so it carries its own dismiss. A regular-width |
| 260 | // primary column draws a horizontal bar beside the detail column's vertical one. The first |
| 261 | // trailing item sits at the bar's far end; the flexible space keeps it next to the keyboard. |
| 262 | if ( |
| 263 | keyboardVisible && |
| 264 | nativeMetrics && |
| 265 | nativeMetrics.verticalBarEdge !== "none" && |
| 266 | (props.column !== "primary" || nativeMetrics.horizontalSizeClass === "compact") |
| 267 | ) { |
| 268 | trailing.unshift( |
| 269 | { |
| 270 | type: "item", |
| 271 | id: "dismiss-keyboard", |
| 272 | title: gt("Dismiss keyboard"), |
| 273 | icon: { type: "sfSymbol", name: "keyboard.chevron.compact.down" }, |
| 274 | onPress: () => void KeyboardController.dismiss(), |
| 275 | // The folded bar is short; keep this out of the overflow menu. |
| 276 | visibilityPriority: "high", |
| 277 | }, |
| 278 | { type: "spacer", id: "dismiss-keyboard-space", sizing: "flexible" }, |
| 279 | ); |
| 280 | } |
| 281 | const left = options.headerLeft?.(itemProps); |
| 282 | const right = options.headerRight?.(itemProps); |
| 283 | if (!options.unstable_headerLeftItems && isValidElement(left)) |
| 284 | leading.push({ type: "item", id: "custom-left", render: () => left }); |
| 285 | if (!options.unstable_headerRightItems && isValidElement(right)) |
| 286 | trailing.push({ type: "item", id: "custom-right", render: () => right }); |
| 287 | const title = typeof options.headerTitle === "string" ? options.headerTitle : options.title; |
| 288 | const titleElement = |
| 289 | typeof options.headerTitle === "function" |
| 290 | ? options.headerTitle({ children: title ?? "", tintColor: options.headerTintColor }) |
| 291 | : undefined; |
| 292 | const searchOptions = options.headerSearchBarOptions; |
| 293 | const autofocusRef = searchOptions?.ref ?? searchRef; |
| 294 | // iOS ignores the native autoFocus prop, and SearchBar's imperative handle changes every |
| 295 | // commit, so a ref callback would refocus on each render; focus once after the header mounts. |
| 296 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to focus the native SearchBar once after the header mounts, since iOS ignores autoFocus |
| 297 | useEffect(() => { |
| 298 | if (!searchOptions?.autoFocus) return; |
| 299 | const frame = requestAnimationFrame(() => autofocusRef.current?.focus()); |
| 300 | return () => cancelAnimationFrame(frame); |
| 301 | }, [searchOptions?.autoFocus, autofocusRef]); |
| 302 | const searchButton = !bottomSearch && searchOptions?.placement === "integratedButton"; |
| 303 | const itemsWidth = |
| 304 | BAR_MARGINS + |
| 305 | (props.canGoBack && options.headerBackVisible !== false ? BAR_BUTTON_WIDTH : 0) + |
| 306 | (searchButton ? BAR_BUTTON_WIDTH : 0) + |
| 307 | [...leading, ...trailing].reduce( |
| 308 | (width, item) => |
| 309 | width + |
| 310 | (item.type === "spacer" |
| 311 | ? item.sizing === "fixed" |
| 312 | ? item.width |
| 313 | : 0 |
| 314 | : // The keyboard dismiss rides the vertical bar, not beside the title. |
| 315 | item.id === "dismiss-keyboard" |
| 316 | ? 0 |
| 317 | : BAR_BUTTON_WIDTH), |
| 318 | 0, |
| 319 | ); |
| 320 | const titleStyle = StyleSheet.flatten(options.headerTitleStyle); |
| 321 | const headerStyle = StyleSheet.flatten(options.headerStyle); |
| 322 | const appearance = { |
| 323 | // Clear colors remove UIKit material; let translucent bars supply their own blur background. |
| 324 | backgroundColor: options.headerTransparent |
| 325 | ? undefined |
| 326 | : (headerStyle?.backgroundColor ?? theme.colors.card), |
| 327 | shadowColor: options.headerShadowVisible === false ? "transparent" : undefined, |
| 328 | titleFontSize: titleStyle?.fontSize, |
| 329 | titleFontFamily: titleStyle?.fontFamily, |
| 330 | titleFontWeight: titleStyle?.fontWeight, |
| 331 | titleFontColor: titleStyle?.color, |
| 332 | subtitleFontSize: options.headerSubtitleStyle?.fontSize, |
| 333 | subtitleFontColor: options.headerSubtitleStyle?.color, |
| 334 | }; |
| 335 | return ( |
| 336 | <HeaderItemsWidthContext value={itemsWidth}> |
| 337 | <Stack.HeaderConfig |
| 338 | hidden={options.headerShown === false} |
| 339 | transparent={options.headerTransparent} |
| 340 | title={title} |
| 341 | subtitle={options.unstable_headerSubtitle ?? options.headerSubtitle} |
| 342 | backButtonHidden={options.headerBackVisible === false} |
| 343 | ios={{ |
| 344 | navigationItemStyle: options.unstable_navigationItemStyle, |
| 345 | // Opt-ins apply only where the Duo is short: folded, or held landscape. The Duo |
| 346 | // resolves `automatic` to minimize on scroll, so the tall unfolded display opts out. |
| 347 | minimizationBehavior: !isDuoDevice(deviceType) |
| 348 | ? undefined |
| 349 | : deviceType === "duo-unfolded" && |
| 350 | nativeMetrics && |
| 351 | nativeMetrics.height > nativeMetrics.width |
| 352 | ? "never" |
| 353 | : options.unstable_headerMinimizationBehavior, |
| 354 | tintColor: options.headerTintColor, |
| 355 | searchBar: |
| 356 | mailSearch || searchOptions ? ( |
| 357 | <SearchBar |
| 358 | {...searchOptions} |
| 359 | ref={searchOptions?.ref ?? searchRef} |
| 360 | hideNavigationBar={false} |
| 361 | hideWhenScrolling={false} |
| 362 | allowToolbarIntegration={searchOptions?.allowToolbarIntegration ?? true} |
| 363 | placement={bottomSearch ? "integrated" : (searchOptions?.placement ?? "automatic")} |
| 364 | placeholder={ |
| 365 | mailSearch?.placeholder ?? |
| 366 | searchOptions?.placeholder ?? |
| 367 | gt("Search", { $context: "search field placeholder" }) |
| 368 | } |
| 369 | onFocus={(event) => { |
| 370 | searchOptions?.onFocus?.(event); |
| 371 | }} |
| 372 | onChangeText={(event) => { |
| 373 | mailSearch?.onSearchTextChange?.(event.nativeEvent.text); |
| 374 | searchOptions?.onChangeText?.(event); |
| 375 | }} |
| 376 | /> |
| 377 | ) : undefined, |
| 378 | toolbarItems: bottom, |
| 379 | leadingItems: leading, |
| 380 | trailingItems: trailing, |
| 381 | titleItem: isValidElement(titleElement) |
| 382 | ? { id: "title", render: () => titleElement } |
| 383 | : undefined, |
| 384 | largeTitleEnabled: options.headerLargeTitle, |
| 385 | backButtonDisplayMode: options.headerBackButtonDisplayMode ?? "minimal", |
| 386 | backButtonMenuEnabled: options.headerBackButtonMenuEnabled ?? true, |
| 387 | backButtonTitle: options.headerBackTitle, |
| 388 | standardAppearance: appearance, |
| 389 | scrollEdgeAppearance: appearance, |
| 390 | }} |
| 391 | /> |
| 392 | </HeaderItemsWidthContext> |
| 393 | ); |
| 394 | } |