The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { BlurView } from "expo-blur"; |
| 2 | import { GlassView, isGlassEffectAPIAvailable } from "expo-glass-effect"; |
| 3 | import { useNavigation } from "@react-navigation/native"; |
| 4 | import { use, useEffect, useState, type ReactNode } from "react"; |
| 5 | import { Platform, Pressable, StyleSheet, TextInput, View } from "react-native"; |
| 6 | import Animated, { |
| 7 | interpolate, |
| 8 | useAnimatedStyle, |
| 9 | useDerivedValue, |
| 10 | useReducedMotion, |
| 11 | withTiming, |
| 12 | } from "react-native-reanimated"; |
| 13 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 14 | import { useGT } from "gt-react-native"; |
| 15 | import { withUniwind } from "uniwind"; |
| 16 | |
| 17 | import { SymbolView, type AppSymbolName } from "../../components/ui/AppSymbol"; |
| 18 | import { AppText } from "../../components/ui/AppText"; |
| 19 | import { deriveSharedTopBand } from "../../lib/reserved-regions"; |
| 20 | import { isDarkThemeColor, themeColorWithAlpha } from "../../lib/mobileTheme"; |
| 21 | import { useBarInset } from "../../components/ui/useBarInset"; |
| 22 | import { WorkspaceColumnMetricsContext } from "../../native/v5-workspace-context"; |
| 23 | import { isDuoDevice } from "../../lib/device-type"; |
| 24 | import { useDeviceType, useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics"; |
| 25 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; |
| 26 | |
| 27 | type BrowserBarProps = { |
| 28 | readonly url: string; |
| 29 | readonly editable: boolean; |
| 30 | readonly canGoBack: boolean; |
| 31 | readonly onClose: () => void; |
| 32 | readonly onBack: () => void; |
| 33 | readonly onReload: () => void; |
| 34 | readonly onSubmit: (value: string) => void; |
| 35 | /** In the inspector pane, opens the page full screen. */ |
| 36 | readonly onMaximize?: () => void; |
| 37 | readonly trailing?: ReactNode; |
| 38 | /** The page's theme or background color; the app's sheet color until the page reports one. */ |
| 39 | readonly pageColor?: string | null; |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Every browser surface: full screen or in the inspector pane, a page on this device or a |
| 44 | * stream. The bar shrinks while the page scrolls down, like Safari's, and on the Duo the full |
| 45 | * screen bar takes the status bar with it. A page that can scroll under the bar gets it laid |
| 46 | * over itself on iOS, inset by `topInset`; a stream keeps the bar above it. |
| 47 | */ |
| 48 | export function BrowserChrome(props: { |
| 49 | readonly bar: BrowserBarProps; |
| 50 | readonly scrollsUnderBar: boolean; |
| 51 | readonly inPane?: boolean; |
| 52 | /** Keeps the whole bar showing, such as over an error. */ |
| 53 | readonly holdExpanded?: boolean; |
| 54 | readonly children: (page: { |
| 55 | readonly topInset: number; |
| 56 | readonly onScrollDirection: (direction: "up" | "down") => void; |
| 57 | }) => ReactNode; |
| 58 | }) { |
| 59 | const [scrolledDown, setScrolledDown] = useState(false); |
| 60 | const compact = scrolledDown && !props.holdExpanded; |
| 61 | const overlay = Platform.OS === "ios" && props.scrollsUnderBar; |
| 62 | const duo = isDuoDevice(useDeviceType()); |
| 63 | const hidesStatusBar = !props.inPane && duo; |
| 64 | const navigation = useNavigation(); |
| 65 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- the status bar is a native screen option |
| 66 | useEffect(() => { |
| 67 | if (!hidesStatusBar) return; |
| 68 | navigation.setOptions({ statusBarHidden: compact, statusBarAnimation: "fade" }); |
| 69 | }, [navigation, hidesStatusBar, compact]); |
| 70 | // The page stays inset by the whole bar, so the bar shrinking never moves it, like Safari's. |
| 71 | const fullBarHeight = useBrowserBarLayout(props.inPane === true).height; |
| 72 | const [barHeight, setBarHeight] = useState(fullBarHeight); |
| 73 | if (!compact && fullBarHeight !== barHeight) setBarHeight(fullBarHeight); |
| 74 | |
| 75 | const bar = ( |
| 76 | <BrowserBar |
| 77 | {...props.bar} |
| 78 | compact={compact} |
| 79 | onExpand={() => setScrolledDown(false)} |
| 80 | compactHidesStatusBar={hidesStatusBar} |
| 81 | inPane={props.inPane === true} |
| 82 | /> |
| 83 | ); |
| 84 | const page = props.children({ |
| 85 | topInset: overlay ? barHeight : 0, |
| 86 | onScrollDirection: (direction) => setScrolledDown(direction === "down"), |
| 87 | }); |
| 88 | if (!overlay) { |
| 89 | return ( |
| 90 | <> |
| 91 | {bar} |
| 92 | <View className="flex-1">{page}</View> |
| 93 | </> |
| 94 | ); |
| 95 | } |
| 96 | return ( |
| 97 | <View className="flex-1"> |
| 98 | {page} |
| 99 | <View className="absolute inset-x-0 top-0">{bar}</View> |
| 100 | </View> |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Safari's bar: frosted and tinted by the page (`pageColor`), with round controls and a centered |
| 106 | * host. `compact`, while the page scrolls down, shrinks it to a small host pill that expands it. |
| 107 | */ |
| 108 | function BrowserBar( |
| 109 | props: BrowserBarProps & { |
| 110 | readonly compact: boolean; |
| 111 | readonly onExpand: () => void; |
| 112 | /** The screen hides the status bar while compact, so the pill can sit at the very top. */ |
| 113 | readonly compactHidesStatusBar: boolean; |
| 114 | readonly inPane: boolean; |
| 115 | }, |
| 116 | ) { |
| 117 | const gt = useGT(); |
| 118 | const [draft, setDraft] = useState<string | null>(null); |
| 119 | const layout = useBrowserBarLayout(props.inPane); |
| 120 | const reduceMotion = useReducedMotion(); |
| 121 | const { themeVariables } = useAppearancePreferences(); |
| 122 | const surface = props.pageColor || themeVariables["--color-sheet-solid"]; |
| 123 | const ink: Ink = isDarkThemeColor(surface) ? "light" : "dark"; |
| 124 | const inkColor = ink === "light" ? "#ffffff" : "#000000"; |
| 125 | const compact = props.compact && draft === null; |
| 126 | const expandedHeight = layout.height; |
| 127 | const pillTop = props.compactHidesStatusBar ? 0 : layout.statusTop; |
| 128 | const compactHeight = pillTop + PILL_ROW_HEIGHT; |
| 129 | // One bar whose height follows the scroll, so nothing under it jumps between two layouts. |
| 130 | const progress = useDerivedValue(() => |
| 131 | withTiming(compact ? 1 : 0, { duration: reduceMotion ? 0 : 280 }), |
| 132 | ); |
| 133 | const barStyle = useAnimatedStyle(() => ({ |
| 134 | height: interpolate(progress.value, [0, 1], [expandedHeight, compactHeight]), |
| 135 | })); |
| 136 | const controlsStyle = useAnimatedStyle(() => ({ |
| 137 | opacity: interpolate(progress.value, [0, 0.5], [1, 0], "clamp"), |
| 138 | transform: [{ scale: interpolate(progress.value, [0, 1], [1, 0.92]) }], |
| 139 | })); |
| 140 | const pillStyle = useAnimatedStyle(() => ({ |
| 141 | opacity: interpolate(progress.value, [0.4, 1], [0, 1], "clamp"), |
| 142 | })); |
| 143 | const hairlineStyle = useAnimatedStyle(() => ({ opacity: progress.value })); |
| 144 | |
| 145 | return ( |
| 146 | <Animated.View style={[{ overflow: "hidden" }, barStyle]}> |
| 147 | {Platform.OS === "ios" ? ( |
| 148 | <BlurView |
| 149 | pointerEvents="none" |
| 150 | intensity={60} |
| 151 | tint={ink === "light" ? "dark" : "light"} |
| 152 | style={StyleSheet.absoluteFill} |
| 153 | /> |
| 154 | ) : null} |
| 155 | <View |
| 156 | pointerEvents="none" |
| 157 | style={[ |
| 158 | StyleSheet.absoluteFill, |
| 159 | { backgroundColor: themeColorWithAlpha(surface, Platform.OS === "ios" ? 0.82 : 1) }, |
| 160 | ]} |
| 161 | /> |
| 162 | <Animated.View |
| 163 | pointerEvents={compact ? "none" : "auto"} |
| 164 | style={[ |
| 165 | { |
| 166 | position: "absolute", |
| 167 | top: layout.rowTop, |
| 168 | left: layout.left, |
| 169 | right: layout.right, |
| 170 | height: layout.rowHeight, |
| 171 | justifyContent: "center", |
| 172 | }, |
| 173 | controlsStyle, |
| 174 | ]} |
| 175 | > |
| 176 | <View |
| 177 | className={ |
| 178 | layout.narrow |
| 179 | ? "flex-row items-center gap-1.5 px-2" |
| 180 | : "flex-row items-center gap-2 px-3" |
| 181 | } |
| 182 | > |
| 183 | <RoundButton icon="xmark" label={gt("Close browser")} ink={ink} onPress={props.onClose} /> |
| 184 | {props.canGoBack ? ( |
| 185 | <RoundButton |
| 186 | icon="chevron.left" |
| 187 | label={gt("Back", { $context: "browser history" })} |
| 188 | ink={ink} |
| 189 | onPress={props.onBack} |
| 190 | /> |
| 191 | ) : null} |
| 192 | {/* On a tablet the address stays a readable width, centered between the buttons. */} |
| 193 | <View className="min-w-0 flex-1 items-center"> |
| 194 | <Glass ink={ink} className="h-11 w-full max-w-2xl flex-row items-center rounded-full"> |
| 195 | <TextInput |
| 196 | accessibilityLabel={gt("Address", { $context: "browser address bar" })} |
| 197 | editable={props.editable} |
| 198 | // Unfocused, the field shows only the host, like Safari; editing starts from the URL. |
| 199 | value={draft ?? hostOf(props.url)} |
| 200 | onFocus={() => setDraft(props.url)} |
| 201 | onChangeText={setDraft} |
| 202 | onBlur={() => setDraft(null)} |
| 203 | onSubmitEditing={() => { |
| 204 | const value = draft ?? props.url; |
| 205 | setDraft(null); |
| 206 | props.onSubmit(value); |
| 207 | }} |
| 208 | placeholder={gt("Enter a URL")} |
| 209 | placeholderTextColor={themeColorWithAlpha(inkColor, 0.45)} |
| 210 | selectionColor={themeVariables["--color-primary"]} |
| 211 | autoCapitalize="none" |
| 212 | autoCorrect={false} |
| 213 | keyboardType="url" |
| 214 | returnKeyType="go" |
| 215 | selectTextOnFocus |
| 216 | lineBreakModeIOS="middle" |
| 217 | numberOfLines={1} |
| 218 | textAlign={draft === null ? "center" : "left"} |
| 219 | className="h-11 min-w-0 flex-1 pl-3 font-sans text-[15px]" |
| 220 | style={{ color: inkColor }} |
| 221 | /> |
| 222 | <Pressable |
| 223 | accessibilityRole="button" |
| 224 | accessibilityLabel={gt("Reload page")} |
| 225 | disabled={!props.editable} |
| 226 | onPress={props.onReload} |
| 227 | className="h-11 w-9 items-center justify-center" |
| 228 | > |
| 229 | <SymbolView |
| 230 | name="arrow.clockwise" |
| 231 | size={16} |
| 232 | tintColor={themeColorWithAlpha(inkColor, props.editable ? 0.85 : 0.35)} |
| 233 | /> |
| 234 | </Pressable> |
| 235 | </Glass> |
| 236 | </View> |
| 237 | {props.trailing} |
| 238 | {props.onMaximize ? ( |
| 239 | <RoundButton |
| 240 | icon="arrow.up.left.and.arrow.down.right" |
| 241 | label={gt("Maximize page")} |
| 242 | ink={ink} |
| 243 | onPress={props.onMaximize} |
| 244 | /> |
| 245 | ) : null} |
| 246 | </View> |
| 247 | </Animated.View> |
| 248 | <Animated.View |
| 249 | pointerEvents={compact ? "auto" : "none"} |
| 250 | style={[ |
| 251 | { position: "absolute", top: pillTop, left: 0, right: 0, height: PILL_ROW_HEIGHT }, |
| 252 | pillStyle, |
| 253 | ]} |
| 254 | className="items-center justify-center" |
| 255 | > |
| 256 | <Pressable |
| 257 | accessibilityRole="button" |
| 258 | accessibilityLabel={gt("Address {url}. Show the browser controls", { url: props.url })} |
| 259 | onPress={props.onExpand} |
| 260 | > |
| 261 | <Glass ink={ink} className="h-7 justify-center rounded-full px-3.5"> |
| 262 | <AppText numberOfLines={1} className="text-xs" style={{ color: inkColor }}> |
| 263 | {hostOf(props.url)} |
| 264 | </AppText> |
| 265 | </Glass> |
| 266 | </Pressable> |
| 267 | </Animated.View> |
| 268 | <Animated.View |
| 269 | pointerEvents="none" |
| 270 | style={[ |
| 271 | { |
| 272 | position: "absolute", |
| 273 | left: 0, |
| 274 | right: 0, |
| 275 | bottom: 0, |
| 276 | height: StyleSheet.hairlineWidth, |
| 277 | backgroundColor: ink === "light" ? "rgba(255,255,255,0.14)" : "rgba(0,0,0,0.12)", |
| 278 | }, |
| 279 | hairlineStyle, |
| 280 | ]} |
| 281 | /> |
| 282 | </Animated.View> |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | const EXPANDED_ROW_HEIGHT = 56; |
| 287 | // UIKit's navigation bar in a workspace column, whose controls sit in its top 48pt. |
| 288 | const NAVIGATION_BAR_HEIGHT = 58; |
| 289 | const NAVIGATION_BAR_CONTROLS_HEIGHT = 48; |
| 290 | const PILL_ROW_HEIGHT = 40; |
| 291 | |
| 292 | /** |
| 293 | * Where the whole bar's controls go. Full screen: below the top safe area, or beside the status |
| 294 | * where it covers only one end of that band. In the inspector pane: centered in the height of |
| 295 | * the headers beside it. `height` is what a page under the bar insets its content by. |
| 296 | */ |
| 297 | function useBrowserBarLayout(inPane: boolean) { |
| 298 | const band = deriveSharedTopBand(useNativeColumnLayoutMetrics()); |
| 299 | const safeTop = useSafeAreaInsets().top; |
| 300 | // The pane draws no native bar of its own, so it lines up with the bars its neighbors draw. |
| 301 | const columns = use(WorkspaceColumnMetricsContext); |
| 302 | const barInset = useBarInset(); |
| 303 | const columnTops = [...(columns?.byColumn.values() ?? [])].map((metrics) => metrics.safeArea.top); |
| 304 | const paneHeight = Math.max(barInset, ...columnTops); |
| 305 | if (inPane) { |
| 306 | return { |
| 307 | rowTop: Math.max(0, paneHeight - NAVIGATION_BAR_HEIGHT), |
| 308 | rowHeight: NAVIGATION_BAR_CONTROLS_HEIGHT, |
| 309 | left: 0, |
| 310 | right: 0, |
| 311 | narrow: false, |
| 312 | statusTop: 0, |
| 313 | height: paneHeight, |
| 314 | }; |
| 315 | } |
| 316 | if (band) { |
| 317 | return { |
| 318 | rowTop: 0, |
| 319 | rowHeight: band.height, |
| 320 | left: band.left, |
| 321 | right: band.right, |
| 322 | narrow: true, |
| 323 | statusTop: safeTop, |
| 324 | height: band.height, |
| 325 | }; |
| 326 | } |
| 327 | return { |
| 328 | rowTop: safeTop, |
| 329 | rowHeight: EXPANDED_ROW_HEIGHT, |
| 330 | left: 0, |
| 331 | right: 0, |
| 332 | narrow: false, |
| 333 | statusTop: safeTop, |
| 334 | height: safeTop + EXPANDED_ROW_HEIGHT, |
| 335 | }; |
| 336 | } |
| 337 | |
| 338 | type Ink = "light" | "dark"; |
| 339 | |
| 340 | const StyledGlassView = withUniwind(GlassView, { style: { fromClassName: "className" } }); |
| 341 | |
| 342 | function RoundButton(props: { |
| 343 | readonly icon: AppSymbolName; |
| 344 | readonly label: string; |
| 345 | readonly ink: Ink; |
| 346 | readonly onPress: () => void; |
| 347 | }) { |
| 348 | return ( |
| 349 | <Pressable accessibilityRole="button" accessibilityLabel={props.label} onPress={props.onPress}> |
| 350 | <Glass ink={props.ink} className="size-10 items-center justify-center rounded-full"> |
| 351 | <SymbolView |
| 352 | name={props.icon} |
| 353 | size={17} |
| 354 | tintColor={props.ink === "light" ? "#ffffff" : "#000000"} |
| 355 | /> |
| 356 | </Glass> |
| 357 | </Pressable> |
| 358 | ); |
| 359 | } |
| 360 | |
| 361 | /** Liquid glass where iOS has it, matched to the page's lightness rather than the app's. */ |
| 362 | function Glass(props: { |
| 363 | readonly ink: Ink; |
| 364 | readonly className: string; |
| 365 | readonly children: ReactNode; |
| 366 | }) { |
| 367 | if (Platform.OS === "ios" && isGlassEffectAPIAvailable()) { |
| 368 | return ( |
| 369 | <StyledGlassView |
| 370 | glassEffectStyle="regular" |
| 371 | colorScheme={props.ink === "light" ? "dark" : "light"} |
| 372 | isInteractive |
| 373 | className={props.className} |
| 374 | > |
| 375 | {props.children} |
| 376 | </StyledGlassView> |
| 377 | ); |
| 378 | } |
| 379 | return ( |
| 380 | <View |
| 381 | className={props.className} |
| 382 | style={{ |
| 383 | backgroundColor: props.ink === "light" ? "rgba(255,255,255,0.12)" : "rgba(0,0,0,0.06)", |
| 384 | }} |
| 385 | > |
| 386 | {props.children} |
| 387 | </View> |
| 388 | ); |
| 389 | } |
| 390 | |
| 391 | function hostOf(value: string): string { |
| 392 | try { |
| 393 | return new URL(value).host; |
| 394 | } catch { |
| 395 | return value; |
| 396 | } |
| 397 | } |