# All files createVerticalPosition.ts

81.57% Statements 248/304
72.97% Branches 27/37
100% Functions 6/6
81.57% Lines 248/304

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
    3051x
    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
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    20x
    7x
    7x
    7x
    7x
    13x
    20x
    9x
    9x
    9x
    9x
    9x
    4x
    4x
    20x
    2x
    20x
    2x
    2x
    2x
    4x
    4x
    4x
    4x

    20x

    4x
    4x
    4x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    47x
    47x
    47x
    47x
    47x
    47x
    47x
    47x

    47x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    8x
    8x
    8x
    8x

    8x
    8x
    8x
    1x
    1x
    8x
    8x
    8x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    3x
    3x
    3x
    3x
    3x
    3x
    3x

    3x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x
    160x

    160x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    229x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    227x
    229x
    9x
    229x
    47x
    229x
    8x
    229x
    3x
    229x
    160x
    229x
    229x
     import { type FixedPositionOptions, type VerticalPosition } from "./types.js";
    import {
      getAboveCoord,
      getBelowCoord,
      getBottomCoord,
      getCenterYCoord,
      getTopCoord,
      type YCoordConfig,
    } from "./utils.js";

    /** @internal */
    interface YPosition {
      top: number;
      bottom?: number;
      actualY: VerticalPosition;
      /** @since 5.1.6 */
      transformOriginY?: number;
    }

    /** @internal */
    export interface FixConfig extends YCoordConfig {
      vhMargin: number;
      screenBottom: number;
      preventOverlap: boolean;
      disableSwapping: boolean;
      disableVHBounds: boolean;
    }

    /** @internal */
    export interface CreateVerticalPositionOptions
      extends Required<
        Pick<
          FixedPositionOptions,
          | "yMargin"
          | "vhMargin"
          | "preventOverlap"
          | "disableSwapping"
          | "disableVHBounds"
        >
      > {
      y: VerticalPosition;
      vh: number;
      initialY?: number;
      elHeight: number;
      containerRect: DOMRect;
    }

    /**
     * Attempts to position the fixed element so that it will appear completely
     * above the container element but also within the viewport boundaries. When
     * swapping is enabled, it will attempt to swap to the below position if it
     * can't fit within the viewport above the container element. If it can't fit in
     * the viewport even after being swapped below or swapping is disabled, it will
     * be positioned to the top viewport boundary.
     *
     * @internal
     */
    export function createAnchoredAbove(config: FixConfig): YPosition {
      const {
        yMargin,
        vhMargin,
        screenBottom,
        elHeight,
        containerRect,
        preventOverlap,
        disableSwapping,
        disableVHBounds,
      } = config;
      let top = getAboveCoord(config);
      let actualY: VerticalPosition = "above";

      if (disableVHBounds) {
        // can't actually allow a top value as a negative number since browsers
        // won't scroll upwards past the normal page top
        return { actualY, top: Math.max(0, top) };
      }

      if (top > vhMargin) {
        // don't need to do anything else since the top is still in the viewport and
        // since it's positioned above, we already know it can't overlap the
        // container element
        return { actualY, top };
      }

      const swappedTop = getBelowCoord(config);
      if (disableSwapping || swappedTop + elHeight > screenBottom) {
        top = Math.min(top, vhMargin);
      } else {
        actualY = "below";
        top = swappedTop;
      }

      let bottom: number | undefined;
      if (
        preventOverlap &&
        // can't overlap if it's positioned below
        actualY === "above" &&
        top + elHeight > containerRect.top
      ) {
        bottom = screenBottom - containerRect.top + yMargin;
      }

      return { actualY, top, bottom };
    }

    /**
     * Attempts to position the fixed element so that it will appear fixed to the
     * top of the container element but also within the viewport boundaries. When
     * swapping is enabled, it will attempt to swap to the bottom position if it
     * can't fit within the viewport. If it can't fit in the viewport even after
     * being swapped to the bottom position or swapping is disabled, it will be
     * positioned to the top viewport boundary.
     *
     * @internal
     */
    export function createAnchoredTop(config: FixConfig): YPosition {
      const { vhMargin, screenBottom, elHeight, disableSwapping, disableVHBounds } =
        config;
      let top = getTopCoord(config);
      let actualY: VerticalPosition = "top";

      if (disableVHBounds || top + elHeight <= screenBottom) {
        return { actualY, top };
      }

      const swappedTop = getBottomCoord(config);
      if (disableSwapping || swappedTop < vhMargin) {
        top = Math.max(top, vhMargin);
      } else {
        actualY = "bottom";
        top = swappedTop;
      }

      return { actualY, top };
    }

    /**
     * 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
     * entered element can't fit within the viewport, it'll update the top value
     * to either be the vhMargin or position to the screen bottom boundary
     *
     * @internal
     */
    export function createAnchoredCenter(config: FixConfig): YPosition {
      const { vhMargin, screenBottom, elHeight, disableVHBounds } = config;
      let top = getCenterYCoord(config);
      const actualY: VerticalPosition = "center";
      if (disableVHBounds) {
        return { actualY, top: Math.max(0, top) };
      }

      top = Math.max(vhMargin, top);
      if (top + elHeight > screenBottom) {
        top = screenBottom - elHeight;
      }

      return { actualY, top };
    }

    /**
     * Attempts to position the fixed element so that it will appear fixed to the
     * bottom of the container element but also within the viewport boundaries. When
     * swapping is enabled, it will attempt to swap to the top position if it can't
     * fit within the viewport. If it can't fit in the viewport even after being
     * swapped to the top position or swapping is disabled, it will be positioned to
     * the bottom viewport boundary.
     *
     * @internal
     */
    export function createAnchoredBottom(config: FixConfig): YPosition {
      const { vhMargin, screenBottom, elHeight, disableSwapping, disableVHBounds } =
        config;
      let top = getBottomCoord(config);
      let actualY: VerticalPosition = "bottom";
      if (disableVHBounds || top > vhMargin) {
        return { actualY, top };
      }

      const swappedTop = getTopCoord(config);
      if (disableSwapping || swappedTop + elHeight > screenBottom) {
        top = Math.min(top, screenBottom - elHeight);
      } else {
        actualY = "top";
        top = swappedTop;
      }

      return { actualY, top };
    }

    /**
     * Attempts to position the fixed element so that it will appear completely
     * below the container element but also within the viewport boundaries. When
     * swapping is enabled, it will attempt to swap to the above position if it
     * can't fit within the viewport below the container element. If it can't fit in
     * the viewport even after being swapped above or swapping is disabled, it will
     * be positioned to the bottom viewport boundary.
     *
     * @internal
     */
    export function createAnchoredBelow(config: FixConfig): YPosition {
      const {
        yMargin,
        vhMargin,
        elHeight,
        screenBottom,
        containerRect,
        preventOverlap,
        disableSwapping,
        disableVHBounds,
      } = config;
      let top = getBelowCoord(config);
      let actualY: VerticalPosition = "below";
      if (disableVHBounds || top + elHeight <= screenBottom) {
        return { actualY, top };
      }

      if (preventOverlap) {
        const availableTop = containerRect.top - yMargin;
        if (disableSwapping || availableTop < screenBottom - top) {
          return {
            actualY,
            top,
            bottom: vhMargin,
          };
        }

        return {
          actualY: "above",
          top: Math.max(vhMargin, availableTop - elHeight),
          // this makes it so that the bottom of the fixed element is the top of the container
          // element. this ensures that it won't ever overlap the container element
          bottom: window.innerHeight - availableTop,
        };
      }

      const swappedTop = getAboveCoord(config);
      if (disableSwapping || swappedTop < vhMargin) {
        top = Math.min(top, screenBottom - elHeight);
      } else {
        actualY = "above";
        top = swappedTop;
      }

      return { actualY, top };
    }

    /**
     * Creates the vertical position for a fixed element with the provided options.
     *
     * @internal
     */
    export function createVerticalPosition(
      options: CreateVerticalPositionOptions
    ): YPosition {
      const {
        y,
        vh,
        vhMargin,
        yMargin,
        elHeight,
        initialY,
        containerRect,
        disableSwapping,
        preventOverlap,
        disableVHBounds,
      } = options;

      if (!disableVHBounds && !preventOverlap && elHeight > vh - vhMargin * 2) {
        // the element is too big to be displayed in the viewport, so just span the
        // full viewport excluding margins
        return {
          top: vhMargin,
          bottom: vhMargin,
          actualY: "center",
          transformOriginY: containerRect.top,
        };
      }

      const config: FixConfig = {
        vhMargin,
        yMargin,
        elHeight,
        initialY,
        containerRect,
        screenBottom: vh - vhMargin,
        preventOverlap,
        disableSwapping,
        disableVHBounds,
      };

      switch (y) {
        case "above":
          return createAnchoredAbove(config);
        case "top":
          return createAnchoredTop(config);
        case "center":
          return createAnchoredCenter(config);
        case "bottom":
          return createAnchoredBottom(config);
        case "below":
          return createAnchoredBelow(config);
      }
    }