# All files / snackbar Toast.tsx

99.23% Statements 259/261
50% Branches 6/12
100% Functions 1/1
99.23% Lines 259/261

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
    2621x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    2x
    2x
    2x
    2x
    2x
    1x
    1x
    1x
    1x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    2x
    273x
    273x
    273x

    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    29x
    29x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    244x
    273x
    273x
    273x
    273x
    273x
    273x
    273x
    1x
     "use client";
    import {
      forwardRef,
      isValidElement,
      type AriaRole,
      type HTMLAttributes,
      type ReactElement,
      type ReactNode,
    } from "react";
    import { type ButtonProps } from "../button/Button.js";
    import { type BackgroundColor } from "../cssUtils.js";
    import {
      type CSSTransitionClassNames,
      type TransitionCallbacks,
      type TransitionTimeout,
    } from "../transition/types.js";
    import { useScaleTransition } from "../transition/useScaleTransition.js";
    import { type PropsWithRef } from "../types.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import { ToastActionButton } from "./ToastActionButton.js";
    import { ToastCloseButton } from "./ToastCloseButton.js";
    import { ToastContent, type ToastContentProps } from "./ToastContent.js";
    import { toast, type ToastPosition } from "./toastStyles.js";

    /**
     * @remarks \@since 6.0.0
     */
    export interface ConfigurableToastProps
      extends HTMLAttributes<HTMLDivElement>,
        TransitionCallbacks {
      /**
       * Note: this default value will only be generated in the `Toast` component.
       *
       * @defaultValue `"toast-" + useId()`
       */
      id?: string;

      /**
       * Note: This is set while creating the toast.
       *
       * @defaultValue `visibleTime === null ? "alert" : "status"`
       */
      role?: AriaRole;

      /**
       * @defaultValue `"surface"`
       */
      theme?: BackgroundColor;

      /**
       * Set this to `true` to stack the content above the {@link action}. It is not
       * recommended to enable this prop if the {@link closeButton} is enabled.
       *
       * @defaultValue `false`
       */
      stacked?: boolean;

      /**
       * If this is not provided, a `ResizeObserver` will be used to determine if
       * there are multiple lines of content.
       */
      multiline?: boolean;

      /**
       * The tooltips position within the viewport.
       *
       * @defaultValue `"bottom"`
       */
      position?: ToastPosition;

      /**
       * When this is a string or React element, it will be rendered as the
       * `children` within a `Button`
       */
      action?: ButtonProps | ReactElement | string;

      /**
       * This can be used to replace the custom action button behavior.
       */
      actionButton?: ReactNode;

      /**
       * @defaultValue `getIcon("close")`
       */
      closeIcon?: ReactNode;

      /**
       * Set this to `true` if a close button should be rendered to the right of the
       * `children`.
       *
       * @defaultValue `!!closeButtonProps`
       */
      closeButton?: boolean;

      /**
       * Use this prop to override most of the close button behavior. The
       */
      closeButtonProps?: ButtonProps;

      /**
       * Any additional props that should be provided to the `<div>` that surrounds
       * the toast `children`.
       */
      contentProps?: PropsWithRef<ToastContentProps, HTMLDivElement>;

      /**
       * Set this to `true` if the `children` for the toast should no longer be
       * wrapped in an additional `<div>` that applies some toast layout styles.
       * This should normally only be used for custom `Toast` implementations.
       *
       * @see the `Snackbar`'s `renderToast` prop for an example.
       */
      disableToastContent?: boolean;

      /**
       * The toast's transition timeout for entering and exiting. This is **not**
       * how long the toast should remain visible.
       *
       * @defaultValue `SCALE_TIMEOUT`
       */
      timeout?: TransitionTimeout;

      /**
       * The toast's transition class names for entering and exiting.
       *
       * @defaultValue `SCALE_CLASSNAMES`
       */
      classNames?: CSSTransitionClassNames;
    }

    /**
     * @remarks \@since 6.0.0
     */
    export interface ToastProps extends ConfigurableToastProps {
      paused?: boolean;
      visible: boolean;
    }

    /**
     * **Client Component**
     *
     * This component is just used for toast styling and does not implement any of
     * the visibility behavior.
     *
     * @remarks \@since 6.0.0
     */
    export const Toast = forwardRef<HTMLDivElement, ToastProps>(
      function Toast(props, ref) {
        const {
          id: propId,
          className,
          timeout,
          classNames,
          theme = "surface",
          action: propAction,
          actionButton: propActionButton,
          position,
          paused,
          visible,
          closeIcon: propCloseIcon,
          closeButtonProps,
          closeButton = !!closeButtonProps,
          contentProps,
          disableToastContent,
          stacked,
          multiline,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
          onExited,
          children,
          ...remaining
        } = props;
        const id = useEnsuredId(propId, "toast");

        let actionButton = propActionButton;
        if (propAction) {
          let overrides: ButtonProps = {};
          let buttonChildren: ReactNode;
          // have to use `any` to correctly filter out all react elements
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          if (isValidElement<any>(propAction) || typeof propAction !== "object") {
            buttonChildren = propAction;
          } else {
            ({ children: buttonChildren, ...overrides } = propAction);
          }

          actionButton = (
            <ToastActionButton
              theme={theme === "surface" ? "secondary" : "clear"}
              reordered={stacked && closeButton}
              {...overrides}
            >
              {buttonChildren}
            </ToastActionButton>
          );
        }

        let closeIcon = propCloseIcon;
        if (typeof closeButtonProps?.children !== "undefined") {
          closeIcon = closeButtonProps.children;
        }

        const action = !!actionButton;
        const reordered = stacked && action && closeButton;
        const { elementProps, rendered } = useScaleTransition({
          appear: true,
          nodeRef: ref,
          className: toast({
            className,
            theme,
            action,
            paused,
            stacked,
            position,
            reordered,
            closeButton,
          }),
          timeout,
          classNames,
          onEnter,
          onEntering,
          onEntered,
          onExit,
          onExiting,
          onExited,
          temporary: true,
          transitionIn: visible,
          exitedHidden: true,
        });

        // this might get rid of the weird popping-back-in for a split second
        // that sometimes happens on mobile firefox
        if (!rendered) {
          return null;
        }

        return (
          <div {...remaining} {...elementProps} id={id}>
            <ToastContent
              action={action}
              stacked={stacked}
              multiline={multiline}
              closeButton={closeButton}
              disableWrapper={disableToastContent}
              {...contentProps}
            >
              {children}
            </ToastContent>
            {actionButton}
            {closeButton && (
              <ToastCloseButton reordered={reordered} {...closeButtonProps}>
                {closeIcon}
              </ToastCloseButton>
            )}
          </div>
        );
      }
    );