All files / src/hooks useWheelHandlers.ts

38.46% Statements 5/13
0% Branches 0/8
50% Functions 1/2
38.46% Lines 5/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  1x   1x               1x 10x                                         10x    
import * as React from "react";
import { pan, TSetZoomPanSettings, zoom } from "../utils/zoomAndPan";
 
import { useWindowEvent } from "./useWindowEvent";
 
interface IUseWheelHandlersOptions {
  svgRef: React.RefObject<SVGSVGElement>;
  setZoomPanSettings: TSetZoomPanSettings;
  getIsCanvasFocused(): boolean;
}
 
export const useWheelHandlers = (options: IUseWheelHandlersOptions) => {
  const onContainerWheel = (evt: WheelEvent) => {
    if (!options.getIsCanvasFocused()) {
      return;
    }
 
    evt.stopPropagation();
    evt.preventDefault();
 
    if (evt.ctrlKey) {
      const scale = evt.deltaY > 0 ? 1.1 : 0.9;
 
      zoom(scale, options.svgRef, options.setZoomPanSettings.bind(undefined));
    } else {
      pan(
        0,
        evt.deltaY < 0 ? 100 : -100,
        options.setZoomPanSettings.bind(undefined)
      );
    }
  };
 
  useWindowEvent("wheel", onContainerWheel, { passive: false });
};