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 : variant === "danger"
76 ? "text-danger-foreground"
77 : "text-foreground",
78 );
79
80 if (
81 Platform.OS === "android" &&
82 (variant === "pill" || variant === "primary") &&
83 props.label &&
84 props.onPress &&
85 !props.icon &&
86 !props.iconNode &&
87 !props.className &&
88 !props.activateOnPressIn &&
89 (!props.accessibilityLabel || props.accessibilityLabel === props.label)
90 ) {
91 return (
92 <MaterialButton
93 label={props.label}
94 onPress={props.onPress}
95 disabled={props.disabled}
96 tone={variant === "primary" ? "primary" : "secondary"}
97 />
98 );
99 }
100
101 if (
102 Platform.OS === "android" &&
103 props.accessibilityLabel &&
104 props.icon &&
105 !props.iconNode &&
106 !props.label &&
107 !props.className &&
108 !props.activateOnPressIn
109 ) {
110 return (
111 <MaterialIconButton
112 accessibilityLabel={props.accessibilityLabel}
113 icon={props.icon}
114 onPress={props.onPress}
115 disabled={props.disabled}
116 variant={variant === "primary" ? "primary" : variant === "danger" ? "danger" : "tonal"}
117 />
118 );
119 }
120
121 return (
122 <Pressable
123 accessibilityLabel={props.accessibilityLabel ?? props.label}
124 accessibilityRole="button"
125 onPress={props.activateOnPressIn ? handlePress : props.onPress}
126 onPressIn={props.activateOnPressIn ? handlePressIn : undefined}
127 onPressOut={props.activateOnPressIn ? handlePressOut : undefined}
128 disabled={props.disabled}
129 className={containerClassName}
130 >
131 {props.iconNode ? (
132 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
133 ) : props.icon ? (
134 <SymbolView
135 name={props.icon}
136 size={16}
137 tintColorClassName={iconTintClassName}
138 type="monochrome"
139 />
140 ) : null}
141 {props.label ? <Text className={labelClassName}>{props.label}</Text> : null}
142 </Pressable>
143 );
144}