# All files / snackbar DefaultToastRenderer.tsx

0% Statements 0/147
0% Branches 0/1
0% Functions 0/1
0% Lines 0/147

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

     "use client";
    import { useEffect, useMemo, type ReactElement } from "react";
    import { usePageInactive } from "../usePageInactive.js";
    import { Toast, type ConfigurableToastProps } from "./Toast.js";
    import { type ToastMeta } from "./ToastManager.js";
    import { useToastManager } from "./ToastManagerProvider.js";
    import {
      CurrentToastActionsProvider,
      type CurrentToastActions,
    } from "./useCurrentToastActions.js";

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

    /**
     * @remarks \@since 6.0.0
     */
    export interface ToastRendererProps extends ConfigurableToastProps, ToastMeta {
      /**
       * Optional props that should be passed to each `Toast` from the `Snackbar`.
       * This is a great way to enforce each toast having a close button, a custom
       * class name, theme, etc.
       */
      toastDefaults?: ConfigurableToastProps;
    }

    /**
     * **Client Component**
     *
     * This is the default implementation for rendering toasts that will:
     *
     * - controls the visibility of the toast with the {@link useToast} hook
     * - add a `closeButton` if the {@link ToastRendererProps.toastDefaults} has
     *   `closeButton === true` or the `closeButtonProps` exists.
     * - add a `closeButton` if the `visibleTime` is `null` and the current toast
     *   does not have an `action` or `actionButton`
     * - ensures both the toast's `onEntered` and the {@link ToastRendererProps.toastDefaults}'s
     *   `onEntered` are called
     * - ensures both the toast's `onExited` and the {@link ToastRendererProps.toastDefaults}'s
     *   `onExited` are called
     * - always renders the {@link ToastRendererProps.toastDefaults}'s `children`
     *   'after the optional toast's `children`
     *
     * @remarks \@since 6.0.0
     */
    export function DefaultToastRenderer(props: ToastRendererProps): ReactElement {
      const {
        toastId,
        paused,
        visible,
        priority: _priority,
        duplicates: _duplicates,
        visibleTime,
        onExited = noop,
        onEntered = noop,
        onMouseEnter = noop,
        onMouseLeave = noop,
        toastDefaults = {},
        ...remaining
      } = props;
      const {
        closeButtonProps,
        closeButton = !!closeButtonProps,
        onEntered: defaultEntered = noop,
        onExited: defaultExited = noop,
        onMouseEnter: defaultMouseEnter = noop,
        onMouseLeave: defaultMouseLeave = noop,
        ...defaults
      } = toastDefaults;

      const toastManager = useToastManager();
      const currentToastActions = useMemo<CurrentToastActions>(
        () => ({
          clearTimer() {
            toastManager.clearTimer(toastId);
          },
          removeToast(transition) {
            toastManager.removeToast(toastId, transition);
          },
          startRemoveTimeout() {
            toastManager.startRemoveTimeout(toastId);
          },
          pauseRemoveTimeout() {
            toastManager.pauseRemoveTimeout(toastId);
          },
          resumeRemoveTimeout() {
            toastManager.resumeRemoveTimeout(toastId);
          },
        }),
        [toastId, toastManager]
      );
      useEffect(() => {
        return () => {
          currentToastActions.clearTimer();
        };
      }, [currentToastActions]);
      usePageInactive({
        disabled: !visible,
        onChange(active) {
          if (active) {
            currentToastActions.resumeRemoveTimeout();
          } else {
            currentToastActions.pauseRemoveTimeout();
          }
        },
      });

      return (
        <CurrentToastActionsProvider value={currentToastActions}>
          <Toast
            closeButton={
              closeButton ||
              (!visibleTime && !remaining.action && !remaining.actionButton)
            }
            closeButtonProps={closeButtonProps}
            {...defaults}
            {...remaining}
            paused={paused}
            visible={visible}
            onEntered={(appearing) => {
              defaultEntered(appearing);
              onEntered(appearing);
              currentToastActions.startRemoveTimeout();
            }}
            onExited={() => {
              defaultExited();
              onExited();
              currentToastActions.removeToast(false);
            }}
            onMouseEnter={(event) => {
              defaultMouseEnter(event);
              onMouseEnter(event);
              currentToastActions.pauseRemoveTimeout();
            }}
            onMouseLeave={(event) => {
              defaultMouseLeave(event);
              onMouseLeave(event);
              currentToastActions.resumeRemoveTimeout();
            }}
          >
            {defaults.children}
            {remaining.children}
          </Toast>
        </CurrentToastActionsProvider>
      );
    }