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

ComposerToolbar.tsx

400 lines13.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 Animated, { FadeIn } from "react-native-reanimated";
16​
17import { cn } from "../lib/cn";
18import { AppText as Text } from "./ui/AppText";
19import { SymbolView } from "./ui/AppSymbol";
20import { useAndroidControlSizing } from "./ui/useAndroidControlSizing";
21​
22const COMPOSER_TOOLBAR_GAP = 8;
23const COMPOSER_TOOLBAR_FADE_WIDTH = 18;
24const COMPOSER_TOOLBAR_SCROLL_EPSILON = 4;
25const INLINE_CONTROL_LABEL_ENTERING = FadeIn.duration(160);
26export function ComposerInlineControl(props: {
27 readonly size?: "regular" | "large";
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 renderIcon?: (size: number) => ReactNode;
34 readonly label: string;
35 readonly suffix?: ReactNode;
36 /** Show only the icon and chevron; the label still names the control for accessibility. */
37 readonly iconOnly?: boolean;
38 readonly maxWidth?: ViewStyle["maxWidth"];
39 readonly onPress?: () => void;
40 readonly selected?: boolean;
41 readonly static?: boolean;
42 readonly chevronDirection?: "down" | "right";
43 readonly showChevron?: boolean;
44}) {
45 const { scale, smallIconSize } = useAndroidControlSizing();
46 // Keep the picker anchored to its parent while the editor changes height.
47 const animatesLabel = props.iconOnly !== undefined;
48 const iconSize = props.size === "large" ? 20 : smallIconSize;
49 return (
50 <Animated.View
51 style={{
52 maxWidth: props.maxWidth ?? 190,
53 overflow: animatesLabel ? "hidden" : undefined,
54 }}
55 >
56 <Pressable
57 accessibilityLabel={props.accessibilityLabel ?? props.label}
58 accessibilityHint={props.accessibilityHint}
59 accessibilityRole={props.static ? undefined : "button"}
60 accessibilityState={
61 props.static ? undefined : { disabled: props.disabled, selected: props.selected }
62 }
63 className="h-[44px] flex-row items-center gap-2 rounded-xl px-2 active:bg-subtle"
64 disabled={props.disabled || props.static}
65 onPress={props.onPress}
66 style={{ opacity: props.disabled ? 0.45 : 1 }}
67 >
68 {props.renderIcon ? (
69 <View
70 className="size-4 shrink-0 items-center justify-center"
71 style={{ width: iconSize, height: iconSize }}
72 >
73 {props.renderIcon(iconSize)}
74 </View>
75 ) : props.icon ? (
76 <SymbolView
77 name={props.icon}
78 size={iconSize}
79 tintColorClassName={
80 props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted"
81 }
82 type="monochrome"
83 />
84 ) : null}
85 {props.iconOnly ? null : (
86 <Animated.View
87 className="shrink"
88 entering={animatesLabel ? INLINE_CONTROL_LABEL_ENTERING : undefined}
89 >
90 <Text
91 className={cn(
92 props.size === "large" ? "text-base font-t3-medium" : "text-sm font-t3-medium",
93 props.emphasized || props.selected ? "text-foreground" : "text-foreground-muted",
94 )}
95 numberOfLines={1}
96 >
97 {props.label}
98 </Text>
99 </Animated.View>
100 )}
101 {!props.iconOnly && props.suffix ? (
102 <View className="min-w-0 shrink">{props.suffix}</View>
103 ) : null}
104 {props.showChevron === false ? null : (
105 <SymbolView
106 name={props.chevronDirection === "right" ? "chevron.right" : "chevron.down"}
107 size={Math.round((props.size === "large" ? 12 : 10) * scale)}
108 tintColorClassName={
109 props.emphasized || props.selected ? "accent-icon" : "accent-icon-muted"
110 }
111 type="monochrome"
112 />
113 )}
114 </Pressable>
115 </Animated.View>
116 );
118​
119export function ComposerToolbarRow(props: {
120 readonly children: ReactNode;
121 readonly paddingBottom?: number;
122 readonly paddingHorizontal?: number;
123 readonly paddingTop?: number;
124 readonly style?: StyleProp<ViewStyle>;
125}) {
126 return (
127 <View
128 className="flex-row items-center gap-1.5"
129 style={[
130 {
131 paddingBottom: props.paddingBottom ?? 8,
132 paddingHorizontal: props.paddingHorizontal ?? 6,
133 paddingTop: props.paddingTop ?? 8,
134 },
135 props.style,
136 ]}
137 >
138 {props.children}
139 </View>
140 );
142​
143export function ComposerToolbarScroller(props: {
144 readonly children: ReactNode;
145 readonly align?: "start" | "end";
146 readonly fadeOpaque?: string;
147 readonly fadeTransparent?: string;
148 readonly fadeSurface?: "card" | "sheet";
149 readonly contentPaddingRight?: number;
150}) {
151 const [metrics, setMetrics] = useState({
152 contentWidth: 0,
153 offsetX: 0,
154 viewportWidth: 0,
155 });
156​
157 const scrollEdges = useMemo(() => {
158 const maxOffset = Math.max(0, metrics.contentWidth - metrics.viewportWidth);
159 return {
160 showLeftFade: metrics.offsetX > COMPOSER_TOOLBAR_SCROLL_EPSILON,
161 showRightFade: metrics.offsetX < maxOffset - COMPOSER_TOOLBAR_SCROLL_EPSILON,
162 };
163 }, [metrics]);
164​
165 const handleLayout = useCallback((event: LayoutChangeEvent) => {
166 const viewportWidth = event.nativeEvent.layout.width;
167 setMetrics((current) =>
168 current.viewportWidth === viewportWidth ? current : { ...current, viewportWidth },
169 );
170 }, []);
171​
172 const handleContentSizeChange = useCallback((contentWidth: number) => {
173 setMetrics((current) =>
174 current.contentWidth === contentWidth ? current : { ...current, contentWidth },
175 );
176 }, []);
177​
178 const handleScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
179 const offsetX = event.nativeEvent.contentOffset.x;
180 setMetrics((current) =>
181 Math.abs(current.offsetX - offsetX) < 1 ? current : { ...current, offsetX },
182 );
183 }, []);
184​
185 return (
186 <View className="relative min-w-0 flex-1">
187 <ScrollView
188 horizontal
189 keyboardShouldPersistTaps="always"
190 onContentSizeChange={handleContentSizeChange}
191 onLayout={handleLayout}
192 onScroll={handleScroll}
193 scrollEventThrottle={16}
194 showsHorizontalScrollIndicator={false}
195 contentContainerStyle={{
196 alignItems: "center",
197 flexGrow: props.align === "end" ? 1 : undefined,
198 justifyContent: props.align === "end" ? "flex-end" : undefined,
199 gap: COMPOSER_TOOLBAR_GAP,
200 paddingLeft: 0,
201 paddingRight: props.contentPaddingRight ?? 1,
202 }}
203 >
204 {props.children}
205 </ScrollView>
206 {scrollEdges.showLeftFade ? (
207 <View
208 className={cn(
209 "absolute inset-y-0 left-0",
210 props.fadeOpaque === undefined && "bg-linear-to-r to-transparent",
211 props.fadeOpaque === undefined &&
212 (props.fadeSurface === "sheet" ? "from-sheet" : "from-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.fadeOpaque} 0%, ${props.fadeTransparent ?? "transparent"} 100%)`,
221 }}
222 />
223 ) : null}
224 {scrollEdges.showRightFade ? (
225 <View
226 className={cn(
227 "absolute inset-y-0 right-0",
228 props.fadeOpaque === undefined && "bg-linear-to-r from-transparent",
229 props.fadeOpaque === undefined &&
230 (props.fadeSurface === "sheet" ? "to-sheet" : "to-card"),
231 )}
232 pointerEvents="none"
233 style={{
234 width: COMPOSER_TOOLBAR_FADE_WIDTH,
235 experimental_backgroundImage:
236 props.fadeOpaque === undefined
237 ? undefined
238 : `linear-gradient(to right, ${props.fadeTransparent ?? "transparent"} 0%, ${props.fadeOpaque} 100%)`,
239 }}
240 />
241 ) : null}
242 </View>
243 );
245​
246export function ComposerActionButton(props: {
247 readonly size?: "regular" | "large";
248 readonly accessibilityLabel: string;
249 readonly disabled?: boolean;
250 readonly icon: ComponentProps<typeof SymbolView>["name"];
251 readonly onPress: () => void;
252 readonly variant?: "primary" | "danger";
253 // Forwarded so a Menu can drive this button as its long-press
254 // anchor: Android injects onLongPress, iOS injects onTouchStart and onPress.
255 readonly onLongPress?: PressableProps["onLongPress"];
256 readonly onTouchStart?: PressableProps["onTouchStart"];
257}) {
258 const { scale, smallIconSize } = useAndroidControlSizing();
259 const circleSize = Math.round((props.size === "large" ? 44 : 30) * scale);
260 return (
261 <Pressable
262 accessibilityLabel={props.accessibilityLabel}
263 accessibilityRole="button"
264 accessibilityState={{ disabled: props.disabled }}
265 className="size-[44px] shrink-0 items-center justify-center active:opacity-70"
266 style={props.size === "large" ? { width: 62 } : undefined}
267 disabled={props.disabled}
268 onPress={props.onPress}
269 onLongPress={props.onLongPress}
270 onTouchStart={props.onTouchStart}
271 >
272 <View
273 style={{ width: circleSize, height: circleSize }}
274 className={cn(
275 "items-center justify-center rounded-full",
276 props.variant === "danger"
277 ? "bg-danger"
278 : props.disabled
279 ? "bg-primary/15"
280 : "bg-primary",
281 )}
282 >
283 <SymbolView
284 name={props.icon}
285 size={props.size === "large" ? Math.round(20 * scale) : smallIconSize}
286 weight="semibold"
287 tintColorClassName={
288 props.variant === "danger" ? "accent-danger-foreground" : "accent-primary-foreground"
289 }
290 type="monochrome"
291 />
292 </View>
293 </Pressable>
294 );
296​
297export function ComposerToolbarButton(props: {
298 readonly icon?: ComponentProps<typeof SymbolView>["name"];
299 readonly iconNode?: ReactNode;
300 readonly label?: string;
301 readonly accessibilityLabel?: string;
302 readonly active?: boolean;
303 readonly disabled?: boolean;
304 readonly maxWidth?: number;
305 readonly minWidth?: number;
306 readonly onPress?: () => void;
307 readonly showChevron?: boolean;
308 readonly textTransform?: "none" | "uppercase";
309 readonly variant?: "default" | "primary" | "danger";
310 readonly className?: string;
311 readonly style?: StyleProp<ViewStyle>;
312}) {
313 const variant = props.variant ?? "default";
314 const isCircle = !props.label && props.showChevron === false;
315 const iconTintClassName =
316 variant === "primary"
317 ? props.disabled
318 ? "accent-icon-subtle"
319 : "accent-primary-foreground"
320 : variant === "danger"
321 ? "accent-danger-foreground"
322 : "accent-icon";
323​
324 return (
325 <Pressable
326 accessibilityLabel={props.accessibilityLabel ?? props.label}
327 accessibilityRole="button"
328 disabled={props.disabled}
329 onPress={props.onPress}
330 className={cn(
331 "h-11 max-w-[172px] flex-row items-center justify-center rounded-full border active:opacity-70",
332 variant === "primary" && "shadow-lg shadow-adaptive-black-a10-a25 disabled:shadow-none",
333 isCircle ? "w-11" : "gap-2 px-3.5",
334 variant === "primary"
335 ? props.disabled
336 ? "bg-subtle-strong"
337 : "bg-primary"
338 : variant === "danger"
339 ? "bg-danger"
340 : props.active
341 ? "bg-subtle-strong"
342 : "bg-subtle",
343 variant === "default"
344 ? props.active
345 ? "border-border"
346 : "border-border-subtle"
347 : variant === "danger"
348 ? "border-danger-border"
349 : props.disabled
350 ? "border-border-subtle"
351 : "border-primary-foreground/20",
352 props.className,
353 )}
354 style={({ pressed }) => [
355 {
356 maxWidth: props.maxWidth,
357 minWidth: props.minWidth,
358 opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1,
359 },
360 props.style,
361 ]}
362 >
363 {props.iconNode ? (
364 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
365 ) : props.icon ? (
366 <SymbolView
367 name={props.icon}
368 size={16}
369 tintColorClassName={iconTintClassName}
370 type="monochrome"
371 />
372 ) : null}
373 {props.label ? (
374 <Text
375 className={cn(
376 "shrink text-center text-sm font-t3-bold",
377 variant === "primary"
378 ? props.disabled
379 ? "text-foreground-muted"
380 : "text-primary-foreground"
381 : "text-foreground",
382 )}
383 ellipsizeMode="tail"
384 numberOfLines={1}
385 style={{ textTransform: props.textTransform ?? "none" }}
386 >
387 {props.label}
388 </Text>
389 ) : null}
390 {props.showChevron === false ? null : (
391 <SymbolView
392 name="chevron.down"
393 size={11}
394 tintColorClassName={iconTintClassName}
395 type="monochrome"
396 />
397 )}
398 </Pressable>
399 );