Open History to see the last commit
1import type { ComponentProps, ReactNode } from "react";
2import { useCallback, useMemo, useState } from "react";
3import {
4 Pressable,
5 ScrollView,
6 View,
7 type LayoutChangeEvent,
8 type NativeScrollEvent,
9 type NativeSyntheticEvent,
10 type StyleProp,
11 type ViewStyle,
12} from "react-native";
13
14import { useThemeColor } from "../lib/useThemeColor";
15import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
16import { themeColorWithAlpha } from "../lib/mobileTheme";
17import { cn } from "../lib/cn";
18import { AppText as Text } from "./AppText";
19import { SymbolView } from "./AppSymbol";
20
21const COMPOSER_TOOLBAR_GAP = 8;
22const COMPOSER_TOOLBAR_FADE_WIDTH = 18;
23const COMPOSER_TOOLBAR_SCROLL_EPSILON = 4;
24
25/**
26 * Quiet inline composer control used inside cards and their context rows.
27 * Unlike ComposerToolbarButton, this does not draw another pill inside the
28 * composer surface, so model and workspace controls read as part of the card.
29 */
30export function ComposerInlineControl(props: {
31 readonly accessibilityHint?: string;
32 readonly accessibilityLabel?: string;
33 readonly disabled?: boolean;
34 readonly emphasized?: boolean;
35 readonly icon?: ComponentProps<typeof SymbolView>["name"];
36 readonly iconNode?: ReactNode;
37 readonly label: string;
38 readonly maxWidth?: number;
39 readonly onPress?: () => void;
40 readonly selected?: boolean;
41 readonly static?: boolean;
42 readonly chevronDirection?: "down" | "right";
43 readonly showChevron?: boolean;
44}) {
45 const iconColor = useThemeColor(
46 props.emphasized || props.selected ? "--color-icon" : "--color-icon-muted",
47 );
48
49 return (
50 <Pressable
51 accessibilityLabel={props.accessibilityLabel ?? props.label}
52 accessibilityHint={props.accessibilityHint}
53 accessibilityRole={props.static ? undefined : "button"}
54 accessibilityState={
55 props.static ? undefined : { disabled: props.disabled, selected: props.selected }
56 }
57 className="h-11 flex-row items-center gap-2 rounded-xl px-2 active:bg-subtle"
58 disabled={props.disabled || props.static}
59 onPress={props.onPress}
60 style={{ maxWidth: props.maxWidth ?? 190, opacity: props.disabled ? 0.45 : 1 }}
61 >
62 {props.iconNode ? (
63 <View className="size-4 shrink-0 items-center justify-center">{props.iconNode}</View>
64 ) : props.icon ? (
65 <SymbolView name={props.icon} size={16} tintColor={iconColor} type="monochrome" />
66 ) : null}
67 <Text
68 className={cn(
69 "shrink text-sm font-t3-medium",
70 props.emphasized || props.selected ? "text-foreground" : "text-foreground-muted",
71 )}
72 numberOfLines={1}
73 >
74 {props.label}
75 </Text>
76 {props.showChevron === false ? null : (
77 <SymbolView
78 name={props.chevronDirection === "right" ? "chevron.right" : "chevron.down"}
79 size={10}
80 tintColor={iconColor}
81 type="monochrome"
82 />
83 )}
84 </Pressable>
85 );
86}
87
88export function ComposerToolbarRow(props: {
89 readonly children: ReactNode;
90 readonly paddingBottom?: number;
91 readonly paddingHorizontal?: number;
92 readonly paddingTop?: number;
93 readonly style?: StyleProp<ViewStyle>;
94}) {
95 return (
96 <View
97 className="flex-row items-center gap-1.5"
98 style={[
99 {
100 paddingBottom: props.paddingBottom ?? 8,
101 paddingHorizontal: props.paddingHorizontal ?? 6,
102 paddingTop: props.paddingTop ?? 8,
103 },
104 props.style,
105 ]}
106 >
107 {props.children}
108 </View>
109 );
110}
111
112export function ComposerToolbarScroller(props: {
113 readonly children: ReactNode;
114 readonly fadeOpaque: string;
115 readonly fadeTransparent: string;
116 readonly contentPaddingRight?: number;
117}) {
118 const [metrics, setMetrics] = useState({
119 contentWidth: 0,
120 offsetX: 0,
121 viewportWidth: 0,
122 });
123
124 const scrollEdges = useMemo(() => {
125 const maxOffset = Math.max(0, metrics.contentWidth - metrics.viewportWidth);
126 return {
127 showLeftFade: metrics.offsetX > COMPOSER_TOOLBAR_SCROLL_EPSILON,
128 showRightFade: metrics.offsetX < maxOffset - COMPOSER_TOOLBAR_SCROLL_EPSILON,
129 };
130 }, [metrics]);
131
132 const handleLayout = useCallback((event: LayoutChangeEvent) => {
133 const viewportWidth = event.nativeEvent.layout.width;
134 setMetrics((current) =>
135 current.viewportWidth === viewportWidth ? current : { ...current, viewportWidth },
136 );
137 }, []);
138
139 const handleContentSizeChange = useCallback((contentWidth: number) => {
140 setMetrics((current) =>
141 current.contentWidth === contentWidth ? current : { ...current, contentWidth },
142 );
143 }, []);
144
145 const handleScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
146 const offsetX = event.nativeEvent.contentOffset.x;
147 setMetrics((current) =>
148 Math.abs(current.offsetX - offsetX) < 1 ? current : { ...current, offsetX },
149 );
150 }, []);
151
152 return (
153 <View className="relative min-w-0 flex-1">
154 <ScrollView
155 horizontal
156 keyboardShouldPersistTaps="always"
157 onContentSizeChange={handleContentSizeChange}
158 onLayout={handleLayout}
159 onScroll={handleScroll}
160 scrollEventThrottle={16}
161 showsHorizontalScrollIndicator={false}
162 contentContainerStyle={{
163 alignItems: "center",
164 gap: COMPOSER_TOOLBAR_GAP,
165 paddingLeft: 0,
166 paddingRight: props.contentPaddingRight ?? 1,
167 }}
168 >
169 {props.children}
170 </ScrollView>
171 {scrollEdges.showLeftFade ? (
172 <View
173 pointerEvents="none"
174 style={{
175 bottom: 0,
176 experimental_backgroundImage: `linear-gradient(to right, ${props.fadeOpaque} 0%, ${props.fadeTransparent} 100%)`,
177 left: 0,
178 position: "absolute",
179 top: 0,
180 width: COMPOSER_TOOLBAR_FADE_WIDTH,
181 }}
182 />
183 ) : null}
184 {scrollEdges.showRightFade ? (
185 <View
186 pointerEvents="none"
187 style={{
188 bottom: 0,
189 experimental_backgroundImage: `linear-gradient(to right, ${props.fadeTransparent} 0%, ${props.fadeOpaque} 100%)`,
190 position: "absolute",
191 right: 0,
192 top: 0,
193 width: COMPOSER_TOOLBAR_FADE_WIDTH,
194 }}
195 />
196 ) : null}
197 </View>
198 );
199}
200
201export function ComposerToolbarButton(props: {
202 readonly icon?: ComponentProps<typeof SymbolView>["name"];
203 readonly iconNode?: ReactNode;
204 readonly label?: string;
205 readonly accessibilityLabel?: string;
206 readonly active?: boolean;
207 readonly disabled?: boolean;
208 readonly maxWidth?: number;
209 readonly minWidth?: number;
210 readonly onPress?: () => void;
211 readonly showChevron?: boolean;
212 readonly textTransform?: "none" | "uppercase";
213 readonly variant?: "default" | "primary" | "danger";
214 readonly className?: string;
215 readonly style?: StyleProp<ViewStyle>;
216}) {
217 const { themeAppearance } = useAppearancePreferences();
218 const isDarkMode = themeAppearance === "dark";
219 const iconColor = useThemeColor("--color-icon");
220 const iconSubtle = useThemeColor("--color-icon-subtle");
221 const primaryFg = useThemeColor("--color-primary-foreground");
222 const dangerFg = useThemeColor("--color-danger-foreground");
223 const variant = props.variant ?? "default";
224 const isCircle = !props.label && props.showChevron === false;
225 const defaultBorderColor = useThemeColor("--color-border-subtle");
226 const activeBorderColor = useThemeColor("--color-border");
227 const filledBorderColor =
228 variant === "danger"
229 ? themeColorWithAlpha(String(dangerFg), 0.14)
230 : props.disabled
231 ? defaultBorderColor
232 : themeColorWithAlpha(String(primaryFg), 0.18);
233 const iconTintColor =
234 variant === "primary"
235 ? props.disabled
236 ? iconSubtle
237 : primaryFg
238 : variant === "danger"
239 ? dangerFg
240 : iconColor;
241
242 return (
243 <Pressable
244 accessibilityLabel={props.accessibilityLabel ?? props.label}
245 accessibilityRole="button"
246 disabled={props.disabled}
247 onPress={props.onPress}
248 className={cn(
249 // Default width cap lives in the class chain (not the inline style)
250 // so callers can lift it with max-w-full — flex-filling pills in the
251 // thread composer stretch to the row's edge. The numeric maxWidth
252 // prop still wins via the inline style below.
253 "h-11 max-w-[172px] flex-row items-center justify-center rounded-full active:opacity-70",
254 isCircle ? "w-11" : "gap-2 px-3.5",
255 variant === "primary"
256 ? props.disabled
257 ? "bg-subtle-strong"
258 : "bg-primary"
259 : variant === "danger"
260 ? "bg-danger"
261 : props.active
262 ? "bg-subtle-strong"
263 : "bg-subtle",
264 props.className,
265 )}
266 style={({ pressed }) => [
267 {
268 borderColor:
269 variant === "default"
270 ? props.active
271 ? activeBorderColor
272 : defaultBorderColor
273 : filledBorderColor,
274 borderWidth: 1,
275 maxWidth: props.maxWidth,
276 minWidth: props.minWidth,
277 opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1,
278 shadowColor: "#000",
279 shadowOffset: { width: 0, height: isDarkMode ? 3 : 2 },
280 shadowOpacity: props.disabled ? 0 : isDarkMode ? 0.24 : 0.08,
281 shadowRadius: isDarkMode ? 10 : 8,
282 },
283 props.style,
284 ]}
285 >
286 {props.iconNode ? (
287 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
288 ) : props.icon ? (
289 <SymbolView name={props.icon} size={16} tintColor={iconTintColor} type="monochrome" />
290 ) : null}
291 {props.label ? (
292 <Text
293 className={cn(
294 "shrink text-center text-sm font-t3-bold",
295 variant === "primary"
296 ? props.disabled
297 ? "text-foreground-muted"
298 : "text-primary-foreground"
299 : "text-foreground",
300 )}
301 ellipsizeMode="tail"
302 numberOfLines={1}
303 style={{ textTransform: props.textTransform ?? "none" }}
304 >
305 {props.label}
306 </Text>
307 ) : null}
308 {props.showChevron === false ? null : (
309 <SymbolView name="chevron.down" size={11} tintColor={iconTintColor} type="monochrome" />
310 )}
311 </Pressable>
312 );
313}