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

ComposerAttachmentStrip.tsx

88 lines3.0 KB

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