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

ComposerAttachmentStrip.tsx

90 lines3.1 KB

1import { SymbolView } from "../components/AppSymbol";
2import { Image, Pressable, ScrollView, View } from "react-native";
3import { useThemeColor } from "../lib/useThemeColor";
4​
5import type { DraftComposerImageAttachment } from "../lib/composerImages";
6​
7export interface ComposerAttachmentStripProps {
8 /** Attachment images to display. */
9 readonly attachments: ReadonlyArray<DraftComposerImageAttachment>;
10 /** Called when the user taps the remove button on an image. */
11 readonly onRemove: (imageId: string) => void;
12 /** Called when the user taps on an image thumbnail to preview it. */
13 readonly onPressImage?: (previewUri: string) => void;
14 /** Image thumbnail size in points. Defaults to 72. */
15 readonly imageSize?: number;
16 /** Border radius of each image thumbnail. Defaults to 16. */
17 readonly imageBorderRadius?: number;
18 /** Whether the remove button should sit in its own gutter instead of overlapping the image. */
19 readonly removeButtonPlacement?: "overlay" | "gutter";
21​
22/**
23 * A horizontally-scrollable strip of image attachment thumbnails with remove
24 * buttons. Used by both the thread composer and the new-task draft screen.
25 */
26export function ComposerAttachmentStrip(props: ComposerAttachmentStripProps) {
27 const subtleBg = useThemeColor("--color-subtle");
28 const size = props.imageSize ?? 72;
29 const radius = props.imageBorderRadius ?? 16;
30 const removeButtonPlacement = props.removeButtonPlacement ?? "overlay";
31 const removeButtonGutter = removeButtonPlacement === "gutter" ? 10 : 0;
32​
33 if (props.attachments.length === 0) {
34 return null;
35 }
36​
37 return (
38 <ScrollView
39 horizontal
40 showsHorizontalScrollIndicator={false}
41 keyboardShouldPersistTaps="always"
42 className="grow-0"
43 >
44 <View className="flex-row gap-2.5">
45 {props.attachments.map((image) => (
46 <View
47 key={image.id}
48 className="relative"
49 style={{
50 paddingTop: removeButtonGutter,
51 paddingRight: removeButtonGutter,
52 }}
53 >
54 <Pressable
55 onPress={props.onPressImage ? () => props.onPressImage!(image.previewUri) : undefined}
56 >
57 <Image
58 source={{ uri: image.previewUri }}
59 style={{
60 width: size,
61 height: size,
62 borderRadius: radius,
63 backgroundColor: subtleBg,
64 }}
65 resizeMode="cover"
66 />
67 </Pressable>
68 <Pressable
69 className="absolute h-[22px] w-[22px] items-center justify-center rounded-[11px] bg-black/55"
70 style={{
71 top: removeButtonPlacement === "gutter" ? 0 : 4,
72 right: removeButtonPlacement === "gutter" ? 0 : 4,
73 }}
74 hitSlop={6}
75 onPress={() => props.onRemove(image.id)}
76 >
77 <SymbolView
78 name="xmark"
79 size={9}
80 tintColor="#ffffff"
81 type="monochrome"
82 weight="bold"
83 />
84 </Pressable>
85 </View>
86 ))}
87 </View>
88 </ScrollView>
89 );