PeakUIPro

ChatAttachment

File previews and drag-and-drop input for chat

Usage

import {ChatAttachment, ChatAttachmentGroup, ChatAttachmentInput} from "@peakui-pro/react";

Anatomy

import {ChatAttachment, ChatAttachmentGroup, ChatAttachmentInput} from "@peakui-pro/react";

export default () => (
  <>
    <ChatAttachmentGroup>
      {/* Without children: Preview, then Info. An image or video with a `src` is a thumbnail. */}
      <ChatAttachment mimeType="application/pdf" name="brief.pdf" size={824120}>
        <ChatAttachment.Preview>
          <ChatAttachment.Icon />
        </ChatAttachment.Preview>
        <ChatAttachment.Info>
          <ChatAttachment.Name />
          <ChatAttachment.Size />
        </ChatAttachment.Info>
        <ChatAttachment.Remove onPress={remove} />
      </ChatAttachment>
    </ChatAttachmentGroup>

    <ChatAttachmentInput onFilesSelected={setFiles}>
      {/* The picker. An icon alone needs `isIconOnly` or an `aria-label` to be named. */}
      <ChatAttachmentInput.Trigger isIconOnly aria-label="Attach file">
        <Paperclip />
      </ChatAttachmentInput.Trigger>
      {/* The drop target is your element: spread the props, and render `props.children`. */}
      <ChatAttachmentInput.Dropzone render={(props) => <div {...props} />}>
        {content}
      </ChatAttachmentInput.Dropzone>
    </ChatAttachmentInput>
  </>
);

Examples

Grouped

ChatAttachmentGroup wraps attachments in a row that wraps.

Formats

mimeType picks the icon, then the extension of name; generic is last.

Removable

Remove sits in the row of a card and in the corner of a thumbnail.

Composer

Revoke each blob: URL on removal, on clear and on unmount.

Composer Disabled

disabled turns off the picker and the drop target.

Drop Target

accept and multiple filter what a drop delivers, as they do for the picker.

Custom Row

Format a row of your own with the two helpers, as ChatAttachment does.

Customization

Tailwind CSS

<ChatAttachment className="[--chat-attachment-max-width:20rem]" name="brief.pdf" />

Global CSS

