Open History to see the last commit
1import { type ComponentProps, type ReactNode, useRef } from "react";
2import { Platform, Pressable, View } from "react-native";
3import { cn } from "../lib/cn";
4import { SymbolView } from "./AppSymbol";
5import { AppText as Text } from "./AppText";
6import { MaterialIconButton } from "./MaterialIconButton";
7import { MaterialButton } from "./MaterialButton";
8
9export { ControlPillMenu } from "./ControlPillMenu";
10
11export function ControlPill(props: {
12 readonly icon?: ComponentProps<typeof SymbolView>["name"];
13 readonly iconNode?: ReactNode;
14 readonly label?: string;
15 readonly accessibilityLabel?: string;
16 readonly onPress?: () => void;
17 readonly activateOnPressIn?: boolean;
18 readonly variant?: "circle" | "pill" | "primary" | "danger";
19 readonly disabled?: boolean;
20 readonly className?: string;
21}) {
22 const variant = props.variant ?? "circle";
23 const activatedOnPressInRef = useRef(false);
24
25 const handlePressIn = () => {
26 activatedOnPressInRef.current = true;
27 props.onPress?.();
28 };
29 const handlePressOut = () => {
30 // Pressability invokes onPressOut immediately before onPress on release.
31 // Defer the reset so onPress can identify the same physical gesture.
32 setTimeout(() => {
33 activatedOnPressInRef.current = false;
34 }, 0);
35 };
36 const handlePress = () => {
37 if (activatedOnPressInRef.current) {
38 return;
39 }
40 props.onPress?.();
41 };
42
43 const iconTintClassName =
44 variant === "primary"
45 ? props.disabled
46 ? "accent-icon-subtle"
47 : "accent-primary-foreground"
48 : variant === "danger"
49 ? "accent-danger-foreground"
50 : "accent-icon";
51
52 const isCircle =
53 variant === "circle" || variant === "danger" || (variant === "primary" && !props.label);
54 const containerClassName = cn(
55 isCircle
56 ? "h-11 w-11 items-center justify-center rounded-full"
57 : variant === "primary"
58 ? "h-11 flex-row items-center justify-center gap-2 rounded-full px-5"
59 : "h-11 flex-row items-center justify-center gap-2 rounded-full px-3.5",
60 variant === "primary"
61 ? props.disabled
62 ? "bg-subtle-strong"
63 : "bg-primary"
64 : variant === "danger"
65 ? "bg-danger"
66 : "bg-subtle",
67 props.className,
68 );
69 const labelClassName = cn(
70 "text-center text-xs font-t3-bold",
71 variant === "primary"
72 ? props.disabled
73 ? "text-foreground-muted"
74 : "text-primary-foreground"
75 : "",
76 );
77
78 if (
79 Platform.OS === "android" &&
80 (variant === "pill" || variant === "primary") &&
81 props.label &&
82 props.onPress &&
83 !props.icon &&
84 !props.iconNode &&
85 !props.className &&
86 !props.activateOnPressIn &&
87 (!props.accessibilityLabel || props.accessibilityLabel === props.label)
88 ) {
89 return (
90 <MaterialButton
91 label={props.label}
92 onPress={props.onPress}
93 disabled={props.disabled}
94 tone={variant === "primary" ? "primary" : "secondary"}
95 />
96 );
97 }
98
99 if (
100 Platform.OS === "android" &&
101 props.accessibilityLabel &&
102 props.icon &&
103 !props.iconNode &&
104 !props.label &&
105 !props.className &&
106 !props.activateOnPressIn
107 ) {
108 return (
109 <MaterialIconButton
110 accessibilityLabel={props.accessibilityLabel}
111 icon={props.icon}
112 onPress={props.onPress}
113 disabled={props.disabled}
114 variant={variant === "primary" ? "primary" : variant === "danger" ? "danger" : "tonal"}
115 />
116 );
117 }
118
119 return (
120 <Pressable
121 accessibilityLabel={props.accessibilityLabel ?? props.label}
122 accessibilityRole="button"
123 onPress={props.activateOnPressIn ? handlePress : props.onPress}
124 onPressIn={props.activateOnPressIn ? handlePressIn : undefined}
125 onPressOut={props.activateOnPressIn ? handlePressOut : undefined}
126 disabled={props.disabled}
127 className={containerClassName}
128 >
129 {props.iconNode ? (
130 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
131 ) : props.icon ? (
132 <SymbolView
133 name={props.icon}
134 size={16}
135 tintColorClassName={iconTintClassName}
136 type="monochrome"
137 />
138 ) : null}
139 {props.label ? <Text className={labelClassName}>{props.label}</Text> : null}
140 </Pressable>
141 );
142}