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

ComposerToolbar.tsx

402 lines13.1 KB

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