@layer components {
  .chat-attachment {
    --chat-attachment-height: 3.5rem;
    --chat-attachment-radius: var(--radius-lg);
  }

  /*
   * A core component used as the drop target writes its own `data-slot` over the dropzone's, so
   * style the target by the class and the attribute, which always arrive.
   */
  .chat-attachment-dropzone[data-drop-target] {
    outline-color: var(--color-success);
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The ChatAttachment component uses these CSS classes (view source styles):

Base Classes

  • .chat-attachment - The root: a thumbnail tile or a file card, told apart by data-variant
  • .chat-attachment-group - A row of attachments that wraps
  • .chat-attachment-dropzone - The drop target; added to your element's className

Element Classes

  • .chat-attachment__preview - The thumbnail, or the box holding the format icon
  • .chat-attachment__preview-image - The thumbnail's image
  • .chat-attachment__preview-video - The thumbnail's video
  • .chat-attachment__icon - The format icon
  • .chat-attachment__info - The name and size; an overlay on a thumbnail
  • .chat-attachment__name - The file name, truncated to the card
  • .chat-attachment__size - The formatted size; hidden on a thumbnail
  • .chat-attachment__remove - The remove button, beside the core .close-button classes

Slots

  • [data-slot="chat-attachment"]
  • [data-slot="chat-attachment-preview"]
  • [data-slot="chat-attachment-preview-image"]
  • [data-slot="chat-attachment-preview-video"]
  • [data-slot="chat-attachment-icon"]
  • [data-slot="chat-attachment-info"]
  • [data-slot="chat-attachment-name"]
  • [data-slot="chat-attachment-size"]
  • [data-slot="chat-attachment-remove"]
  • [data-slot="chat-attachment-group"]
  • [data-slot="chat-attachment-trigger"] - The picker button
  • [data-slot="chat-attachment-input"] - The hidden file input
  • [data-slot="chat-attachment-dropzone"] - Only when the drop target is a plain element

CSS Variables

VariablePurpose
--chat-attachment-heightHeight of a card and the side of a thumbnail; defaults to calc(var(--spacing) * 16)
--chat-attachment-radiusCorner radius; defaults to var(--radius-xl)
--chat-attachment-padding-inlineSpace beside a card's content; defaults to calc(var(--spacing) * 3)
--chat-attachment-gapSpace between a card's icon and text; defaults to calc(var(--spacing) * 2.5)
--chat-attachment-max-widthWidest a card grows; defaults to calc(var(--spacing) * 64)
--chat-attachment-icon-sizeSide of the format icon; defaults to calc(var(--spacing) * 5)
--chat-attachment-remove-insetDistance of the remove button from a thumbnail's corner; defaults to calc(var(--spacing) * 1)

Interactive States

data-variant and data-media-type mark the root; a thumbnail reveals its overlay on hover and focus:

  • Variant: [data-variant="media"] for an image or video with a src, [data-variant="file"] otherwise
  • Media type: [data-media-type="image"], "video", "audio", "document" or "unknown"
  • Hover: :hover shows the overlay and the remove button on a thumbnail
  • Focus: :focus-within shows them too, so a keyboard user can reach Remove
  • No hover: @media (hover: none) shows them at rest
  • Drop target: [data-drop-target] draws a dashed outline while a drag is over it
  • Reduced motion: @media (prefers-reduced-motion: reduce) ends the fade
  • Forced colors: @media (forced-colors: active) draws the border in ButtonBorder, the icon in CanvasText, the overlay as solid Canvas and the drop outline in Highlight

API Reference

ChatAttachment

PropTypeDefaultDescription
mediaType'audio' | 'document' | 'image' | 'unknown' | 'video'inferredWhat the file is; an image, audio or video also picks the icon
mimeTypestring-MIME type; it picks the media type and the icon, and wins over the extension
namestring-File name; the default text of Name, the root's title and the thumbnail's alt
sizenumber-Size in bytes, shown by Size in 1024-based units
srcstring-URL of the image or video, which makes the root a thumbnail. Revoking an object URL is yours, see Composer
childrenReactNodePreview and InfoReplaces the default content
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'div'
...restComponentProps<'div'>-Native <div> attributes

ChatAttachment.Preview

PropTypeDefaultDescription
childrenReactNodeimage, video or IconReplaces the preview content
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'div'
...restComponentProps<'div'>-Native <div> attributes

ChatAttachment.Icon

PropTypeDefaultDescription
childrenReactNodethe glyph of the file kindReplaces the icon
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'span'
...restComponentProps<'span'>-Native <span> attributes

ChatAttachment.Info

PropTypeDefaultDescription
childrenReactNodeName and SizeReplaces the content. On a thumbnail this part is a hover overlay
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'div'
...restComponentProps<'div'>-Native <div> attributes

ChatAttachment.Name

PropTypeDefaultDescription
childrenReactNodename of the rootReplaces the text. Renders nothing when it is empty
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'span'
...restComponentProps<'span'>-Native <span> attributes

ChatAttachment.Size

PropTypeDefaultDescription
childrenReactNodesize of the root, formattedReplaces the text. Renders nothing when the host passed no size
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'span'
...restComponentProps<'span'>-Native <span> attributes

ChatAttachment.Remove

PropTypeDefaultDescription
aria-labelstring"Remove attachment"Accessible name; name the file it removes when a row holds more than one
onPress(e: PressEvent) => void-Called on press; take the attachment out of your state here
childrenReactNode | (values: ButtonRenderProps) => ReactNodebuilt-in X glyphButton content, or a function of the button's state
classNamestring | (values: ButtonRenderProps) => string-Additional classes, or a function of the button's state
...restCloseButtonProps-Every other core CloseButton prop

ChatAttachmentGroup

PropTypeDefaultDescription
childrenReactNode-The attachments
classNamestring-Additional classes
renderDOMRenderFunction<E, undefined>-Replaces the rendered element; E defaults to 'div'
...restComponentProps<'div'>-Native <div> attributes

ChatAttachmentInput

PropTypeDefaultDescription
acceptstring-Native accept filter: extensions, MIME types or family/*, comma separated. It filters a drop as it does the picker
multiplebooleantrueWhether more than one file can be chosen or dropped
disabledbooleanfalseTurns off the picker and the drop target
onFilesSelected(files: File[]) => void-Called with the chosen or dropped files; never with an empty list
childrenReactNode-Where Trigger and Dropzone go; the input renders no element of its own

ChatAttachmentInput.Trigger

PropTypeDefaultDescription
childrenReactNode | (values: ButtonRenderProps) => ReactNode"Attach file"Button content. An icon without isIconOnly and without a label of your own leaves the button unnamed
aria-labelstring"Attach file"Applied when children is omitted or isIconOnly is set; yours replaces it
isIconOnlybooleanfalseSquare button for one icon
variant'primary' | 'secondary' | 'tertiary' | 'outline' | 'ghost' | 'danger' | 'danger-soft''tertiary'Core button style
isDisabledbooleanfalseAlso disabled by disabled on the input
renderDOMRenderFunction<'button', ButtonRenderProps>-Wires the picker to another button; receives the props with data-slot set
...restButtonProps-Every other core Button prop

ChatAttachmentInput.Dropzone

PropTypeDefaultDescription
render(props: ComponentProps<'div'>) => ReactNoderequiredRenders your element as the drop target; it must return a div. A core component used as the target writes its own data-slot over the dropzone's, so merge props.className and style by .chat-attachment-dropzone and [data-drop-target]
childrenReactNode-Content for your element, delivered as props.children together with React Aria's hidden drop button, which keyboards and screen readers drop onto. Render props.children and the button survives

Helpers

FunctionReturnsDescription
inferChatAttachmentFileKind(mimeType?, name?)ChatAttachmentFileKindExported for a host that formats its own rows, which must match ChatAttachment; call it from a client component. The kind that picks the icon: the MIME type first, then the extension, then 'generic'. One of 'archive', 'audio', 'code', 'document', 'generic', 'image', 'pdf', 'spreadsheet' or 'video'
formatChatAttachmentSize(bytes?)string | undefinedExported for the same reason. 1024-based: "0 B", whole "805 KB", then one decimal "14.5 MB" and "3.0 GB". undefined for no size

Render Props

PropTypeDescription
isPendingbooleanWhether the button is waiting on an operation
isPressedbooleanWhether the button is currently pressed
isHoveredbooleanWhether the button is hovered
isFocusedbooleanWhether the button is focused
isFocusVisiblebooleanWhether the button is focused by keyboard

On this page