# All files / src/transition useCrossFadeTransition.ts

100% Statements 128/128
100% Branches 1/1
100% Functions 1/1
100% Lines 128/128

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
    1291x
    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
    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
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
    74x
     "use client";
    import type {
      CSSTransitionClassNames,
      CSSTransitionClassNamesObject,
      CSSTransitionHookReturnValue,
      PreconfiguredCSSTransitionInDefaultedOptions,
      TransitionTimeout,
      TransitionTimeoutObject,
    } from "./types.js";
    import { useCSSTransition } from "./useCSSTransition.js";

    /**
     * The default cross fade transition classes to use.
     *
     * @remarks \@since 2.0.0
     */
    export const CROSS_FADE_CLASSNAMES: Readonly<CSSTransitionClassNamesObject> = {
      appear: "rmd-cross-fade",
      appearActive: "rmd-cross-fade--active",
      enter: "rmd-cross-fade",
      enterActive: "rmd-cross-fade--active",
    };

    /**
     * The default cross fade transition timeout.
     *
     * @remarks \@since 2.0.0
     */
    export const CROSS_FADE_TIMEOUT: Readonly<TransitionTimeoutObject> = {
      appear: 300,
      enter: 300,
      exit: 0,
    };

    /**
     * @typeParam E - An HTMLElement type used for the ref required for the
     * transition.
     * @remarks \@since 4.0.0
     */
    export interface CrossFadeTransitionHookOptions<E extends HTMLElement>
      extends PreconfiguredCSSTransitionInDefaultedOptions<E> {
      /**
       * @see {@link TransitionTimeout}
       * @see {@link CROSS_FADE_TIMEOUT}
       * @defaultValue `CROSS_FADE_TIMEOUT`
       */
      timeout?: TransitionTimeout;

      /**
       * @see {@link CSSTransitionClassNames}
       * @see {@link CROSS_FADE_CLASSNAMES}
       * @defaultValue `CROSS_FADE_CLASSNAMES`
       */
      classNames?: CSSTransitionClassNames;

      /**
       * @see {@link PreconfiguredCSSTransitionInDefaultedOptions.transitionIn}
       * @defaultValue `true`
       */
      transitionIn?: boolean;
    }

    /**
     * This hook is used to create a "cross fade" transition -- a transition that
     * gradually increases the opacity and transforms the element vertically a short
     * distance. This is generally used for full page transitions when a route
     * changes.
     *
     * @example
     * New Page Transition with the Layout component
     * ```tsx
     * import { ReactElement, ReactNode, useLayoutEffect } from "react";
     * import { useLocation } from "react-router-dom":
     * import { Layout, useCrossFadeTransition, useLayoutNavigation } from "@react-md/core";
     *
     * import { navItems } from "./navItems";
     *
     * interface ExampleProps {
     *   children: ReactNode;
     * }
     *
     * function Example({ children }: ExampleProps): ReactElement {
     *   const { pathname } = useLocation();
     *   const { elementProps, transitionTo } = useCrossFadeTransition();
     *
     *   const prevPathname = useRef(pathname);
     *   useLayoutEffect(() => {
     *     if (prevPathname.current === pathname) {
     *       return
     *     }
     *
     *     prevPathname.current = pathname;
     *     transitionTo('enter');
     *   }, [pathname, transitionTo])
     *
     *   return (
     *     <Layout
     *       {...useLayoutNavigation(navItems, pathname)}
     *       appBarTitle="My App"
     *       mainProps={elementProps}
     *     >
     *       {children}
     *     </Layout>
     *   );
     * }
     * ```
     *
     * @typeParam E - An HTMLElement type used for the ref required for the
     * transition.
     * @remarks \@since 4.0.0
     */
    export function useCrossFadeTransition<E extends HTMLElement>(
      options: CrossFadeTransitionHookOptions<E> = {}
    ): CSSTransitionHookReturnValue<E> {
      const {
        transitionIn = true,
        timeout = CROSS_FADE_TIMEOUT,
        classNames = CROSS_FADE_CLASSNAMES,
        ...cssOptions
      } = options;

      return useCSSTransition({
        ...cssOptions,
        timeout,
        classNames,
        transitionIn,
      });
    }