Open History to see the last commit
1import { MOBILE_SHAPE } from "../../lib/designTokens";
2import { useEffect } from "react";
3import { AccessibilityInfo, Platform, View, useWindowDimensions } from "react-native";
4
5import { AppText } from "./AppText";
6import { Button } from "./Button";
7import { GlassSurface } from "./GlassSurface";
8import { IconButton } from "./IconButton";
9import { AppActivityIndicator } from "./AppActivityIndicator";
10import { SymbolView } from "./AppSymbol";
11
12export interface ToastProps {
13 readonly message: string;
14 readonly description?: string;
15 readonly tone?: "default" | "success" | "danger";
16 readonly busy?: boolean;
17 readonly action?: { readonly label: string; readonly onPress: () => void };
18 readonly onDismiss?: () => void;
19}
20
21/** The owning screen places and dismisses feedback; actions are never implicit. */
22export function Toast(props: ToastProps) {
23 const { fontScale } = useWindowDimensions();
24 // A one-line message keeps its action on the same row. Descriptions and
25 // accessibility text sizes need the full width, so the action moves below.
26 const inlineAction = !props.description && fontScale < 1.5;
27 const action = props.action ? (
28 <Button label={props.action.label} onPress={props.action.onPress} />
29 ) : null;
30 useEffect(() => {
31 if (Platform.OS === "ios") AccessibilityInfo.announceForAccessibility(props.message);
32 }, [props.message]);
33 return (
34 <GlassSurface className="p-3" style={{ borderRadius: MOBILE_SHAPE.toast }}>
35 <View className="flex-row items-center gap-3">
36 {props.busy ? (
37 <AppActivityIndicator />
38 ) : (
39 <SymbolView
40 name={props.tone === "danger" ? "exclamationmark.triangle" : "checkmark.circle"}
41 size={20}
42 tintColorClassName={
43 props.tone === "danger" ? "accent-danger-foreground" : "accent-icon"
44 }
45 />
46 )}
47 <View className="min-w-0 flex-1" accessibilityLiveRegion="polite">
48 <AppText textRole="footnote" className="font-t3-semibold">
49 {props.message}
50 </AppText>
51 {props.description ? (
52 <AppText textRole="label" className="text-foreground-muted">
53 {props.description}
54 </AppText>
55 ) : null}
56 </View>
57 {inlineAction ? action : null}
58 {props.onDismiss ? (
59 <IconButton accessibilityLabel="Dismiss message" icon="xmark" onPress={props.onDismiss} />
60 ) : null}
61 </View>
62 {action && !inlineAction ? <View className="mt-2 self-end">{action}</View> : null}
63 </GlassSurface>
64 );
65}