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

Notice.tsx

85 lines2.5 KB

1import { useEffect } from "react";
2import { AccessibilityInfo, Platform, Pressable, View } from "react-native";
3​
4import { cn } from "../../lib/cn";
5import { AppText } from "./AppText";
6import { SymbolView, type AppSymbolName } from "./AppSymbol";
7import { GlassSurface } from "./GlassSurface";
8​
9export function Notice(props: {
10 readonly message: string;
11 readonly tone?: "default" | "danger";
12 readonly icon?: AppSymbolName;
13 readonly iconTone?: "default" | "danger";
14 readonly floating?: boolean;
15 readonly onDismiss?: () => void;
16 readonly dismissLabel?: string;
17}) {
18 const danger = props.tone === "danger";
19 useEffect(() => {
20 // Live regions reach TalkBack; VoiceOver needs an explicit announcement.
21 if (Platform.OS === "ios") AccessibilityInfo.announceForAccessibility(props.message);
22 }, [props.message]);
23​
24 const content = (
25 <View className="flex-row items-start gap-3">
26 {props.icon ? (
27 <View className="pt-0.5">
28 <SymbolView
29 name={props.icon}
30 size={16}
31 tintColorClassName={
32 danger || props.iconTone === "danger"
33 ? "accent-danger-foreground"
34 : "accent-icon-muted"
35 }
36 />
37 </View>
38 ) : null}
39 <AppText
40 selectable
41 textRole="footnote"
42 accessibilityLiveRegion="polite"
43 className={cn(
44 "min-w-0 flex-1",
45 danger ? "text-danger-foreground" : "text-foreground",
46 !props.floating && "font-t3-medium",
47 )}
48 >
49 {props.message}
50 </AppText>
51 {props.onDismiss ? (
52 <Pressable
53 accessibilityRole="button"
54 accessibilityLabel={props.dismissLabel ?? "Dismiss message"}
55 onPress={props.onDismiss}
56 hitSlop={10}
57 className="min-h-6 min-w-6 items-center justify-center active:opacity-60"
58 >
59 <SymbolView
60 name="xmark"
61 size={14}
62 tintColorClassName={danger ? "accent-danger-foreground" : "accent-icon-muted"}
63 />
64 </Pressable>
65 ) : null}
66 </View>
67 );
68 if (props.floating && Platform.OS === "ios")
69 return (
70 <GlassSurface className="p-4" style={{ borderRadius: 20 }}>
71 {content}
72 </GlassSurface>
73 );
74 return (
75 <View
76 className={cn(
77 "rounded-2xl border px-3.5 py-3",
78 danger ? "border-danger-border bg-danger" : "border-border bg-card",
79 props.floating && "p-4",
80 )}
81 >
82 {content}
83 </View>
84 );