aiellie

Search

Find a component, template or page

GitHub
Design

File Preview

A file opened in a dialog: an image, a PDF, text or code highlighted by language, or the files in a folder, with a download button that zips a folder.

greet.tsTS · 67 B
src3 files
Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/file-preview"

shadcn needs the @aiellie registry in components.json first. npx aiellie add file-preview adds it on its first run, then installs the same way.

File preview opens an attachment in a dialog to look at before it's sent, or after. Images and PDFs show as themselves, text and code are read in and highlighted, and a folder lists every file inside. Its footer downloads the file, zipping a folder first, and can remove it from the message.

Getting started

  1. Describe the file

    A FilePreviewItem wraps the File the browser gave you. Images and PDFs need a url to show; make one with URL.createObjectURL.

    const item: FilePreviewItem = {
      name: file.name,
      kind: file.type.startsWith("image/") ? "image" : "file",
      size: file.size,
      url: URL.createObjectURL(file),
      files: [file],
    }
  2. Open it from an attachment

    Keep one dialog for the whole message and change what it shows. item can be null while nothing is chosen.

    components/composer-files.tsx
    "use client"
    
    import * as React from "react"
    
    import { File02Icon } from "@hugeicons/core-free-icons"
    
    import { AttachmentFile, Attachments } from "@/components/aiellie/attachments"
    import {
      FilePreview,
      describe,
      type FilePreviewItem,
    } from "@/components/ui/file-preview"
    
    export function ComposerFiles({
      items,
      onRemove,
    }: {
      items: FilePreviewItem[]
      onRemove: (item: FilePreviewItem) => void
    }) {
      const [shown, setShown] = React.useState<FilePreviewItem | null>(null)
      const [open, setOpen] = React.useState(false)
    
      return (
        <>
          <Attachments>
            {items.map((item) => (
              <AttachmentFile
                key={item.name}
                icon={File02Icon}
                name={item.name}
                description={describe(item)}
                onOpen={() => {
                  setShown(item)
                  setOpen(true)
                }}
                onRemove={() => onRemove(item)}
              />
            ))}
          </Attachments>
          <FilePreview
            item={shown}
            open={open}
            onOpenChange={setOpen}
            onRemove={() => {
              if (shown) onRemove(shown)
              setOpen(false)
            }}
          />
        </>
      )
    }

Anatomy

<div data-slot="dialog-content">
  <div data-slot="dialog-header">
    <h2 data-slot="dialog-title">{item.name}</h2>
    <p data-slot="dialog-description">{describe(item)}</p> {/* TS · 2 KB, or 3 files */}
  </div>
  {/* one of: */}
  <img /> {/* an image with a url */}
  <iframe /> {/* a PDF, or anything else with a url */}
  <pre /> {/* text and code, highlighted */}
  <ul /> {/* a folder's files, by path */}
  <div /> {/* nothing to show */}
  <div data-slot="dialog-footer">
    <button /> {/* Remove, only with onRemove */}
    <button /> {/* Close */}
    <button /> {/* Download, zipping a folder */}
  </div>
</div>

Text is read up to the first 100 KB, with a note when there's more, and highlighted by the file's extension once the highlighter has loaded. The preview area is capped at 60% of the viewport and scrolls inside. A folder downloads as name.zip, built in the browser with fflate, keeping each file's path.

Examples

Any file

Choose an image, a PDF or a text file from your computer.

A folder

For a folder, files holds every file in it. A file's webkitRelativePath is what the list shows and what the zip keeps; an input with webkitdirectory fills it in.

<input
  type="file"
  webkitdirectory=""
  onChange={(event) => {
    const files = [...(event.target.files ?? [])]
    const name = files[0]?.webkitRelativePath.split("/")[0] ?? "folder"
    show({ name, kind: "folder", files })
  }}
/>

API reference

FilePreview

PropTypeDefaultDescription
itemFilePreviewItem | nullrequiredWhat to show.
openbooleanrequiredWhether the dialog is open.
onOpenChange(open: boolean) => voidrequiredCalled to open or close it.
onRemove() => voidAdds a Remove button. Left out where the file can no longer be taken back, like a message already sent.

FilePreviewItem

FieldTypeDescription
idstringKeeps what the preview has read apart between files. Defaults to the name.
namestringThe file's or the folder's name.
kind"file" | "image" | "folder"What it is.
sizenumberIn bytes. Left out for a folder.
urlstringA link to it made in the browser, for an image or a PDF to show.
filesFile[]One for a file or an image, every file inside for a folder.

Helpers

ExportTypeDescription
describe(item)(item: FilePreviewItem) => stringA short line about it: TS · 2 KB, or 3 files for a folder.
formatSize(bytes)(bytes: number) => string512 B, 12 KB, 1.4 MB.
isText(file)(file: File) => booleanWhether a file is text the preview can read in.