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

ComposerToolbar.tsx

378 lines12.2 KB

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