Skip to main content

Attachments

The Attachment component takes a list of message attachments and conditionally renders the UI of each individual attachment based upon its type. The following table shows the attachment UI components that will be rendered for various attachment types:

Attachment TypeUI ComponentFile type(s) (non-exhaustive)
audioAudioMP3, WAV, M4A, FLAC, AAC
fileFileDOC, DOCX, PDF, PPT, PPTX, TXT, XLS, XLSX
galleryGallerywhen a message has more than 1 'image' type attachment
imageImageHEIC, GIF, JPEG, JPG, PNG, TIFF, BMP
videoReactPlayerMP4, OGG, WEBM, Quicktime(QTFF, QT, or MOV)

Message attachments that do not conform to one of the above types are rendered with the Card component.

Basic Usage#

By default, the Attachment component is included within MessageSimple. To render message attachment UI within a custom Message UI component, import the Attachment component and render it conditionally based upon the presence of message attachments.

const CustomMessage = () => {
// consume `MessageContext`
const { message } = useMessageContext();

return (
<div>
{message.attachments && <Attachment attachments={message.attachments} />}
// render remaining custom Message UI
</div>
);
};

<Chat client={client}>
<Channel channel={channel} Message={CustomMessage}>
<MessageList />
<MessageInput />
</Channel>
</Chat>;

UI Customization#

Attachment accepts component override props for each attachment UI component. Building upon the previous example, the below snippet shows how to customize one of the individual attachment UI components.

const CustomImage = (props) => {
// render custom image component here
};

const CustomMessage = () => {
// consume `MessageContext`
const { message } = useMessageContext();

return (
<div>
{message.attachments && <Attachment attachments={message.attachments} Image={CustomImage} />}
// render remaining custom Message UI
</div>
);
};

<Chat client={client}>
<Channel channel={channel} Message={CustomMessage}>
<MessageList />
<MessageInput />
</Channel>
</Chat>;

Props#

Required
attachments#

The message attachments to render, see Attachment Format in the general JavaScript docs.

Type
array

actionHandler#

The handler function to call when an action is performed on an attachment, examples include canceling a /giphy command or shuffling the results.

Type
(dataOrName?: string | FormData, value?: string, event?: React.BaseSyntheticEvent) => Promise<void>

AttachmentActions#

Custom UI component for displaying attachment actions.

TypeDefault
componentAttachmentActions

Audio#

Custom UI component for displaying an audio type attachment.

TypeDefault
componentAudio

Card#

Custom UI component for displaying a card type attachment.

TypeDefault
componentCard

File#

Custom UI component for displaying a file type attachment.

TypeDefault
componentFile

Gallery#

Custom UI component for displaying a gallery of image type attachments.

TypeDefault
componentGallery

Image#

Custom UI component for displaying an image type attachment.

TypeDefault
componentImage

Media#

Custom UI component for displaying a video type attachment, defaults to use the react-player dependency.

TypeDefault
componentReactPlayer

Did you find this page helpful?