The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useGT } from "gt-react-native"; |
| 2 | import { cn } from "../../lib/cn"; |
| 3 | import { MOBILE_MOTION, MOBILE_SHAPE } from "../../lib/designTokens"; |
| 4 | import { useCallback, useEffect, useLayoutEffect, useState } from "react"; |
| 5 | import { AccessibilityInfo, Platform, StyleSheet, View, useWindowDimensions } from "react-native"; |
| 6 | import { GestureDetector, usePanGesture } from "react-native-gesture-handler"; |
| 7 | import Animated, { |
| 8 | cancelAnimation, |
| 9 | Easing, |
| 10 | FadeOut, |
| 11 | ReduceMotion, |
| 12 | runOnJS, |
| 13 | useAnimatedStyle, |
| 14 | useSharedValue, |
| 15 | withTiming, |
| 16 | type EntryAnimationsValues, |
| 17 | } from "react-native-reanimated"; |
| 18 | |
| 19 | import { AppText } from "./AppText"; |
| 20 | import { Button } from "./Button"; |
| 21 | import { GlassSurface } from "./GlassSurface"; |
| 22 | import { useControlTheme } from "./useControlTheme"; |
| 23 | import { IconButton } from "./IconButton"; |
| 24 | import { AppActivityIndicator } from "./AppActivityIndicator"; |
| 25 | import { SymbolView } from "./AppSymbol"; |
| 26 | |
| 27 | export type ToastProps = { |
| 28 | readonly message: string; |
| 29 | readonly description?: string; |
| 30 | readonly singleLine?: boolean; |
| 31 | readonly tone?: "default" | "success" | "danger"; |
| 32 | readonly busy?: boolean; |
| 33 | readonly action?: { readonly label: string; readonly onPress: () => void }; |
| 34 | } & ( |
| 35 | | { readonly dismissMode: "swipe"; readonly onDismiss: () => void } |
| 36 | | { readonly dismissMode?: "button"; readonly onDismiss?: () => void } |
| 37 | ); |
| 38 | |
| 39 | export function Toast(props: ToastProps) { |
| 40 | return props.dismissMode === "swipe" ? ( |
| 41 | <SwipeableToast {...props} /> |
| 42 | ) : ( |
| 43 | <ToastContent {...props} /> |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | function ToastContent(props: ToastProps & { readonly glassReady?: boolean }) { |
| 48 | const gt = useGT(); |
| 49 | const { fontScale } = useWindowDimensions(); |
| 50 | const { colorScheme } = useControlTheme(); |
| 51 | const borderRadius = Platform.OS === "ios" ? 28 : MOBILE_SHAPE.toast; |
| 52 | // Explicit single-line notices reserve the action's width and truncate the text. |
| 53 | const inlineAction = props.singleLine || (!props.description && fontScale < 1.5); |
| 54 | const message = |
| 55 | props.singleLine && props.description |
| 56 | ? `${props.message}: ${props.description}` |
| 57 | : props.message; |
| 58 | const action = props.action ? ( |
| 59 | <Button |
| 60 | label={props.action.label} |
| 61 | onPress={props.action.onPress} |
| 62 | size={Platform.OS === "ios" ? "compact" : "default"} |
| 63 | /> |
| 64 | ) : null; |
| 65 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- announces the toast message to VoiceOver through AccessibilityInfo |
| 66 | useEffect(() => { |
| 67 | if (Platform.OS === "ios") AccessibilityInfo.announceForAccessibility(props.message); |
| 68 | }, [props.message]); |
| 69 | const surface = ( |
| 70 | <GlassSurface |
| 71 | key={props.glassReady === false ? "entering" : "settled"} |
| 72 | chrome={Platform.OS === "ios" && colorScheme === "dark" ? "none" : undefined} |
| 73 | tintColor={Platform.OS === "ios" ? null : undefined} |
| 74 | className={Platform.OS === "ios" ? "bg-transparent p-2" : "p-3"} |
| 75 | style={{ borderRadius }} |
| 76 | > |
| 77 | <View |
| 78 | className={cn( |
| 79 | "flex-row items-center gap-3", |
| 80 | // Match the leading inset to the action label's, which sits inside the button's padding. |
| 81 | Platform.OS === "ios" && inlineAction && action && "pl-5", |
| 82 | )} |
| 83 | > |
| 84 | {props.busy ? ( |
| 85 | <AppActivityIndicator /> |
| 86 | ) : ( |
| 87 | <SymbolView |
| 88 | name={props.tone === "danger" ? "exclamationmark.triangle" : "checkmark.circle"} |
| 89 | size={20} |
| 90 | tintColorClassName={ |
| 91 | props.tone === "danger" ? "accent-danger-foreground" : "accent-icon" |
| 92 | } |
| 93 | /> |
| 94 | )} |
| 95 | <View className="min-w-0 flex-1" accessibilityLiveRegion="polite"> |
| 96 | <AppText |
| 97 | textRole="footnote" |
| 98 | className="font-t3-semibold" |
| 99 | numberOfLines={props.singleLine ? 1 : undefined} |
| 100 | accessibilityLabel={props.singleLine ? message : undefined} |
| 101 | > |
| 102 | {message} |
| 103 | </AppText> |
| 104 | {props.description && !props.singleLine ? ( |
| 105 | <AppText textRole="label" className="text-foreground-muted"> |
| 106 | {props.description} |
| 107 | </AppText> |
| 108 | ) : null} |
| 109 | </View> |
| 110 | {inlineAction && action ? <View className="self-center">{action}</View> : null} |
| 111 | {props.onDismiss && props.dismissMode !== "swipe" ? ( |
| 112 | <IconButton |
| 113 | accessibilityLabel={gt("Dismiss message")} |
| 114 | icon="xmark" |
| 115 | onPress={props.onDismiss} |
| 116 | /> |
| 117 | ) : null} |
| 118 | </View> |
| 119 | {action && !inlineAction ? <View className="mt-2 self-end">{action}</View> : null} |
| 120 | </GlassSurface> |
| 121 | ); |
| 122 | return Platform.OS === "ios" ? ( |
| 123 | <View |
| 124 | style={{ |
| 125 | borderRadius, |
| 126 | boxShadow: [ |
| 127 | { |
| 128 | offsetX: 0, |
| 129 | offsetY: 0, |
| 130 | blurRadius: 8, |
| 131 | color: colorScheme === "dark" ? "rgba(0, 0, 0, 0.22)" : "rgba(0, 0, 0, 0.08)", |
| 132 | }, |
| 133 | ], |
| 134 | }} |
| 135 | > |
| 136 | {surface} |
| 137 | </View> |
| 138 | ) : ( |
| 139 | surface |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | const SWIPE_TIMING = { duration: 160, reduceMotion: ReduceMotion.System }; |
| 144 | const ENTER_TIMING = { |
| 145 | duration: MOBILE_MOTION.enter, |
| 146 | easing: Easing.out(Easing.cubic), |
| 147 | reduceMotion: ReduceMotion.System, |
| 148 | }; |
| 149 | const TOAST_EXITING = FadeOut.duration(MOBILE_MOTION.exit).reduceMotion(ReduceMotion.System); |
| 150 | function SwipeableToast(props: ToastProps & { readonly onDismiss: () => void }) { |
| 151 | const gt = useGT(); |
| 152 | const [glassReady, setGlassReady] = useState(Platform.OS !== "ios"); |
| 153 | const finishEntering = useCallback(() => setGlassReady(true), []); |
| 154 | // UIKit can skip glass on a moving host; remount the surface after entry so its blur installs. |
| 155 | const toastEntering = (values: EntryAnimationsValues) => { |
| 156 | "worklet"; |
| 157 | return { |
| 158 | initialValues: { originY: values.targetOriginY + values.targetHeight + 12 }, |
| 159 | animations: { originY: withTiming(values.targetOriginY, ENTER_TIMING) }, |
| 160 | callback: (finished: boolean) => { |
| 161 | if (finished) runOnJS(finishEntering)(); |
| 162 | }, |
| 163 | }; |
| 164 | }; |
| 165 | const { onDismiss } = props; |
| 166 | const translation = useSharedValue(0); |
| 167 | const width = useSharedValue(0); |
| 168 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- cancels the Reanimated swipe translation animation on unmount |
| 169 | useLayoutEffect( |
| 170 | () => () => { |
| 171 | cancelAnimation(translation); |
| 172 | }, |
| 173 | [translation], |
| 174 | ); |
| 175 | const gesture = usePanGesture({ |
| 176 | activeOffsetX: [-10, 10], |
| 177 | failOffsetY: [-12, 12], |
| 178 | onUpdate: (event) => { |
| 179 | translation.set(event.translationX); |
| 180 | }, |
| 181 | onDeactivate: (event) => { |
| 182 | if ( |
| 183 | !event.canceled && |
| 184 | (Math.abs(event.translationX) > width.value * 0.25 || |
| 185 | (Math.abs(event.velocityX) > 800 && event.translationX * event.velocityX > 0)) |
| 186 | ) { |
| 187 | const direction = Math.sign(event.translationX || event.velocityX); |
| 188 | translation.set( |
| 189 | withTiming(direction * (width.value + 24), SWIPE_TIMING, (finished) => { |
| 190 | if (finished) runOnJS(onDismiss)(); |
| 191 | }), |
| 192 | ); |
| 193 | } else { |
| 194 | translation.set(withTiming(0, SWIPE_TIMING)); |
| 195 | } |
| 196 | }, |
| 197 | }); |
| 198 | const animatedStyle = useAnimatedStyle(() => ({ |
| 199 | transform: [{ translateX: translation.value }], |
| 200 | })); |
| 201 | return ( |
| 202 | <Animated.View |
| 203 | className={Platform.OS === "android" ? "overflow-hidden" : undefined} |
| 204 | pointerEvents="box-none" |
| 205 | exiting={TOAST_EXITING} |
| 206 | onAccessibilityEscape={onDismiss} |
| 207 | onLayout={(event) => { |
| 208 | width.set(event.nativeEvent.layout.width); |
| 209 | }} |
| 210 | > |
| 211 | <GestureDetector gesture={gesture}> |
| 212 | <Animated.View collapsable={false} entering={toastEntering} style={animatedStyle}> |
| 213 | {Platform.OS === "android" ? ( |
| 214 | // Keep the dismiss target separate so TalkBack can also focus the action button. |
| 215 | <View |
| 216 | accessible |
| 217 | accessibilityRole="alert" |
| 218 | accessibilityLiveRegion="assertive" |
| 219 | accessibilityLabel={props.message} |
| 220 | accessibilityActions={[{ name: "dismiss", label: gt("Dismiss message") }]} |
| 221 | onAccessibilityAction={({ nativeEvent }) => { |
| 222 | if (nativeEvent.actionName === "dismiss") onDismiss(); |
| 223 | }} |
| 224 | pointerEvents="none" |
| 225 | style={StyleSheet.absoluteFill} |
| 226 | /> |
| 227 | ) : null} |
| 228 | <ToastContent {...props} glassReady={glassReady} /> |
| 229 | </Animated.View> |
| 230 | </GestureDetector> |
| 231 | </Animated.View> |
| 232 | ); |
| 233 | } |