Skip to content

Commit a0dac69

Browse files
authored
feat: add support for custom offset when zooming to element (#535)
1 parent f5677c5 commit a0dac69

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/core/handlers/handlers.logic.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export const zoomToElement =
100100
scale?: number,
101101
animationTime = 600,
102102
animationType: keyof typeof animations = "easeOut",
103+
offsetX = 0,
104+
offsetY = 0,
103105
): void => {
104106
handleCancelAnimation(contextInstance);
105107

@@ -109,7 +111,13 @@ export const zoomToElement =
109111
typeof node === "string" ? document.getElementById(node) : node;
110112

111113
if (wrapperComponent && target && wrapperComponent.contains(target)) {
112-
const targetState = calculateZoomToNode(contextInstance, target, scale);
114+
const targetState = calculateZoomToNode(
115+
contextInstance,
116+
target,
117+
scale,
118+
offsetX,
119+
offsetY,
120+
);
113121
animate(contextInstance, targetState, animationTime, animationType);
114122
}
115123
};

src/core/handlers/handlers.utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ export function calculateZoomToNode(
142142
contextInstance: ReactZoomPanPinchContext,
143143
node: HTMLElement,
144144
customZoom?: number,
145+
customOffsetX = 0,
146+
customOffsetY = 0,
145147
): { positionX: number; positionY: number; scale: number } {
146148
const { wrapperComponent, contentComponent, transformState } =
147149
contextInstance;
@@ -176,8 +178,10 @@ export function calculateZoomToNode(
176178
const offsetX = (wrapperRect.width - nodeWidth * newScale) / 2;
177179
const offsetY = (wrapperRect.height - nodeHeight * newScale) / 2;
178180

179-
const newPositionX = (wrapperRect.left - nodeLeft) * newScale + offsetX;
180-
const newPositionY = (wrapperRect.top - nodeTop) * newScale + offsetY;
181+
const newPositionX =
182+
(wrapperRect.left - nodeLeft) * newScale + offsetX + customOffsetX;
183+
const newPositionY =
184+
(wrapperRect.top - nodeTop) * newScale + offsetY + customOffsetY;
181185

182186
const bounds = calculateBounds(contextInstance, newScale);
183187

0 commit comments

Comments
 (0)