Blocks / Document viewer

Document viewer

Pages as images or a render callback, thumbnails, zoom 50–300 %, navigation, search, overlay.

import { DocumentViewer, fitScale, stepZoom } from "@forgeron/ui";
/01 → Default
Page 1 of the agreement
Page 2 of the agreement
Page 3 of the agreement
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>
  );
}
/02 → Search and overlay
Page 1 of the agreement
Draft
Page 2 of the agreement
Page 3 of the agreement
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
PropTypeDefaultDescription
labelstring—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 / onPageChangenumber1The page in view, from 1.
zoom / defaultZoom / onZoomChange"fit" | number"fit"Fit width, or 50 to 300 (percent).
thumbnailsbooleantrueThe 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 / heightReactNode; number | string560Extras at the end of the toolbar; the height.
classNamestring—Extra classes.