Open History to see the last commit
1import { SymbolView, type AppSymbolName } from "./AppSymbol";
2import { Pressable } from "react-native";
3import { AppActivityIndicator } from "./AppActivityIndicator";
4import { AppText } from "./AppText";
5import { cn } from "../../lib/cn";
6
7export interface ButtonProps {
8 readonly label: string;
9 readonly accessibilityLabel?: string;
10 readonly icon?: AppSymbolName;
11 readonly onPress: () => void;
12 readonly disabled?: boolean;
13 readonly accessibilityHint?: string;
14 readonly loading?: boolean;
15 readonly tone?: "primary" | "secondary" | "danger" | "text" | "contrast";
16 readonly fullWidth?: boolean;
17}
18
19const TONE_CLASS_NAMES = {
20 contrast: ["bg-foreground", "text-screen"],
21 primary: ["bg-primary", "text-primary-foreground"],
22 secondary: ["bg-secondary", "text-secondary-foreground"],
23 danger: ["bg-danger", "text-danger-foreground"],
24 text: ["bg-transparent", "text-primary-text"],
25} as const;
26
27export function Button(props: ButtonProps) {
28 const [containerClassName, labelClassName] = TONE_CLASS_NAMES[props.tone ?? "secondary"];
29 const disabled = Boolean(props.disabled || props.loading);
30 return (
31 <Pressable
32 accessibilityRole="button"
33 accessibilityLabel={props.accessibilityLabel ?? props.label}
34 accessibilityHint={props.accessibilityHint}
35 accessibilityState={{ disabled, busy: Boolean(props.loading) }}
36 disabled={disabled}
37 onPress={props.onPress}
38 className={cn(
39 "min-h-12 flex-row items-center justify-center gap-2 rounded-full px-6 active:opacity-70",
40 props.fullWidth ? "w-full" : "self-start",
41 disabled ? "bg-subtle-strong" : containerClassName,
42 )}
43 >
44 {props.loading ? (
45 <AppActivityIndicator colorClassName="accent-foreground-muted" />
46 ) : props.icon ? (
47 <SymbolView
48 name={props.icon}
49 size={18}
50 tintColorClassName={
51 disabled ? "accent-foreground-muted" : labelClassName.replace("text-", "accent-")
52 }
53 />
54 ) : null}
55 <AppText
56 className={cn(
57 "min-w-0 shrink text-center font-t3-medium",
58 disabled ? "text-foreground-muted" : labelClassName,
59 )}
60 >
61 {props.label}
62 </AppText>
63 </Pressable>
64 );
65}