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

ComposerToolbar.tsx

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