Files
FilePreview
Preview a selected image or PDF and keep a download action available alongside the file name and size.
import '@reactloom/files/styles.css';
import { FilePreview } from '@reactloom/files';Examples
Local preview picker
import { useState } from 'react';
import { FilePreview } from '@reactloom/files';
import { Stack } from '@reactloom/ui';
export default function Example() {
const [file, setFile] = useState<File>();
return <Stack gap="3">
<label>Choose an image or PDF <input type="file" accept="image/png,image/jpeg,image/webp,application/pdf"
onChange={(event) => setFile(event.target.files?.[0])} /></label>
{file && <FilePreview file={file} />}
</Stack>;
}Download fallback
Preparing preview…
import { FilePreview } from '@reactloom/files';
import { useState } from 'react';
export default function Example() {
const [file] = useState(() => new File(['Payment reconciliation report'], 'reconciliation.txt', { type: 'text/plain' }));
return <FilePreview file={file} messages={{ unavailable: 'Download this report to view it in a text editor.' }} />;
}API reference
| Prop | Type | Default |
|---|---|---|
fileThe browser File to display. | File | — |
altImage description. Falls back to the filename. | string | — |
messagesCustomize preview and download text. | Partial<FilePreviewProps["messages"]> | — |
Keyboard interaction
| Key | Action |
|---|---|
| Tab | Focus the download link or the native PDF viewer |
| Enter | Download the file |
Usage guidelines
The root figure accepts className, style, and a ref.
Do
- Supported raster image formats include PNG, JPEG, GIF, WebP, BMP, and AVIF. In browsers with an inline PDF viewer, PDFs open within the preview. Downloads are available for other formats.
Don’t
- Avoid requiring an inline preview to access the file.