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

IconButton.android.tsx

82 lines2.8 KB

1import type { IconButtonProps } from "./IconButton.types";
2import { NativeThemeHost } from "./NativeThemeHost.android";
3import {
4 FilledIconButton,
5 FilledTonalIconButton,
6 IconButton as ComposeIconButton,
7} from "@expo/ui/jetpack-compose";
8import { size } from "@expo/ui/jetpack-compose/modifiers";
9import { View } from "react-native";
10​
11import { useControlTheme } from "./useControlTheme";
12import { useAndroidControlSizing } from "./useAndroidControlSizing";
13import { SymbolView } from "./AppSymbol";
14​
15export function IconButton(props: IconButtonProps) {
16 const { colors } = useControlTheme();
17 const { iconSize, buttonSize } = useAndroidControlSizing();
18 const variant = props.variant ?? "standard";
19 const Component =
20 variant === "standard"
21 ? ComposeIconButton
22 : variant === "tonal"
23 ? FilledTonalIconButton
24 : FilledIconButton;
25 const containerColor =
26 variant === "primary"
27 ? colors["--color-primary"]
28 : variant === "danger"
29 ? colors["--color-danger"]
30 : colors["--color-secondary"];
31 const iconTint = props.disabled
32 ? "accent-icon-subtle"
33 : variant === "primary"
34 ? "accent-primary-foreground"
35 : variant === "danger"
36 ? "accent-danger-foreground"
37 : variant === "tonal"
38 ? "accent-secondary-foreground"
39 : (props.tintColorClassName ?? "accent-foreground");
40 return (
41 <View
42 accessible
43 accessibilityLabel={props.accessibilityLabel}
44 accessibilityRole="button"
45 accessibilityState={{ disabled: Boolean(props.disabled), selected: props.selected }}
46 accessibilityActions={[{ name: "activate" }]}
47 onAccessibilityAction={() => {
48 if (!props.disabled) props.onPress?.();
49 }}
50 style={{ width: buttonSize, height: buttonSize }}
51 >
52 <View importantForAccessibility="no-hide-descendants">
53 <NativeThemeHost
54 ignoreSafeAreaKeyboardInsets
55 style={{ width: buttonSize, height: buttonSize }}
56 >
57 <Component
58 onClick={props.onPress}
59 enabled={!props.disabled}
60 modifiers={[size(buttonSize, buttonSize)]}
61 colors={
62 variant === "standard"
63 ? undefined
64 : { containerColor, disabledContainerColor: colors["--color-subtle-strong"] }
65 }
66 >
67 {null}
68 </Component>
69 </NativeThemeHost>
70 </View>
71 {/* Keep RN SVG measurement outside Compose; the native button owns touch and ripple. */}
72 <View pointerEvents="none" className="absolute inset-0 items-center justify-center">
73 <SymbolView
74 name={props.icon === "ellipsis" ? { ios: "ellipsis", android: "more_vert" } : props.icon}
75 size={iconSize}
76 tintColorClassName={iconTint}
77 type="monochrome"
78 />
79 </View>
80 </View>
81 );