The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useRoute } from "@react-navigation/native"; |
| 2 | import type { |
| 3 | NativeStackHeaderItem, |
| 4 | NativeStackHeaderItemMenu, |
| 5 | NativeStackNavigationOptions, |
| 6 | } from "@react-navigation/native-stack"; |
| 7 | import { |
| 8 | Children, |
| 9 | isValidElement, |
| 10 | useLayoutEffect, |
| 11 | useMemo, |
| 12 | useRef, |
| 13 | use, |
| 14 | type ReactElement, |
| 15 | type ReactNode, |
| 16 | } from "react"; |
| 17 | import type { ColorValue } from "react-native"; |
| 18 | import type { HeaderBarButtonSearchBarPlacementItem } from "react-native-screens"; |
| 19 | import { NativeWorkspaceTabActivityContext } from "./v5-workspace-context"; |
| 20 | import { NativeStackScreenOptions, useScreenOptionsWriter } from "./StackHeader"; |
| 21 | |
| 22 | function labelFromChildren(children: ReactNode): string { |
| 23 | const parts: string[] = []; |
| 24 | Children.forEach(children, (child) => { |
| 25 | if (typeof child === "string" || typeof child === "number") { |
| 26 | parts.push(String(child)); |
| 27 | } else if (isValidElement<{ children?: ReactNode }>(child)) { |
| 28 | parts.push(labelFromChildren(child.props.children)); |
| 29 | } |
| 30 | }); |
| 31 | return parts.join(""); |
| 32 | } |
| 33 | |
| 34 | type NativeStackHeaderIcon = NonNullable< |
| 35 | Extract<NativeStackHeaderItem, { type: "button" }>["icon"] |
| 36 | >; |
| 37 | type NativeStackHeaderAxisBehavior = Extract< |
| 38 | NativeStackHeaderItem, |
| 39 | { type: "button" } |
| 40 | >["axisBehavior"]; |
| 41 | type NativeStackHeaderVisibilityPriority = Extract< |
| 42 | NativeStackHeaderItem, |
| 43 | { type: "button" } |
| 44 | >["visibilityPriority"]; |
| 45 | type NativeToolbarItem = NativeStackHeaderItem | HeaderBarButtonSearchBarPlacementItem; |
| 46 | type NativeStackOptionsWithToolbar = NativeStackNavigationOptions & { |
| 47 | unstable_headerToolbarItems?: () => NativeToolbarItem[]; |
| 48 | }; |
| 49 | |
| 50 | function iconFromProp(icon: unknown): NativeStackHeaderIcon | undefined { |
| 51 | if (typeof icon === "string") return { type: "sfSymbol", name: icon as never }; |
| 52 | if ( |
| 53 | typeof icon === "object" && |
| 54 | icon !== null && |
| 55 | "type" in icon && |
| 56 | icon.type === "image" && |
| 57 | "source" in icon |
| 58 | ) { |
| 59 | return icon as NativeStackHeaderIcon; |
| 60 | } |
| 61 | return undefined; |
| 62 | } |
| 63 | |
| 64 | type RegisterHandler = (handler: unknown) => () => void; |
| 65 | |
| 66 | type ToolbarElementProps = Record<string, unknown> & { readonly children?: ReactNode }; |
| 67 | |
| 68 | function elementTypeName(element: ReactElement): string | undefined { |
| 69 | const type = element.type; |
| 70 | if (typeof type === "function") { |
| 71 | return (type as { displayName?: string; name?: string }).displayName ?? type.name; |
| 72 | } |
| 73 | return undefined; |
| 74 | } |
| 75 | |
| 76 | function convertMenuAction( |
| 77 | element: ReactElement<ToolbarElementProps>, |
| 78 | register: RegisterHandler, |
| 79 | ): NativeStackHeaderItemMenu["menu"]["items"][number] | null { |
| 80 | const typeName = elementTypeName(element); |
| 81 | if (typeName === "NativeHeaderToolbarMenuAction") { |
| 82 | const label = labelFromChildren(element.props.children); |
| 83 | return { |
| 84 | type: "action", |
| 85 | label, |
| 86 | description: typeof element.props.subtitle === "string" ? element.props.subtitle : undefined, |
| 87 | disabled: Boolean(element.props.disabled), |
| 88 | icon: iconFromProp(element.props.icon), |
| 89 | onPress: register(element.props.onPress), |
| 90 | state: element.props.isOn === true ? "on" : undefined, |
| 91 | destructive: Boolean(element.props.destructive), |
| 92 | discoverabilityLabel: |
| 93 | typeof element.props.discoverabilityLabel === "string" |
| 94 | ? element.props.discoverabilityLabel |
| 95 | : undefined, |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | if (typeName === "NativeHeaderToolbarMenu") { |
| 100 | return { |
| 101 | type: "submenu", |
| 102 | label: |
| 103 | typeof element.props.title === "string" |
| 104 | ? element.props.title |
| 105 | : labelFromChildren(element.props.children), |
| 106 | icon: iconFromProp(element.props.icon), |
| 107 | inline: Boolean(element.props.inline), |
| 108 | items: collectMenuItems(element.props.children, register), |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | function collectMenuItems( |
| 116 | children: ReactNode, |
| 117 | register: RegisterHandler, |
| 118 | ): NativeStackHeaderItemMenu["menu"]["items"] { |
| 119 | const items: NativeStackHeaderItemMenu["menu"]["items"] = []; |
| 120 | Children.forEach(children, (child) => { |
| 121 | if (!isValidElement<ToolbarElementProps>(child)) { |
| 122 | return; |
| 123 | } |
| 124 | const item = convertMenuAction(child, register); |
| 125 | if (item) { |
| 126 | items.push(item); |
| 127 | return; |
| 128 | } |
| 129 | items.push(...collectMenuItems(child.props.children, register)); |
| 130 | }); |
| 131 | return items; |
| 132 | } |
| 133 | |
| 134 | function convertToolbarChild( |
| 135 | child: ReactNode, |
| 136 | register: RegisterHandler, |
| 137 | ): NativeToolbarItem | null { |
| 138 | if (!isValidElement<ToolbarElementProps>(child)) { |
| 139 | return null; |
| 140 | } |
| 141 | |
| 142 | const typeName = elementTypeName(child); |
| 143 | if (typeName === "NativeHeaderToolbarCustom" && isValidElement(child.props.children)) { |
| 144 | return { type: "custom", element: child.props.children }; |
| 145 | } |
| 146 | if (typeName === "NativeHeaderToolbarSearchBarSlot") { |
| 147 | return { type: "searchBarPlacement" }; |
| 148 | } |
| 149 | |
| 150 | if (typeName === "NativeHeaderToolbarButton") { |
| 151 | return { |
| 152 | type: "button", |
| 153 | label: |
| 154 | typeof child.props.label === "string" |
| 155 | ? child.props.label |
| 156 | : String(child.props.accessibilityLabel ?? ""), |
| 157 | accessibilityLabel: |
| 158 | typeof child.props.accessibilityLabel === "string" |
| 159 | ? child.props.accessibilityLabel |
| 160 | : undefined, |
| 161 | axisBehavior: child.props.axisBehavior as NativeStackHeaderAxisBehavior, |
| 162 | disabled: Boolean(child.props.disabled), |
| 163 | icon: iconFromProp(child.props.icon), |
| 164 | onPress: register(child.props.onPress), |
| 165 | sharesBackground: !child.props.separateBackground, |
| 166 | tintColor: child.props.tintColor as ColorValue | undefined, |
| 167 | variant: |
| 168 | child.props.variant === "done" || child.props.variant === "prominent" |
| 169 | ? child.props.variant |
| 170 | : "plain", |
| 171 | identifier: typeof child.props.identifier === "string" ? child.props.identifier : undefined, |
| 172 | visibilityPriority: child.props.visibilityPriority as NativeStackHeaderVisibilityPriority, |
| 173 | }; |
| 174 | } |
| 175 | |
| 176 | if (typeName === "NativeHeaderToolbarMenu") { |
| 177 | return { |
| 178 | type: "menu", |
| 179 | label: typeof child.props.title === "string" ? child.props.title : "", |
| 180 | accessibilityLabel: |
| 181 | typeof child.props.accessibilityLabel === "string" |
| 182 | ? child.props.accessibilityLabel |
| 183 | : undefined, |
| 184 | disabled: Boolean(child.props.disabled), |
| 185 | axisBehavior: child.props.axisBehavior as NativeStackHeaderAxisBehavior, |
| 186 | icon: iconFromProp(child.props.icon), |
| 187 | menu: { |
| 188 | title: typeof child.props.title === "string" ? child.props.title : undefined, |
| 189 | items: collectMenuItems(child.props.children, register), |
| 190 | }, |
| 191 | sharesBackground: !child.props.separateBackground, |
| 192 | tintColor: child.props.tintColor as ColorValue | undefined, |
| 193 | variant: |
| 194 | child.props.variant === "done" || child.props.variant === "prominent" |
| 195 | ? child.props.variant |
| 196 | : "plain", |
| 197 | identifier: typeof child.props.identifier === "string" ? child.props.identifier : undefined, |
| 198 | }; |
| 199 | } |
| 200 | |
| 201 | if (typeName === "NativeHeaderToolbarSpacer") { |
| 202 | return { |
| 203 | type: "spacing", |
| 204 | spacing: typeof child.props.width === "number" ? child.props.width : 8, |
| 205 | flexible: Boolean(child.props.flexible), |
| 206 | } as NativeStackHeaderItem; |
| 207 | } |
| 208 | |
| 209 | return null; |
| 210 | } |
| 211 | |
| 212 | function collectToolbarItems(children: ReactNode, register: RegisterHandler): NativeToolbarItem[] { |
| 213 | const items: NativeToolbarItem[] = []; |
| 214 | Children.forEach(children, (child) => { |
| 215 | const item = convertToolbarChild(child, register); |
| 216 | if (item) { |
| 217 | if (item.type === "spacing") { |
| 218 | // Native defaults missing spacing-item indices to zero, moving spacers before earlier siblings. |
| 219 | (item as { index?: number }).index = items.length; |
| 220 | } |
| 221 | items.push(item); |
| 222 | } |
| 223 | }); |
| 224 | return items; |
| 225 | } |
| 226 | |
| 227 | function NativeHeaderToolbarRoot(props: { |
| 228 | readonly placement?: "left" | "right" | "bottom"; |
| 229 | readonly children?: ReactNode; |
| 230 | }) { |
| 231 | const setOptions = useScreenOptionsWriter(); |
| 232 | const route = useRoute(); |
| 233 | const tabActivity = use(NativeWorkspaceTabActivityContext); |
| 234 | const isTabActive = tabActivity?.isActive; |
| 235 | const committedHandlers = useRef<(() => void)[]>([]); |
| 236 | const { items, handlers } = useMemo(() => { |
| 237 | const handlers: (() => void)[] = []; |
| 238 | const register: RegisterHandler = (handler) => { |
| 239 | const index = handlers.length; |
| 240 | handlers.push(typeof handler === "function" ? (handler as () => void) : () => undefined); |
| 241 | return () => committedHandlers.current[index]?.(); |
| 242 | }; |
| 243 | return { items: collectToolbarItems(props.children, register), handlers }; |
| 244 | }, [props.children, committedHandlers]); |
| 245 | // Native retains actions when unchanged presentation skips re-registration. |
| 246 | // Publish only committed handlers; abandoned renders must not replace them. |
| 247 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- publishes committed toolbar handlers to the ref native actions call; discarded renders must not leak |
| 248 | useLayoutEffect(() => { |
| 249 | committedHandlers.current = handlers; |
| 250 | return () => { |
| 251 | committedHandlers.current = []; |
| 252 | }; |
| 253 | }, [handlers, committedHandlers]); |
| 254 | // UIKit's search placement item belongs to the toolbar, never a header group. |
| 255 | const headerItems = items.filter( |
| 256 | (item): item is NativeStackHeaderItem => item.type !== "searchBarPlacement", |
| 257 | ); |
| 258 | |
| 259 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears this placement's native header items through the navigator when the toolbar unmounts |
| 260 | useLayoutEffect(() => { |
| 261 | if (!setOptions) return; |
| 262 | return () => { |
| 263 | if (isTabActive && !isTabActive()) return; |
| 264 | if (props.placement === "bottom") { |
| 265 | setOptions({ |
| 266 | unstable_headerToolbarItems: () => [], |
| 267 | } as NativeStackOptionsWithToolbar); |
| 268 | } else if (props.placement === "left") { |
| 269 | setOptions({ unstable_headerLeftItems: () => [] }); |
| 270 | } else { |
| 271 | setOptions({ unstable_headerRightItems: () => [] }); |
| 272 | } |
| 273 | }; |
| 274 | }, [setOptions, props.placement, isTabActive]); |
| 275 | |
| 276 | return ( |
| 277 | <NativeStackScreenOptions |
| 278 | optionsVersion={[items, route.params]} |
| 279 | options={ |
| 280 | props.placement === "bottom" |
| 281 | ? { unstable_headerToolbarItems: () => items } |
| 282 | : props.placement === "left" |
| 283 | ? { unstable_headerLeftItems: () => headerItems } |
| 284 | : { unstable_headerRightItems: () => headerItems } |
| 285 | } |
| 286 | /> |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | function NativeHeaderToolbarButton(_props: { |
| 291 | readonly accessibilityLabel?: string; |
| 292 | readonly axisBehavior?: NativeStackHeaderAxisBehavior; |
| 293 | readonly disabled?: boolean; |
| 294 | readonly icon?: string | Extract<NativeStackHeaderIcon, { type: "image" }>; |
| 295 | readonly label?: string; |
| 296 | readonly onPress?: () => void; |
| 297 | readonly separateBackground?: boolean; |
| 298 | readonly tintColor?: ColorValue; |
| 299 | readonly variant?: "plain" | "done" | "prominent"; |
| 300 | readonly identifier?: string; |
| 301 | readonly visibilityPriority?: NativeStackHeaderVisibilityPriority; |
| 302 | }) { |
| 303 | return null; |
| 304 | } |
| 305 | NativeHeaderToolbarButton.displayName = "NativeHeaderToolbarButton"; |
| 306 | |
| 307 | function NativeHeaderToolbarMenu(_props: { |
| 308 | readonly accessibilityLabel?: string; |
| 309 | readonly axisBehavior?: NativeStackHeaderAxisBehavior; |
| 310 | readonly children?: ReactNode; |
| 311 | readonly disabled?: boolean; |
| 312 | readonly icon?: string; |
| 313 | readonly identifier?: string; |
| 314 | readonly inline?: boolean; |
| 315 | readonly separateBackground?: boolean; |
| 316 | readonly tintColor?: ColorValue; |
| 317 | readonly title?: string; |
| 318 | readonly variant?: "plain" | "done" | "prominent"; |
| 319 | }) { |
| 320 | return null; |
| 321 | } |
| 322 | NativeHeaderToolbarMenu.displayName = "NativeHeaderToolbarMenu"; |
| 323 | |
| 324 | function NativeHeaderToolbarMenuAction(_props: { |
| 325 | readonly children?: ReactNode; |
| 326 | readonly destructive?: boolean; |
| 327 | readonly disabled?: boolean; |
| 328 | readonly discoverabilityLabel?: string; |
| 329 | readonly icon?: string; |
| 330 | readonly isOn?: boolean; |
| 331 | readonly onPress?: () => void; |
| 332 | readonly subtitle?: string; |
| 333 | }) { |
| 334 | return null; |
| 335 | } |
| 336 | NativeHeaderToolbarMenuAction.displayName = "NativeHeaderToolbarMenuAction"; |
| 337 | |
| 338 | function NativeHeaderToolbarCustom(_props: { readonly children: ReactElement }) { |
| 339 | return null; |
| 340 | } |
| 341 | NativeHeaderToolbarCustom.displayName = "NativeHeaderToolbarCustom"; |
| 342 | |
| 343 | function NativeHeaderToolbarLabel(_props: { readonly children?: ReactNode }) { |
| 344 | return null; |
| 345 | } |
| 346 | NativeHeaderToolbarLabel.displayName = "NativeHeaderToolbarLabel"; |
| 347 | |
| 348 | function NativeHeaderToolbarSpacer(_props: { |
| 349 | readonly flexible?: boolean; |
| 350 | readonly sharesBackground?: boolean; |
| 351 | readonly width?: number; |
| 352 | }) { |
| 353 | return null; |
| 354 | } |
| 355 | NativeHeaderToolbarSpacer.displayName = "NativeHeaderToolbarSpacer"; |
| 356 | |
| 357 | function NativeHeaderToolbarSearchBarSlot() { |
| 358 | return null; |
| 359 | } |
| 360 | NativeHeaderToolbarSearchBarSlot.displayName = "NativeHeaderToolbarSearchBarSlot"; |
| 361 | |
| 362 | export const NativeHeaderToolbar = Object.assign(NativeHeaderToolbarRoot, { |
| 363 | Button: NativeHeaderToolbarButton, |
| 364 | Custom: NativeHeaderToolbarCustom, |
| 365 | Label: NativeHeaderToolbarLabel, |
| 366 | Menu: Object.assign(NativeHeaderToolbarMenu, { |
| 367 | Action: NativeHeaderToolbarMenuAction, |
| 368 | }), |
| 369 | MenuAction: NativeHeaderToolbarMenuAction, |
| 370 | SearchBarSlot: NativeHeaderToolbarSearchBarSlot, |
| 371 | Spacer: NativeHeaderToolbarSpacer, |
| 372 | }); |