FilePreview
Show any file with the right viewer, picked from its name or type, with a download fallback. Pairs with FileUpload.
Status: beta. Category: Viewers.
import { FilePreview, FilePreviewDialog } from '@glyphui/viewers'
Exports: FilePreview, filePreviewStyles, FilePreviewDialog, detectFileKind, formatBytes.
Notes
- Images, video, audio, JSON and plain text work with no extra packages.
- PDF, CSV, spreadsheets, code and Markdown need their viewer’s packages. Pass loaders for the ones you use, e.g. loaders={{ pdf: () => import("@glyphui/viewers/pdf") }}, so your bundle only includes what you can show.
- Files without a viewer get a clear message and a download button.
FilePreview props
| Prop | Type | Default | Description |
|---|---|---|---|
file (required) | ViewerSource | The file: a File/Blob (e.g. from FileUpload), a URL, or bytes. | |
bordered | boolean | Draw the preview as a bordered card. Default true. | |
className | string | ||
header | boolean | Show the header with name, size and download. Default true. | |
height | string | number | Height of the preview. Default 520. | |
loaders | ViewerLoaders | Viewers that need extra packages (PDF, CSV, spreadsheets, code, Markdown). | |
name | string | File name, when file isn't a File. Used to pick the viewer and for downloads. | |
type | string | MIME type, when the name doesn't tell. |
Example: Every kind of file
Pick a sample file; the matching viewer loads on demand.
import { Select, SelectItem } from '@glyphui/ui'
import { FilePreview, type ViewerLoaders } from '@glyphui/viewers'
import { useState } from 'react'
import { fixture } from '../../../lib/fixtures'
// Viewers with extra dependencies load only when a file needs them.
const loaders: ViewerLoaders = {
pdf: () => import('@glyphui/viewers/pdf'),
csv: () => import('@glyphui/viewers/csv'),
spreadsheet: () => import('@glyphui/viewers/spreadsheet'),
code: () => import('@glyphui/viewers/code'),
markdown: () => import('@glyphui/viewers/markdown'),
}
const files = [
'report.pdf',
'customers.csv',
'sales.xlsx',
'workspace.json',
'reminders.ts',
'README.md',
'aurora.jpg',
'clip.webm',
'chime.wav',
'archive.zip',
]
export default function Example() {
const [name, setName] = useState('report.pdf')
return (
<div className="grid w-full gap-3">
<Select value={name} onValueChange={setName} aria-label="Sample file" className="w-56">
{files.map((file) => (
<SelectItem key={file} value={file}>
{file}
</SelectItem>
))}
</Select>
<FilePreview key={name} file={fixture(name)} name={name} loaders={loaders} height={520} />
</div>
)
}
Example: Previews for uploads
Add files, then open any of them in a dialog.
import { Button, type FileItem, FileUpload } from '@glyphui/ui'
import { FilePreviewDialog, formatBytes, type ViewerLoaders } from '@glyphui/viewers'
import { Eye } from 'lucide-react'
import { useState } from 'react'
const loaders: ViewerLoaders = {
pdf: () => import('@glyphui/viewers/pdf'),
csv: () => import('@glyphui/viewers/csv'),
spreadsheet: () => import('@glyphui/viewers/spreadsheet'),
code: () => import('@glyphui/viewers/code'),
markdown: () => import('@glyphui/viewers/markdown'),
}
export default function Example() {
const [files, setFiles] = useState<FileItem[]>([])
return (
<div className="grid w-full max-w-lg gap-4">
<FileUpload
files={files}
onFilesChange={setFiles}
maxFiles={6}
hint="Any files. They stay on your device."
/>
{files.length > 0 && (
<ul className="grid gap-2">
{files.map(({ id, file }) => (
<li
key={id}
className="flex items-center justify-between gap-3 rounded-md border border-border px-3 py-2 text-small"
>
<span className="min-w-0 truncate">
{file.name}{' '}
<span className="text-muted-foreground">· {formatBytes(file.size)}</span>
</span>
<FilePreviewDialog
file={file}
loaders={loaders}
trigger={
<Button size="sm" variant="outline" leftIcon={<Eye />}>
Preview
</Button>
}
/>
</li>
))}
</ul>
)}
</div>
)
}
Live page: https://glyphui.xyz/components/file-preview