File viewer

Hand it a file and it works out how to show it. There is no renderer prop and nothing to configure per type: a viewer that asks the caller how to draw a PDF has not saved the caller anything.

Reach for it

Showing a document the user has, whatever kind of file it turns out to be.

Not for

Choosing a file to upload. That is File upload, and it comes first.

One prop, five kinds of file

The declared type decides first and the extension second, because a download endpoint that answers application/octet-stream for every document is common enough that the type cannot be the only signal. A type with no preview says so and still offers the file.

invoice-INV-2024-0042.pdfDownload
Loading the document

How much room it takes

Compact sits beside other content as a preview. Fill takes whatever the parent gives it, for a drawer or a page that is only this. The body scrolls inside whichever is chosen, so the frame never grows with the document.

proof-of-delivery.svgDownload
Loading the document
proof-of-delivery.svgDownload
Loading the document

While the caller is still fetching

isLoading is separate from the viewer's own reading of the file. A screen that has not received the document yet shows the same frame and the same spinner, rather than flashing an empty box first and then a loading one.

invoice-INV-2024-0042.pdfDownload
Loading the document

API reference

FileViewer

PropTypeDefaultDescription
file*File | Blob | string-The file itself, or a URL to it. A `File` or `Blob` is shown from memory. A string is treated as a URL and loaded with the document's own credentials.
downloadLabel*string-Names the download action, e.g. "Download this invoice".
loadingLabel*string-Announced while the file is being read, e.g. "Loading the document".
errorLabel*string-Shown when the file cannot be read or is not valid, e.g. "This file could not be opened".
unsupportedLabel*string-Shown for a type with no preview, e.g. "There is no preview for this kind of file".
collapseLabel*string-Names the fold button of an open node in a JSON file, e.g. "Collapse".
expandLabel*string-Names the fold button of a closed node in a JSON file, e.g. "Expand".
data-testidstring-Names the element for a test. Never rendered, never announced.
height'compact' | 'tall' | 'fill'tall
fileNamestring-What the file is called. Taken from a `File` or from the last segment of a URL when it is not given. It names the frame for assistive technology and is what a download is saved as, so a `Blob` from an API is worth naming.
isLoadingboolean-The caller is still fetching the file. Separate from this component's own reading: a screen that has not received the document yet should show the same frame and the same spinner, rather than flashing an empty box first.