Controlled state
Own the page, zoom, rotation and fullscreen state in your app, or let the viewer keep it, with one consistent pattern.
Every piece of viewer state follows the same trio: x controls the value, defaultX sets its initial value while uncontrolled, and onXChange reports every change in both modes.
| State | Controlled | Initial | Change |
|---|---|---|---|
| Page | page | defaultPage | onPageChange |
| Zoom | scale | defaultScale | onScaleChange |
| Rotation | rotation | defaultRotation | onRotationChange |
| Fullscreen | fullscreen | defaultFullscreen | onFullscreenChange |
Basics
Pass a value and its change handler to put that state in your app.
The toolbar and keyboard keep working: they call your handlers, and the viewer shows whatever you pass back. Out-of-range values are clamped and reported: “Go to page 99” shows page 40 and calls onPageChange(40).
Mixing modes
Control only what you need. Leave x undefined to let the viewer keep that state, and use defaultX to choose where it starts; the demo under Customization opens on page 5 at 125%, rotated.
Switching a prop between controlled and uncontrolled during the component’s life logs a development warning, as React inputs do.
The same pattern in your components
useControllableState is the hook the viewer uses. Build your own controls with the same contract.
The setter is stable and accepts a value or an updater function; onChange fires only when the value actually changes.
Customization
Start an uncontrolled viewer exactly where you want it. The zoom reset button returns to defaultScale.
For styling, see the customization guide.
Advanced / escape hatch
For one-off commands (“go to page 3”) without owning the state, use the imperative API.
Limitations
- Values cannot be functions.
useControllableStatetreats a function passed to the setter as an updater. Workaround: wrap function values in an object. - New documents reset uncontrolled state only. A new
sourceopens ondefaultPage; a controlledpageis yours to reset. Workaround: reset your state when you change the source.
API
useControllableState— The value / defaultValue / onValueChange pattern as a hook: the one behind every controlled prop of the viewer.UseControllableStateOptions— Options for useControllableState: the controlled value, the initial value while uncontrolled, and the change callback.SetControllableState— Setter returned by useControllableState; accepts a value or an updater function.