# All files / src/transition utils.ts

100% Statements 148/148
97.72% Branches 43/44
100% Functions 3/3
100% Lines 148/148

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
    1491x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    7567x
    7567x
    7567x
    7567x
    1385x
    1385x
    1385x
    1385x
    1385x
    1385x
    6182x
    6182x
    7567x
    7567x
    7567x
    7567x
    7567x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    38x
    38x
    38x
    38x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    37x
    1x
    1x
    1x
    37x
    37x
    1x
    1x
    1x
    37x
    37x
    38x
    38x
    38x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    43x
    43x
    43x
    43x
    43x
    43x
    43x
    43x
    43x
    43x
    43x
    43x
    43x
    1894x
    1894x
    1894x
    1894x
    1894x
    1894x
    1894x
    1894x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
    1937x
     import type {
      CSSTransitionClassNames,
      CSSTransitionClassNamesObject,
      TransitionActions,
      TransitionTimeout,
      TransitionTimeoutObject,
    } from "./types.js";

    /**
     * @remarks \@since 4.0.0
     * @internal
     */
    export interface TransitionTimeoutOptions extends Required<TransitionActions> {
      timeout: TransitionTimeout;
    }

    /**
     * @remarks \@since 4.0.0
     * @internal
     */
    export function getTransitionTimeout(
      options: Readonly<TransitionTimeoutOptions>
    ): Readonly<Required<TransitionTimeoutObject>> {
      const { timeout, appear, enter, exit } = options;
      if (typeof timeout === "number") {
        return {
          appear: appear ? timeout : 0,
          enter: enter ? timeout : 0,
          exit: exit ? timeout : 0,
        };
      }

      return {
        appear: (appear && (timeout.appear ?? timeout.enter)) || 0,
        enter: (enter && timeout.enter) || 0,
        exit: (exit && timeout.exit) || 0,
      };
    }

    /**
     * @remarks \@since 4.0.0
     * @internal
     */
    export interface CollapseSizing {
      maxHeight?: number;
      paddingTop?: number;
      paddingBottom?: number;
    }

    /**
     * A small util that will find the max-height, padding-top, and padding-bottom
     * for the provided element. This is really used to be able to transition the
     * max-height value since `max-height: auto` does not transition. The only way
     * to get transition is to change max-height values manually.
     *
     * @remarks \@since 4.0.0
     * @internal
     */
    export function getElementSizing(element: HTMLElement | null): CollapseSizing {
      let maxHeight;
      let paddingTop;
      let paddingBottom;
      if (element) {
        // clone the element so that the total height and padding can be calculated
        // without being affected by the collapse transition inline styles
        const cloned = element.cloneNode(true) as HTMLElement;
        cloned.style.maxHeight = "";
        cloned.style.padding = "";
        cloned.style.paddingLeft = element.style.paddingLeft;
        cloned.style.paddingRight = element.style.paddingRight;
        cloned.style.visibility = "hidden";

        const container = element.parentElement || document.body;
        container.appendChild(cloned);
        maxHeight = cloned.scrollHeight;
        const style = window.getComputedStyle(cloned);
        if (style.paddingTop) {
          paddingTop = parseFloat(style.paddingTop);
          maxHeight += paddingTop;
        }

        if (style.paddingBottom) {
          paddingBottom = parseFloat(style.paddingBottom);
          maxHeight += paddingBottom;
        }
        container.removeChild(cloned);
      }

      return { maxHeight, paddingTop, paddingBottom };
    }

    /**
     * @remarks \@since 4.0.0
     * @internal
     */
    export interface TransitionClassNamesOptions extends TransitionTimeoutOptions {
      classNames: CSSTransitionClassNames;
    }

    /**
     * @remarks \@since 4.0.0
     * @internal
     */
    export function getTransitionClassNames(
      options: TransitionClassNamesOptions
    ): Readonly<Required<CSSTransitionClassNamesObject>> {
      const { classNames, ...timeoutOptions } = options;
      const timeout = getTransitionTimeout(timeoutOptions);

      if (typeof classNames === "string") {
        const { appear, enter, exit } = timeout;
        return {
          appear: appear ? `${classNames}--appear` : "",
          appearActive: appear ? `${classNames}--appear-active` : "",
          appearDone: "",
          enter: enter ? `${classNames}--enter` : "",
          enterActive: enter ? `${classNames}--enter-active` : "",
          enterDone: "",
          exit: exit ? `${classNames}--exit` : "",
          exitActive: exit ? `${classNames}--exit-active` : "",
          exitDone: "",
        };
      }

      const {
        enter = "",
        enterActive = "",
        enterDone = "",
        exit = "",
        exitActive = "",
        exitDone = "",
        appear = (timeout.appear && enter) || "",
        appearActive = (timeout.appear && enterActive) || "",
        appearDone = (timeout.appear && enterDone) || "",
      } = classNames;

      return {
        appear,
        appearActive,
        appearDone,
        enter,
        enterActive,
        enterDone,
        exit,
        exitActive,
        exitDone,
      };
    }