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

ComposerToolbar.tsx

311 lines10.0 KB

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