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