Open History to see the last commit
1import {
2 Box,
3 ExtendedFloatingActionButton,
4 FloatingActionButton,
5 Host,
6 LargeFloatingActionButton,
7 Text,
8} from "@expo/ui/jetpack-compose";
9import { size } from "@expo/ui/jetpack-compose/modifiers";
10import { View, type StyleProp, type ViewStyle } from "react-native";
11import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
12import { useScaledTextRole } from "../features/settings/appearance/useScaledTextRole";
13import { SymbolView, type AppSymbolName } from "./AppSymbol";
14
15export function MaterialFloatingActionButton(props: {
16 readonly onPress: () => void;
17 readonly label: string;
18 readonly icon: AppSymbolName;
19 readonly variant?: "extended" | "large";
20 readonly expanded?: boolean;
21 readonly tone?: "primary" | "secondary";
22 readonly className?: string;
23 readonly style?: StyleProp<ViewStyle>;
24}) {
25 const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
26 const typography = useScaledTextRole("footnote");
27 const primary = props.tone === "primary";
28 const containerColor = colors[primary ? "--color-primary" : "--color-secondary"];
29 const contentColor =
30 colors[primary ? "--color-primary-foreground" : "--color-secondary-foreground"];
31 const Component =
32 props.variant === "extended"
33 ? ExtendedFloatingActionButton
34 : props.variant === "large"
35 ? LargeFloatingActionButton
36 : FloatingActionButton;
37 const iconSize = props.variant === "large" ? 36 : 24;
38 return (
39 <View
40 accessible
41 accessibilityRole="button"
42 accessibilityLabel={props.label}
43 accessibilityActions={[{ name: "activate" }]}
44 onAccessibilityAction={props.onPress}
45 className={props.className}
46 style={props.style}
47 >
48 <View importantForAccessibility="no-hide-descendants">
49 <Host matchContents colorScheme={themeAppearance} ignoreSafeAreaKeyboardInsets>
50 <Component
51 containerColor={containerColor}
52 onClick={props.onPress}
53 expanded={props.expanded}
54 >
55 <Component.Icon>
56 <Box modifiers={[size(iconSize, iconSize)]} />
57 </Component.Icon>
58 {props.variant === "extended" ? (
59 <ExtendedFloatingActionButton.Text>
60 <Text color={contentColor} style={{ ...typography, fontWeight: "500" }}>
61 {props.label}
62 </Text>
63 </ExtendedFloatingActionButton.Text>
64 ) : null}
65 </Component>
66 </Host>
67 </View>
68 {/* The RN icon stays outside Compose so it cannot intercept native button taps. */}
69 <View
70 pointerEvents="none"
71 className="absolute inset-y-0 justify-center"
72 style={
73 props.variant === "extended" ? { left: 16 } : { left: 0, right: 0, alignItems: "center" }
74 }
75 >
76 <SymbolView
77 name={props.icon}
78 size={iconSize}
79 tintColorClassName={primary ? "accent-primary-foreground" : "accent-secondary-foreground"}
80 />
81 </View>
82 </View>
83 );
84}