# All files / src/positioning createHorizontalPosition.ts

100% Statements 330/330
100% Branches 59/59
100% Functions 7/7
100% Lines 330/330

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
    3311x
    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
    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
    21x
    21x
    21x
    21x
    21x
    8x
    8x
    13x
    13x
    21x
    8x
    21x
    5x
    5x
    5x
    13x
    13x
    13x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    37x
    37x
    37x
    37x
    37x
    5x
    5x
    32x
    37x
    3x
    1x
    3x
    2x
    2x
    3x
    3x
    3x
    29x
    29x
    37x
    26x
    37x
    1x
    1x
    3x
    2x
    2x
    2x
    29x
    29x
    29x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    91x
    91x
    91x
    81x
    91x
    4x
    4x
    91x
    91x
    91x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    45x
    45x
    45x
    45x
    45x
    6x
    6x
    39x
    39x
    45x
    4x
    45x
    35x
    35x
    35x
    39x
    39x
    39x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    19x
    19x
    19x
    19x
    19x
    16x
    16x
    3x
    3x
    19x
    2x
    12x
    1x
    1x
    1x
    3x
    3x
    3x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    9x
    9x
    9x
    9x
    9x
    9x
    5x
    5x
    5x
    9x
    9x
    9x
    9x
    4x
    4x
    4x
    9x
    9x
    9x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    15x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    151x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    149x
    151x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    148x
    148x
    148x
    148x
    148x
    148x
    148x
    148x
    148x
    148x
    148x
    148x
    151x
    1x
    151x
    25x
    151x
    79x
    151x
    35x
    151x
    8x
    151x
    151x
     import type { XCoordConfig } from "./utils.js";
    import {
      getCenterXCoord,
      getInnerLeftCoord,
      getInnerRightCoord,
      getLeftCoord,
      getRightCoord,
    } from "./utils.js";
    import type { FixedPositionOptions, HorizontalPosition } from "./types.js";

    /**
     * @internal
     */
    interface XPosition {
      left: number;
      right?: number;
      width?: number;
      minWidth?: number;
      actualX: HorizontalPosition;
    }

    /**
     * @internal
     */
    export interface FixConfig extends XCoordConfig {
      vwMargin: number;
      screenRight: number;
      disableSwapping: boolean;
    }

    /**
     * @internal
     */
    export interface CreateHorizontalPositionOptions
      extends Required<
        Pick<
          FixedPositionOptions,
          "vwMargin" | "xMargin" | "width" | "disableSwapping"
        >
      > {
      x: HorizontalPosition;
      vw: number;
      elWidth: number;
      initialX?: number;
      containerRect: DOMRect;
    }

    /**
     * Attempts to position the fixed element so that it will appear to the left of
     * the container element but also within the viewport boundaries. When swapping
     * is enabled, it will attempt to swap to the right position if it can't fit
     * within the viewport to the left. If it can't fit in the viewport even after
     * being swapped to the right or swapping is disabled, it will be positioned to
     * the viewport left boundary.
     *
     * @internal
     */
    export function createAnchoredLeft(config: FixConfig): XPosition {
      const { vwMargin, screenRight, elWidth, disableSwapping } = config;

      let left = getLeftCoord(config);
      let actualX: HorizontalPosition = "left";
      if (left >= vwMargin) {
        return { actualX, left };
      }

      const swappedLeft = getRightCoord(config);
      if (disableSwapping || swappedLeft + elWidth > screenRight) {
        left = vwMargin;
      } else {
        left = swappedLeft;
        actualX = "right";
      }

      return { actualX, left };
    }

    /**
     * Attempts to position the fixed element so that it will appear to the
     * inner-left of the container element but also within the viewport boundaries.
     * When swapping is enabled, it will attempt to swap to the right position if it
     * can't fit within the viewport to the left. If it can't fit in the viewport
     * even after being swapped to the right or swapping is disabled, it will be
     * positioned to the viewport left boundary.
     *
     * @internal
     */
    export function createAnchoredInnerLeft(config: FixConfig): XPosition {
      const { vwMargin, screenRight, elWidth, disableSwapping } = config;

      let left = getInnerLeftCoord(config);
      let actualX: HorizontalPosition = "inner-left";
      if (left + elWidth <= screenRight && left >= vwMargin) {
        return { actualX, left };
      }

      if (disableSwapping) {
        if (left + elWidth > screenRight) {
          left = screenRight - elWidth;
        } else {
          left = vwMargin;
        }

        return { actualX, left };
      }

      const swappedLeft = getInnerRightCoord(config);
      if (swappedLeft < vwMargin) {
        left = vwMargin;
      } else if (swappedLeft + elWidth > screenRight) {
        left = screenRight - elWidth;
        actualX = "inner-right";
      } else {
        left = swappedLeft;
        actualX = "inner-right";
      }

      return { actualX, left };
    }

    /**
     * Attempts to position the fixed element so that it will appear at the center
     * of the container element but also within the viewport boundaries. If the
     * centered element can't fit within the viewport, it will use the vwMargin
     * value if it overflowed to the left, it'll position to the screen right
     * boundary.
     *
     * @internal
     */
    export function createAnchoredCenter(config: FixConfig): XPosition {
      const { vwMargin, screenRight, elWidth } = config;
      let left = getCenterXCoord(config);
      if (left < vwMargin) {
        left = vwMargin;
      } else if (left + elWidth > screenRight || left < vwMargin) {
        left = screenRight - elWidth;
      }

      return { actualX: "center", left };
    }

    /**
     * Attempts to position the fixed element so that it will appear to the
     * inner-right of the container element but also within the viewport boundaries.
     * When swapping is enabled, it will attempt to swap to the inner-left position
     * if it can't fit within the viewport to the right. If it can't fit in the
     * viewport even after being swapped to the left or swapping is disabled, it
     * will be positioned to the viewport right boundary.
     *
     * @internal
     */
    export function createAnchoredInnerRight(config: FixConfig): XPosition {
      const { screenRight, vwMargin, elWidth, disableSwapping } = config;

      let left = getInnerRightCoord(config);
      let actualX: HorizontalPosition = "inner-right";
      if (left >= vwMargin) {
        return { actualX, left: Math.min(left, screenRight - elWidth) };
      }

      const swappedLeft = getInnerLeftCoord(config);
      if (disableSwapping || swappedLeft + elWidth > screenRight) {
        left = vwMargin;
      } else {
        left = Math.max(swappedLeft, vwMargin);
        actualX = "inner-left";
      }

      return { actualX, left };
    }

    /**
     * Attempts to position the fixed element so that it will appear to the right of
     * the container element but also within the viewport boundaries. When swapping
     * is enabled, it will attempt to swap to the left position if it can't fit
     * within the viewport to the right. If it can't fit in the viewport even after
     * being swapped to the left or swapping is disabled, it will be positioned to
     * the viewport right boundary.
     *
     * @internal
     */
    export function createAnchoredRight(config: FixConfig): XPosition {
      const { screenRight, vwMargin, elWidth, disableSwapping } = config;

      let left = getRightCoord(config);
      let actualX: HorizontalPosition = "right";
      if (left + elWidth <= screenRight) {
        return { actualX, left };
      }

      const swappedLeft = getLeftCoord(config);
      if (disableSwapping || swappedLeft < vwMargin) {
        left = screenRight - elWidth;
      } else {
        left = swappedLeft;
        actualX = "left";
      }

      return { actualX, left };
    }

    export interface EqualWidthOptions
      extends Pick<
        CreateHorizontalPositionOptions,
        "x" | "elWidth" | "xMargin" | "vwMargin" | "containerRect" | "initialX"
      > {
      screenRight: number;
      isMinWidth: boolean;
    }

    /**
     * @internal
     */
    export function createEqualWidth(options: EqualWidthOptions): XPosition {
      const {
        x,
        elWidth,
        xMargin,
        vwMargin,
        initialX,
        containerRect,
        screenRight,
        isMinWidth,
      } = options;

      let left = initialX ?? containerRect.left + xMargin;

      let width: number | undefined = containerRect.width - xMargin * 2;
      let minWidth: number | undefined;
      let right: number | undefined;
      if (isMinWidth) {
        minWidth = width;
        // if the fixed element has a width greater than the element it is fixed to,
        // update the width to be the fixed element's width. since the "min-width"
        // option is only possible for horizontally centered elements, need to then
        // update the `left` position again.
        if (elWidth > width) {
          left -= (elWidth - width) / 2;
          minWidth = elWidth;
        }

        width = undefined;
        const elRight = left + elWidth;
        if (elRight > screenRight) {
          left -= elRight - screenRight;
          right = vwMargin;
        }

        left = Math.max(vwMargin, left);
      }

      // going to assume that the container element is visible in the DOM and just
      // make the fixed element have the same left and right corners
      return {
        left,
        right,
        width,
        minWidth,
        actualX: x,
      };
    }

    /**
     * Creates the horizontal position for a fixed element with the provided
     * options.
     * @internal
     */
    export function createHorizontalPosition(
      options: CreateHorizontalPositionOptions
    ): XPosition {
      const {
        x,
        vw,
        vwMargin,
        xMargin,
        width,
        elWidth,
        initialX,
        containerRect,
        disableSwapping,
      } = options;

      const screenRight = vw - vwMargin;
      if (width === "min" || width === "equal") {
        return createEqualWidth({
          x,
          vwMargin,
          xMargin,
          elWidth,
          initialX,
          containerRect,
          screenRight,
          isMinWidth: width === "min",
        });
      }

      if (elWidth > vw - vwMargin * 2) {
        // if the element's width is greater than the viewport's width minus the
        // margin on both sides, just make the element span the entire viewport with
        // the margin
        return {
          left: vwMargin,
          right: vwMargin,
          actualX: x,
        };
      }

      const config: FixConfig = {
        vwMargin,
        xMargin,
        elWidth,
        initialX,
        screenRight,
        containerRect,
        disableSwapping,
      };

      switch (x) {
        case "left":
          return createAnchoredLeft(config);
        case "inner-left":
          return createAnchoredInnerLeft(config);
        case "center":
          return createAnchoredCenter(config);
        case "inner-right":
          return createAnchoredInnerRight(config);
        case "right":
          return createAnchoredRight(config);
      }
    }