# All files / src/transition useScaleTransition.ts

100% Statements 150/150
100% Branches 3/3
100% Functions 1/1
100% Lines 150/150

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
    1511x
    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
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
    879x
     "use client";
    import type {
      CSSTransitionClassNames,
      CSSTransitionClassNamesObject,
      CSSTransitionHookReturnValue,
      PreconfiguredCSSTransitionOptions,
      TransitionTimeoutObject,
    } from "./types.js";
    import { useCSSTransition } from "./useCSSTransition.js";

    /**
     * The default {@link CSSTransitionClassNames} for a horizontal scale
     * transition.
     *
     * @remarks
     * \@since 2.0.0
     * \@since 6.0.0 The class names were updated to be prefixed with
     * `rmd-scale-transition`
     */
    export const SCALE_CLASSNAMES: Readonly<CSSTransitionClassNamesObject> = {
      appear: "rmd-scale-transition--enter",
      appearActive: "rmd-scale-transition--enter-active",
      enter: "rmd-scale-transition--enter",
      enterActive: "rmd-scale-transition--enter-active",
      enterDone: "",
      exit: "rmd-scale-transition--exit",
      exitActive: "rmd-scale-transition--exit-active",
    };

    /**
     * The default {@link CSSTransitionClassNames} for a vertical scale transition.
     *
     * @remarks
     * \@since 2.0.0
     * \@since 6.0.0 The class names were updated to be prefixed with
     * `rmd-scale-y-transition` and merged with the {@link SCALE_CLASSNAMES}
     */
    export const SCALE_Y_CLASSNAMES: Readonly<CSSTransitionClassNamesObject> = {
      appear: "rmd-scale-transition--enter rmd-scale-transition--y-enter",
      appearActive:
        "rmd-scale-transition--enter-active rmd-scale-transition--y-enter-active",
      enter: "rmd-scale-transition--enter rmd-scale-transition--y-enter",
      enterActive:
        "rmd-scale-transition--enter-active rmd-scale-transition--y-enter-active",
      enterDone: "",
      exit: "rmd-scale-transition--exit rmd-scale-transition--y-exit",
      exitActive:
        "rmd-scale-transition--exit-active rmd-scale-transition--y-exit-active",
    };

    /**
     * The default {@link TransitionTimeout} to use for horizontal and vertical
     * scale transitions.
     *
     * @remarks \@since 2.0.0
     */
    export const SCALE_TIMEOUT: Readonly<TransitionTimeoutObject> = {
      enter: 200,
      exit: 150,
    };

    /**
     * @typeParam E - The HTMLElement type used or the ref required for the
     * transition.
     * @remarks \@since 4.0.0
     */
    export interface ScaleTransitionHookOptions<E extends HTMLElement>
      extends PreconfiguredCSSTransitionOptions<E> {
      /**
       * Boolean if the scale transition should be vertical instead of horizontal.
       * This really only changes the default value for the {@link classNames}.
       *
       * @defaultValue `false`
       */
      vertical?: boolean;

      /**
       * @see {@link PreconfiguredCSSTransitionOptions.temporary}
       * @defaultValue `true`
       */
      temporary?: boolean;

      /**
       * @see {@link vertical}
       * @see {@link SCALE_CLASSNAMES}
       * @see {@link SCALE_Y_CLASSNAMES}
       * @defaultValue `vertical ? SCALE_Y_CLASSNAMES : SCALE_CLASSNAMES`
       */
      classNames?: CSSTransitionClassNames;
    }

    /**
     * Implements a scale transition that should generally be used for temporary
     * elements that are positioned via `position: absolute` or `position: fixed`.
     *
     * @example
     * Dropdown Menu Example
     * ```tsx
     * import { ReactElement, useRef, useState } from "react";
     * import { Button, useFixedPositioning, useScaleTransition } from "@react-md/core";
     *
     * function Example(): ReactElement {
     *   const buttonRef = useRef<HTMLButtonElement>(null);
     *   const [transitionIn, setTransitionIn] = useState(false);
     *   const { style, transitionOptions } = useFixedPositioning({
     *     fixedTo: buttonRef,
     *   });
     *   const { elementProps, rendered } = useScaleTransition({
     *     ...transitionOptions,
     *     transitionIn,
     *     vertical: true,
     *   });
     *
     *   return (
     *     <>
     *       <Button ref={buttonRef} onClick={() => setTransitionIn(!transitionIn)}>
     *         Toggle
     *       </Button>
     *       {rendered && (
     *         <div {...elementProps} style={style}>
     *           Some content within a menu
     *         </div>
     *       )}
     *     </>
     *   );
     * }
     * ```
     *
     * @typeParam E - The HTMLElement type used or the ref required for the
     * transition.
     * @remarks \@since 4.0.0
     */
    export function useScaleTransition<E extends HTMLElement>(
      options: ScaleTransitionHookOptions<E>
    ): CSSTransitionHookReturnValue<E> {
      const {
        timeout = SCALE_TIMEOUT,
        vertical = false,
        temporary = true,
        classNames = vertical ? SCALE_Y_CLASSNAMES : SCALE_CLASSNAMES,
        ...transitionOptions
      } = options;

      return useCSSTransition({
        ...transitionOptions,
        timeout,
        temporary,
        classNames,
      });
    }