Attachment previews for chat messages and prompt composers.
Attachment previews for chat messages and prompt composers.
import { ChatAttachment, ChatAttachmentGroup } from 'heroui-native-pro';<ChatAttachmentGroup>
<ChatAttachment name="..." mimeType="..." size={...} src="...">
<ChatAttachment.Preview>
<ChatAttachment.Icon />
</ChatAttachment.Preview>
<ChatAttachment.Info>
<ChatAttachment.Name />
<ChatAttachment.Size />
</ChatAttachment.Info>
<ChatAttachment.Remove />
</ChatAttachment>
</ChatAttachmentGroup>media (a 64px square thumbnail tile) for images, and for videos with a thumbnailSrc; file (a 64px tall card) for everything else. Becomes a pressable button with onPress / onLongPress. Without children it renders the preview and the info.icon.CloseButton. It sits on the top-end corner of tiles and at the end of cards. Inside a pressable attachment, screen readers reach it as the attachment's remove accessibility action.Pass the file data to ChatAttachment. The media type is inferred from mimeType, then from the name extension.
<ChatAttachment
mimeType="image/png"
name="screenshot.png"
size={248_320}
src="https://example.com/screenshot.png"
/>
<ChatAttachment mimeType="application/pdf" name="brief.pdf" size={824_120} />Place a ChatAttachmentGroup above the bubble of a user message. justify-end aligns it with the bubble.
<ChatMessage.User className="gap-2">
<ChatAttachmentGroup className="justify-end">
<ChatAttachment mediaType="image" name="screenshot.png" src={uri} />
</ChatAttachmentGroup>
<ChatMessage.Bubble>
<ChatMessage.Content>What is in this screenshot?</ChatMessage.Content>
</ChatMessage.Bubble>
</ChatMessage.User>A video renders as a tile only when it has a thumbnailSrc poster, shown with a play badge. Without one, the video renders as a file card. For images, thumbnailSrc is a smaller rendition used instead of src. While a thumbnail loads, or after it fails to load, the tile shows the format icon on a placeholder fill. A new src or thumbnailSrc retries.
<ChatAttachment
mimeType="video/mp4"
name="walkthrough.mp4"
src={videoUri}
thumbnailSrc={posterUri}
/>Set isLoading while a file uploads. Media tiles dim and show a spinner badge. File cards show a spinner instead of the format icon.
<ChatAttachment mimeType="image/jpeg" name="desk.jpg" src={uri} isLoading />
<ChatAttachment mimeType="application/pdf" name="brief.pdf" size={824_120} isLoading />Use the horizontal group in a prompt composer and compose a ChatAttachment.Remove to remove files. The root is an Animated.View, so Reanimated entering, exiting, and layout props animate attachments as they are added and removed. File picking is up to your app, for example with expo-image-picker or expo-document-picker. Set scrollShadowProps.color to the surface behind the strip.
import * as DocumentPicker from 'expo-document-picker';
import { FadeIn, FadeOut, LinearTransition } from 'react-native-reanimated';
const pickFiles = async () => {
const result = await DocumentPicker.getDocumentAsync({ multiple: true });
if (result.canceled) return;
setAttachments((current) => [
...current,
...result.assets.map((asset) => ({
id: asset.uri,
name: asset.name,
size: asset.size,
mimeType: asset.mimeType,
src: asset.uri,
})),
]);
};
<ChatAttachmentGroup
orientation="horizontal"
scrollShadowProps={{ color: themeColorSurface }}
>
{attachments.map((file) => (
<ChatAttachment
key={file.id}
name={file.name}
size={file.size}
mimeType={file.mimeType}
src={file.src}
isLoading={file.isUploading}
entering={FadeIn}
exiting={FadeOut}
layout={LinearTransition}
>
<ChatAttachment.Preview />
<ChatAttachment.Info />
<ChatAttachment.Remove onPress={() => removeAttachment(file.id)} />
</ChatAttachment>
))}
</ChatAttachmentGroup>;Pass onPress (or onLongPress) to make the attachment a button, for example to open a full-screen viewer or Quick Look.
<ChatAttachment
mimeType="application/pdf"
name="brief.pdf"
src={uri}
onPress={() => openPreview(uri)}
/>Media tiles hide the name and use it as the accessibility label. A flex class on ChatAttachment.Info shows it as a scrim at the bottom of the tile.
<ChatAttachment mimeType="image/jpeg" name="desk.jpg" src={uri} className="size-24">
<ChatAttachment.Preview />
<ChatAttachment.Info className="flex" />
</ChatAttachment>Pass icon to replace the format icon wherever the attachment shows it: on file cards, and on tiles whose thumbnail is loading or failed to load. The upload spinner still replaces it while isLoading. Use inferChatAttachmentFileKind to map file kinds to your own icon set. Custom icons render as-is, so size and color them yourself. ChatAttachment.Preview takes a playIcon for the play badge of video tiles. For a single attachment, ChatAttachment.Icon children work too, and ChatAttachment.Remove takes a custom icon as children.
import Ionicons from '@expo/vector-icons/Ionicons';
import {
inferChatAttachmentFileKind,
type ChatAttachmentFileKind,
} from 'heroui-native-pro';
const FILE_ICONS: Record<ChatAttachmentFileKind, keyof typeof Ionicons.glyphMap> = {
archive: 'archive-outline',
audio: 'musical-notes-outline',
code: 'code-slash-outline',
document: 'document-text-outline',
image: 'image-outline',
pdf: 'reader-outline',
presentation: 'easel-outline',
spreadsheet: 'grid-outline',
unknown: 'document-outline',
video: 'videocam-outline',
};
<ChatAttachment
{...file}
icon={
<Ionicons
name={FILE_ICONS[inferChatAttachmentFileKind(file.mimeType, file.name)]}
size={20}
color={themeColorAccent}
/>
}
/>
<ChatAttachment mimeType="video/mp4" name="clip.mp4" src={videoUri} thumbnailSrc={posterUri}>
<ChatAttachment.Preview playIcon={<Ionicons name="play" size={12} />} />
</ChatAttachment>Replace any part. String children of Name and Size replace their labels, and Preview children replace the thumbnail or icon (for example with an expo-image view).
<ChatAttachment mimeType="application/pdf" name="report.pdf" size={2_411_008}>
<ChatAttachment.Preview>
<ChatAttachment.Icon
iconProps={{ size: 24 }}
className="size-10 rounded-xl bg-background"
/>
</ChatAttachment.Preview>
<ChatAttachment.Info>
<ChatAttachment.Name ellipsizeMode="tail" />
<ChatAttachment.Size>PDF · 2.3 MB</ChatAttachment.Size>
</ChatAttachment.Info>
</ChatAttachment>import { useToast } from 'heroui-native';
import { ChatAttachment, ChatAttachmentGroup, ChatMessage } from 'heroui-native-pro';
import { View } from 'react-native';
const SCREENSHOT = {
mimeType: 'image/webp',
name: 'screenshot.webp',
size: 248_320,
src: 'https://heroui-assets.nyc3.cdn.digitaloceanspaces.com/images/egg.webp',
};
const BRIEF = {
mimeType: 'application/pdf',
name: 'project-brief.pdf',
size: 824_120,
};
export default function ChatAttachmentExample() {
const { toast } = useToast();
const openAttachment = (name: string) => {
toast.show({ label: `Opening ${name}` });
};
return (
<View className="w-full gap-6 px-5">
<ChatMessage.User className="gap-2">
<ChatAttachmentGroup className="justify-end">
<ChatAttachment
{...SCREENSHOT}
onPress={() => openAttachment(SCREENSHOT.name)}
/>
<ChatAttachment {...BRIEF} onPress={() => openAttachment(BRIEF.name)} />
</ChatAttachmentGroup>
<ChatMessage.Bubble>
<ChatMessage.Content>Compare the screenshot with the brief.</ChatMessage.Content>
</ChatMessage.Bubble>
</ChatMessage.User>
</View>
);
}| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Custom parts. Defaults to ChatAttachment.Preview and ChatAttachment.Info |
name | string | - | File name. Shown on cards, used to infer the file kind, and announced with the size by screen readers |
size | number | - | File size in bytes, formatted for display ("248 KB") |
mimeType | string | - | MIME type, used to infer the media type and the file kind |
src | string | - | URL of the attachment. Image attachments render it as their thumbnail |
thumbnailSrc | string | - | Video poster (shown with a play badge), or a smaller rendition of an image |
mediaType | ChatAttachmentMediaType | inferred from mimeType, then from the name extension | Media type of the attachment |
isLoading | boolean | false | Uploading state. Reported to screen readers as busy |
className | string | - | Additional CSS classes for the root |
style | StyleProp<ViewStyle> | - | Inline style of the root |
icon | ReactNode | - | Custom format icon for file cards and loading or failed thumbnails. Rendered as-is |
onPress | (event: GestureResponderEvent) => void | - | Makes the attachment a button, for example to open a viewer |
onLongPress | (event: GestureResponderEvent) => void | - | Makes the attachment a button, for example to open a context menu |
animation | ChatAttachmentRootAnimation | - | Animation configuration of the press feedback |
entering / exiting / layout | AnimatedProps<ViewProps>['entering' | 'exiting' | 'layout'] | - | Reanimated layout animations of the root, for composer lists |
...ViewProps | Omit<ViewProps, 'children' | 'style'> | - | All other React Native View props are supported |
'audio' | 'document' | 'image' | 'unknown' | 'video'. Only image and video can render as a thumbnail tile.
Animation configuration of the press feedback of pressable attachments. Can be:
false or "disabled": Disable the press scale and the highlight"disable-all": Disable all animations including children (remove button, upload spinner)true or undefined: Use default animationsobject: Custom animation configuration| prop | type | default | description |
|---|---|---|---|
state | 'disabled' | 'disable-all' | boolean | - | Disable animations while customizing properties |
scale | PressableFeedbackScaleAnimation | - | Press scale. Pass false to keep the highlight without the scale |
highlight | PressableFeedbackHighlightAnimation | - | Highlight overlay that fades in while pressed. Visible on file cards only |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Custom preview content, rendered inside the preview container |
className | string | - | Additional CSS classes for the container |
classNames | ElementSlots<PreviewSlots> | - | Additional CSS classes for the slots |
styles | ChatAttachmentPreviewStyles | - | Inline styles for the slots |
imageProps | Omit<ImageProps, 'source' | 'style' | 'className'> | - | Props of the thumbnail Image (resizeMode defaults to "cover") |
playIcon | ReactNode | - | Custom glyph inside the play badge of video tiles. Rendered as-is |
| prop | type | description |
|---|---|---|
container | string | Custom class name for the preview box |
image | string | Custom class name for the thumbnail Image (tiles only) |
overlay | string | Custom class name for the layer centering the badge |
badge | string | Custom class name for the play / upload badge |
| prop | type | description |
|---|---|---|
container | ViewStyle | Styles for the preview box |
image | ImageStyle | Styles for the thumbnail Image (tiles only) |
overlay | ViewStyle | Styles for the layer centering the badge |
badge | ViewStyle | Styles for the play / upload badge |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Custom icon, rendered instead of the root icon and the inferred icon |
className | string | - | Additional CSS classes for the icon wrapper |
iconProps | ChatAttachmentIconIconProps | - | Props of the default icon. Ignored by custom icons |
...ViewProps | ViewProps | - | All standard React Native View props are supported |
| prop | type | default | description |
|---|---|---|---|
size | number | 20 | Size of the icon (pixels) |
color | string | theme muted | Color of the icon |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Custom content. Defaults to ChatAttachment.Name and ChatAttachment.Size |
className | string | - | Additional CSS classes. flex shows the column as a scrim on media tiles |
...ViewProps | ViewProps | - | All standard React Native View props are supported |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Custom label. Defaults to the root name |
numberOfLines | number | 1 | Maximum number of lines |
ellipsizeMode | 'head' | 'middle' | 'tail' | 'clip' | 'middle' | Truncation of long names |
className | string | - | Additional CSS classes for the label |
...TextProps | TextProps | - | All standard React Native Text props are supported |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | Custom label. Defaults to the formatted root size |
numberOfLines | number | 1 | Maximum number of lines |
className | string | - | Additional CSS classes for the label |
...TextProps | TextProps | - | All standard React Native Text props are supported |
| prop | type | default | description |
|---|---|---|---|
hitSlop | number | Insets | 12 | Extra touch area. Makes the 20px button a 44pt target |
className | string | - | Additional CSS classes for the button |
iconProps | CloseButtonIconProps | { size: 12 } | size and color of the close icon |
accessibilityLabel | string | 'Remove attachment' | Label of the button and of the root's remove accessibility action |
onPress | (event?: GestureResponderEvent) => void | - | Removes the attachment. The event is undefined when triggered by the root's remove accessibility action |
...CloseButtonProps | Omit<CloseButtonProps, 'onPress'> | - | All other heroui-native CloseButton props are supported |
| prop | type | default | description |
|---|---|---|---|
children | ReactNode | - | ChatAttachment elements |
orientation | 'wrap' | 'horizontal' | 'wrap' | Wrapping rows, or a single horizontally scrolling strip |
className | string | - | Additional CSS classes for the container |
classNames | ElementSlots<GroupSlots> | - | Additional CSS classes for the slots |
styles | Partial<Record<GroupSlots, ViewStyle>> | - | Inline styles for the slots |
scrollShadowProps | Omit<ScrollShadowProps, 'children' | 'LinearGradientComponent'> | - | Props of the edge ScrollShadow of the horizontal strip (size defaults to 24, color defaults to the theme background) |
animation | ChatAttachmentGroupAnimation | - | Animation configuration of the group |
...ViewProps | ViewProps | - | All standard React Native View props are supported |
| prop | type | description |
|---|---|---|
container | string | Custom class name for the outer view |
scrollShadow | string | Custom class name for the ScrollShadow (horizontal only) |
scrollView | string | Custom class name for the horizontal ScrollView (horizontal only) |
content | string | Custom class name for the scroll content row (horizontal only) |
| prop | type | description |
|---|---|---|
container | ViewStyle | Styles for the outer view |
scrollShadow | ViewStyle | Styles for the ScrollShadow (horizontal only) |
scrollView | ViewStyle | Styles for the horizontal ScrollView (horizontal only) |
content | ViewStyle | Styles for the scroll content row (horizontal only) |
The group animates nothing itself. Can be:
"disable-all": Disable the animations of every attachment inside itundefined: Use default animationsReturns the resolved data of the enclosing ChatAttachment, for custom parts. Throws outside a ChatAttachment.
| property | type | description |
|---|---|---|
name | string | undefined | File name |
size | number | undefined | File size in bytes |
sizeLabel | string | undefined | Formatted file size ("248 KB") |
mimeType | string | undefined | MIME type |
mediaType | ChatAttachmentMediaType | Resolved media type |
fileKind | ChatAttachmentFileKind | Inferred file kind |
icon | ReactNode | Custom format icon from the root icon prop |
variant | 'file' | 'media' | Resolved layout |
previewUri | string | undefined | Image rendered by the media tile, or undefined for file cards |
isPreviewError | boolean | Whether the preview image failed to load |
isLoading | boolean | Whether the attachment is uploading |
isAccessibilityElement | boolean | Whether the root is announced as a single accessibility element |
onPreviewError | () => void | Marks the preview image as failed |
setRemoveAction | (action: ChatAttachmentRemoveAction | null) => void | Registers the remove action of a mounted ChatAttachment.Remove |
| function | type | description |
|---|---|---|
inferChatAttachmentMediaType | (mimeType?: string) => ChatAttachmentMediaType | Returns the broad media type of a MIME type |
inferChatAttachmentFileKind | (mimeType?: string, name?: string) => ChatAttachmentFileKind | Returns the file kind used to pick the format icon |
formatChatAttachmentSize | (bytes?: number) => string | undefined | Formats a byte count as B, KB, MB, or GB (undefined if invalid) |
'archive' | 'audio' | 'code' | 'document' | 'image' | 'pdf' | 'presentation' | 'spreadsheet' | 'unknown' | 'video'