ChatAttachment

GitHub
Display a file attached to a prompt or a chat message.

Usage

The ChatAttachment component displays a file with its name and a description, next to an image preview or an icon.

This component is automatically used by the ChatPrompt component to list the files bound with v-model:files.

File

Use the file prop to display a File, such as one picked by the user. Its name, type and size are read from it and images are previewed through an object URL.

<script setup lang="ts">
defineProps<{
  file: File;
}>();
</script>

<template>
  <PChatAttachment :file="file" />
</template>

Name

Use the name and type props to display a file you only have the details of, like a file part of a message.

quarterly-report.pdfPDF
<template>
  <PChatAttachment name="quarterly-report.pdf" type="application/pdf" />
</template>

Url

Use the url prop to preview an image. The image is displayed when type is an image media type, and its name is used as the alternative text.

landscape.jpg
landscape.jpgJPG
<template>
  <PChatAttachment name="landscape.jpg" type="image/jpeg" url="https://picsum.photos/id/1015/200" />
</template>
Map a file part of a message to these props in the #files slot of ChatMessage or ChatMessages.
<template>
  <PChatMessages
    :messages="messages"
    :status="status"
  >
    <template #files="{ parts }">
      <PChatAttachment
        v-for="(part, index) in parts"
        :key="index"
        :name="part.filename"
        :type="part.mediaType"
        :url="part.url"
      />
    </template>
  </PChatMessages>
</template>

Description

Use the description prop to set the text under the name. Defaults to the file extension and, with the file prop, its size.

quarterly-report.pdfUploaded 2 minutes ago
<template>
  <PChatAttachment
    description="Uploaded 2 minutes ago"
    name="quarterly-report.pdf"
    type="application/pdf"
  />
</template>

Status

Use the status prop to show that the file is uploading or failed to upload with error. Defaults to idle.

While uploading, the name shimmers, an image is dimmed and a file icon turns into a spinner. Pass the progress or, with error, the reason to the description prop.
quarterly-report.pdfPDF
Uploading
<template>
  <PChatAttachment status="uploading" name="quarterly-report.pdf" type="application/pdf" />
</template>

Orientation

Use the orientation prop to change the layout of the attachment. Defaults to horizontal, vertical gives an image a large preview.

landscape.jpg
landscape.jpgJPG
<template>
  <PChatAttachment
    orientation="vertical"
    name="landscape.jpg"
    type="image/jpeg"
    url="https://picsum.photos/id/1015/200"
  />
</template>

Variant

Use the variant prop to change the style of the attachment. Defaults to card, thumbnail only displays the image or file icon with the remove button on its corner and the name available to screen readers. Combine it with the size prop for a larger preview, from 40px with xs to 120px with xl.

landscape.jpg
landscape.jpgJPG
<template>
  <PChatAttachment
    variant="thumbnail"
    name="landscape.jpg"
    type="image/jpeg"
    url="https://picsum.photos/id/1015/200"
    removable
  />
</template>
The orientation prop only applies to the card variant.

Size

Use the size prop to change the size of the attachment. Defaults to md.

quarterly-report.pdfPDF
<template>
  <PChatAttachment size="sm" name="quarterly-report.pdf" type="application/pdf" />
</template>

Removable

Use the removable prop to display a button that emits a remove event.

quarterly-report.pdfPDF
<template>
  <PChatAttachment removable name="quarterly-report.pdf" type="application/pdf" />
</template>

Icon

Use the icon prop to customize the Icon displayed for files that are not images. Defaults to i-lucide-file.

quarterly-report.pdfPDF
<template>
  <PChatAttachment icon="i-lucide-file-text" name="quarterly-report.pdf" type="application/pdf" />
</template>
You can customize this icon globally in your app.config.ts under ui.icons.file key.
You can customize this icon globally in your vite.config.ts under ui.icons.file key.

API

Props

Prop Default Type
as
'div'
any

The element or component this component should render as.

icon
appConfig.ui.icons.file
any

The icon displayed for files that are not images.

loadingIcon
appConfig.ui.icons.loading
any

The icon displayed while the file uploads, over the image if any.

errorIcon
appConfig.ui.icons.error
any

The icon displayed when the upload fails, over the image if any.

removeIcon
appConfig.ui.icons.close
any

The icon of the remove button.

class
any
file
File

The file to display, its name, type and size are read from it and images are previewed.

name
string

The name of the file.

type
string

The media type of the file, images are previewed.

url
string

The URL of the file, used to preview images.

description
string

