Components/Attachment Tray

Attachment Tray

A tray that displays a message's image and file attachments.

VerifiedSince 0.3.9

Usage

Basic

Loading demo...

Best Practices

  • Set removable in a composer and drop the id on remove and cancel; keep sent messages read-only.
  • Give an in-flight attachment both uploading: true and progress (0–1); without progress, no progress shows.
  • Handle open; otherwise clicking a file chip does nothing.
  • On non-English surfaces, override every text prop and sizeFormatter; previousText and nextText are visible text.
  • Resize thumbnails with --tx-attachment-tray-thumb-size rather than overriding the component's internal classes.

API Reference

TxAttachmentTray

Props

NameTypeDefaultDescription
attachmentsAiAttachment[]—Images and files in one list. Required.
removablebooleanfalseShows remove buttons; cancel-upload buttons don't depend on it.
previewTitlestring'Preview'Viewer title when the image has no name.
previousLabelstring'Previous image'Accessible name of the previous button.
nextLabelstring'Next image'Accessible name of the next button.
previousTextstring'Prev'Visible text of the previous button.
nextTextstring'Next'Visible text of the next button.
closeLabelstring'Close'Accessible name of the preview's close button.
removeLabelstring'Remove attachment'Accessible name of remove buttons.
cancelLabelstring'Cancel upload'Accessible name of cancel-upload buttons.
sizeFormatter(bytes: number) => string—Formats file sizes; defaults to whole bytes under 1 KB, then KB/MB/GB with one decimal.

Events

NamePayloadDescription
remove(id: string)Fires when remove is clicked.
cancel(id: string)Fires when cancel-upload is clicked.
open(attachment: AiAttachmentFile)Fires when a file chip is clicked; images open the built-in viewer.

TxAttachmentChip

Props

NameTypeDefaultDescription
attachmentAiAttachment & { kind: 'file' }—The file attachment to show. Required.
removablebooleanfalseShows the remove button; the cancel-upload button doesn't depend on it.
removeLabelstring'Remove attachment'Accessible name of the remove button.
cancelLabelstring'Cancel upload'Accessible name of the cancel-upload button.
sizeFormatter(bytes: number) => string—Formats the file size; defaults to the same format as TxAttachmentTray.

Events

NamePayloadDescription
remove(id: string)Fires when remove is clicked.
cancel(id: string)Fires when cancel-upload is clicked.
open(attachment: AiAttachment & { kind: 'file' })Fires when the chip body is clicked.

CSS Variables

VariableDefaultDescription
--tx-attachment-tray-thumb-size72pxSide length of an image thumbnail.
  • The variable is not declared on the root, so setting it on the component or any ancestor takes effect.

Overview

  • kind: 'image' items go to the image grid and kind: 'file' items to file chips; pass both in one attachments array.
  • Each file renders as a TxAttachmentChip; the tray forwards removable, removeLabel, cancelLabel, and sizeFormatter to it.
  • An uploading attachment always has a cancel button: images draw a progress ring, file chips show a percentage and a thin progress bar.
  • An image's remove or cancel button sits inside the thumbnail's top-right corner; it appears on hover or keyboard focus and stays visible on touch screens.
  • Clicking an image opens the built-in viewer, which emits nothing; with focus in the viewer, ← / → page through images, and focus moves to the opposite button at either end.
  • Until an image arrives, the viewer holds its place, shaped by the attachment's width / height when given and otherwise like the last image that loaded, and shows a shimmering placeholder there once 150 ms have passed.
  • An image that fails to load shows a placeholder icon in both the thumbnail and the viewer; the same URL is not retried, and a new url loads again.

Technologies

  • Thumbnails pin their own img margin and size, so an article's typography cannot shift them; the viewer is mounted on <body>, outside the article's styles.
  • The viewer is the same internal component as TxImageGallery's lightbox, shown here as a centered TxModal panel, and its styles live in the image-gallery sheet. The on-demand style plugin loads it; when importing styles by hand, add @talex-touch/tuffex/image-gallery/style.css and @talex-touch/tuffex/modal/style.css.
  • Source: packages/tuffex/packages/components/src/attachment-tray/.
查看源码
packages/tuffex/packages/components/src/attachment-tray/index.ts