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 { cn } from "../lib/cn";
15import { AppText as Text } from "./AppText";
16import { SymbolView } from "./AppSymbol";
17
18const COMPOSER_TOOLBAR_GAP = 8;
19const COMPOSER_TOOLBAR_FADE_WIDTH = 18;
20const COMPOSER_TOOLBAR_SCROLL_EPSILON = 4;
21
22/**
23 * Quiet inline composer control used inside cards and their context rows.
24 * Unlike ComposerToolbarButton, this does not draw another pill inside the
25 * composer surface, so model and workspace controls read as part of the card.
26 */
27export function ComposerInlineControl(props: {
28 readonly accessibilityHint?: string;
29 readonly accessibilityLabel?: string;
30 readonly disabled?: boolean;
31 readonly emphasized?: boolean;
32 readonly icon?: ComponentProps<typeof SymbolView>["name"];
33 readonly iconNode?: ReactNode;
34 readonly label: string;
35 readonly maxWidth?: ViewStyle["maxWidth"];
36 readonly onPress?: () => void;
37 readonly selected?: boolean;
38 readonly static?: boolean;
39 readonly chevronDirection?: "down" | "right";
40 readonly showChevron?: boolean;
41}) {
42 return (
43 <Pressable
44 accessibilityLabel={props.accessibilityLabel ?? props.label}
45 accessibilityHint={props.accessibilityHint}
46 accessibilityRole={props.static ? undefined : "button"}
47 accessibilityState={
48 props.static ? undefined : { disabled: props.disabled, selected: props.selected }
49 }
50 className="h-11 flex-row items-center gap-2 rounded-xl px-2 active:bg-subtle"
51 disabled={props.disabled || props.static}
52 onPress={props.onPress}
53 style={{ maxWidth: props.maxWidth ?? 190, opacity: props.disabled ? 0.45 : 1 }}
54 >
55 {props.iconNode ? (
56 <View className="size-4 shrink-0 items-center justify-center">{props.iconNode}</View>
57 ) : props.icon ? (
58 <SymbolView
59 name={props.icon}
60 size={16}
61 tintColorClassName={
62 props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted"
63 }
64 type="monochrome"
65 />
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 tintColorClassName={
81 props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted"
82 }
83 type="monochrome"
84 />
85 )}
86 </Pressable>
87 );
88}
89
90export function ComposerToolbarRow(props: {
91 readonly children: ReactNode;
92 readonly paddingBottom?: number;
93 readonly paddingHorizontal?: number;
94 readonly paddingTop?: number;
95 readonly style?: StyleProp<ViewStyle>;
96}) {
97 return (
98 <View
99 className="flex-row items-center gap-1.5"
100 style={[
101 {
102 paddingBottom: props.paddingBottom ?? 8,
103 paddingHorizontal: props.paddingHorizontal ?? 6,
104 paddingTop: props.paddingTop ?? 8,
105 },
106 props.style,
107 ]}
108 >
109 {props.children}
110 </View>
111 );
112}
113
114export function ComposerToolbarScroller(props: {
115 readonly children: ReactNode;
116 readonly align?: "start" | "end";
117 /** Only for non-Uniwind surfaces such as the native terminal palette. */
118 readonly fadeOpaque?: string;
119 /** Only for non-Uniwind surfaces such as the native terminal palette. */
120 readonly fadeTransparent?: string;
121 /** Semantic Uniwind surface behind the toolbar. Defaults to card. */
122 readonly fadeSurface?: "card" | "sheet";
123 readonly contentPaddingRight?: number;
124}) {
125 const [metrics, setMetrics] = useState({
126 contentWidth: 0,
127 offsetX: 0,
128 viewportWidth: 0,
129 });
130
131 const scrollEdges = useMemo(() => {
132 const maxOffset = Math.max(0, metrics.contentWidth - metrics.viewportWidth);
133 return {
134 showLeftFade: metrics.offsetX > COMPOSER_TOOLBAR_SCROLL_EPSILON,
135 showRightFade: metrics.offsetX < maxOffset - COMPOSER_TOOLBAR_SCROLL_EPSILON,
136 };
137 }, [metrics]);
138
139 const handleLayout = useCallback((event: LayoutChangeEvent) => {
140 const viewportWidth = event.nativeEvent.layout.width;
141 setMetrics((current) =>
142 current.viewportWidth === viewportWidth ? current : { ...current, viewportWidth },
143 );
144 }, []);
145
146 const handleContentSizeChange = useCallback((contentWidth: number) => {
147 setMetrics((current) =>
148 current.contentWidth === contentWidth ? current : { ...current, contentWidth },
149 );
150 }, []);
151
152 const handleScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
153 const offsetX = event.nativeEvent.contentOffset.x;
154 setMetrics((current) =>
155 Math.abs(current.offsetX - offsetX) < 1 ? current : { ...current, offsetX },
156 );
157 }, []);
158
159 return (
160 <View className="relative min-w-0 flex-1">
161 <ScrollView
162 horizontal
163 keyboardShouldPersistTaps="always"
164 onContentSizeChange={handleContentSizeChange}
165 onLayout={handleLayout}
166 onScroll={handleScroll}
167 scrollEventThrottle={16}
168 showsHorizontalScrollIndicator={false}
169 contentContainerStyle={{
170 alignItems: "center",
171 flexGrow: props.align === "end" ? 1 : undefined,
172 justifyContent: props.align === "end" ? "flex-end" : undefined,
173 gap: COMPOSER_TOOLBAR_GAP,
174 paddingLeft: 0,
175 paddingRight: props.contentPaddingRight ?? 1,
176 }}
177 >
178 {props.children}
179 </ScrollView>
180 {scrollEdges.showLeftFade ? (
181 <View
182 className={cn(
183 "absolute inset-y-0 left-0",
184 props.fadeOpaque === undefined && "bg-linear-to-r to-transparent",
185 props.fadeOpaque === undefined &&
186 (props.fadeSurface === "sheet" ? "from-sheet" : "from-card"),
187 )}
188 pointerEvents="none"
189 style={{
190 width: COMPOSER_TOOLBAR_FADE_WIDTH,
191 experimental_backgroundImage:
192 props.fadeOpaque === undefined
193 ? undefined
194 : `linear-gradient(to right, ${props.fadeOpaque} 0%, ${props.fadeTransparent ?? "transparent"} 100%)`,
195 }}
196 />
197 ) : null}
198 {scrollEdges.showRightFade ? (
199 <View
200 className={cn(
201 "absolute inset-y-0 right-0",
202 props.fadeOpaque === undefined && "bg-linear-to-r from-transparent",
203 props.fadeOpaque === undefined &&
204 (props.fadeSurface === "sheet" ? "to-sheet" : "to-card"),
205 )}
206 pointerEvents="none"
207 style={{
208 width: COMPOSER_TOOLBAR_FADE_WIDTH,
209 experimental_backgroundImage:
210 props.fadeOpaque === undefined
211 ? undefined
212 : `linear-gradient(to right, ${props.fadeTransparent ?? "transparent"} 0%, ${props.fadeOpaque} 100%)`,
213 }}
214 />
215 ) : null}
216 </View>
217 );
218}
219
220export function ComposerActionButton(props: {
221 readonly accessibilityLabel: string;
222 readonly disabled?: boolean;
223 readonly icon: ComponentProps<typeof SymbolView>["name"];
224 readonly onPress: () => void;
225 readonly variant?: "primary" | "danger";
226}) {
227 return (
228 <Pressable
229 accessibilityLabel={props.accessibilityLabel}
230 accessibilityRole="button"
231 accessibilityState={{ disabled: props.disabled }}
232 className="size-[44px] shrink-0 items-center justify-center active:opacity-70"
233 disabled={props.disabled}
234 onPress={props.onPress}
235 >
236 <View
237 className={cn(
238 "size-[30px] items-center justify-center rounded-full",
239 props.variant === "danger"
240 ? "bg-danger"
241 : props.disabled
242 ? "bg-primary/15"
243 : "bg-primary",
244 )}
245 >
246 <SymbolView
247 name={props.icon}
248 size={16}
249 weight="semibold"
250 tintColorClassName={
251 props.variant === "danger" ? "accent-danger-foreground" : "accent-primary-foreground"
252 }
253 type="monochrome"
254 />
255 </View>
256 </Pressable>
257 );
258}
259
260export function ComposerToolbarButton(props: {
261 readonly icon?: ComponentProps<typeof SymbolView>["name"];
262 readonly iconNode?: ReactNode;
263 readonly label?: string;
264 readonly accessibilityLabel?: string;
265 readonly active?: boolean;
266 readonly disabled?: boolean;
267 readonly maxWidth?: number;
268 readonly minWidth?: number;
269 readonly onPress?: () => void;
270 readonly showChevron?: boolean;
271 readonly textTransform?: "none" | "uppercase";
272 readonly variant?: "default" | "primary" | "danger";
273 readonly className?: string;
274 readonly style?: StyleProp<ViewStyle>;
275}) {
276 const variant = props.variant ?? "default";
277 const isCircle = !props.label && props.showChevron === false;
278 const iconTintClassName =
279 variant === "primary"
280 ? props.disabled
281 ? "accent-icon-subtle"
282 : "accent-primary-foreground"
283 : variant === "danger"
284 ? "accent-danger-foreground"
285 : "accent-icon";
286
287 return (
288 <Pressable
289 accessibilityLabel={props.accessibilityLabel ?? props.label}
290 accessibilityRole="button"
291 disabled={props.disabled}
292 onPress={props.onPress}
293 className={cn(
294 // Default width cap lives in the class chain (not the inline style)
295 // so callers can lift it with max-w-full — flex-filling pills in the
296 // thread composer stretch to the row's edge. The numeric maxWidth
297 // prop still wins via the inline style below.
298 "h-11 max-w-[172px] flex-row items-center justify-center rounded-full border active:opacity-70",
299 variant === "primary" && "shadow-lg shadow-adaptive-black-a10-a25 disabled:shadow-none",
300 isCircle ? "w-11" : "gap-2 px-3.5",
301 variant === "primary"
302 ? props.disabled
303 ? "bg-subtle-strong"
304 : "bg-primary"
305 : variant === "danger"
306 ? "bg-danger"
307 : props.active
308 ? "bg-subtle-strong"
309 : "bg-subtle",
310 variant === "default"
311 ? props.active
312 ? "border-border"
313 : "border-border-subtle"
314 : variant === "danger"
315 ? "border-danger-border"
316 : props.disabled
317 ? "border-border-subtle"
318 : "border-primary-foreground/20",
319 props.className,
320 )}
321 style={({ pressed }) => [
322 {
323 maxWidth: props.maxWidth,
324 minWidth: props.minWidth,
325 opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1,
326 },
327 props.style,
328 ]}
329 >
330 {props.iconNode ? (
331 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
332 ) : props.icon ? (
333 <SymbolView
334 name={props.icon}
335 size={16}
336 tintColorClassName={iconTintClassName}
337 type="monochrome"
338 />
339 ) : null}
340 {props.label ? (
341 <Text
342 className={cn(
343 "shrink text-center text-sm font-t3-bold",
344 variant === "primary"
345 ? props.disabled
346 ? "text-foreground-muted"
347 : "text-primary-foreground"
348 : "text-foreground",
349 )}
350 ellipsizeMode="tail"
351 numberOfLines={1}
352 style={{ textTransform: props.textTransform ?? "none" }}
353 >
354 {props.label}
355 </Text>
356 ) : null}
357 {props.showChevron === false ? null : (
358 <SymbolView
359 name="chevron.down"
360 size={11}
361 tintColorClassName={iconTintClassName}
362 type="monochrome"
363 />
364 )}
365 </Pressable>
366 );
367}