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

ComposerToolbarTrigger.tsx

251 lines7.9 KB

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