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

ComposerToolbar.tsx

374 lines12.1 KB

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