react-zoom-pan-pinch 4.0.6 → 4.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-zoom-pan-pinch",
3
- "version": "4.0.6",
3
+ "version": "4.0.7",
4
4
  "packageManager": "yarn@4.9.2",
5
5
  "description": "Zoom and pan html elements in easy way.",
6
6
  "author": "prc5",
@@ -1,6 +1,7 @@
1
- import React, { useContext, useEffect, useRef } from "react";
1
+ import React, { useContext, useRef } from "react";
2
2
 
3
3
  import { mergeRefs } from "utils/ref.utils";
4
+ import { useIsomorphicLayoutEffect } from "utils/effect.utils";
4
5
  import { Context } from "../transform-wrapper/transform-wrapper";
5
6
 
6
7
  export const KeepScale = React.forwardRef<
@@ -10,17 +11,25 @@ export const KeepScale = React.forwardRef<
10
11
  const localRef = useRef<HTMLDivElement>(null);
11
12
  const instance = useContext(Context);
12
13
 
13
- useEffect(() => {
14
- return instance.onChange((ctx) => {
14
+ useIsomorphicLayoutEffect(() => {
15
+ const applyInverseScale = (scale: number) => {
15
16
  if (localRef.current) {
16
17
  const positionX = 0;
17
18
  const positionY = 0;
18
19
  localRef.current.style.transform = instance.handleTransformStyles(
19
20
  positionX,
20
21
  positionY,
21
- 1 / ctx.instance.state.scale,
22
+ 1 / scale,
22
23
  );
23
24
  }
25
+ };
26
+
27
+ // Apply immediately: an element mounted while already zoomed must not
28
+ // wait for the next transform change to get its counter-scale.
29
+ applyInverseScale(instance.state.scale);
30
+
31
+ return instance.onChange((ctx) => {
32
+ applyInverseScale(ctx.instance.state.scale);
24
33
  });
25
34
  }, [instance]);
26
35
 
@@ -13,7 +13,6 @@ import {
13
13
  useTransformInit,
14
14
  } from "hooks";
15
15
  import { useResize } from "./use-resize.hook";
16
- import { ReactZoomPanPinchRef } from "models";
17
16
  import { boundLimiter } from "core/bounds/bounds.utils";
18
17
 
19
18
  export type MiniMapProps = {
@@ -59,7 +58,6 @@ export const MiniMap: React.FC<MiniMapProps> = ({
59
58
  const [initialized, setInitialized] = useState(false);
60
59
  const instance = useTransformContext();
61
60
  const [isDown, setIsDown] = useState(false);
62
- const miniMapInstance = useRef<ReactZoomPanPinchRef>(null);
63
61
 
64
62
  const mainRef = useRef<HTMLDivElement | null>(null);
65
63
  const wrapperRef = useRef<HTMLDivElement | null>(null);
@@ -178,19 +176,6 @@ export const MiniMap: React.FC<MiniMapProps> = ({
178
176
 
179
177
  useResize(instance.contentComponent, initialize, [initialized]);
180
178
 
181
- useEffect(() => {
182
- return instance.onChange((zpp) => {
183
- const scale = computeMiniMapScale();
184
- if (miniMapInstance.current) {
185
- miniMapInstance.current.instance.state.scale = zpp.instance.state.scale;
186
- miniMapInstance.current.instance.state.positionX =
187
- zpp.instance.state.positionX * scale;
188
- miniMapInstance.current.instance.state.positionY =
189
- zpp.instance.state.positionY * scale;
190
- }
191
- });
192
- }, [computeMiniMapScale, instance, miniMapInstance]);
193
-
194
179
  useEffect(() => {
195
180
  const move = (e: MouseEvent) => {
196
181
  if (
@@ -250,14 +235,18 @@ export const MiniMap: React.FC<MiniMapProps> = ({
250
235
  const setUp = () => {
251
236
  setIsDown(false);
252
237
  };
253
- document.addEventListener("mousedown", setDown);
254
- document.addEventListener("mouseup", setUp);
255
- document.addEventListener("mousemove", move);
238
+ // Listen on the mini map's own document so it keeps working inside
239
+ // portal windows and iframes.
240
+ const doc = mainRef.current?.ownerDocument ?? document;
241
+ doc.addEventListener("mousedown", setDown);
242
+ doc.addEventListener("mouseup", setUp);
243
+ doc.addEventListener("mousemove", move);
256
244
  return () => {
257
- document.removeEventListener("mousemove", move);
258
- document.removeEventListener("mouseup", setUp);
245
+ doc.removeEventListener("mousedown", setDown);
246
+ doc.removeEventListener("mousemove", move);
247
+ doc.removeEventListener("mouseup", setUp);
259
248
  };
260
- });
249
+ }, [computeMiniMapScale, instance, isDown, panning]);
261
250
 
262
251
  const wrapperStyle = useMemo(() => {
263
252
  return {
@@ -4,6 +4,7 @@ import React, { useContext, useEffect, useRef } from "react";
4
4
  import { Context } from "../transform-wrapper/transform-wrapper";
5
5
  import { baseClasses } from "../../constants/state.constants";
6
6
  import { getTransformStyles } from "../../utils/styles.utils";
7
+ import { useIsomorphicLayoutEffect } from "../../utils/effect.utils";
7
8
 
8
9
  import styles from "./transform-component.module.css";
9
10
 
@@ -40,7 +41,10 @@ export const TransformComponent: React.FC<Props> = ({
40
41
  const contentRef = useRef<HTMLDivElement | null>(null);
41
42
  const gridRef = useRef<HTMLDivElement | null>(null);
42
43
 
43
- useEffect(() => {
44
+ // Layout effect: the initial transform (initialPosition*, centerOnInit)
45
+ // and the first visibility check of virtualized children happen before the
46
+ // browser paints, so nothing jumps into place a frame late.
47
+ useIsomorphicLayoutEffect(() => {
44
48
  const wrapper = wrapperRef.current;
45
49
  const content = contentRef.current;
46
50
  if (wrapper !== null && content !== null && init) {
@@ -24,12 +24,25 @@ export const TransformWrapper = React.forwardRef(
24
24
  props: Omit<ReactZoomPanPinchProps, "ref">,
25
25
  ref: React.Ref<ReactZoomPanPinchContentRef>,
26
26
  ) => {
27
- const instance = useRef(new ZoomPanPinch(props)).current;
27
+ // Lazy so a fresh (immediately discarded) instance is not built on every
28
+ // render.
29
+ const instanceRef = useRef<ZoomPanPinch | null>(null);
30
+ if (instanceRef.current === null) {
31
+ instanceRef.current = new ZoomPanPinch(props);
32
+ }
33
+ const instance = instanceRef.current;
28
34
 
29
35
  const content = getContent(props.children, getControls(instance));
30
36
 
31
37
  useImperativeHandle(ref, () => getControls(instance), [instance]);
32
38
 
39
+ useEffect(() => {
40
+ instance.mount();
41
+ return () => {
42
+ instance.unmount();
43
+ };
44
+ }, [instance]);
45
+
33
46
  useEffect(() => {
34
47
  instance.update(props);
35
48
  }, [instance, props]);
@@ -1,6 +1,7 @@
1
- import React, { useContext, useEffect, useRef, useState } from "react";
1
+ import React, { useContext, useRef, useState } from "react";
2
2
 
3
3
  import { Context } from "../transform-wrapper/transform-wrapper";
4
+ import { useIsomorphicLayoutEffect } from "../../utils/effect.utils";
4
5
  import { isElementVisible } from "./virtualize.utils";
5
6
 
6
7
  /* eslint-disable react/require-default-props */
@@ -67,7 +68,30 @@ export const Virtualize = React.forwardRef<HTMLDivElement, VirtualizeProps>(
67
68
  ref,
68
69
  ) => {
69
70
  const instance = useContext(Context);
70
- const [visible, setVisible] = useState(false);
71
+
72
+ const computeVisible = () => {
73
+ const wrapper = instance.wrapperComponent;
74
+ if (!wrapper) return false;
75
+
76
+ return isElementVisible({
77
+ elementX: x,
78
+ elementY: y,
79
+ elementWidth: width,
80
+ elementHeight: height,
81
+ scale: instance.state.scale,
82
+ positionX: instance.state.positionX,
83
+ positionY: instance.state.positionY,
84
+ viewportWidth: wrapper.offsetWidth,
85
+ viewportHeight: wrapper.offsetHeight,
86
+ margin,
87
+ threshold,
88
+ });
89
+ };
90
+
91
+ // Elements mounted after the wrapper is initialized (items added to a
92
+ // canvas at runtime) render their children in the very first commit
93
+ // instead of one frame later.
94
+ const [visible, setVisible] = useState(computeVisible);
71
95
  const visibleRef = useRef(false);
72
96
 
73
97
  const onShowRef = useRef(onShow);
@@ -75,24 +99,13 @@ export const Virtualize = React.forwardRef<HTMLDivElement, VirtualizeProps>(
75
99
  onShowRef.current = onShow;
76
100
  onHideRef.current = onHide;
77
101
 
78
- useEffect(() => {
102
+ // Layout effect so the check triggered by the wrapper's init (also a
103
+ // layout effect) re-renders before the browser paints.
104
+ useIsomorphicLayoutEffect(() => {
79
105
  const check = () => {
80
- const wrapper = instance.wrapperComponent;
81
- if (!wrapper) return;
82
-
83
- const nowVisible = isElementVisible({
84
- elementX: x,
85
- elementY: y,
86
- elementWidth: width,
87
- elementHeight: height,
88
- scale: instance.state.scale,
89
- positionX: instance.state.positionX,
90
- positionY: instance.state.positionY,
91
- viewportWidth: wrapper.offsetWidth,
92
- viewportHeight: wrapper.offsetHeight,
93
- margin,
94
- threshold,
95
- });
106
+ if (!instance.wrapperComponent) return;
107
+
108
+ const nowVisible = computeVisible();
96
109
 
97
110
  if (nowVisible !== visibleRef.current) {
98
111
  visibleRef.current = nowVisible;
@@ -6,18 +6,14 @@ import {
6
6
  StateType,
7
7
  } from "../../models";
8
8
 
9
- const handleCancelAnimationFrame = (animation: AnimationType | null) => {
10
- if (typeof animation === "number") {
11
- cancelAnimationFrame(animation);
12
- }
13
- };
14
-
15
9
  export const handleCancelAnimation = (
16
10
  contextInstance: ReactZoomPanPinchContext,
17
11
  ): void => {
18
- if (!contextInstance.mounted) return;
19
- handleCancelAnimationFrame(contextInstance.animation);
12
+ if (contextInstance.animationFrame !== null) {
13
+ cancelAnimationFrame(contextInstance.animationFrame);
14
+ }
20
15
  // Clear animation state
16
+ contextInstance.animationFrame = null;
21
17
  contextInstance.isAnimating = false;
22
18
  contextInstance.animation = null;
23
19
  contextInstance.velocity = null;
@@ -37,9 +33,12 @@ export function handleSetupAnimation(
37
33
  handleCancelAnimation(contextInstance);
38
34
 
39
35
  // new animation
40
- contextInstance.animation = () => {
41
- if (!contextInstance.mounted) {
42
- return handleCancelAnimationFrame(contextInstance.animation);
36
+ const animation: AnimationType = () => {
37
+ // A frame that was already queued when this animation got cancelled or
38
+ // replaced must not run: it would apply a stale target and re-schedule
39
+ // the replacement animation a second time per frame.
40
+ if (!contextInstance.mounted || contextInstance.animation !== animation) {
41
+ return;
43
42
  }
44
43
 
45
44
  const frameTime = new Date().getTime() - startTime;
@@ -49,15 +48,19 @@ export function handleSetupAnimation(
49
48
  const step = animationType(animationProgress);
50
49
 
51
50
  if (frameTime >= animationTime) {
52
- callback(lastStep);
51
+ // Clear before the final step so a callback that starts a new
52
+ // animation (e.g. from onTransform) is not wiped out afterwards.
53
53
  contextInstance.animation = null;
54
- } else if (contextInstance.animation) {
54
+ contextInstance.animationFrame = null;
55
+ callback(lastStep);
56
+ } else {
55
57
  callback(step);
56
- requestAnimationFrame(contextInstance.animation);
58
+ contextInstance.animationFrame = requestAnimationFrame(animation);
57
59
  }
58
60
  };
59
61
 
60
- requestAnimationFrame(contextInstance.animation);
62
+ contextInstance.animation = animation;
63
+ contextInstance.animationFrame = requestAnimationFrame(animation);
61
64
  }
62
65
 
63
66
  function isValidTargetState(targetState: StateType): boolean {
@@ -7,6 +7,7 @@ import {
7
7
  cancelTimeout,
8
8
  getContext,
9
9
  handleCallback,
10
+ isEditableTarget,
10
11
  isExcludedNode,
11
12
  } from "../../utils";
12
13
  import {
@@ -24,6 +25,7 @@ export const handleDoubleClickStop = (
24
25
  cancelTimeout(contextInstance.doubleClickStopEventTimer);
25
26
  contextInstance.doubleClickStopEventTimer = setTimeout(() => {
26
27
  contextInstance.doubleClickStopEventTimer = null;
28
+ if (!contextInstance.mounted) return;
27
29
  handleCallback(getContext(contextInstance), event, onZoomStop);
28
30
  }, animationTime);
29
31
  };
@@ -123,5 +125,8 @@ export const isDoubleClickAllowed = (
123
125
 
124
126
  if (isExcluded) return false;
125
127
 
128
+ // Double-clicking a word inside an input selects it; it must not zoom.
129
+ if (isEditableTarget(target)) return false;
130
+
126
131
  return true;
127
132
  };
@@ -106,8 +106,16 @@ export const zoomToElement =
106
106
 
107
107
  const { wrapperComponent } = contextInstance;
108
108
 
109
+ // Resolve ids in the wrapper's own document so it works inside portal
110
+ // windows and iframes, not only the top-level document (#290).
111
+ const ownerDocument =
112
+ wrapperComponent?.ownerDocument ??
113
+ (typeof document !== "undefined" ? document : null);
114
+
109
115
  const target: HTMLElement | null =
110
- typeof node === "string" ? document.getElementById(node) : node;
116
+ typeof node === "string"
117
+ ? ownerDocument?.getElementById(node) ?? null
118
+ : node;
111
119
 
112
120
  if (wrapperComponent && target && wrapperComponent.contains(target)) {
113
121
  const targetState = calculateZoomToNode(
@@ -22,16 +22,17 @@ export const handleCalculateButtonZoom = (
22
22
  ): number => {
23
23
  const { scale } = contextInstance.state;
24
24
  const { wrapperComponent, setup } = contextInstance;
25
- const { maxScale, minScale, zoomAnimation, smooth } = setup;
25
+ const { maxScale, minScale, zoomAnimation } = setup;
26
26
  const { size } = zoomAnimation;
27
27
 
28
28
  if (!wrapperComponent) {
29
29
  throw new Error("Wrapper is not mounted");
30
30
  }
31
31
 
32
- const targetScale = smooth
33
- ? scale * Math.exp(delta * step)
34
- : scale + delta * step;
32
+ // `step` is an absolute scale increment for zoomIn/zoomOut and double
33
+ // click: zoomIn(0.25) from 1 lands on 1.25. The `smooth` option only
34
+ // shapes wheel/trackpad deltas (#545, #431).
35
+ const targetScale = scale + delta * step;
35
36
 
36
37
  const newScale = checkZoomBounds(
37
38
  roundNumber(targetScale, 3),
@@ -107,6 +107,9 @@ export class ZoomPanPinch {
107
107
  // animations helpers
108
108
  public isAnimating = false;
109
109
  public animation: AnimationType | null = null;
110
+ public animationFrame: number | null = null;
111
+ // init helpers
112
+ public initObserverTimer: ReturnType<typeof setTimeout> | null = null;
110
113
  // key press
111
114
  public pressedKeys: { [key: string]: boolean } = {};
112
115
 
@@ -117,19 +120,38 @@ export class ZoomPanPinch {
117
120
  }
118
121
 
119
122
  mount = () => {
123
+ this.mounted = true;
120
124
  this.initializeWindowEvents();
121
125
  };
122
126
 
127
+ // Marks the instance as gone so pending timers, animation frames and
128
+ // callbacks scheduled before unmount become no-ops.
123
129
  unmount = () => {
130
+ this.mounted = false;
124
131
  this.cleanupWindowEvents();
125
132
  };
126
133
 
127
134
  update = (newProps: ReactZoomPanPinchProps) => {
128
135
  this.props = newProps;
136
+ // Setup first: bounds depend on the new props (min/max positions,
137
+ // centerZoomedOut, disablePadding).
138
+ this.setup = createSetup(newProps);
129
139
  if (this.wrapperComponent && this.contentComponent) {
130
140
  handleCalculateBounds(this, this.state.scale);
131
141
  }
132
- this.setup = createSetup(newProps);
142
+ };
143
+
144
+ clearTimers = (): void => {
145
+ if (this.wheelStopEventTimer) clearTimeout(this.wheelStopEventTimer);
146
+ if (this.wheelAnimationTimer) clearTimeout(this.wheelAnimationTimer);
147
+ if (this.doubleClickStopEventTimer) {
148
+ clearTimeout(this.doubleClickStopEventTimer);
149
+ }
150
+ if (this.initObserverTimer) clearTimeout(this.initObserverTimer);
151
+ this.wheelStopEventTimer = null;
152
+ this.wheelAnimationTimer = null;
153
+ this.doubleClickStopEventTimer = null;
154
+ this.initObserverTimer = null;
133
155
  };
134
156
 
135
157
  initializeWindowEvents = (): void => {
@@ -180,7 +202,6 @@ export class ZoomPanPinch {
180
202
  currentWindow?.removeEventListener("keyup", this.setKeyUnPressed, passive);
181
203
  currentWindow?.removeEventListener("keydown", this.setKeyPressed, passive);
182
204
  currentWindow?.removeEventListener("blur", this.handleWindowBlur);
183
- document.removeEventListener("mouseleave", this.clearPanning, passive);
184
205
  this.wrapperComponent?.removeEventListener(
185
206
  "wheel",
186
207
  this.onWheelPanning,
@@ -196,10 +217,28 @@ export class ZoomPanPinch {
196
217
  this.setKeyPressed,
197
218
  passive,
198
219
  );
220
+ this.cleanupWrapperEvents();
221
+ this.clearTimers();
199
222
  handleCancelAnimation(this);
200
223
  this.observer?.disconnect();
201
224
  };
202
225
 
226
+ cleanupWrapperEvents = (): void => {
227
+ const wrapper = this.wrapperComponent;
228
+ if (!wrapper) return;
229
+ const passive = makePassiveEventOption();
230
+
231
+ wrapper.removeEventListener("wheel", this.onWheelZoom, passive);
232
+ wrapper.removeEventListener("dblclick", this.onDoubleClick, passive);
233
+ wrapper.removeEventListener(
234
+ "touchstart",
235
+ this.onTouchPanningStart,
236
+ passive,
237
+ );
238
+ wrapper.removeEventListener("touchmove", this.onTouchPanning, passive);
239
+ wrapper.removeEventListener("touchend", this.onTouchPanningStop, passive);
240
+ };
241
+
203
242
  handleInitializeWrapperEvents = (wrapper: HTMLDivElement): void => {
204
243
  // Zooming events on wrapper
205
244
  const passive = makePassiveEventOption();
@@ -218,6 +257,7 @@ export class ZoomPanPinch {
218
257
 
219
258
  if (centerOnInit) {
220
259
  this.setCenter();
260
+ this.observer?.disconnect();
221
261
  this.observer = new ResizeObserver(() => {
222
262
  const currentWidth = contentComponent.offsetWidth;
223
263
  const currentHeight = contentComponent.offsetHeight;
@@ -234,7 +274,9 @@ export class ZoomPanPinch {
234
274
 
235
275
  // TODO: CHANGE to first interaction?
236
276
  // if nothing about the contentComponent has changed after 5 seconds, disconnect the observer
237
- setTimeout(() => {
277
+ if (this.initObserverTimer) clearTimeout(this.initObserverTimer);
278
+ this.initObserverTimer = setTimeout(() => {
279
+ this.initObserverTimer = null;
238
280
  this.observer?.disconnect();
239
281
  }, 5000);
240
282
 
@@ -435,6 +477,9 @@ export class ZoomPanPinch {
435
477
  const isDoubleTap = this.lastTouch && +new Date() - this.lastTouch < 200;
436
478
 
437
479
  if (isDoubleTapAllowed && isDoubleTap && event.touches.length === 1) {
480
+ // Consume the pair so a third quick tap starts a fresh sequence
481
+ // instead of counting as another double tap.
482
+ this.lastTouch = null;
438
483
  this.onDoubleClick(event);
439
484
  } else {
440
485
  this.lastTouch = +new Date();
@@ -697,6 +742,7 @@ export class ZoomPanPinch {
697
742
  wrapperComponent: HTMLDivElement,
698
743
  contentComponent: HTMLDivElement,
699
744
  ): void => {
745
+ this.mounted = true;
700
746
  this.cleanupWindowEvents();
701
747
  this.wrapperComponent = wrapperComponent;
702
748
  this.contentComponent = contentComponent;
@@ -4,7 +4,11 @@ import {
4
4
  ReactZoomPanPinchContext,
5
5
  ReactZoomPanPinchState,
6
6
  } from "../../models";
7
- import { isExcludedNode } from "../../utils";
7
+ import {
8
+ isDraggableTarget,
9
+ isEditableTarget,
10
+ isExcludedNode,
11
+ } from "../../utils";
8
12
  import { getMouseBoundedPosition } from "../bounds/bounds.utils";
9
13
  import { handleCalculateZoomPositions } from "../zoom/zoom.utils";
10
14
 
@@ -35,11 +39,7 @@ export const isPanningStartAllowed = (
35
39
 
36
40
  if (isExcluded) return false;
37
41
 
38
- if (
39
- target.getAttribute("draggable") === "true" ||
40
- target.getAttribute("contenteditable") === "true" ||
41
- target.isContentEditable
42
- ) {
42
+ if (isDraggableTarget(target) || isEditableTarget(target)) {
43
43
  return false;
44
44
  }
45
45
 
@@ -130,6 +130,9 @@ export const handleWheelPanningStart = (
130
130
 
131
131
  if (!contextInstance.wheelStopEventTimer) {
132
132
  handleCancelAnimation(contextInstance);
133
+ // Same as a mouse pan start: bounds must reflect the current scale and
134
+ // content size, not the ones measured at mount (#396, #433).
135
+ handleCalculateBounds(contextInstance, contextInstance.state.scale);
133
136
  handleCallback(getContext(contextInstance), event, onWheelStart);
134
137
  handleCallback(getContext(contextInstance), event, onPanningStart);
135
138
  }
@@ -14,6 +14,10 @@ export const isWheelAllowed = (
14
14
  const isAllowed = isInitialized && !isPanning && !disabled && target;
15
15
 
16
16
  if (!isAllowed) return false;
17
+ // A purely horizontal wheel/swipe (Magic Mouse, trackpad) carries no zoom
18
+ // intent. Treating its deltaY of 0 as "zoom out" was #168; leaving it
19
+ // unclaimed also lets trackpad panning pick it up.
20
+ if (event.deltaY === 0) return false;
17
21
  // Event ctrlKey detects if touchpad action is executing wheel or pinch gesture
18
22
  if (wheelDisabled && !event.ctrlKey) return false;
19
23
  if (touchPadDisabled && event.ctrlKey) return false;
@@ -9,7 +9,7 @@ export function useTransformComponent<T>(
9
9
  ): T {
10
10
  const libraryContext = useTransformContext();
11
11
 
12
- const [transformRender, setTransformRender] = useState<T>(
12
+ const [transformRender, setTransformRender] = useState<T>(() =>
13
13
  callback(getState(libraryContext)),
14
14
  );
15
15
 
@@ -1,4 +1,4 @@
1
- import { useLayoutEffect, useRef } from "react";
1
+ import React, { useLayoutEffect, useRef } from "react";
2
2
 
3
3
  import { initialSetup } from "constants/state.constants";
4
4
  import { ZoomPanPinch } from "core/instance.core";
@@ -7,7 +7,13 @@ import { ReactZoomPanPinchProps, ReactZoomPanPinchRef } from "models";
7
7
  export const useZoomPanPinch = (props?: ReactZoomPanPinchProps) => {
8
8
  const contentRef = useRef<HTMLDivElement>(null);
9
9
  const wrapperRef = useRef<HTMLDivElement>(null);
10
- const instance = useRef(new ZoomPanPinch({ ...initialSetup, ...props }));
10
+ // Lazy so a fresh (immediately discarded) instance is not built on every
11
+ // render.
12
+ const instanceRef = useRef<ZoomPanPinch | null>(null);
13
+ if (instanceRef.current === null) {
14
+ instanceRef.current = new ZoomPanPinch({ ...initialSetup, ...props });
15
+ }
16
+ const instance = instanceRef as React.MutableRefObject<ZoomPanPinch>;
11
17
 
12
18
  const useTransformCallback = useRef<
13
19
  (data: {
@@ -29,7 +35,7 @@ export const useZoomPanPinch = (props?: ReactZoomPanPinchProps) => {
29
35
  });
30
36
 
31
37
  return () => {
32
- inst.cleanupWindowEvents();
38
+ inst.unmount();
33
39
  unmount();
34
40
  };
35
41
  // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -0,0 +1,11 @@
1
+ import { useEffect, useLayoutEffect } from "react";
2
+
3
+ /**
4
+ * `useLayoutEffect` in the browser, `useEffect` during server rendering.
5
+ *
6
+ * DOM measurements and the initial transform must run before the first paint
7
+ * (otherwise `centerOnInit`, `initialPosition*` and virtualized children flash
8
+ * into place one frame late), but `useLayoutEffect` warns on the server.
9
+ */
10
+ export const useIsomorphicLayoutEffect =
11
+ typeof window !== "undefined" ? useLayoutEffect : useEffect;
@@ -13,6 +13,43 @@ export const isExcludedNode = (
13
13
  );
14
14
  };
15
15
 
16
+ const EDITABLE_TAGS = new Set(["INPUT", "TEXTAREA", "SELECT", "OPTION"]);
17
+ const CONTENT_EDITABLE_SELECTOR =
18
+ '[contenteditable=""], [contenteditable="true"], [contenteditable="plaintext-only"]';
19
+ const DRAGGABLE_SELECTOR = '[draggable="true"]';
20
+
21
+ const asElement = (node: EventTarget | null): HTMLElement | null => {
22
+ // Duck-typed on purpose: `instanceof Element` fails for nodes that belong
23
+ // to another window (portals, popups), see #290.
24
+ if (!node || typeof (node as HTMLElement).closest !== "function") {
25
+ return null;
26
+ }
27
+ return node as HTMLElement;
28
+ };
29
+
30
+ /**
31
+ * Form controls and contenteditable regions (or anything nested inside them).
32
+ * Starting a pan or a double-click zoom from these would steal focus and block
33
+ * native text editing (#437, #544).
34
+ */
35
+ export const isEditableTarget = (node: EventTarget | null): boolean => {
36
+ const element = asElement(node);
37
+ if (!element) return false;
38
+ if (EDITABLE_TAGS.has(element.tagName)) return true;
39
+ if (element.isContentEditable) return true;
40
+ return element.closest(CONTENT_EDITABLE_SELECTOR) !== null;
41
+ };
42
+
43
+ /**
44
+ * HTML5 drag sources (or anything nested inside them). A pan started here
45
+ * would call `preventDefault` on mousedown and cancel the native drag (#460).
46
+ */
47
+ export const isDraggableTarget = (node: EventTarget | null): boolean => {
48
+ const element = asElement(node);
49
+ if (!element) return false;
50
+ return element.closest(DRAGGABLE_SELECTOR) !== null;
51
+ };
52
+
16
53
  export const cancelTimeout = (
17
54
  timeout: ReturnType<typeof setTimeout> | null,
18
55
  ): void => {
@@ -6,3 +6,4 @@ export * from "./helpers.utils";
6
6
  export * from "./state.utils";
7
7
  export * from "./styles.utils";
8
8
  export * from "./ref.utils";
9
+ export * from "./effect.utils";