The text displayed under the name. Defaults to the file extension and size.

status
'idle'
"idle" | "uploading" | "error"

The upload status of the file.

size
'md'
"md" | "xs" | "sm" | "lg" | "xl"
orientation
'horizontal'
"horizontal" | "vertical"

The orientation of the attachment, vertical gives an image a large preview.

variant
'card'
"card" | "thumbnail"

The style of the attachment, thumbnail only shows the preview with the remove button on its corner.

removable
false
boolean

Display a button to remove the file.

ui
{ root?: SlotClass; leading?: SlotClass; leadingIcon?: SlotClass; image?: SlotClass; wrapper?: SlotClass; name?: SlotClass; description?: SlotClass; remove?: SlotClass; }

Emits

Event Type
remove
[event: MouseEvent]

Theme

app.config.ts
export default defineAppConfig({
  ui: {
    chatAttachment: {
      slots: {
        root: 'group/attachment relative flex min-w-0 max-w-full w-fit shrink-0',
        leading: 'relative flex items-center justify-center shrink-0 aspect-square overflow-hidden rounded-lg bg-background-elevated color-text-muted',
        leadingIcon: 'absolute shrink-0',
        image: 'size-full object-cover',
        wrapper: 'grid grid-cols-[minmax(0,1fr)_auto] items-center min-w-0 flex-1',
        name: 'block truncate font-500 color-text-highlighted',
        description: 'block truncate row-start-2 text-xs color-text-muted',
        remove: 'shrink-0'
      },
      variants: {
        size: {
          xs: {
            root: 'gap-1.5 p-1 rounded-lg text-xs data-[orientation=vertical]:w-20 data-[variant=thumbnail]:w-10',
            leading: 'w-7 pohon:rounded-md',
            leadingIcon: 'size-3.5',
            description: 'pohon:text-[10px]/3'
          },
          sm: {
            root: 'gap-2 p-1.5 text-xs data-[orientation=vertical]:w-24 data-[variant=thumbnail]:w-12',
            leading: 'w-8',
            leadingIcon: 'size-4'
          },
          md: {
            root: 'gap-2.5 p-2 text-sm data-[orientation=vertical]:w-30 data-[variant=thumbnail]:w-14',
            leading: 'w-10',
            leadingIcon: 'size-5',
            description: 'mt-0.5'
          },
          lg: {
            root: 'gap-3 p-2.5 text-sm data-[orientation=vertical]:w-36 data-[variant=thumbnail]:w-20',
            leading: 'w-12',
            leadingIcon: 'size-6',
            description: 'mt-0.5'
          },
          xl: {
            root: 'gap-3 p-3 text-base data-[orientation=vertical]:w-44 data-[variant=thumbnail]:w-30',
            leading: 'w-14 pohon:rounded-xl',
            leadingIcon: 'size-7',
            description: 'mt-0.5 pohon:text-sm'
          }
        },
        orientation: {
          horizontal: {
            root: 'items-center pe-2.5',
            remove: 'col-start-2 row-start-1 -my-1 ms-1'
          },
          vertical: {
            root: 'flex-col',
            leading: 'pohon:w-full',
            leadingIcon: 'pohon:size-6',
            wrapper: 'px-1',
            remove: 'absolute top-3 end-3 pohon:rounded-full bg-background/75 backdrop-blur-xs'
          }
        },
        variant: {
          card: {
            root: 'pohon:rounded-xl border border-border bg-background'
          },
          thumbnail: {
            root: 'pohon:p-0',
            leading: 'pohon:w-full outline -outline-offset-1 outline-border',
            wrapper: 'contents',
            name: 'sr-only',
            description: 'sr-only',
            remove: [
              'absolute pohon:-top-1.5 pohon:-end-1.5 pohon:m-0 pohon:p-0 pohon:rounded-full pohon:bg-background-inverted pohon:color-text-inverted pohon:hover:bg-background-inverted/90 pohon:active:bg-background-inverted/90 ring-2 ring-background [@media(hover:hover)]:opacity-0 group-hover/attachment:opacity-100 group-focus-within/attachment:opacity-100',
              'transition-opacity ease-out'
            ]
          }
        },
        status: {
          idle: '',
          uploading: {
            leading: 'has-data-[slot=image]:color-text-highlighted',
            leadingIcon: 'animate-spin',
            image: 'opacity-50'
          },
          error: {
            root: 'pohon:border-error/30',
            leading: 'pohon:bg-error/10 pohon:color-error pohon:outline-error/30',
            image: 'opacity-50',
            description: 'pohon:color-error'
          }
        }
      },
      compoundVariants: [
        {
          variant: 'card',
          orientation: 'horizontal',
          class: {
            root: 'pohon:min-w-40'
          }
        }
      ],
      defaultVariants: {
        size: 'md',
        orientation: 'horizontal',
        status: 'idle',
        variant: 'card'
      }
    }
  }
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from 'pohon-ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      ui: {
        chatAttachment: {
          slots: {
            root: 'group/attachment relative flex min-w-0 max-w-full w-fit shrink-0',
            leading: 'relative flex items-center justify-center shrink-0 aspect-square overflow-hidden rounded-lg bg-background-elevated color-text-muted',
            leadingIcon: 'absolute shrink-0',
            image: 'size-full object-cover',
            wrapper: 'grid grid-cols-[minmax(0,1fr)_auto] items-center min-w-0 flex-1',
            name: 'block truncate font-500 color-text-highlighted',
            description: 'block truncate row-start-2 text-xs color-text-muted',
            remove: 'shrink-0'
          },
          variants: {
            size: {
              xs: {
                root: 'gap-1.5 p-1 rounded-lg text-xs data-[orientation=vertical]:w-20 data-[variant=thumbnail]:w-10',
                leading: 'w-7 pohon:rounded-md',
                leadingIcon: 'size-3.5',
                description: 'pohon:text-[10px]/3'
              },
              sm: {
                root: 'gap-2 p-1.5 text-xs data-[orientation=vertical]:w-24 data-[variant=thumbnail]:w-12',
                leading: 'w-8',
                leadingIcon: 'size-4'
              },
              md: {
                root: 'gap-2.5 p-2 text-sm data-[orientation=vertical]:w-30 data-[variant=thumbnail]:w-14',
                leading: 'w-10',
                leadingIcon: 'size-5',
                description: 'mt-0.5'
              },
              lg: {
                root: 'gap-3 p-2.5 text-sm data-[orientation=vertical]:w-36 data-[variant=thumbnail]:w-20',
                leading: 'w-12',
                leadingIcon: 'size-6',
                description: 'mt-0.5'
              },
              xl: {
                root: 'gap-3 p-3 text-base data-[orientation=vertical]:w-44 data-[variant=thumbnail]:w-30',
                leading: 'w-14 pohon:rounded-xl',
                leadingIcon: 'size-7',
                description: 'mt-0.5 pohon:text-sm'
              }
            },
            orientation: {
              horizontal: {
                root: 'items-center pe-2.5',
                remove: 'col-start-2 row-start-1 -my-1 ms-1'
              },
              vertical: {
                root: 'flex-col',
                leading: 'pohon:w-full',
                leadingIcon: 'pohon:size-6',
                wrapper: 'px-1',
                remove: 'absolute top-3 end-3 pohon:rounded-full bg-background/75 backdrop-blur-xs'
              }
            },
            variant: {
              card: {
                root: 'pohon:rounded-xl border border-border bg-background'
              },
              thumbnail: {
                root: 'pohon:p-0',
                leading: 'pohon:w-full outline -outline-offset-1 outline-border',
                wrapper: 'contents',
                name: 'sr-only',
                description: 'sr-only',
                remove: [
                  'absolute pohon:-top-1.5 pohon:-end-1.5 pohon:m-0 pohon:p-0 pohon:rounded-full pohon:bg-background-inverted pohon:color-text-inverted pohon:hover:bg-background-inverted/90 pohon:active:bg-background-inverted/90 ring-2 ring-background [@media(hover:hover)]:opacity-0 group-hover/attachment:opacity-100 group-focus-within/attachment:opacity-100',
                  'transition-opacity ease-out'
                ]
              }
            },
            status: {
              idle: '',
              uploading: {
                leading: 'has-data-[slot=image]:color-text-highlighted',
                leadingIcon: 'animate-spin',
                image: 'opacity-50'
              },
              error: {
                root: 'pohon:border-error/30',
                leading: 'pohon:bg-error/10 pohon:color-error pohon:outline-error/30',
                image: 'opacity-50',
                description: 'pohon:color-error'
              }
            }
          },
          compoundVariants: [
            {
              variant: 'card',
              orientation: 'horizontal',
              class: {
                root: 'pohon:min-w-40'
              }
            }
          ],
          defaultVariants: {
            size: 'md',
            orientation: 'horizontal',
            status: 'idle',
            variant: 'card'
          }
        }
      }
    })
  ]
})

Changelog

No recent changes