API Reference

React API

React components, hooks, props, and imperative handles from @imposia/react.

@imposia/react binds one Core controller and one canonical iframe into the React lifecycle. It also re-exports the Core and Viewer API surface, so a React application usually needs no other Imposia import. Import @imposia/react/styles.css once.

ImposiaPageViewer and ImposiaDocument

ImposiaPageViewer mounts a Core page document, then binds the page Viewer after the first commit. ImposiaDocument is a compatibility wrapper with the same public props and handle types: ImposiaDocumentProps = ImposiaPageViewerProps and ImposiaDocumentHandle = ImposiaPageViewerHandle.

app/report-preview.tsx
const viewer = useRef<ImposiaPageViewerHandle>(null);

<ImposiaPageViewer
  ref={viewer}
  source={{ html: "<main><h1>Report</h1></main>" }}
  documentOptions={{ page: { size: "A4", margin: "18mm" } }}
  viewerOptions={{ mode: "single", inspector: true }}
  onError={console.error}
/>;
PropTypeRequiredPurpose
sourcePageSourceYesHTML or light DOM to paginate.
preparation"immediate" | "idle" | "manual"NoSchedules the first preparation; default "immediate".
sourceRevisionstring | numberNoForces an update when source identity/content comparison is otherwise unchanged.
documentOptionsPageDocumentOptionsNoCore pagination options.
documentOptionsRevisionstring | numberNoRemounts Core with new document options.
viewerOptionsPageViewerOptionsNoPage presentation, reader, and inspector options.
onViewerStateChange(state: PageViewerState) => voidNoReceives page, zoom, mode, effective-mode, and generation changes.
className, stylestring, CSSPropertiesNoHost div presentation.
onReady(document: PageDocument) => voidNoRuns after the document and Viewer binding are ready.
onError(error: unknown) => voidNoReceives Core or Viewer binding failures.
onStateChange(state: ImposiaDocumentState) => voidNoReceives idle, loading, ready, aborted, and error.

Options props are read only when their revision changes

documentOptions is captured when the controller mounts. Passing a changed options object โ€” a new resolver, extension set, or page geometry โ€” does nothing by itself: bump documentOptionsRevision to remount the controller with the new options. The same rule applies to publicationOptions and publicationOptionsRevision below. Only source (and snapshot) changes are detected on their own.

ImposiaPageViewerHandle

Handle methods act on the latest committed generation. Methods that need the Viewer or a commit throw (or reject) before the first commit and after unmount.

MemberReturnsConstraint
currentPageDocument | undefinedCurrent commit while mounted; undefined otherwise.
viewerStatePageViewerState | undefinedCurrent presentation snapshot while ready.
goToPage(page), nextPage(), previousPage()voidMove through the committed global page sequence.
setZoom(zoom)voidRounds and clamps the presentation zoom.
setMode(mode)voidAccepts continuous, single, or spread.
setSpreadCover(cover)voidPlaces page 1 alone, pairing 2โ€“3 onward.
openInspector(), closeInspector(), toggleInspector()voidRequire viewerOptions.inspector: true.
selectWarning(warning)voidRequires the inspector and a warning from its current generation.
print()Promise<void>Opens native print for the latest completed pages. Readers can choose Save as PDF; the method does not return PDF bytes.
exportEpub(options)Promise<Blob>Exports the current commit as a reflowable EPUB.

Deferred preparation and retry (0.7.0)

preparation is available on ImposiaPageViewer, its ImposiaDocument wrapper, and useImposiaDocument. "immediate" (default) starts on mount; "idle" uses requestIdleCallback with a 1500 ms timeout, or a 200 ms timer when unavailable; "manual" waits for prepare() or retry(). Changing the policy does not pause an already started controller. Subsequent source/revision changes update normally after preparation has started.

The hook result and component handle both expose prepare(): void and retry(): void. prepare() starts once; repeated calls do nothing. retry() starts if needed, remounts after a synchronous mount failure, or calls update with the latest source on the existing controller. Options are captured at mount; increment documentOptionsRevision to apply changed options. Retry does not replace an already-aborted documentOptions.signal; provide a fresh signal and option revision for that case.

