# All files / src/positioning utils.ts

99.73% Statements 379/380
98.11% Branches 52/53
100% Functions 14/14
99.73% Lines 379/380

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
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    3811x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    49x
    49x
    49x
    49x
    49x
    49x
    49x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    93x
    93x
    93x
    93x
    93x
    93x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    106x
    106x
    106x
    106x
    106x
    106x
    106x
    106x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    90x
    90x
    90x
    90x
    90x
    90x
    90x
    90x
    90x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    54x
    54x
    54x
    54x
    54x
    54x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    36x
    36x
    36x
    36x
    36x
    36x
    36x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    51x
    51x
    51x
    51x
    51x
    51x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    115x
    115x
    115x
    115x
    115x
    115x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    185x
    185x
    185x
    185x
    185x
    185x
    138x
    138x
    185x
    5x
    5x
    185x
    185x
    41x
    41x
    185x
    1x
    185x
    185x
    185x
    185x

    185x
    185x
    185x
    185x
    10x
    10x
    185x
    20x
    20x
    185x
    185x
    154x
    154x
    185x
    1x
    185x
    185x
    185x
    185x
    185x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    294x
    294x
    294x
    93x
    93x
    201x
    294x
    4x
    4x
    4x
    4x
    2x
    2x
    294x
    1x
    1x
    1x
    1x
    1x
    198x
    198x
    294x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    2x
    196x
    196x
    196x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    146x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
     import type { PositionAnchor } from "./types.js";

    /** @internal */
    export interface XCoordConfig {
      xMargin: number;
      elWidth: number;
      initialX?: number;
      containerRect: DOMRect;
    }

    /** @internal */
    export interface YCoordConfig {
      yMargin: number;
      elHeight: number;
      initialY?: number;
      containerRect: DOMRect;
    }

    type Left = number;
    type Top = number;

    /**
     * Creates the `left` style value for an element that should be fixed to the
     * (outer) left of the container element. So the right bounds of the fixed
     * element will be equal to the left bounds of the container element (before the
     * xMargin is applied).
     *
     * @internal
     */
    export function getLeftCoord({
      xMargin,
      elWidth,
      initialX,
      containerRect,
    }: XCoordConfig): Left {
      return (initialX ?? containerRect.left) - elWidth - xMargin;
    }

    /**
     * Creates the `left` style value for an element that should be fixed to the
     * inner-left of the container element. So the left bounds of both the container
     * and fixed elements will overlap (before the xMargin is applied)
     *
     * @internal
     */
    export function getInnerLeftCoord({
      xMargin,
      initialX,
      containerRect,
    }: XCoordConfig): Left {
      return (initialX ?? containerRect.left) + xMargin;
    }

    /**
     * Creates the `left` style value for an element that should be fixed to the
     * center of the container element. So the center point of the fixed element
     * should be the center point of the container element.
     *
     * Note: Unlike all the other horizontal positioning logic, the center position
     * does not use the xMargin.
     *
     * @internal
     */
    export function getCenterXCoord({
      elWidth,
      initialX,
      containerRect,
    }: XCoordConfig): Left {
      const containerCenter = containerRect.width / 2;
      const elementCenter = elWidth / 2;
      return (initialX ?? containerRect.left + containerCenter) - elementCenter;
    }

    /**
     * Creates the `left` style value for an element that should be fixed to the
     * inner-right of the container element. So the right bounds for both the
     * container and fixed elements will overlap (before the xMargin is applied).
     *
     * @internal
     */
    export function getInnerRightCoord({
      xMargin,
      elWidth,
      initialX,
      containerRect,
    }: XCoordConfig): Left {
      return (
        (initialX ?? containerRect.left + containerRect.width) - elWidth - xMargin
      );
    }

    /**
     * Creates the `left` style value for an element that should be fixed to the
     * (outer) right of the container element. So the left bounds of the fixed
     * element will overlap with the right bounds of the container element (before
     * the xMargin is applied).
     *
     * @internal
     */
    export function getRightCoord({
      xMargin,
      initialX,
      containerRect,
    }: XCoordConfig): Left {
      return (initialX ?? containerRect.left + containerRect.width) + xMargin;
    }

    /**
     * Creates the `top` style value for an element that should be fixed above the
     * container element. So the bottom bounds of the fixed element will overlap
     * with the top bounds of the container element (before the yMargin is applied).
     *
     * @internal
     */
    export function getAboveCoord({
      yMargin,
      initialY,
      elHeight,
      containerRect,
    }: YCoordConfig): Top {
      return (initialY ?? containerRect.top) - elHeight - yMargin;
    }

    /**
     * Creates the `top` style value for an element that should be fixed to the top
     * of the container element. So the top bounds for both the container and fixed
     * elements will overlap (before the yMargin is applied).
     *
     * @internal
     */
    export function getTopCoord({
      yMargin,
      initialY,
      containerRect,
    }: YCoordConfig): Top {
      return (initialY ?? containerRect.top) + yMargin;
    }

    /**
     * Creates the `top` style value for an element that should be fixed vertically
     * centered relative to the container element. So the vertical center point for
     * the fixed element should overlap the vertical center point of the container
     * element.
     *
     * Note: Unlike all the other vertical positioning logic, the center position
     * does not use the yMargin.
     *
     * @internal
     */
    export function getCenterYCoord({
      yMargin,
      elHeight,
      initialY,
      containerRect,
    }: YCoordConfig): Top {
      const containerCenter = containerRect.height / 2;
      const elementCenter = elHeight / 2;
      return (
        (initialY ?? containerRect.top + containerCenter + yMargin) - elementCenter
      );
    }

    /**
     * Creates the `top` style value for an element that should be fixed to the
     * bottom of the container element. So the top bounds of the fixed element
     * should overlap the bottom bounds of the container element (before the yMargin
     * is applied).
     *
     * @internal
     */
    export function getBottomCoord({
      yMargin,
      initialY,
      elHeight,
      containerRect,
    }: YCoordConfig): Top {
      return (
        (initialY ?? containerRect.top + containerRect.height) - elHeight - yMargin
      );
    }

    /**
     * Creates the `top` style value for an element that should be fixed to the
     * bottom of the container element. So the bottom bounds of both the container
     * and fixed elements should overlap (before the yMargin is applied).
     *
     * @internal
     */
    export function getBelowCoord({
      yMargin,
      initialY,
      containerRect,
    }: YCoordConfig): Top {
      return (initialY ?? containerRect.top + containerRect.height) + yMargin;
    }

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export interface TransformOriginOptions extends PositionAnchor {
      transformOriginY?: number;
    }

    /**
     * This is a simple util that'll generate a css `transform-origin` string so
     * that the fixed element can animate from the correct point based on the
     * provided anchor.
     *
     * @param options - The anchor that should be used to create the transform origin
     * for.
     * @returns the transform origin string
     * @internal
     */
    export function getTransformOrigin(options: TransformOriginOptions): string {
      const { transformOriginY: yPosition } = options;

      let x = "0";
      switch (options.x) {
        case "right":
        case "inner-left":
          x = "0";
          break;
        case "center":
          x = "50%";
          break;
        case "left":
        case "inner-right":
          x = "100%";
          break;
        default:
          x = "0";
      }

      let y = "0";
      if (typeof yPosition === "number") {
        y = `${yPosition}px`;
      } else {
        switch (options.y) {
          case "above":
          case "bottom":
            y = "100%";
            break;
          case "center":
            y = "50%";
            break;
          case "below":
          case "top":
            y = "0";
            break;
          default:
            y = "0";
        }
      }

      return `${x} ${y}`;
    }

    /**
     * Attempts to find a sizing container based on the provided HTMLElement. By
     * default, the sizing element will just be the provided element unless:
     * - the item has a known role within react-md that can target known classes
     * - the item has a `data-sizing-selector` attribute that is a valid query
     *   selector for the nested item.
     *
     * NOTE: The `data-sizing-selector` will be run from the current element instead
     * of the `document`.
     *
     * @param el - The element to find a sizing container for.
     * @returns the sizing container relative to the provided element, or `null` if
     * none could be found.
     * @throws This error will be thrown if using the `data-query-selector` and the
     * query selector does not return an element on the page.
     * @internal
     */
    export function findSizingContainer(
      el: HTMLElement | null
    ): HTMLElement | null {
      if (!el) {
        return null;
      }

      if (/(tree|list)item/.test(el.getAttribute("role") || "")) {
        const content = el.querySelector(
          ".rmd-tree-item__content, .rmd-item-text"
        ) as HTMLElement;
        if (content) {
          return content;
        }
      } else if (el.getAttribute("type") === "file") {
        const label = document.querySelector<HTMLLabelElement>(`[for="${el.id}"]`);
        if (label) {
          return label;
        }
      }

      const data = el.getAttribute("data-sizing-selector");
      if (data) {
        const content = el.querySelector(data) as HTMLElement;
        if (content) {
          return content;
        }

        if (process.env.NODE_ENV !== "production") {
          throw new Error(
            "Unable to find a child element using the `data-sizing-selector`"
          );
        }
      }

      return el;
    }

    /**
     * This util is used to get the "true" `element.getBoundingClientRect()` that
     * ensures that transitions using transforms don't mess up the sizing so that
     * position calculations are easier to do.
     *
     * @param element - The element to get a rect for.
     * @returns either a DOMRect for the element
     * @internal
     */
    export function getElementRect(element: HTMLElement): DOMRect {
      const cloned = element.cloneNode(true) as HTMLElement;
      // remove the id so there won't be two elements with the same id on the page
      cloned.removeAttribute("id");

      // remove the role just in case the role would alert screen readers once added
      // to the dom
      cloned.removeAttribute("role");

      // ensure the cloned node won't shift the page or be visible
      cloned.style.position = "fixed";
      cloned.style.visibility = "hidden";

      // reset positioning to get a "pure" calculation. otherwise this will mess up
      // the height and width if the element is able to line wrap.
      cloned.style.left = "";
      cloned.style.top = "";
      cloned.style.right = "";
      cloned.style.bottom = "";

      // reset transforms so that custom animations don't mess with the sizing
      cloned.style.transform = "none";

      const parent = element.parentElement || document.body;
      parent.appendChild(cloned);

      const rect = cloned.getBoundingClientRect();
      parent.removeChild(cloned);

      return rect;
    }

    /**
     * @remarks \@since 4.0.0
     * @internal
     */
    interface IsWithinViewportOptions {
      fixedElement: HTMLElement;
      fixedToElement: HTMLElement;
    }

    /**
     * @remarks \@since 4.0.0
     * @internal
     */
    export function isWithinViewport(options: IsWithinViewportOptions): boolean {
      const { fixedElement, fixedToElement } = options;
      const fixedElementRect = fixedElement.getBoundingClientRect();
      const fixedToElementRect = fixedToElement.getBoundingClientRect();
      const vh = window.innerHeight;
      const vw = window.innerWidth;
      const top = Math.min(fixedElementRect.top, fixedToElementRect.top);
      const right = Math.max(fixedElementRect.right, fixedToElementRect.right);
      const bottom = Math.max(fixedElementRect.bottom, fixedToElementRect.bottom);
      const left = Math.min(fixedElementRect.left, fixedToElementRect.left);

      return bottom >= 0 && top <= vh && right >= 0 && left <= vw;
    }