Open History to see the last commit
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 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 );
118}
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 );
142}
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 metricsRef = useRef({
153 contentWidth: 0,
154 offsetX: 0,
155 viewportWidth: 0,
156 showLeftFade: false,
157 showRightFade: false,
158 });
159 const [scrollEdges, setScrollEdges] = useState({
160 showLeftFade: false,
161 showRightFade: false,
162 });
163
164 const updateScrollEdges = useCallback(() => {
165 const metrics = metricsRef.current;
166 const maxOffset = Math.max(0, metrics.contentWidth - metrics.viewportWidth);
167 const showLeftFade = metrics.offsetX > COMPOSER_TOOLBAR_SCROLL_EPSILON;
168 const showRightFade = metrics.offsetX < maxOffset - COMPOSER_TOOLBAR_SCROLL_EPSILON;
169 if (metrics.showLeftFade === showLeftFade && metrics.showRightFade === showRightFade) {
170 return;
171 }
172 metrics.showLeftFade = showLeftFade;
173 metrics.showRightFade = showRightFade;
174 setScrollEdges({ showLeftFade, showRightFade });
175 }, []);
176
177 const handleLayout = useCallback(
178 (event: LayoutChangeEvent) => {
179 const viewportWidth = event.nativeEvent.layout.width;
180 if (metricsRef.current.viewportWidth === viewportWidth) return;
181 metricsRef.current.viewportWidth = viewportWidth;
182 updateScrollEdges();
183 },
184 [updateScrollEdges],
185 );
186
187 const handleContentSizeChange = useCallback(
188 (contentWidth: number) => {
189 if (metricsRef.current.contentWidth === contentWidth) return;
190 metricsRef.current.contentWidth = contentWidth;
191 updateScrollEdges();
192 },
193 [updateScrollEdges],
194 );
195
196 const handleScroll = useCallback(
197 (event: NativeSyntheticEvent<NativeScrollEvent>) => {
198 const offsetX = event.nativeEvent.contentOffset.x;
199 if (Math.abs(metricsRef.current.offsetX - offsetX) < 1) return;
200 metricsRef.current.offsetX = offsetX;
201 updateScrollEdges();
202 },
203 [updateScrollEdges],
204 );
205
206 return (
207 <View className="relative min-w-0 flex-1">
208 <ScrollView
209 horizontal
210 keyboardShouldPersistTaps="always"
211 onContentSizeChange={handleContentSizeChange}
212 onLayout={handleLayout}
213 onScroll={handleScroll}
214 scrollEventThrottle={16}
215 showsHorizontalScrollIndicator={false}
216 contentContainerStyle={{
217 alignItems: "center",
218 flexGrow: props.align === "end" ? 1 : undefined,
219 justifyContent: props.align === "end" ? "flex-end" : undefined,
220 gap: COMPOSER_TOOLBAR_GAP,
221 paddingLeft: 0,
222 paddingRight: props.contentPaddingRight ?? 1,
223 }}
224 >
225 {props.children}
226 </ScrollView>
227 {scrollEdges.showLeftFade ? (
228 <View
229 className={cn(
230 "absolute inset-y-0 left-0",
231 props.fadeOpaque === undefined && "bg-linear-to-r to-transparent",
232 props.fadeOpaque === undefined &&
233 (props.fadeSurface === "sheet" ? "from-sheet" : "from-card"),
234 )}
235 pointerEvents="none"
236 style={{
237 width: COMPOSER_TOOLBAR_FADE_WIDTH,
238 experimental_backgroundImage:
239 props.fadeOpaque === undefined
240 ? undefined
241 : `linear-gradient(to right, ${props.fadeOpaque} 0%, ${props.fadeTransparent ?? "transparent"} 100%)`,
242 }}
243 />
244 ) : null}
245 {scrollEdges.showRightFade ? (
246 <View
247 className={cn(
248 "absolute inset-y-0 right-0",
249 props.fadeOpaque === undefined && "bg-linear-to-r from-transparent",
250 props.fadeOpaque === undefined &&
251 (props.fadeSurface === "sheet" ? "to-sheet" : "to-card"),
252 )}
253 pointerEvents="none"
254 style={{
255 width: COMPOSER_TOOLBAR_FADE_WIDTH,
256 experimental_backgroundImage:
257 props.fadeOpaque === undefined
258 ? undefined
259 : `linear-gradient(to right, ${props.fadeTransparent ?? "transparent"} 0%, ${props.fadeOpaque} 100%)`,
260 }}
261 />
262 ) : null}
263 </View>
264 );
265}
266
267export function ComposerActionButton(props: {
268 readonly size?: "regular" | "large";
269 readonly accessibilityLabel: string;
270 readonly disabled?: boolean;
271 readonly icon: ComponentProps<typeof SymbolView>["name"];
272 readonly onPress: () => void;
273 readonly variant?: "primary" | "danger";
274 // Menu injects onLongPress on Android and onTouchStart/onPress on iOS.
275 readonly onLongPress?: PressableProps["onLongPress"];
276 readonly onTouchStart?: PressableProps["onTouchStart"];
277}) {
278 const { scale, smallIconSize } = useAndroidControlSizing();
279 const circleSize = Math.round((props.size === "large" ? 44 : 30) * scale);
280 return (
281 <Pressable
282 accessibilityLabel={props.accessibilityLabel}
283 accessibilityRole="button"
284 accessibilityState={{ disabled: props.disabled }}
285 className="size-[44px] shrink-0 items-center justify-center active:opacity-70"
286 style={props.size === "large" ? { width: 62 } : undefined}
287 disabled={props.disabled}
288 onPress={props.onPress}
289 onLongPress={props.onLongPress}
290 onTouchStart={props.onTouchStart}
291 >
292 <View
293 style={{ width: circleSize, height: circleSize }}
294 className={cn(
295 "items-center justify-center rounded-full",
296 props.variant === "danger"
297 ? "bg-danger"
298 : props.disabled
299 ? "bg-foreground/12"
300 : "bg-primary",
301 )}
302 >
303 <SymbolView
304 name={props.icon}
305 size={props.size === "large" ? Math.round(20 * scale) : smallIconSize}
306 weight="semibold"
307 tintColorClassName={
308 props.variant === "danger"
309 ? "accent-danger-foreground"
310 : props.disabled
311 ? "accent-foreground/38"
312 : "accent-primary-foreground"
313 }
314 type="monochrome"
315 />
316 </View>
317 </Pressable>
318 );
319}
320
321export function ComposerToolbarButton(props: {
322 readonly icon?: ComponentProps<typeof SymbolView>["name"];
323 readonly iconNode?: ReactNode;
324 readonly label?: string;
325 readonly accessibilityLabel?: string;
326 readonly active?: boolean;
327 readonly disabled?: boolean;
328 readonly maxWidth?: number;
329 readonly minWidth?: number;
330 readonly onPress?: () => void;
331 readonly showChevron?: boolean;
332 readonly textTransform?: "none" | "uppercase";
333 readonly variant?: "default" | "primary" | "danger";
334 readonly className?: string;
335 readonly style?: StyleProp<ViewStyle>;
336}) {
337 const variant = props.variant ?? "default";
338 const isCircle = !props.label && props.showChevron === false;
339 const iconTintClassName =
340 variant === "primary"
341 ? props.disabled
342 ? "accent-icon-subtle"
343 : "accent-primary-foreground"
344 : variant === "danger"
345 ? "accent-danger-foreground"
346 : "accent-icon";
347
348 return (
349 <Pressable
350 accessibilityLabel={props.accessibilityLabel ?? props.label}
351 accessibilityRole="button"
352 disabled={props.disabled}
353 onPress={props.onPress}
354 className={cn(
355 "h-11 max-w-[172px] flex-row items-center justify-center rounded-full border active:opacity-70",
356 variant === "primary" && "shadow-lg shadow-adaptive-black-a10-a25 disabled:shadow-none",
357 isCircle ? "w-11" : "gap-2 px-3.5",
358 variant === "primary"
359 ? props.disabled
360 ? "bg-subtle-strong"
361 : "bg-primary"
362 : variant === "danger"
363 ? "bg-danger"
364 : props.active
365 ? "bg-subtle-strong"
366 : "bg-subtle",
367 variant === "default"
368 ? props.active
369 ? "border-border"
370 : "border-border-subtle"
371 : variant === "danger"
372 ? "border-danger-border"
373 : props.disabled
374 ? "border-border-subtle"
375 : "border-primary-foreground/20",
376 props.className,
377 )}
378 style={({ pressed }) => [
379 {
380 maxWidth: props.maxWidth,
381 minWidth: props.minWidth,
382 opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1,
383 },
384 props.style,
385 ]}
386 >
387 {props.iconNode ? (
388 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
389 ) : props.icon ? (
390 <SymbolView
391 name={props.icon}
392 size={16}
393 tintColorClassName={iconTintClassName}
394 type="monochrome"
395 />
396 ) : null}
397 {props.label ? (
398 <Text
399 className={cn(
400 "shrink text-center text-sm font-t3-bold",
401 variant === "primary"
402 ? props.disabled
403 ? "text-foreground-muted"
404 : "text-primary-foreground"
405 : "text-foreground",
406 )}
407 ellipsizeMode="tail"
408 numberOfLines={1}
409 style={{ textTransform: props.textTransform ?? "none" }}
410 >
411 {props.label}
412 </Text>
413 ) : null}
414 {props.showChevron === false ? null : (
415 <SymbolView
416 name="chevron.down"
417 size={11}
418 tintColorClassName={iconTintClassName}
419 type="monochrome"
420 />
421 )}
422 </Pressable>
423 );
424}