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

ComposerToolbar.tsx

373 lines11.8 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 PressableProps,
12 type StyleProp,
13 type ViewStyle,
14} from "react-native";
15​
16import { cn } from "../lib/cn";
17import { AppText as Text } from "./ui/AppText";
18import { SymbolView } from "./ui/AppSymbol";
19import { useAndroidControlSizing } from "./ui/useAndroidControlSizing";
20​
21const COMPOSER_TOOLBAR_GAP = 8;
22const COMPOSER_TOOLBAR_FADE_WIDTH = 18;
23const COMPOSER_TOOLBAR_SCROLL_EPSILON = 4;
24​
25export function ComposerInlineControl(props: {
26 readonly accessibilityHint?: string;
27 readonly accessibilityLabel?: string;
28 readonly disabled?: boolean;
29 readonly emphasized?: boolean;
30 readonly icon?: ComponentProps<typeof SymbolView>["name"];
31 readonly renderIcon?: (size: number) => ReactNode;
32 readonly label: string;
33 readonly maxWidth?: ViewStyle["maxWidth"];
34 readonly onPress?: () => void;
35 readonly selected?: boolean;
36 readonly static?: boolean;
37 readonly chevronDirection?: "down" | "right";
38 readonly showChevron?: boolean;
39}) {
40 const { scale, smallIconSize } = useAndroidControlSizing();
41 return (
42 <Pressable
43 accessibilityLabel={props.accessibilityLabel ?? props.label}
44 accessibilityHint={props.accessibilityHint}
45 accessibilityRole={props.static ? undefined : "button"}
46 accessibilityState={
47 props.static ? undefined : { disabled: props.disabled, selected: props.selected }
48 }
49 className="h-11 flex-row items-center gap-2 rounded-xl px-2 active:bg-subtle"
50 disabled={props.disabled || props.static}
51 onPress={props.onPress}
52 style={{ maxWidth: props.maxWidth ?? 190, opacity: props.disabled ? 0.45 : 1 }}
53 >
54 {props.renderIcon ? (
55 <View
56 className="size-4 shrink-0 items-center justify-center"
57 style={Platform.OS === "android" ? { width: 14 * scale, height: 14 * scale } : undefined}
58 >
59 {props.renderIcon(smallIconSize)}
60 </View>
61 ) : props.icon ? (
62 <SymbolView
63 name={props.icon}
64 size={smallIconSize}
65 tintColorClassName={
66 props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted"
67 }
68 type="monochrome"
69 />
70 ) : null}
71 <Text
72 className={cn(
73 "shrink text-sm font-t3-medium",
74 props.emphasized || props.selected ? "text-foreground" : "text-foreground-muted",
75 )}
76 numberOfLines={1}
77 >
78 {props.label}
79 </Text>
80 {props.showChevron === false ? null : (
81 <SymbolView
82 name={props.chevronDirection === "right" ? "chevron.right" : "chevron.down"}
83 size={Math.round(10 * scale)}
84 tintColorClassName={
85 props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted"
86 }
87 type="monochrome"
88 />
89 )}
90 </Pressable>
91 );
93​
94export function ComposerToolbarRow(props: {
95 readonly children: ReactNode;
96 readonly paddingBottom?: number;
97 readonly paddingHorizontal?: number;
98 readonly paddingTop?: number;
99 readonly style?: StyleProp<ViewStyle>;
100}) {
101 return (
102 <View
103 className="flex-row items-center gap-1.5"
104 style={[
105 {
106 paddingBottom: props.paddingBottom ?? 8,
107 paddingHorizontal: props.paddingHorizontal ?? 6,
108 paddingTop: props.paddingTop ?? 8,
109 },
110 props.style,
111 ]}
112 >
113 {props.children}
114 </View>
115 );
117​
118export function ComposerToolbarScroller(props: {
119 readonly children: ReactNode;
120 readonly align?: "start" | "end";
121 readonly fadeOpaque?: string;
122 readonly fadeTransparent?: string;
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 Menu 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 const { scale, smallIconSize } = useAndroidControlSizing();
233 const circleSize = Math.round(30 * scale);
234 return (
235 <Pressable
236 accessibilityLabel={props.accessibilityLabel}
237 accessibilityRole="button"
238 accessibilityState={{ disabled: props.disabled }}
239 className="size-[44px] shrink-0 items-center justify-center active:opacity-70"
240 disabled={props.disabled}
241 onPress={props.onPress}
242 onLongPress={props.onLongPress}
243 onTouchStart={props.onTouchStart}
244 >
245 <View
246 style={{ width: circleSize, height: circleSize }}
247 className={cn(
248 "items-center justify-center rounded-full",
249 props.variant === "danger"
250 ? "bg-danger"
251 : props.disabled
252 ? "bg-primary/15"
253 : "bg-primary",
254 )}
255 >
256 <SymbolView
257 name={props.icon}
258 size={smallIconSize}
259 weight="semibold"
260 tintColorClassName={
261 props.variant === "danger" ? "accent-danger-foreground" : "accent-primary-foreground"
262 }
263 type="monochrome"
264 />
265 </View>
266 </Pressable>
267 );
269​
270export function ComposerToolbarButton(props: {
271 readonly icon?: ComponentProps<typeof SymbolView>["name"];
272 readonly iconNode?: ReactNode;
273 readonly label?: string;
274 readonly accessibilityLabel?: string;
275 readonly active?: boolean;
276 readonly disabled?: boolean;
277 readonly maxWidth?: number;
278 readonly minWidth?: number;
279 readonly onPress?: () => void;
280 readonly showChevron?: boolean;
281 readonly textTransform?: "none" | "uppercase";
282 readonly variant?: "default" | "primary" | "danger";
283 readonly className?: string;
284 readonly style?: StyleProp<ViewStyle>;
285}) {
286 const variant = props.variant ?? "default";
287 const isCircle = !props.label && props.showChevron === false;
288 const iconTintClassName =
289 variant === "primary"
290 ? props.disabled
291 ? "accent-icon-subtle"
292 : "accent-primary-foreground"
293 : variant === "danger"
294 ? "accent-danger-foreground"
295 : "accent-icon";
296​
297 return (
298 <Pressable
299 accessibilityLabel={props.accessibilityLabel ?? props.label}
300 accessibilityRole="button"
301 disabled={props.disabled}
302 onPress={props.onPress}
303 className={cn(
304 "h-11 max-w-[172px] flex-row items-center justify-center rounded-full border active:opacity-70",
305 variant === "primary" && "shadow-lg shadow-adaptive-black-a10-a25 disabled:shadow-none",
306 isCircle ? "w-11" : "gap-2 px-3.5",
307 variant === "primary"
308 ? props.disabled
309 ? "bg-subtle-strong"
310 : "bg-primary"
311 : variant === "danger"
312 ? "bg-danger"
313 : props.active
314 ? "bg-subtle-strong"
315 : "bg-subtle",
316 variant === "default"
317 ? props.active
318 ? "border-border"
319 : "border-border-subtle"
320 : variant === "danger"
321 ? "border-danger-border"
322 : props.disabled
323 ? "border-border-subtle"
324 : "border-primary-foreground/20",
325 props.className,
326 )}
327 style={({ pressed }) => [
328 {
329 maxWidth: props.maxWidth,
330 minWidth: props.minWidth,
331 opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1,
332 },
333 props.style,
334 ]}
335 >
336 {props.iconNode ? (
337 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
338 ) : props.icon ? (
339 <SymbolView
340 name={props.icon}
341 size={16}
342 tintColorClassName={iconTintClassName}
343 type="monochrome"
344 />
345 ) : null}
346 {props.label ? (
347 <Text
348 className={cn(
349 "shrink text-center text-sm font-t3-bold",
350 variant === "primary"
351 ? props.disabled
352 ? "text-foreground-muted"
353 : "text-primary-foreground"
354 : "text-foreground",
355 )}
356 ellipsizeMode="tail"
357 numberOfLines={1}
358 style={{ textTransform: props.textTransform ?? "none" }}
359 >
360 {props.label}
361 </Text>
362 ) : null}
363 {props.showChevron === false ? null : (
364 <SymbolView
365 name="chevron.down"
366 size={11}
367 tintColorClassName={iconTintClassName}
368 type="monochrome"
369 />
370 )}
371 </Pressable>
372 );