In ready, state.source identifies the source used for that committed document. In loading, error, or aborted, a previous committed state.document may remain, but state.source is absent. All five statuses are idle, loading, ready, aborted, and error. Abortion is terminal and does not call onError.

const { hostRef, state, prepare, retry } = useImposiaDocument({
  source: { html }, preparation: "manual",
});
return <>
  <button onClick={prepare}>Prepare preview</button>
  {state.status === "error" && <button onClick={retry}>Retry</button>}
  <div ref={hostRef} aria-busy={state.status === "loading"} />
</>;

Viewer fit, zoomable, and keyboard changes remount the presentation binding while preserving the current page, uncontrolled mode/zoom, and Core iframe. The deferred preparation API is for a single document; useImposiaPublication does not expose these methods.

ImposiaPublicationViewer

app/guide-preview.tsx
const publication = useRef<ImposiaPublicationViewerHandle>(null);

<ImposiaPublicationViewer
  ref={publication}
  snapshot={{
    metadata: { title: "Guide", language: "en" },
    entries: [{ id: "intro", title: "Introduction", html: "<h1>Introduction</h1>" }],
  }}
/>;

Its required snapshot prop accepts PublicationSnapshot. Optional props are snapshotRevision, publicationOptions, publicationOptionsRevision, viewerOptions (without reader), readerOptions (without controller), className, style, onReady, onError, and onStateChange. Revision props force updates/remounts as their names indicate. The component supplies the reader/controller binding itself, which is why reader and controller are excluded from its option props.

ImposiaPublicationViewerHandle

All actions except current and resolveDestination throw after unmount. Reader actions also throw before the Publication reader is ready.

MemberReturnsEffect or constraint
currentPublicationDocument | undefinedCurrent commit while mounted.
resolveDestination(id)PublicationDestination | undefinedundefined before commit, after unmount, or for an unknown ID.
navigate(destination)voidA stale destination throws.
openTableOfContents(), closeTableOfContents(), toggleTableOfContents()voidControl the outline panel.
openThumbnails(), closeThumbnails(), toggleThumbnails()voidControl the thumbnail panel.
getThumbnails()readonly PublicationThumbnail[]Current-generation thumbnails.
selectThumbnail(thumbnail)voidA thumbnail from another generation throws.
restoreDeepLink(value)PublicationDestination | undefinedResolves and navigates a valid current link.
openSearch(), closeSearch(), toggleSearch()voidControl the search panel.
search(query)readonly PublicationSearchResult[]Searches the current commit.
nextSearchResult(), previousSearchResult()PublicationSearchResult | undefinedMove through results.
selectSearchResult(result)voidNavigates to a result.
setMode(mode), setSpreadCover(cover)voidControl page presentation.
openInspector(), closeInspector(), toggleInspector()voidRequire viewerOptions.inspector: true.
selectWarning(warning)voidRequires an inspector warning from the current generation.
print()Promise<void>Opens native print for the latest commit, including the browser's Save as PDF option.
exportEpub(options)Promise<Blob>Exports the latest commit.

useImposiaDocument and useImposiaPublication

useImposiaDocument(props: UseImposiaDocumentProps): UseImposiaDocumentResult;
useImposiaPublication(props: UseImposiaPublicationProps): UseImposiaPublicationResult;

Both hooks return a hostRef, lifecycle state, and a controller that is undefined until the mount effect creates it. Attach hostRef to one div. A loading or error state can retain the last committed document/publication.

useImposiaDocument accepts the page component's source, revision, options, and callbacks. HTML sources update when HTML/base URL changes; light-DOM sources update when node identity changes. sourceRevision forces an update, and documentOptionsRevision remounts the controller.

useImposiaPublication accepts the publication component's snapshot, revision, options, and callbacks. It updates when the snapshot reference or snapshotRevision changes; publicationOptionsRevision remounts the controller. Both hooks destroy their controller on cleanup and treat AbortError as lifecycle cancellation.

app/article.tsx
function Article({ html }: { html: string }) {
  const { hostRef, state } = useImposiaDocument({ source: { html } });
  return <div ref={hostRef} aria-busy={state.status === "loading"} />;
}

On this page