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

Toast.tsx

61 lines2.2 KB

1import { MOBILE_SHAPE } from "../../lib/designTokens";
2import { useEffect } from "react";
3import { AccessibilityInfo, Platform, View } 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​
21/** The owning screen places and dismisses feedback; actions are never implicit. */
22export function Toast(props: ToastProps) {
23 useEffect(() => {
24 if (Platform.OS === "ios") AccessibilityInfo.announceForAccessibility(props.message);
25 }, [props.message]);
26 return (
27 <GlassSurface className="p-3" style={{ borderRadius: MOBILE_SHAPE.toast }}>
28 <View className="flex-row items-center gap-3">
29 {props.busy ? (
30 <AppActivityIndicator />
31 ) : (
32 <SymbolView
33 name={props.tone === "danger" ? "exclamationmark.triangle" : "checkmark.circle"}
34 size={20}
35 tintColorClassName={
36 props.tone === "danger" ? "accent-danger-foreground" : "accent-icon"
37 }
38 />
39 )}
40 <View className="min-w-0 flex-1" accessibilityLiveRegion="polite">
41 <AppText textRole="footnote" className="font-t3-semibold">
42 {props.message}
43 </AppText>
44 {props.description ? (
45 <AppText textRole="label" className="text-foreground-muted">
46 {props.description}
47 </AppText>
48 ) : null}
49 </View>
50 {props.onDismiss ? (
51 <IconButton accessibilityLabel="Dismiss message" icon="xmark" onPress={props.onDismiss} />
52 ) : null}
53 </View>
54 {props.action ? (
55 <View className="mt-2 self-end">
56 <Button label={props.action.label} onPress={props.action.onPress} />
57 </View>
58 ) : null}
59 </GlassSurface>
60 );