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

ComposerToolbar.tsx

432 lines14.2 KB

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