Zoom and fit to screen

Fit the document to the viewport, control zoom with useZoom, and let the review pane shrink the page.

Set a zoom mode

Pass zoomMode, or pass zoom for a fixed scale.

<DocxEditor document={bytes} />                          {/* auto */}
<DocxEditor document={bytes} zoomMode={{ type: 'fit', fit: 'pageWidth' }} />
<DocxEditor document={bytes} zoom={1} zoomMode={{ type: 'fixed' }} />
ModeBehavior
'auto' (default)Fits the page width between 50% and 100%. A narrower container scrolls horizontally.
{ type: 'fit', fit: 'pageWidth' }Fits the page width in both directions. A wide window magnifies the page. Accepts minZoom and maxZoom.
{ type: 'fixed' }Uses the zoom scale at every container size.

Passing zoom without zoomMode selects fixed mode. Fit modes update when the container, comments pane, or navigation pane changes size.

Build a zoom control

Call useZoom inside DocxEditor.Root.

import { useZoom } from '@docx-editor.dev/react';

function ZoomControl() {
  const { zoom, isFit, auto, zoomIn, zoomOut, canZoomIn, canZoomOut } = useZoom();

  return (
    <div>
      <button type="button" aria-label="Zoom out" onClick={zoomOut} disabled={!canZoomOut}>
        &minus;
      </button>
      <span>{Math.round(zoom * 100)}%</span>
      <button type="button" aria-label="Zoom in" onClick={zoomIn} disabled={!canZoomIn}>
        +
      </button>
      <button type="button" onClick={auto} aria-pressed={isFit}>
        Fit
      </button>
    </div>
  );
}
MemberDescription
zoomResolved scale. 1 is 100%.
mode, isFitSource of the scale. Derive selected state from these, not from zoom.
setZoom(n)Sets a fixed scale and leaves fit mode.
setMode(m)Sets 'auto', a fit, or { type: 'fixed' }.
auto(), fitToWidth()Selects one of the two fits.
reset()Selects fixed 100%.
zoomIn(), zoomOut()Moves to the next preset in levels.
canZoomIn, canZoomOutWhether a higher or lower preset remains.

Without an adapter, call the same operations on the editor: setZoomMode(), setZoom(), getZoom(), and getZoomMode(). snapshot() includes zoom and zoomMode.

Make room for the review pane

Set the review pane overflow to 'shrinkPage' so a fit shrinks the page to show the full card column.

const MODULES = [reviewModule({ pane: { overflow: 'shrinkPage' } })];

<DocxEditor
  document={bytes}
  modules={MODULES}
  zoomMode={{ type: 'fit', fit: 'pageWidth', minZoom: 0.35, maxZoom: 1 }}
/>;

If you omit minZoom, the page can shrink to 10%. Set a readable minimum, such as 0.35. To change the setting at runtime, call editor.setReviewPaneOptions({ overflow: 'float' }).

For the other overflow values, see Control the review pane.

Limits

  • Selecting a scale or preset leaves fit mode. Resizing then keeps that scale.
  • Fixed zoom ignores the 'shrinkPage' setting.
  • With custom fit bounds, the packaged menu shows the percentage without a checkmark.
  • setReviewPaneOptions rejects an unknown field or value with invalidArgs and keeps the previous settings.
  • The editor emits selectionChange when zoom or zoomMode changes, including after a resize.

See also