# All files / snackbar useCurrentToastActions.ts

100% Statements 47/47
100% Branches 3/3
100% Functions 1/1
100% Lines 47/47

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
    481x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    29x
    29x
    8x
    8x
    8x
    8x
    21x
    21x
    21x
     "use client";
    import { createContext, useContext } from "react";

    /**
     * A simplified version of the {@link ToastManager} that has the actions bound
     * to the current {@link QueuedToast.toastId}.
     *
     * @remarks \@since 6.0.0
     */
    export interface CurrentToastActions {
      /** @see {@link ToastManager.clearTimer} */
      clearTimer(): void;
      /** @see {@link ToastManager.removeToast} */
      removeToast(transition: boolean): void;
      /** @see {@link ToastManager.startRemoveTimeout} */
      startRemoveTimeout(): void;
      /** @see {@link ToastManager.pauseRemoveTimeout} */
      pauseRemoveTimeout(): void;
      /** @see {@link ToastManager.resumeRemoveTimeout} */
      resumeRemoveTimeout(): void;
    }

    const context = createContext<CurrentToastActions | null>(null);
    context.displayName = "CurrentToastActions";

    /**
     * @remarks \@since 6.0.0
     * @internal
     */
    export const { Provider: CurrentToastActionsProvider } = context;

    /**
     * This is only required if you have multiple `Snackbar` implementations in your
     * app.
     *
     * @remarks \@since 6.0.0
     */
    export function useCurrentToastActions(): CurrentToastActions {
      const actions = useContext(context);
      if (!actions) {
        throw new Error(
          "The `CurrentToastActionsProvider` has not been initialized"
        );
      }

      return actions;
    }