# All files / src/overlay Overlay.tsx

100% Statements 157/157
55.55% Branches 5/9
100% Functions 1/1
100% Lines 157/157

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
    1581x
    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
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    209x
    1x
     "use client";
    import { forwardRef, type HTMLAttributes } from "react";
    import { type BoxAlignItems, type BoxJustifyContent } from "../box/styles.js";
    import { Portal } from "../portal/Portal.js";
    import { useSsr } from "../SsrProvider.js";
    import {
      type CSSTransitionComponentProps,
      type TransitionActions,
    } from "../transition/types.js";
    import { useCSSTransition } from "../transition/useCSSTransition.js";
    import {
      DEFAULT_OVERLAY_CLASSNAMES,
      DEFAULT_OVERLAY_TIMEOUT,
      overlay,
    } from "./overlayStyles.js";

    /**
     * @remarks \@since 6.0.0 Added `align` and `justify` props.
     */
    export interface OverlayProps
      extends HTMLAttributes<HTMLSpanElement>,
        CSSTransitionComponentProps,
        TransitionActions {
      /**
       * @defaultValue `"center"`
       * @remarks \@since 6.0.0
       */
      align?: BoxAlignItems;

      /**
       * @defaultValue `"center"`
       * @remarks \@since 6.0.0
       */
      justify?: BoxJustifyContent;

      /**
       * Set this to `true` for when the overlay should be visible. Toggling this
       * value will trigger the enter/exit animation.
       */
      visible: boolean;

      /**
       * Set this to `true` if the overlay should be rendered with an `opacity: 0`
       * and disabling the animation. This is useful if you'd like a "close on
       * outside click" behavior.
       *
       * @defaultValue `false`
       */
      noOpacity?: boolean;

      /**
       * @see {@link OverlayClassNameOptions.clickable}
       * @defaultValue `!noOpacity`
       */
      clickable?: boolean;

      /**
       * @defaultValue `false`
       */
      disablePortal?: boolean;

      /**
       * @defaultValue `false`
       */
      disableTransition?: boolean;
    }

    /**
     * **Client Component**
     *
     * @example
     * Simple Example
     * ```tsx
     * import { Button, Overlay, useToggle } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   const { toggle, disable, toggled: visible } = useToggle(false);
     *
     *   return (
     *     <>
     *       <Button onClick={toggle}>Toggle</Button>
     *       <Overlay visible={visible} onClick={disable} />
     *     </>
     *   );
     * }
     * ```
     *
     * @remarks \@since 6.0.0 Removed the `onRequestClose` prop in favor of using
     * the `onClick` prop instead.
     */
    export const Overlay = forwardRef<HTMLSpanElement, OverlayProps>(
      function Overlay(props, nodeRef) {
        const {
          children,
          className,
          visible,
          noOpacity = false,
          clickable = !noOpacity,
          temporary = true,
          timeout = DEFAULT_OVERLAY_TIMEOUT,
          classNames = DEFAULT_OVERLAY_CLASSNAMES,
          disableTransition = false,
          align = "center",
          justify = "center",
          appear,
          enter,
          exit,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
          onExited,
          exitedHidden = true,
          disablePortal: propDisablePortal = false,
          ...remaining
        } = props;

        const ssr = useSsr();
        const { elementProps, rendered, disablePortal } = useCSSTransition({
          nodeRef,
          transitionIn: visible,
          timeout: noOpacity ? 0 : timeout,
          classNames: noOpacity ? "" : classNames,
          className: overlay({
            visible,
            clickable,
            align,
            justify,
            className,
          }),
          appear: appear && !disableTransition && !ssr,
          enter: enter && !disableTransition,
          exit: exit && !disableTransition,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
          onExited,
          temporary,
          exitedHidden,
          disablePortal: propDisablePortal,
        });

        return (
          <Portal disabled={disablePortal}>
            {rendered && (
              <span {...remaining} {...elementProps}>
                {children}
              </span>
            )}
          </Portal>
        );
      }
    );