Open History to see the last commit
1import { SymbolView } from "expo-symbols";
2import type { ComponentProps, ReactNode } from "react";
3import { useCallback, useMemo, useState } from "react";
4import {
5 Pressable,
6 ScrollView,
7 View,
8 useColorScheme,
9 type LayoutChangeEvent,
10 type NativeScrollEvent,
11 type NativeSyntheticEvent,
12 type StyleProp,
13 type ViewStyle,
14} from "react-native";
15
16import { useThemeColor } from "../lib/useThemeColor";
17import { cn } from "../lib/cn";
18import { AppText as Text } from "./AppText";
19
20export const COMPOSER_TOOLBAR_CONTROL_HEIGHT = 44;
21export const COMPOSER_TOOLBAR_GAP = 8;
22export const COMPOSER_TOOLBAR_FADE_WIDTH = 18;
23const COMPOSER_TOOLBAR_SCROLL_EPSILON = 4;
24
25export function ComposerToolbarRow(props: {
26 readonly children: ReactNode;
27 readonly paddingBottom?: number;
28 readonly paddingHorizontal?: number;
29 readonly paddingTop?: number;
30 readonly style?: StyleProp<ViewStyle>;
31}) {
32 return (
33 <View
34 style={[
35 {
36 alignItems: "center",
37 flexDirection: "row",
38 gap: 6,
39 paddingBottom: props.paddingBottom ?? 8,
40 paddingHorizontal: props.paddingHorizontal ?? 6,
41 paddingTop: props.paddingTop ?? 8,
42 },
43 props.style,
44 ]}
45 >
46 {props.children}
47 </View>
48 );
49}
50
51export function ComposerToolbarScroller(props: {
52 readonly children: ReactNode;
53 readonly fadeOpaque: string;
54 readonly fadeTransparent: string;
55 readonly contentPaddingRight?: number;
56}) {
57 const [metrics, setMetrics] = useState({
58 contentWidth: 0,
59 offsetX: 0,
60 viewportWidth: 0,
61 });
62
63 const scrollEdges = useMemo(() => {
64 const maxOffset = Math.max(0, metrics.contentWidth - metrics.viewportWidth);
65 return {
66 showLeftFade: metrics.offsetX > COMPOSER_TOOLBAR_SCROLL_EPSILON,
67 showRightFade: metrics.offsetX < maxOffset - COMPOSER_TOOLBAR_SCROLL_EPSILON,
68 };
69 }, [metrics]);
70
71 const handleLayout = useCallback((event: LayoutChangeEvent) => {
72 const viewportWidth = event.nativeEvent.layout.width;
73 setMetrics((current) =>
74 current.viewportWidth === viewportWidth ? current : { ...current, viewportWidth },
75 );
76 }, []);
77
78 const handleContentSizeChange = useCallback((contentWidth: number) => {
79 setMetrics((current) =>
80 current.contentWidth === contentWidth ? current : { ...current, contentWidth },
81 );
82 }, []);
83
84 const handleScroll = useCallback((event: NativeSyntheticEvent<NativeScrollEvent>) => {
85 const offsetX = event.nativeEvent.contentOffset.x;
86 setMetrics((current) =>
87 Math.abs(current.offsetX - offsetX) < 1 ? current : { ...current, offsetX },
88 );
89 }, []);
90
91 return (
92 <View className="min-w-0 flex-1" style={{ position: "relative" }}>
93 <ScrollView
94 horizontal
95 keyboardShouldPersistTaps="always"
96 onContentSizeChange={handleContentSizeChange}
97 onLayout={handleLayout}
98 onScroll={handleScroll}
99 scrollEventThrottle={16}
100 showsHorizontalScrollIndicator={false}
101 contentContainerStyle={{
102 alignItems: "center",
103 gap: COMPOSER_TOOLBAR_GAP,
104 paddingLeft: 0,
105 paddingRight: props.contentPaddingRight ?? 1,
106 }}
107 >
108 {props.children}
109 </ScrollView>
110 {scrollEdges.showLeftFade ? (
111 <View
112 pointerEvents="none"
113 style={{
114 bottom: 0,
115 experimental_backgroundImage: `linear-gradient(to right, ${props.fadeOpaque} 0%, ${props.fadeTransparent} 100%)`,
116 left: 0,
117 position: "absolute",
118 top: 0,
119 width: COMPOSER_TOOLBAR_FADE_WIDTH,
120 }}
121 />
122 ) : null}
123 {scrollEdges.showRightFade ? (
124 <View
125 pointerEvents="none"
126 style={{
127 bottom: 0,
128 experimental_backgroundImage: `linear-gradient(to right, ${props.fadeTransparent} 0%, ${props.fadeOpaque} 100%)`,
129 position: "absolute",
130 right: 0,
131 top: 0,
132 width: COMPOSER_TOOLBAR_FADE_WIDTH,
133 }}
134 />
135 ) : null}
136 </View>
137 );
138}
139
140export function ComposerToolbarButton(props: {
141 readonly icon?: ComponentProps<typeof SymbolView>["name"];
142 readonly iconNode?: ReactNode;
143 readonly label?: string;
144 readonly accessibilityLabel?: string;
145 readonly active?: boolean;
146 readonly disabled?: boolean;
147 readonly maxWidth?: number;
148 readonly minWidth?: number;
149 readonly onPress?: () => void;
150 readonly showChevron?: boolean;
151 readonly textTransform?: "none" | "uppercase";
152 readonly variant?: "default" | "primary" | "danger";
153 readonly style?: StyleProp<ViewStyle>;
154}) {
155 const isDarkMode = useColorScheme() === "dark";
156 const iconColor = useThemeColor("--color-icon");
157 const iconSubtle = useThemeColor("--color-icon-subtle");
158 const primaryFg = useThemeColor("--color-primary-foreground");
159 const dangerFg = useThemeColor("--color-danger-foreground");
160 const variant = props.variant ?? "default";
161 const isCircle = !props.label && props.showChevron === false;
162 const defaultBorderColor = isDarkMode ? "rgba(255,255,255,0.08)" : "rgba(0,0,0,0.06)";
163 const activeBorderColor = isDarkMode ? "rgba(255,255,255,0.13)" : "rgba(0,0,0,0.1)";
164 const filledBorderColor =
165 variant === "danger"
166 ? "rgba(255,255,255,0.14)"
167 : props.disabled
168 ? defaultBorderColor
169 : "rgba(255,255,255,0.18)";
170 const iconTintColor =
171 variant === "primary"
172 ? props.disabled
173 ? iconSubtle
174 : primaryFg
175 : variant === "danger"
176 ? dangerFg
177 : iconColor;
178
179 return (
180 <Pressable
181 accessibilityLabel={props.accessibilityLabel ?? props.label}
182 accessibilityRole="button"
183 disabled={props.disabled}
184 onPress={props.onPress}
185 className={cn(
186 "h-11 flex-row items-center justify-center rounded-full active:opacity-70",
187 isCircle ? "w-11" : "gap-2 px-3.5",
188 variant === "primary"
189 ? props.disabled
190 ? "bg-subtle-strong"
191 : "bg-primary"
192 : variant === "danger"
193 ? "bg-danger"
194 : props.active
195 ? "bg-subtle-strong"
196 : "bg-subtle",
197 )}
198 style={({ pressed }) => [
199 {
200 borderColor:
201 variant === "default"
202 ? props.active
203 ? activeBorderColor
204 : defaultBorderColor
205 : filledBorderColor,
206 borderWidth: 1,
207 maxWidth: props.maxWidth ?? 172,
208 minWidth: props.minWidth,
209 opacity: props.disabled ? 0.55 : pressed ? 0.72 : 1,
210 shadowColor: "#000",
211 shadowOffset: { width: 0, height: isDarkMode ? 3 : 2 },
212 shadowOpacity: props.disabled ? 0 : isDarkMode ? 0.24 : 0.08,
213 shadowRadius: isDarkMode ? 10 : 8,
214 },
215 props.style,
216 ]}
217 >
218 {props.iconNode ? (
219 <View className="h-4 w-4 items-center justify-center">{props.iconNode}</View>
220 ) : props.icon ? (
221 <SymbolView name={props.icon} size={16} tintColor={iconTintColor} type="monochrome" />
222 ) : null}
223 {props.label ? (
224 <Text
225 className={cn(
226 "shrink text-center text-sm font-t3-bold",
227 variant === "primary"
228 ? props.disabled
229 ? "text-foreground-muted"
230 : "text-primary-foreground"
231 : "text-foreground",
232 )}
233 ellipsizeMode="tail"
234 numberOfLines={1}
235 style={{ textTransform: props.textTransform ?? "none" }}
236 >
237 {props.label}
238 </Text>
239 ) : null}
240 {props.showChevron === false ? null : (
241 <SymbolView name="chevron.down" size={11} tintColor={iconTintColor} type="monochrome" />
242 )}
243 </Pressable>
244 );
245}
246
247export const ComposerToolbarTrigger = ComposerToolbarButton;