All files / Rindu/utils contextMenu.ts

39.62% Statements 21/53
33.33% Branches 5/15
33.33% Functions 4/12
41.17% Lines 21/51

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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191            44x           22x           20x       20x 20x           44x 2x     44x 2x     44x       44x                                                       40x             45x 45x 43x     2x       2x         2x         2x           2x               2x                                                                                                                                                                  
import { Dispatch, SetStateAction } from "react";
 
import {
  CONTEXT_MENU_ITEM_HEIGHT,
  CONTEXT_MENU_SIDE_OFFSET,
  CONTEXT_MENU_TOP_BOTTOM_OFFSET,
} from "./constants";
import {
  ICardContentContextMenuData,
  ICardTrackContextMenuData,
} from "types/contextMenu";
 
export function calculateContextMenuPosition(
  isOffScreen: boolean,
  maxEdgePosition: number,
  position: number | undefined,
  defaultValue: number
): number {
  Iif (isOffScreen) {
    return maxEdgePosition;
  }
 
  if (!position || position < 45) {
    return defaultValue;
  }
 
  return position;
}
 
const isOffScreenWidth = (x: number, width: number) => {
  return x + width > window.innerWidth;
};
 
const isOffScreenHeight = (y: number, height: number) => {
  return y + height > window.innerHeight;
};
 
const calculateOffScreenWidth = (width: number, offset = 0) => {
  return window.innerWidth - width - offset;
};
 
const calculateOffScreenHeight = (height: number, offset = 0) => {
  return window.innerHeight - height - offset;
};
 
interface IHandlePositioning {
  currentPosition:
    | ICardTrackContextMenuData["position"]
    | ICardContentContextMenuData["position"]
    | undefined;
  element: HTMLElement | null;
  setPosition: Dispatch<
    SetStateAction<{
      x: number;
      y: number;
    }>
  >;
  setIsOffScreen: Dispatch<
    SetStateAction<{
      x: boolean;
      y: boolean;
    }>
  >;
  offsets?: {
    x: number;
    y: number;
  };
}
 
export function positionContextMenu({
  currentPosition,
  element,
  setPosition,
  setIsOffScreen,
  offsets,
}: IHandlePositioning): void {
  const contextMenuRect = element?.getBoundingClientRect();
  if (!contextMenuRect || !currentPosition) {
    return;
  }
 
  const isContextMenuWidthOffScreen = isOffScreenWidth(
    currentPosition.x,
    contextMenuRect.width
  );
  const isContextMenuHeightOffScreen = isOffScreenHeight(
    currentPosition.y,
    contextMenuRect.height
  );
 
  setIsOffScreen({
    x: isContextMenuWidthOffScreen,
    y: isContextMenuHeightOffScreen,
  });
 
  Iif (isContextMenuWidthOffScreen) {
    setPosition((prevState) => ({
      ...prevState,
      x: calculateOffScreenWidth(contextMenuRect.width, offsets?.x),
    }));
  }
  Iif (isContextMenuHeightOffScreen) {
    setPosition((prevState) => ({
      ...prevState,
      y: calculateOffScreenHeight(contextMenuRect.height, offsets?.y),
    }));
  }
}
 
export function positionSubMenu(
  subMenuContainer: HTMLDivElement,
  menuContainer: HTMLUListElement,
  items: number,
  hoveredItem: HTMLButtonElement
): void {
  const screenHeight = window.innerHeight;
  const menuContainerClientRect = menuContainer.getBoundingClientRect();
  const subMenuContainerClientRect = subMenuContainer.getBoundingClientRect();
  const hoveredItemClientRect = hoveredItem.getBoundingClientRect();
  const subMenuHeight = items * CONTEXT_MENU_ITEM_HEIGHT;
 
  const calculatedSubMenuTop = calculateSubMenuTop(
    subMenuContainerClientRect,
    subMenuHeight,
    hoveredItemClientRect
  );
 
  const calculatedSubMenuHeight = calculateSubMenuHeight(
    subMenuHeight,
    screenHeight
  );
 
  subMenuContainer.style.top = `${calculatedSubMenuTop}px`;
  subMenuContainer.style.height = `${calculatedSubMenuHeight}px`;
 
  applyTransformTranslation(
    subMenuContainerClientRect,
    menuContainerClientRect,
    subMenuContainer
  );
}
 
function calculateSubMenuTop(
  subMenuContainerClientRect: DOMRect,
  subMenuHeight: number,
  hoveredItemClientRect: DOMRect
) {
  const inlineBaseHoveredItemWithSubMenuY =
    hoveredItemClientRect.y - subMenuContainerClientRect.y;
  const centeredVerticallyBaseSubMenu =
    inlineBaseHoveredItemWithSubMenuY -
    subMenuHeight / 2 +
    CONTEXT_MENU_ITEM_HEIGHT / 2;
 
  const isSubMenuOutSideScreenOnTop =
    subMenuHeight / 2 > hoveredItemClientRect.y - CONTEXT_MENU_SIDE_OFFSET;
  const isSubMenuOutSideScreenOnBottom =
    subMenuHeight / 2 >
    window.innerHeight - hoveredItemClientRect.y - CONTEXT_MENU_SIDE_OFFSET;
 
  Iif (isSubMenuOutSideScreenOnTop) {
    return -hoveredItemClientRect.y + CONTEXT_MENU_SIDE_OFFSET * 2;
  }
 
  Iif (isSubMenuOutSideScreenOnBottom) {
    return window.innerHeight - hoveredItemClientRect.y - subMenuHeight;
  }
 
  return centeredVerticallyBaseSubMenu;
}
 
function calculateSubMenuHeight(subMenuHeight: number, screenHeight: number) {
  Iif (subMenuHeight > screenHeight) {
    return screenHeight - CONTEXT_MENU_TOP_BOTTOM_OFFSET;
  }
 
  return subMenuHeight;
}
 
function applyTransformTranslation(
  subMenuContainerClientRect: DOMRect,
  menuContainerClientRect: DOMRect,
  subMenuContainer: HTMLDivElement
) {
  Iif (subMenuContainerClientRect.right > window.innerWidth) {
    subMenuContainer.style.transform = `translateX(-${
      subMenuContainerClientRect.width + menuContainerClientRect.width
    }px)`;
  }
}