Open History to see the last commit
1import {
2 Box,
3 Button,
4 CircularProgressIndicator,
5 FilledTonalButton,
6 Host,
7 Row,
8 Text,
9 TextButton,
10} from "@expo/ui/jetpack-compose";
11import { defaultMinSize, fillMaxWidth, size } from "@expo/ui/jetpack-compose/modifiers";
12import { View } from "react-native";
13
14import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
15import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
16import type { MaterialButtonProps } from "./MaterialButton";
17
18export function MaterialButton(props: MaterialButtonProps) {
19 const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
20 const typography = useScaledTextRole("footnote");
21 const tone = props.tone ?? "secondary";
22 const Component =
23 tone === "text" ? TextButton : tone === "secondary" ? FilledTonalButton : Button;
24 const containerColor =
25 tone === "primary"
26 ? colors["--color-primary"]
27 : tone === "danger"
28 ? colors["--color-danger"]
29 : tone === "text"
30 ? "#00000000"
31 : colors["--color-secondary"];
32 const contentColor =
33 tone === "primary"
34 ? colors["--color-primary-foreground"]
35 : tone === "danger"
36 ? colors["--color-danger-foreground"]
37 : tone === "text"
38 ? colors["--color-primary-text"]
39 : colors["--color-secondary-foreground"];
40 return (
41 <View
42 accessible
43 accessibilityRole="button"
44 accessibilityLabel={props.label}
45 accessibilityState={{
46 disabled: Boolean(props.disabled || props.loading),
47 busy: Boolean(props.loading),
48 }}
49 accessibilityActions={[{ name: "activate" }]}
50 onAccessibilityAction={() => {
51 if (!props.disabled && !props.loading) props.onPress();
52 }}
53 style={props.fullWidth ? { width: "100%" } : { alignSelf: "flex-start" }}
54 >
55 <View importantForAccessibility="no-hide-descendants">
56 <Host
57 matchContents={props.fullWidth ? { vertical: true } : true}
58 colorScheme={themeAppearance}
59 ignoreSafeAreaKeyboardInsets
60 style={props.fullWidth ? { width: "100%" } : { alignSelf: "flex-start" }}
61 >
62 <Component
63 enabled={!props.disabled && !props.loading}
64 onClick={props.onPress}
65 modifiers={[
66 defaultMinSize({ minHeight: 48 }),
67 ...(props.fullWidth ? [fillMaxWidth()] : []),
68 ]}
69 colors={{
70 containerColor,
71 contentColor,
72 disabledContainerColor: colors["--color-subtle-strong"],
73 disabledContentColor: colors["--color-foreground-muted"],
74 }}
75 >
76 <Row verticalAlignment="center">
77 {props.loading ? (
78 <>
79 <CircularProgressIndicator
80 modifiers={[size(18, 18)]}
81 strokeWidth={2}
82 color={colors["--color-foreground-muted"]}
83 />
84 <Box modifiers={[size(8, 1)]} />
85 </>
86 ) : null}
87 <Text style={{ ...typography, fontWeight: "500" }}>{props.label}</Text>
88 </Row>
89 </Component>
90 </Host>
91 </View>
92 </View>
93 );
94}