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