Functions
openPreview(input, options?)
Section titled “openPreview(input, options?)”Opens the full-screen modal and returns a handle. One modal is reused per document, so calling it twice never fights over focus.
const handle = openPreview(files, { index: 0, // which file in a gallery to open on theme: 'dark', // 'light' | 'dark' — omit to follow the OS onLoad: ({ source, viewer }) => {}, onError: ({ source, error }) => {}, onChange: ({ index, source }) => {}, onClose: () => {},});
handle.element; // the <file-preview-modal>handle.go(2);handle.close();await handle.closed; // settles when the modal closes, however it was closedopenGallery is an alias that reads better when you are passing an array.
It is safe to call from inside another library’s modal, such as a Radix or shadcn Dialog or
Sheet. The preview stacks on top, takes focus and Escape for itself, and keeps its clicks,
focus changes and scrolling from reaching the modal underneath, so that modal stays open. When the
preview closes, focus goes back to the control that opened it.
renderPreview(target, input, options?)
Section titled “renderPreview(target, input, options?)”Renders inline into an element or a selector, and returns the <file-preview> element.
const viewer = renderPreview('#viewer', '/report.pdf', { theme: 'dark', toolbar: true, bordered: true, minHeight: '520px',});
viewer.file = '/another.xlsx'; // swap without re-renderingWhat you can pass
Section titled “What you can pass”Every entry point accepts the same shapes:
| Input | Example |
|---|---|
| URL string | '/report.pdf' |
File |
from an <input type="file"> or a drop event |
Blob |
new Blob([bytes], { type: 'application/pdf' }) |
ArrayBuffer |
bytes you already hold |
| Data URI | 'data:application/pdf;base64,…' |
| Descriptor object | see below |
| An array of any | becomes a gallery |
Descriptor objects
Section titled “Descriptor objects”Use one when the URL alone doesn’t carry enough: a signed link with no extension, or an endpoint that needs credentials.
openPreview({ url: 'https://api.example.com/files/8fa21c', name: 'Q3 report.xlsx', // gives the detector an extension to work with type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', headers: { Authorization: `Bearer ${token}` }, credentials: 'include',});Utilities
Section titled “Utilities”| Export | Purpose |
|---|---|
configure |
Set library-wide options. See configuration. |
getConfig |
Read the current configuration back. |
registerViewer |
Add or override a viewer. See custom viewers. |
listViewers |
A copy of the registry, for debugging. |
toSource |
Normalise any accepted input into a Source. |
describe |
Run type detection over { name, mime, bytes }. |
formatBytes |
1048576 becomes "1.0 MB". |
Kind |
The viewer categories: IMAGE, PDF, DOC, SHEET and so on. |
PreviewError |
The error class every failure uses. |