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

ComposerToolbar.tsx

457 lines15.3 KB

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