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

ComposerToolbar.tsx

324 lines10.4 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 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?: number;
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 );
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 );
113​
114export function ComposerToolbarScroller(props: {
115 readonly children: ReactNode;
116 /** Only for non-Uniwind surfaces such as the native terminal palette. */
117 readonly fadeOpaque?: string;
118 /** Only for non-Uniwind surfaces such as the native terminal palette. */
119 readonly fadeTransparent?: string;
120 /** Semantic Uniwind surface behind the toolbar. Defaults to card. */
121 readonly fadeSurface?: "card" | "sheet";
122 readonly contentPaddingRight?: number;
123}) {
124 const [metrics, setMetrics] = useState({
125 contentWidth: 0,
126 offsetX: 0,
127 viewportWidth: 0,
128 });
129​
130 const scrollEdges = useMemo(() => {
131 const maxOffset = Math.max(0, metrics.contentWidth - metrics.viewportWidth);
132 return {
133 showLeftFade: metrics.offsetX > COMPOSER_TOOLBAR_SCROLL_EPSILON,
134 showRightFade: metrics.offsetX < maxOffset - COMPOSER_TOOLBAR_SCROLL_EPSILON,
135 };
136 }, [metrics]);
137​
138 const handleLayout = useCallback((event: LayoutChangeEvent) => {
139 const viewportWidth = event.nativeEvent.layout.width;
140 setMetrics((current) =>
141 current.viewportWidth === viewportWidth ? current : { ...current, viewportWidth },
142 );
143 }, []);
144​
145 const handleContentSizeChange = useCallback((contentWidth: number) => {
146 setMetrics((current) =>
147 current.contentWidth === contentWidth ? current : { ...current, contentWidth },
148 );
149 }, []);
150​
151 const handleScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
152 const offsetX = event.nativeEvent.contentOffset.x;
153 setMetrics((current) =>
154 Math.abs(current.offsetX - offsetX) < 1 ? current : { ...current, offsetX },
155 );
156 }, []);
157​
158 return (
159 <View className="relative min-w-0 flex-1">
160 <ScrollView
161 horizontal
162 keyboardShouldPersistTaps="always"
163 onContentSizeChange={handleContentSizeChange}
164 onLayout={handleLayout}
165 onScroll={handleScroll}
166 scrollEventThrottle={16}
167 showsHorizontalScrollIndicator={false}
168 contentContainerStyle={{
169 alignItems: "center",
170 gap: COMPOSER_TOOLBAR_GAP,
171 paddingLeft: 0,
172 paddingRight: props.contentPaddingRight ?? 1,
173 }}
174 >
175 {props.children}
176 </ScrollView>
177 {scrollEdges.showLeftFade ? (
178 <View
179 className={cn(
180 "absolute inset-y-0 left-0",
181 props.fadeOpaque === undefined && "bg-linear-to-r to-transparent",
182 props.fadeOpaque === undefined &&
183 (props.fadeSurface === "sheet" ? "from-sheet" : "from-card"),
184 )}
185 pointerEvents="none"
186 style={{
187 width: COMPOSER_TOOLBAR_FADE_WIDTH,
188 experimental_backgroundImage:
189 props.fadeOpaque === undefined
190 ? undefined
191 : `linear-gradient(to right, ${props.fadeOpaque} 0%, ${props.fadeTransparent ?? "transparent"} 100%)`,
192 }}
193 />
194 ) : null}
195 {scrollEdges.showRightFade ? (
196 <View
197 className={cn(
198 "absolute inset-y-0 right-0",
199 props.fadeOpaque === undefined && "bg-linear-to-r from-transparent",
200 props.fadeOpaque === undefined &&
201 (props.fadeSurface === "sheet" ? "to-sheet" : "to-card"),
202 )}
203 pointerEvents="none"
204 style={{
205 width: COMPOSER_TOOLBAR_FADE_WIDTH,
206 experimental_backgroundImage:
207 props.fadeOpaque === undefined
208 ? undefined
209 : `linear-gradient(to right, ${props.fadeTransparent ?? "transparent"} 0%, ${props.fadeOpaque} 100%)`,
210 }}
211 />
212 ) : null}
213 </View>
214 );
216​
217export function ComposerToolbarButton(props: {
218 readonly icon?: ComponentProps<typeof SymbolView>["name"];
219 readonly iconNode?: ReactNode;
220 readonly label?: string;
221 readonly accessibilityLabel?: string;
222 readonly active?: boolean;
223 readonly disabled?: boolean;
224 readonly maxWidth?: number;
225 readonly minWidth?: number;
226 readonly onPress?: () => void;
227 readonly showChevron?: boolean;
228 readonly textTransform?: "none" | "uppercase";
229 readonly variant?: "default" | "primary" | "danger";
230 readonly className?: string;
231 readonly style?: StyleProp<ViewStyle>;
232}) {
233 const variant = props.variant ?? "default";
234 const isCircle = !props.label && props.showChevron === false;
235 const iconTintClassName =
236 variant === "primary"
237 ? props.disabled
238 ? "accent-icon-subtle"
239 : "accent-primary-foreground"
240 : variant === "danger"
241 ? "accent-danger-foreground"
242 : "accent-icon";
243​
244 return (
245 <Pressable
246 accessibilityLabel={props.accessibilityLabel ?? props.label}
247 accessibilityRole="button"
248 disabled={props.disabled}
249 onPress={props.onPress}
250 className={cn(
251 // Default width cap lives in the class chain (not the inline style)
252 // so callers can lift it with max-w-full — flex-filling pills in the
253 // thread composer stretch to the row's edge. The numeric maxWidth
254 // prop still wins via the inline style below.
255 "h-11 max-w-[172px] flex-row items-center justify-center rounded-full border active:opacity-70",
256 variant === "primary" && "shadow-lg shadow-adaptive-black-a10-a25 disabled:shadow-none",
257 isCircle ? "w-11" : "gap-2 px-3.5",
258 variant === "primary"
259 ? props.disabled
260 ? "bg-subtle-strong"
261 : "bg-primary"
262 : variant === "danger"
263 ? "bg-danger"
264 : props.active
265 ? "bg-subtle-strong"
266 : "bg-subtle",
267 variant === "default"
268 ? props.active
269 ? "border-border"
270 : "border-border-subtle"
271 : variant === "danger"
272 ? "border-danger-border"
273 : props.disabled
274 ? "border-border-subtle"
275 : "border-primary-foreground/20",
276 props.className,
277 )}
278 style={({ pressed }) => [
279 {
280 maxWidth: props.maxWidth,
281 minWidth: props.minWidth,
282 opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1,
283 },
284 props.style,
285 ]}
286 >
287 {props.iconNode ? (
288 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
289 ) : props.icon ? (
290 <SymbolView
291 name={props.icon}
292 size={16}
293 tintColorClassName={iconTintClassName}
294 type="monochrome"
295 />
296 ) : null}
297 {props.label ? (
298 <Text
299 className={cn(
300 "shrink text-center text-sm font-t3-bold",
301 variant === "primary"
302 ? props.disabled
303 ? "text-foreground-muted"
304 : "text-primary-foreground"
305 : "text-foreground",
306 )}
307 ellipsizeMode="tail"
308 numberOfLines={1}
309 style={{ textTransform: props.textTransform ?? "none" }}
310 >
311 {props.label}
312 </Text>
313 ) : null}
314 {props.showChevron === false ? null : (
315 <SymbolView
316 name="chevron.down"
317 size={11}
318 tintColorClassName={iconTintClassName}
319 type="monochrome"
320 />
321 )}
322 </Pressable>
323 );