Document viewer
Pages as images or a render callback, thumbnails, zoom 50–300 %, navigation, search, overlay.
import { DocumentViewer, fitScale, stepZoom } from "@forgeron/ui";import { DocumentViewer } from "@forgeron/ui";
const page = (n: number) => ({
id: `p${n}`,
width: 600,
height: 800,
alt: `Page ${n} of the agreement`,
src: `data:image/svg+xml;utf8,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="600" height="800"><rect width="600" height="800" fill="#fff"/><text x="48" y="80" font-family="monospace" font-size="22" fill="#222">Service agreement - page ${n}</text>${Array.from({ length: 14 }, (_, i) => `<rect x="48" y="${130 + i * 28}" width="${i % 5 === 4 ? 300 : 504}" height="8" fill="#ccc"/>`).join("")}</svg>`,
)}`,
});
const pages = [1, 2, 3].map(page);
export default function Default() {
return (
<div style={{ width: "100%" }}>
<DocumentViewer label="Service agreement" pages={pages} height={460} />
</div>
);
}import { DocumentViewer } from "@forgeron/ui";
const page = (n: number) => ({
id: `p${n}`,
width: 600,
height: 800,
alt: `Page ${n} of the agreement`,
src: `data:image/svg+xml;utf8,${encodeURIComponent(
`<svg xmlns="http://www.w3.org/2000/svg" width="600" height="800"><rect width="600" height="800" fill="#fff"/><text x="48" y="80" font-family="monospace" font-size="22" fill="#222">Service agreement - page ${n}</text>${Array.from({ length: 14 }, (_, i) => `<rect x="48" y="${130 + i * 28}" width="${i % 5 === 4 ? 300 : 504}" height="8" fill="#ccc"/>`).join("")}</svg>`,
)}`,
});
const pages = [1, 2, 3].map(page);
export default function SearchAndOverlay() {
return (
<div style={{ width: "100%" }}>
<DocumentViewer
label="Service agreement"
pages={pages}
height={460}
thumbnails={false}
onSearch={async (query) =>
query.toLowerCase().includes("page") ? [{ page: 1 }, { page: 3 }] : []
}
overlay={({ number, width }) =>
number === 1 ? (
<span
style={{
position: "absolute",
top: 8,
right: 8,
padding: "2px 6px",
background: "var(--accent)",
color: "var(--accent-ink)",
font: "500 11px/16px var(--font-mono)",
textTransform: "uppercase",
}}
title={`${width}px wide`}
>
Draft
</span>
) : null
}
/>
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | string | — | Names the viewer. |
pages | { id?, src?, alt?, width, height, render? }[] | — | width / height at 100 %. src draws an image; render(info) draws anything (a PDF canvas, an invoice). |
page / defaultPage / onPageChange | number | 1 | The page in view, from 1. |
zoom / defaultZoom / onZoomChange | "fit" | number | "fit" | Fit width, or 50 to 300 (percent). |
thumbnails | boolean | true | The column of thumbnails, with a button to hide it. |
overlay | (info: { page, number, scale, width, height }) => ReactNode | — | Drawn over each page, in its box: fields, stamps, highlights. |
onSearch | (query) => { page, text? }[] | Promise<…> | — | Looks for text. Absent, there is no search box. Enter searches, Shift Enter goes back. |
toolbar / height | ReactNode; number | string | 560 | Extras at the end of the toolbar; the height. |
className | string | — | Extra classes. |