The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ComponentProps, ReactNode } from "react"; |
| 2 | import { useCallback, useRef, useState } from "react"; |
| 3 | import { |
| 4 | Pressable, |
| 5 | ScrollView, |
| 6 | View, |
| 7 | type LayoutChangeEvent, |
| 8 | type NativeScrollEvent, |
| 9 | type NativeSyntheticEvent, |
| 10 | type PressableProps, |
| 11 | type StyleProp, |
| 12 | type ViewStyle, |
| 13 | } from "react-native"; |
| 14 | |
| 15 | import Animated, { FadeIn, useReducedMotion } from "react-native-reanimated"; |
| 16 | |
| 17 | import { cn } from "../lib/cn"; |
| 18 | import { AppText as Text } from "./ui/AppText"; |
| 19 | import { SymbolView } from "./ui/AppSymbol"; |
| 20 | import { useAndroidControlSizing } from "./ui/useAndroidControlSizing"; |
| 21 | |
| 22 | const COMPOSER_TOOLBAR_GAP = 8; |
| 23 | const COMPOSER_TOOLBAR_FADE_WIDTH = 18; |
| 24 | const COMPOSER_TOOLBAR_SCROLL_EPSILON = 4; |
| 25 | const INLINE_CONTROL_LABEL_ENTERING = FadeIn.duration(160); |
| 26 | export function ComposerInlineControl(props: { |
| 27 | readonly size?: "regular" | "large"; |
| 28 | readonly accessibilityHint?: string; |
| 29 | readonly accessibilityLabel?: string; |
| 30 | readonly disabled?: boolean; |
| 31 | readonly emphasized?: boolean; |
| 32 | readonly icon?: ComponentProps<typeof SymbolView>["name"]; |
| 33 | readonly renderIcon?: (size: number) => ReactNode; |
| 34 | readonly label: string; |
| 35 | readonly labelEllipsizeMode?: ComponentProps<typeof Text>["ellipsizeMode"]; |
| 36 | readonly suffix?: ReactNode; |
| 37 | readonly suffixWidth?: number; |
| 38 | /** Show only the icon and chevron; the label still names the control for accessibility. */ |
| 39 | readonly iconOnly?: boolean; |
| 40 | readonly maxWidth?: ViewStyle["maxWidth"]; |
| 41 | readonly onPress?: () => void; |
| 42 | readonly selected?: boolean; |
| 43 | readonly static?: boolean; |
| 44 | readonly chevronDirection?: "down" | "right"; |
| 45 | readonly showChevron?: boolean; |
| 46 | }) { |
| 47 | const { scale, smallIconSize } = useAndroidControlSizing(); |
| 48 | const reducedMotion = useReducedMotion(); |
| 49 | // Keep the picker anchored to its parent while the editor changes height. |
| 50 | const animatesLabel = props.iconOnly !== undefined; |
| 51 | const iconSize = props.size === "large" ? 20 : smallIconSize; |
| 52 | return ( |
| 53 | <Animated.View |
| 54 | style={{ |
| 55 | maxWidth: props.maxWidth ?? 190, |
| 56 | overflow: animatesLabel ? "hidden" : undefined, |
| 57 | }} |
| 58 | > |
| 59 | <Pressable |
| 60 | accessibilityLabel={props.accessibilityLabel ?? props.label} |
| 61 | accessibilityHint={props.accessibilityHint} |
| 62 | accessibilityRole={props.static ? undefined : "button"} |
| 63 | accessibilityState={ |
| 64 | props.static ? undefined : { disabled: props.disabled, selected: props.selected } |
| 65 | } |
| 66 | className="h-[44px] flex-row items-center gap-2 rounded-xl px-2 active:bg-subtle" |
| 67 | disabled={props.disabled || props.static} |
| 68 | onPress={props.onPress} |
| 69 | style={{ opacity: props.disabled ? 0.45 : 1 }} |
| 70 | > |
| 71 | {props.renderIcon ? ( |
| 72 | <View |
| 73 | className="size-4 shrink-0 items-center justify-center" |
| 74 | style={{ width: iconSize, height: iconSize }} |
| 75 | > |
| 76 | {props.renderIcon(iconSize)} |
| 77 | </View> |
| 78 | ) : props.icon ? ( |
| 79 | <SymbolView |
| 80 | name={props.icon} |
| 81 | size={iconSize} |
| 82 | tintColorClassName={ |
| 83 | props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted" |
| 84 | } |
| 85 | type="monochrome" |
| 86 | /> |
| 87 | ) : null} |
| 88 | {props.iconOnly ? null : ( |
| 89 | <Animated.View |
| 90 | className="shrink" |
| 91 | entering={animatesLabel && !reducedMotion ? INLINE_CONTROL_LABEL_ENTERING : undefined} |
| 92 | > |
| 93 | <Text |
| 94 | className={cn( |
| 95 | props.size === "large" ? "text-base font-t3-medium" : "text-sm font-t3-medium", |
| 96 | props.emphasized || props.selected ? "text-foreground" : "text-foreground-muted", |
| 97 | )} |
| 98 | numberOfLines={1} |
| 99 | ellipsizeMode={props.labelEllipsizeMode} |
| 100 | > |
| 101 | {props.label} |
| 102 | </Text> |
| 103 | </Animated.View> |
| 104 | )} |
| 105 | {!props.iconOnly && props.suffix ? ( |
| 106 | <View |
| 107 | className={cn("min-w-0", props.suffixWidth === undefined ? "shrink" : "shrink-0")} |
| 108 | style={{ width: props.suffixWidth }} |
| 109 | > |
| 110 | {props.suffix} |
| 111 | </View> |
| 112 | ) : null} |
| 113 | {props.showChevron === false ? null : ( |
| 114 | <SymbolView |
| 115 | name={props.chevronDirection === "right" ? "chevron.right" : "chevron.down"} |
| 116 | size={Math.round((props.size === "large" ? 12 : 10) * scale)} |
| 117 | tintColorClassName={ |
| 118 | props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted" |
| 119 | } |
| 120 | type="monochrome" |
| 121 | /> |
| 122 | )} |
| 123 | </Pressable> |
| 124 | </Animated.View> |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | export function ComposerToolbarRow(props: { |
| 129 | readonly children: ReactNode; |
| 130 | readonly paddingBottom?: number; |
| 131 | readonly paddingHorizontal?: number; |
| 132 | readonly paddingTop?: number; |
| 133 | readonly style?: StyleProp<ViewStyle>; |
| 134 | }) { |
| 135 | return ( |
| 136 | <View |
| 137 | className="flex-row items-center gap-1.5" |
| 138 | style={[ |
| 139 | { |
| 140 | paddingBottom: props.paddingBottom ?? 8, |
| 141 | paddingHorizontal: props.paddingHorizontal ?? 6, |
| 142 | paddingTop: props.paddingTop ?? 8, |
| 143 | }, |
| 144 | props.style, |
| 145 | ]} |
| 146 | > |
| 147 | {props.children} |
| 148 | </View> |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | export function ComposerToolbarScroller(props: { |
| 153 | readonly children: ReactNode; |
| 154 | readonly align?: "start" | "end"; |
| 155 | readonly fadeOpaque?: string; |
| 156 | readonly fadeTransparent?: string; |
| 157 | readonly fadeSurface?: "card" | "sheet"; |
| 158 | readonly contentPaddingRight?: number; |
| 159 | }) { |
| 160 | const metricsRef = useRef({ |
| 161 | contentWidth: 0, |
| 162 | offsetX: 0, |
| 163 | viewportWidth: 0, |
| 164 | showLeftFade: false, |
| 165 | showRightFade: false, |
| 166 | }); |
| 167 | const [scrollEdges, setScrollEdges] = useState({ |
| 168 | showLeftFade: false, |
| 169 | showRightFade: false, |
| 170 | }); |
| 171 | |
| 172 | const updateScrollEdges = useCallback(() => { |
| 173 | const metrics = metricsRef.current; |
| 174 | const maxOffset = Math.max(0, metrics.contentWidth - metrics.viewportWidth); |
| 175 | const showLeftFade = metrics.offsetX > COMPOSER_TOOLBAR_SCROLL_EPSILON; |
| 176 | const showRightFade = metrics.offsetX < maxOffset - COMPOSER_TOOLBAR_SCROLL_EPSILON; |
| 177 | if (metrics.showLeftFade === showLeftFade && metrics.showRightFade === showRightFade) { |
| 178 | return; |
| 179 | } |
| 180 | metrics.showLeftFade = showLeftFade; |
| 181 | metrics.showRightFade = showRightFade; |
| 182 | setScrollEdges({ showLeftFade, showRightFade }); |
| 183 | }, []); |
| 184 | |
| 185 | const handleLayout = useCallback( |
| 186 | (event: LayoutChangeEvent) => { |
| 187 | const viewportWidth = event.nativeEvent.layout.width; |
| 188 | if (metricsRef.current.viewportWidth === viewportWidth) return; |
| 189 | metricsRef.current.viewportWidth = viewportWidth; |
| 190 | updateScrollEdges(); |
| 191 | }, |
| 192 | [updateScrollEdges], |
| 193 | ); |
| 194 | |
| 195 | const handleContentSizeChange = useCallback( |
| 196 | (contentWidth: number) => { |
| 197 | if (metricsRef.current.contentWidth === contentWidth) return; |
| 198 | metricsRef.current.contentWidth = contentWidth; |
| 199 | updateScrollEdges(); |
| 200 | }, |
| 201 | [updateScrollEdges], |
| 202 | ); |
| 203 | |
| 204 | const handleScroll = useCallback( |
| 205 | (event: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 206 | const offsetX = event.nativeEvent.contentOffset.x; |
| 207 | if (Math.abs(metricsRef.current.offsetX - offsetX) < 1) return; |
| 208 | metricsRef.current.offsetX = offsetX; |
| 209 | updateScrollEdges(); |
| 210 | }, |
| 211 | [updateScrollEdges], |
| 212 | ); |
| 213 | |
| 214 | return ( |
| 215 | <View className="relative min-w-0 flex-1"> |
| 216 | <ScrollView |
| 217 | horizontal |
| 218 | keyboardShouldPersistTaps="always" |
| 219 | onContentSizeChange={handleContentSizeChange} |
| 220 | onLayout={handleLayout} |
| 221 | onScroll={handleScroll} |
| 222 | scrollEventThrottle={16} |
| 223 | showsHorizontalScrollIndicator={false} |
| 224 | contentContainerStyle={{ |
| 225 | alignItems: "center", |
| 226 | flexGrow: props.align === "end" ? 1 : undefined, |
| 227 | justifyContent: props.align === "end" ? "flex-end" : undefined, |
| 228 | gap: COMPOSER_TOOLBAR_GAP, |
| 229 | paddingLeft: 0, |
| 230 | paddingRight: props.contentPaddingRight ?? 1, |
| 231 | }} |
| 232 | > |
| 233 | {props.children} |
| 234 | </ScrollView> |
| 235 | {scrollEdges.showLeftFade ? ( |
| 236 | <View |
| 237 | className={cn( |
| 238 | "absolute inset-y-0 left-0", |
| 239 | props.fadeOpaque === undefined && "bg-linear-to-r to-transparent", |
| 240 | props.fadeOpaque === undefined && |
| 241 | (props.fadeSurface === "sheet" ? "from-sheet" : "from-card"), |
| 242 | )} |
| 243 | pointerEvents="none" |
| 244 | style={{ |
| 245 | width: COMPOSER_TOOLBAR_FADE_WIDTH, |
| 246 | experimental_backgroundImage: |
| 247 | props.fadeOpaque === undefined |
| 248 | ? undefined |
| 249 | : `linear-gradient(to right, ${props.fadeOpaque} 0%, ${props.fadeTransparent ?? "transparent"} 100%)`, |
| 250 | }} |
| 251 | /> |
| 252 | ) : null} |
| 253 | {scrollEdges.showRightFade ? ( |
| 254 | <View |
| 255 | className={cn( |
| 256 | "absolute inset-y-0 right-0", |
| 257 | props.fadeOpaque === undefined && "bg-linear-to-r from-transparent", |
| 258 | props.fadeOpaque === undefined && |
| 259 | (props.fadeSurface === "sheet" ? "to-sheet" : "to-card"), |
| 260 | )} |
| 261 | pointerEvents="none" |
| 262 | style={{ |
| 263 | width: COMPOSER_TOOLBAR_FADE_WIDTH, |
| 264 | experimental_backgroundImage: |
| 265 | props.fadeOpaque === undefined |
| 266 | ? undefined |
| 267 | : `linear-gradient(to right, ${props.fadeTransparent ?? "transparent"} 0%, ${props.fadeOpaque} 100%)`, |
| 268 | }} |
| 269 | /> |
| 270 | ) : null} |
| 271 | </View> |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | export function ComposerActionButton(props: { |
| 276 | readonly size?: "regular" | "large"; |
| 277 | readonly accessibilityLabel: string; |
| 278 | readonly disabled?: boolean; |
| 279 | readonly icon: ComponentProps<typeof SymbolView>["name"]; |
| 280 | readonly onPress: () => void; |
| 281 | readonly variant?: "primary" | "danger"; |
| 282 | // Menu injects onLongPress on Android and onTouchStart/onPress on iOS. |
| 283 | readonly onLongPress?: PressableProps["onLongPress"]; |
| 284 | readonly onTouchStart?: PressableProps["onTouchStart"]; |
| 285 | }) { |
| 286 | const { scale, smallIconSize } = useAndroidControlSizing(); |
| 287 | const circleSize = Math.round((props.size === "large" ? 44 : 30) * scale); |
| 288 | const fadeIcon = props.disabled === true && props.variant !== "danger"; |
| 289 | return ( |
| 290 | <Pressable |
| 291 | accessibilityLabel={props.accessibilityLabel} |
| 292 | accessibilityRole="button" |
| 293 | accessibilityState={{ disabled: props.disabled }} |
| 294 | className="size-[44px] shrink-0 items-center justify-center active:opacity-70" |
| 295 | style={props.size === "large" ? { width: 62 } : undefined} |
| 296 | disabled={props.disabled} |
| 297 | onPress={props.onPress} |
| 298 | onLongPress={props.onLongPress} |
| 299 | onTouchStart={props.onTouchStart} |
| 300 | > |
| 301 | <View |
| 302 | style={{ width: circleSize, height: circleSize }} |
| 303 | className={cn( |
| 304 | "items-center justify-center rounded-full", |
| 305 | props.variant === "danger" |
| 306 | ? "bg-danger" |
| 307 | : props.disabled |
| 308 | ? "bg-foreground/12" |
| 309 | : "bg-primary", |
| 310 | )} |
| 311 | > |
| 312 | {/* Fade the icon as one layer: a translucent tint, or opacity on a Tabler icon (which |
| 313 | copies it onto every path), stacks where the SVG strokes overlap. */} |
| 314 | <View |
| 315 | className={fadeIcon ? "opacity-38" : undefined} |
| 316 | needsOffscreenAlphaCompositing={fadeIcon} |
| 317 | > |
| 318 | <SymbolView |
| 319 | name={props.icon} |
| 320 | size={props.size === "large" ? Math.round(20 * scale) : smallIconSize} |
| 321 | weight="semibold" |
| 322 | tintColorClassName={ |
| 323 | props.variant === "danger" |
| 324 | ? "accent-danger-foreground" |
| 325 | : props.disabled |
| 326 | ? "accent-foreground" |
| 327 | : "accent-primary-foreground" |
| 328 | } |
| 329 | type="monochrome" |
| 330 | /> |
| 331 | </View> |
| 332 | </View> |
| 333 | </Pressable> |
| 334 | ); |
| 335 | } |
| 336 | |
| 337 | export function ComposerToolbarButton(props: { |
| 338 | readonly icon?: ComponentProps<typeof SymbolView>["name"]; |
| 339 | readonly iconNode?: ReactNode; |
| 340 | readonly label?: string; |
| 341 | readonly accessibilityLabel?: string; |
| 342 | readonly active?: boolean; |
| 343 | readonly disabled?: boolean; |
| 344 | readonly maxWidth?: number; |
| 345 | readonly minWidth?: number; |
| 346 | readonly onPress?: () => void; |
| 347 | readonly showChevron?: boolean; |
| 348 | readonly textTransform?: "none" | "uppercase"; |
| 349 | readonly variant?: "default" | "primary" | "danger"; |
| 350 | readonly className?: string; |
| 351 | readonly style?: StyleProp<ViewStyle>; |
| 352 | }) { |
| 353 | const variant = props.variant ?? "default"; |
| 354 | const isCircle = !props.label && props.showChevron === false; |
| 355 | const iconTintClassName = |
| 356 | variant === "primary" |
| 357 | ? props.disabled |
| 358 | ? "accent-icon-subtle" |
| 359 | : "accent-primary-foreground" |
| 360 | : variant === "danger" |
| 361 | ? "accent-danger-foreground" |
| 362 | : "accent-icon"; |
| 363 | |
| 364 | return ( |
| 365 | <Pressable |
| 366 | accessibilityLabel={props.accessibilityLabel ?? props.label} |
| 367 | accessibilityRole="button" |
| 368 | disabled={props.disabled} |
| 369 | onPress={props.onPress} |
| 370 | className={cn( |
| 371 | "h-11 max-w-[172px] flex-row items-center justify-center rounded-full border active:opacity-70", |
| 372 | variant === "primary" && "shadow-lg shadow-adaptive-black-a10-a25 disabled:shadow-none", |
| 373 | isCircle ? "w-11" : "gap-2 px-3.5", |
| 374 | variant === "primary" |
| 375 | ? props.disabled |
| 376 | ? "bg-subtle-strong" |
| 377 | : "bg-primary" |
| 378 | : variant === "danger" |
| 379 | ? "bg-danger" |
| 380 | : props.active |
| 381 | ? "bg-subtle-strong" |
| 382 | : "bg-subtle", |
| 383 | variant === "default" |
| 384 | ? props.active |
| 385 | ? "border-border" |
| 386 | : "border-border-subtle" |
| 387 | : variant === "danger" |
| 388 | ? "border-danger-border" |
| 389 | : props.disabled |
| 390 | ? "border-border-subtle" |
| 391 | : "border-primary-foreground/20", |
| 392 | props.className, |
| 393 | )} |
| 394 | style={({ pressed }) => [ |
| 395 | { |
| 396 | maxWidth: props.maxWidth, |
| 397 | minWidth: props.minWidth, |
| 398 | opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1, |
| 399 | }, |
| 400 | props.style, |
| 401 | ]} |
| 402 | > |
| 403 | {props.iconNode ? ( |
| 404 | <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View> |
| 405 | ) : props.icon ? ( |
| 406 | <SymbolView |
| 407 | name={props.icon} |
| 408 | size={16} |
| 409 | tintColorClassName={iconTintClassName} |
| 410 | type="monochrome" |
| 411 | /> |
| 412 | ) : null} |
| 413 | {props.label ? ( |
| 414 | <Text |
| 415 | className={cn( |
| 416 | "shrink text-center text-sm font-t3-bold", |
| 417 | variant === "primary" |
| 418 | ? props.disabled |
| 419 | ? "text-foreground-muted" |
| 420 | : "text-primary-foreground" |
| 421 | : "text-foreground", |
| 422 | )} |
| 423 | ellipsizeMode="tail" |
| 424 | numberOfLines={1} |
| 425 | style={{ textTransform: props.textTransform ?? "none" }} |
| 426 | > |
| 427 | {props.label} |
| 428 | </Text> |
| 429 | ) : null} |
| 430 | {props.showChevron === false ? null : ( |
| 431 | <SymbolView |
| 432 | name="chevron.down" |
| 433 | size={11} |
| 434 | tintColorClassName={iconTintClassName} |
| 435 | type="monochrome" |
| 436 | /> |
| 437 | )} |
| 438 | </Pressable> |
| 439 | ); |
| 440 | } |