# All files / transition useTransition.ts

100% Statements 286/286
97.1% Branches 67/69
100% Functions 3/3
100% Lines 286/286

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
    2871x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    55x
    55x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    57x
    57x
    57x
    57x
    57x
    57x
    57x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    57x
    57x
    18x
    18x
    18x
    18x
    18x
    57x
    10x
    10x
    10x
    10x
    10x
    10x
    10x
    57x
    57x
    14x
    14x
    14x
    14x
    14x
    57x
    6x
    4x
    4x
    2x
    2x
    2x
    2x
    2x
    2x
    57x
    57x
    99x
    99x
    13x
    13x
    2x
    2x
    13x
    13x
    13x
    13x
    13x
    13x
    13x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    87x
    13x
    13x
    13x
    13x
    13x
    87x
    87x
    87x
    87x
    87x
    5x
    3x
    1x
    87x
    1x
    1x
    87x
    87x
    87x
    87x
    10x
    10x
    10x
    10x
    77x
    77x
    77x
    87x
    10x
    10x
    10x
    67x
    87x
    4x
    4x
    4x
    67x
    67x
    67x
    67x
    87x
    10x
    1x
    10x
    9x
    9x
    9x
    10x
    87x
    9x
    9x
    9x
    9x
    87x
    10x
    10x
    87x
    9x
    1x
    9x
    8x
    8x
    8x
    9x
    87x
    8x
    8x
    8x
    8x
    87x
    21x
    21x
    21x
    87x
    67x
    87x
    31x
    31x
    31x
    4x
    4x
    31x
    31x
    31x
    36x
    36x
    36x
    36x
    36x
    87x
    87x
    19x
    19x
    19x
    17x
    17x
    14x
    17x
    17x
    17x
    17x
    17x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
    99x
     "use client";
    import { useEffect, useReducer, useRef, useState } from "react";
    import { useSsr } from "../SsrProvider.js";
    import { useEnsuredRef } from "../useEnsuredRef.js";
    import { useIsomorphicLayoutEffect } from "../useIsomorphicLayoutEffect.js";
    import type {
      TransitionHookOptions,
      TransitionHookReturnValue,
      TransitionStage,
      TransitionState,
    } from "./types.js";
    import { getTransitionTimeout } from "./utils.js";
    import { TRANSITION_CONFIG } from "./config.js";

    const INITIAL_STATE: TransitionState = {
      appearing: false,
      rendered: true,
      stage: "exited",
    };

    const noop = (): void => {
      // do nothing
    };

    /**
     * You'll most likely want to use the {@link useCSSTransition} hook instead
     * since this is just a low-level hook that can be used to transition using
     * timeouts.
     *
     * @typeParam E - The HTMLElement type used or the ref required for the
     * transition.
     * @remarks \@since 4.0.0
     */
    export function useTransition<E extends HTMLElement>(
      options: TransitionHookOptions<E>
    ): TransitionHookReturnValue<E> {
      const {
        nodeRef,
        timeout,
        transitionIn,
        reflow = false,
        temporary = false,
        appear = false,
        enter = true,
        exit = true,
        onEnter = noop,
        onEntering = noop,
        onEntered = noop,
        onExit = noop,
        onExiting = noop,
        onExited = noop,
        disablePortal: propDisablePortal,
      } = options;

      const configurationRef = useRef({
        timeout: getTransitionTimeout({ timeout, appear, enter, exit }),
        reflow,
        onEnter,
        onEntering,
        onEntered,
        onExit,
        onExiting,
        onExited,
      } as const);
      useIsomorphicLayoutEffect(() => {
        configurationRef.current = {
          timeout: getTransitionTimeout({ timeout, appear, enter, exit }),
          reflow,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
          onExited,
        };
      }, [
        appear,
        enter,
        exit,
        onEnter,
        onEntered,
        onEntering,
        onExit,
        onExited,
        onExiting,
        reflow,
        timeout,
      ]);

      const ssr = useSsr();
      const [ref, refCallback] = useEnsuredRef<E>(nodeRef);
      const [disablePortal, setDisablePortal] = useState(ssr);
      const [state, dispatch] = useReducer(
        function reducer(
          state: TransitionState,
          action: TransitionStage | "unmount"
        ): TransitionState {
          const { appear, enter, exit } = configurationRef.current.timeout;
          const { appearing } = state;
          switch (action) {
            case "enter": {
              const duration = appearing ? appear : enter;
              return {
                stage: duration > 0 ? "enter" : "entered",
                rendered: true,
                appearing,
              };
            }
            case "entering":
            case "entered":
              return {
                stage: action,
                rendered: true,
                appearing,
              };
            case "exit": {
              const stage = exit > 0 ? "exit" : "exited";
              return {
                stage,
                rendered: !temporary || stage !== "exited",
                appearing: false,
              };
            }
            case "exiting":
            case "exited":
              return {
                stage: action,
                rendered: true,
                appearing: false,
              };
            case "unmount":
              if (state.stage === "exited" && !state.appearing && !state.rendered) {
                return state;
              }

              return {
                stage: "exited",
                rendered: false,
                appearing: false,
              };
          }
        },
        INITIAL_STATE,
        () => {
          let stage: TransitionStage = "exited";
          if (transitionIn) {
            stage = appear && !TRANSITION_CONFIG.disabled ? "enter" : "entered";
          }

          return {
            appearing: appear && transitionIn && !TRANSITION_CONFIG.disabled,
            rendered: !temporary || transitionIn,
            stage,
          };
        }
      );
      const { appearing, rendered, stage } = state;

      const isFirstRender = useRef(true);
      const isRehydrateAppear = useRef(ssr && !transitionIn);
      const defaultTransitionIn = useRef(transitionIn);
      useEffect(() => {
        const {
          timeout,
          reflow,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
        } = configurationRef.current;
        let { onExited } = configurationRef.current;
        if (isFirstRender.current) {
          // The exited hook should **not** fire on first render since the element
          // was never in the DOM.
          isFirstRender.current = false;
          onExited = noop;
        }

        // if the transitionIn did not change between initial render and rehydration,
        // allow the enter transition to behave like normal.
        if (
          isRehydrateAppear.current &&
          !ssr &&
          !transitionIn &&
          !defaultTransitionIn.current
        ) {
          isRehydrateAppear.current = false;
        }

        // Cancel any exiting/exited transitions and instead immediately start the
        // enter transition
        if (transitionIn && stage.startsWith("exit")) {
          const nextStage = isRehydrateAppear.current ? "entered" : "enter";
          dispatch(nextStage);
          return;
        }

        // Cancel any entering/entered transitions and instead immediately start the
        // exit transition
        if (!transitionIn && stage.startsWith("enter")) {
          dispatch("exit");
          return;
        }

        if (reflow && ref.current && stage !== "exited" && stage !== "entered") {
          // force reflow by accessing scrollTop
          ref.current.scrollTop;
        }

        let duration = 0;
        let nextStage: TransitionStage = stage;
        switch (stage) {
          case "enter":
            if (TRANSITION_CONFIG.disabled) {
              nextStage = "entered";
            } else {
              onEnter(appearing);
              nextStage = "entering";
            }
            break;
          case "entering":
            onEntering(appearing);
            duration = timeout.enter;
            nextStage = "entered";
            break;
          case "entered":
            onEntered(appearing);
            break;
          case "exit":
            if (TRANSITION_CONFIG.disabled) {
              nextStage = "exited";
            } else {
              onExit();
              nextStage = "exiting";
            }
            break;
          case "exiting":
            onExiting();
            duration = timeout.exit;
            nextStage = "exited";
            break;
          case "exited":
            onExited();
            setDisablePortal(false);
            break;
        }

        if (stage === nextStage) {
          // this is used to help catch changing the temporary prop.
          // not sure if I should really support that though...
          if (stage === "exited" && temporary) {
            dispatch("unmount");
          }

          return;
        }

        // I used to rely on the `dispatch("unmount")` above, but it seems like
        // there are some cases where re-rendering takes too long so the temporary
        // element flashes
        const dispatchStage =
          temporary && nextStage === "exited" ? "unmount" : nextStage;
        if (duration <= 0) {
          dispatch(dispatchStage);
          return;
        }

        const timer = window.setTimeout(() => {
          dispatch(dispatchStage);
        }, duration);

        return () => {
          window.clearTimeout(timer);
        };
      }, [appearing, ref, ssr, stage, temporary, transitionIn]);

      return {
        ref: refCallback,
        stage,
        rendered,
        appearing,
        transitionTo: dispatch,
        disablePortal: propDisablePortal || disablePortal,
      };
    }