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