# All files / src/snackbar useDefaultToastRenderer.tsx

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

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

     import type { ComponentProps } from "react";
    import { useCallback, useEffect } from "react";
    import { usePageInactive } from "../usePageInactive";
    import type { ToastRendererProps } from "./DefaultToastRenderer";
    import type { ToastProps } from "./Toast";
    import { useToastManager } from "./ToastManagerProvider";
    import type { HideToastProvider } from "./useToast";

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

    interface DefaultToastRendererResult {
      toastProps: ToastProps;
      providerProps: ComponentProps<typeof HideToastProvider>;
    }

    export function useDefaultToastRenderer(
      options: ToastRendererProps
    ): DefaultToastRendererResult {
      const {
        toastId,
        paused,
        visible,
        duplicates: _duplicates,
        visibleTime,
        onExited = noop,
        onEntered = noop,
        onMouseEnter = noop,
        onMouseLeave = noop,
        toastDefaults = {},
        ...remaining
      } = options;
      const {
        closeButtonProps,
        closeButton = !!closeButtonProps,
        onEntered: defaultEntered = noop,
        onExited: defaultExited = noop,
        onMouseEnter: defaultMouseEnter = noop,
        onMouseLeave: defaultMouseLeave = noop,
        ...defaults
      } = toastDefaults;

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

      return {
        providerProps: {
          value: useCallback(() => {
            toastManager.removeToast(toastId);
          }, [toastId, toastManager]),
        },
        toastProps: {
          closeButton:
            closeButton ||
            (!visibleTime && !remaining.action && !remaining.actionButton),
          closeButtonProps,
          ...defaults,
          paused,
          visible,
          onEntered: (appearing) => {
            defaultEntered(appearing);
            onEntered(appearing);
            toastManager.startRemoveTimeout(toastId);
          },
          onExited: () => {
            defaultExited();
            onExited();
            toastManager.removeToast(toastId);
          },
          onMouseEnter: (event) => {
            defaultMouseEnter(event);
            onMouseEnter(event);
            toastManager.pauseRemoveTimeout(toastId);
          },
          onMouseLeave: (event) => {
            defaultMouseLeave(event);
            onMouseLeave(event);
            toastManager.resumeRemoveTimeout(toastId);
          },
          children: (
            <>
              {defaults.children}
              {remaining.children}
            </>
          ),
        },
      };
    }