Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Toast.tsx

64 lines2.4 KB

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;
20​
21export function Toast(props: ToastProps) {
22 const { fontScale } = useWindowDimensions();
23 // A one-line message keeps its action on the same row. Descriptions and
24 // accessibility text sizes need the full width, so the action moves below.
25 const inlineAction = !props.description && fontScale < 1.5;
26 const action = props.action ? (
27 <Button label={props.action.label} onPress={props.action.onPress} />
28 ) : null;
29 useEffect(() => {
30 if (Platform.OS === "ios") AccessibilityInfo.announceForAccessibility(props.message);
31 }, [props.message]);
32 return (
33 <GlassSurface className="p-3" style={{ borderRadius: MOBILE_SHAPE.toast }}>
34 <View className="flex-row items-center gap-3">
35 {props.busy ? (
36 <AppActivityIndicator />
37 ) : (
38 <SymbolView
39 name={props.tone === "danger" ? "exclamationmark.triangle" : "checkmark.circle"}
40 size={20}
41 tintColorClassName={
42 props.tone === "danger" ? "accent-danger-foreground" : "accent-icon"
43 }
44 />
45 )}
46 <View className="min-w-0 flex-1" accessibilityLiveRegion="polite">
47 <AppText textRole="footnote" className="font-t3-semibold">
48 {props.message}
49 </AppText>
50 {props.description ? (
51 <AppText textRole="label" className="text-foreground-muted">
52 {props.description}
53 </AppText>
54 ) : null}
55 </View>
56 {inlineAction ? action : null}
57 {props.onDismiss ? (
58 <IconButton accessibilityLabel="Dismiss message" icon="xmark" onPress={props.onDismiss} />
59 ) : null}
60 </View>
61 {action && !inlineAction ? <View className="mt-2 self-end">{action}</View> : null}
62 </GlassSurface>
63 );