Installation
Available Hooks
The Preact adapter exports four hooks, one for each zoom mode:useZoomImageWheel- Zoom with mouse wheel/pinch gesturesuseZoomImageHover- Zoom on hover with a separate zoom targetuseZoomImageMove- Zoom follows mouse/touch movementuseZoomImageClick- Toggle zoom on click
Hook Return Values
Each hook returns an object with:createZoomImage- Function to initialize zoom on a container elementzoomImageState- Current state object (varies by zoom mode)setZoomImageState- Function to update state (available for wheel and hover modes)
Wheel Zoom
Zoom in and out using the mouse wheel or pinch gestures. Supports programmatic zoom control, rotation, and image cropping.Basic Example
Complete Example with Controls
State Properties
currentZoom: number- Current zoom level (1 = 100%)enable: boolean- Whether zoom is enabledcurrentPositionX: number- X position offsetcurrentPositionY: number- Y position offsetcurrentRotation: number- Rotation angle in degrees
Hover Zoom
Display a zoomed version in a separate container when hovering over the image.Basic Example
State Properties
enabled: boolean- Whether hover is activezoomedImgStatus: string- Loading status (“idle” | “loading” | “loaded”)
Move Zoom
Zoom follows the mouse or touch movement within the image container.Basic Example
State Properties
zoomedImgStatus: string- Loading status (“idle” | “loading” | “loaded”)
Click Zoom
Toggle between zoomed and normal states by clicking the image.Basic Example
State Properties
zoomedImgStatus: string- Loading status (“idle” | “loading” | “loaded”)
TypeScript Support
All hooks are fully typed. Import types from@zoom-image/core:
Cleanup
All hooks automatically clean up on component unmount. The cleanup is handled internally using Preact’suseEffect cleanup function.
Differences from React
The Preact adapter is nearly identical to the React adapter but usespreact/hooks instead of react. If you’re familiar with the React adapter, you’ll feel right at home.
Key differences:
- Import hooks from
preact/hooksinstead ofreact - Use
classinstead ofclassNamefor CSS classes (Preact supports both)