# All files / src/tooltip utils.ts

100% Statements 51/51
100% Branches 18/18
100% Functions 2/2
100% Lines 51/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
    521x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    212x
    212x
    12x
    212x
    197x
    212x
    1x
    212x
    1x
    212x
    1x
    212x
    212x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    33x
    33x
    33x
    33x
    33x
    33x
    33x
    1x
    33x
    1x
    32x
    1x
    31x
    1x
    1x
    33x
    33x
    33x
     import {
      ABOVE_CENTER_ANCHOR,
      BELOW_CENTER_ANCHOR,
      CENTER_LEFT_ANCHOR,
      CENTER_RIGHT_ANCHOR,
    } from "../positioning/constants.js";
    import type { PositionAnchor, SimplePosition } from "../positioning/types.js";

    /** @internal */
    export function getAnchor(position: SimplePosition): PositionAnchor {
      switch (position) {
        case "above":
          return ABOVE_CENTER_ANCHOR;
        case "below":
          return BELOW_CENTER_ANCHOR;
        case "left":
          return CENTER_LEFT_ANCHOR;
        case "right":
          return CENTER_RIGHT_ANCHOR;
        default:
          throw new Error(`Invalid tooltip position: "${position}"`);
      }
    }

    /** @internal */
    export interface GetPositionOptions {
      container: HTMLElement;
      threshold: number;
      defaultPosition: SimplePosition;
    }

    /** @internal */
    export function getPosition(options: GetPositionOptions): SimplePosition {
      const { container, defaultPosition, threshold } = options;

      const { top, left } = container.getBoundingClientRect();
      const vh = window.innerHeight;
      const vw = window.innerWidth;
      let nextPosition = defaultPosition;
      if (defaultPosition === "above" && top < vh - vh * threshold) {
        nextPosition = "below";
      } else if (defaultPosition === "below" && top > vh * threshold) {
        nextPosition = "above";
      } else if (defaultPosition === "left" && left < vw - vw * threshold) {
        nextPosition = "right";
      } else if (defaultPosition === "right" && left > vw * threshold) {
        nextPosition = "left";
      }

      return nextPosition;
    }