# All files / src/snackbar Snackbar.tsx

100% Statements 181/181
100% Branches 1/1
100% Functions 1/1
100% Lines 181/181

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
    1821x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    124x
    1x
     "use client";
    import { forwardRef, type ComponentType, type HTMLAttributes } from "react";
    import { Portal } from "../portal/Portal.js";
    import { useEnsuredId } from "../useEnsuredId.js";
    import {
      DefaultToastRenderer,
      type ToastRendererProps,
    } from "./DefaultToastRenderer.js";
    import { type ConfigurableToastProps } from "./Toast.js";
    import { useToastQueue } from "./ToastManagerProvider.js";
    import { snackbar, type SnackbarPosition } from "./snackbarStyles.js";

    /**
     * @remarks \@since 6.0.0 The `id` prop is optional
     */
    export interface SnackbarProps extends HTMLAttributes<HTMLDivElement> {
      /**
       * @defaultValue `"snackbar-" + useId()`
       */
      id?: string;

      /**
       * Set this to the number of toasts that can be visible within the snackbar at
       * the same time. Any toasts added after this value will be added into the
       * queue to be shown.
       *
       * @defaultValue `1`
       */
      limit?: number;

      /**
       * @defaultValue `"bottom"`
       */
      position?: SnackbarPosition;

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

      /**
       * This can be used to create a custom toast implementation.
       *
       * @example
       * ```ts
       * import type { ToastRendererProps } from "@react-md/core";
       * import {
       *   Snackbar,
       *   Toast,
       *   ToastContent,
       *   useToastManager,
       * } from "@react-md/core";
       * import type { ReactElement } from "react";
       *
       * function CustomToast(props: CustomToastRendererProps): ReactElement {
       *   // Pretend like we don't need anything else from the toast since the
       *   // custom behavior is related to the `toastId`
       *   const { toastId, visible, duplicates, visibleTime } = props;
       *
       *   const toastManager = useToastManager();
       *
       *   // Note: If you want to rely on the `action` and `closeButton` behavior,
       *   // you must also wrap the `Toast` with:
       *   // <RemoveToastProvider value={() => toastManager.removeToast(toastId, true)}>
       *
       *   return (
       *     <Toast
       *       theme={isError(toastId) ? "error" : "surface"}
       *       visible={visible}
       *       onEntered={() => {
       *         toastManager.startRemoveTimeout(toastId);
       *       }}
       *       onExited={() => {
       *         toastManager.removeToast(toastId, false)
       *       }}
       *       disableContentWrapper
       *     >
       *       <ToastContent>
       *         <TranslateMessage id={toastId} />
       *       </ToastContent>
       *       {isActionable(toastId) && (
       *         <Button
       *           onClick={async () => {
       *             await someApiCall();
       *             toastManager.removeToast(toastId, true);
       *           }}
       *         >
       *           Dismiss
       *         </Button>
       *       )}
       *     </Toast>
       *   );
       * }
       *
       * function Example(): ReactElement {
       *   return <Snackbar renderToast={CustomToast} />
       * }
       * ```
       *
       * @see {@link DefaultToastRenderer}
       * @defaultValue `DefaultToastRenderer`
       */
      renderToast?: ComponentType<ToastRendererProps>;

      /**
       * 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.
       *
       * @see {@link DefaultToastRenderer}
       */
      toastDefaults?: ConfigurableToastProps;
    }

    /**
     * **Client Component**
     *
     * @example
     * Super SImple Example
     * ```tsx
     * import { Button, Snackbar, addToast } from "@react-md/core";
     * import type { ReactElement } from "react";
     *
     * function Example(): ReactElement {
     *   return (
     *     <>
     *       <Button
     *         onClick={() => {
     *           addToast({ children: "A new toast!" });
     *         }}
     *       >
     *         Toast!
     *       </Button>
     *       <Snackbar />
     *     </>
     *   );
     * }
     * ```
     *
     * @see {@link SnackbarProps.renderToast} for creating a custom toast
     * implementation.
     * @remarks \@since 6.0.0 Rewritten to use a new API that supports adding toasts
     * outside of React components and rendering multiple toasts at once.
     */
    export const Snackbar = forwardRef<HTMLDivElement, SnackbarProps>(
      function Snackbar(props, ref) {
        const {
          id: propId,
          className,
          limit = 1,
          position = "bottom",
          renderToast: RenderToast = DefaultToastRenderer,
          disablePortal,
          toastDefaults,
          ...remaining
        } = props;
        const id = useEnsuredId(propId, "snackbar");
        const queue = useToastQueue(limit);

        return (
          <Portal disabled={disablePortal}>
            {queue.length > 0 && (
              <div
                {...remaining}
                id={id}
                ref={ref}
                className={snackbar({ position, className })}
              >
                {queue.map((toast) => (
                  <RenderToast
                    {...toast}
                    key={toast.toastId}
                    toastDefaults={toastDefaults}
                  />
                ))}
              </div>
            )}
          </Portal>
        );
      }
    );