All files / Rindu/hooks useDisableGlobalContextMenu.ts

25% Statements 2/8
0% Branches 0/3
0% Functions 0/4
25% Lines 2/8

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 2019x   19x                                  
import { useEffect } from "react";
 
import { isServer } from "utils";
 
export function useDisableGlobalContextMenu(
  w: (Window & typeof globalThis) | undefined = isServer() ? undefined : window
): void {
  useEffect(() => {
    function preventDefault(e: MouseEvent) {
      e.preventDefault();
    }
 
    w?.addEventListener("contextmenu", preventDefault);
 
    return (): void => {
      w?.removeEventListener("contextmenu", preventDefault);
    };
  }, [w]);
}