Skip to main content

Common Examples

Toast methods

There are seven toast methods available in Anvil2. Four of them are used to create standard toasts with varying styles:
  • toast.info()
  • toast.success()
  • toast.warning()
  • toast.danger()
The fifth, toast.promise(), generates a pending toast, and uses a completed promise to update the toast based on a response.The other two are used to manage existing toast messages:
  • toast.update()
  • toast.dismiss()
import { Button, toast } from "@servicetitan/anvil2";

function ExampleComponent() {
  return (
    <Button
      onClick={() =>
        toast.info({
          title: "Toast title",
          message: "Lorem ipsum dolor sit amet",
        })
      }
    >
      Click me!
    </Button>
  );
}

Toasters

To use the toast methods, there must be a Toaster component rendered somewhere up the tree, usually at the root of the application. The toaster is the container that toast messages appear in, and manages positioning, stacking, and dismiss timing updates.
import { AnvilProvider, Toaster } from "@servicetitan/anvil2";

function App({ children }) {
  return (
    <>
      <Toaster id="app-toaster" />
      <AnvilProvider>
        {children}
      </AnvilProvider>
    </>
  )
}

Multiple toasters

In order to avoid double-rendering toast messages, the Toaster component will only render toast messages if it doesn’t detect another Toaster. This means if you are using a Toaster in a standalone MFE, it will display toasts, but will defer to the host’s Toaster when it is loaded in the host application.Note: A limitation exists where the Toaster cannot detect a Toaster in a sibling MFE. For this reason, there must be a Toaster in the host app to which MFEs will defer.

Toaster config

To ensure consistency, the Toaster component should handle most of the config options for toast messages within an app. The following props can be added:
type ToasterProps = {
  // defaults
  id: string;
  focusKey?: string; // "t"
  toastsBeforeStack?: number; // 3
};

Showing basic toasts

Basic toast method examples

Toast method config object

All of the basic toast methods accept a config object with the following shape:
type toastProps = {
  title: string;
  message?: string;
  actions?: { primary: ToastAction; secondary?: ToastAction };
  containerClassName?: string;
  duration?: number | false;
  onClose?: MouseEventHandler<HTMLButtonElement>];
  onDismiss?: () => void;
  toastClassName?: string;
}

Notes for basic toast methods

  • Toasts with actions will ignore the duration setting to allow users a chance to perform an action.
  • The toast config passed to a toast method will override some of the defaults set on the Toaster component.

Using promise toasts

Promise toast example

Promise toast method config object

The config object passed to toast.promise() should have three properties – loading, success, and error.
type PromiseToastMethodConfig = {
  loading: toastProps; // same as basic config object
  success: (message: string) => toastProps;
  error: (message: string) => toastProps;
};

Notes for promise toasts

  • The first parameter of the toast.promise() method should be a Promise that either resolves or rejects. Otherwise, the loading state will render indefinitely.

Updating toasts

Notes for updating toasts

  • An id for the toast message to be updated is required as the first parameter of the toast.update() method. This id is provided as the return value of any of the toast creation methods.
  • Any of the toastProps can be updated using this method.

Dismissing toasts

When toasts are dismissed

Toasts can be dismissed in four ways:
  1. By pressing the “x” button on the toast message.
  2. By swiping the toast message off the screen on touch screens.
  3. Automatically after the duration expires, if supplied on the Toaster or in the toast method.
  4. Programmatically using the toast.dismiss() method.

Notes for dismissing toasts

  • The toast.dismiss() method accepts an optional id parameter to dismiss a specific toast message. This id is given as the return value of any of the toast creation methods.
  • If no id is provided, all visible toast messages will be dismissed.
  • If a duration is provided, and the toast becomes stacked (hidden), the timeout is paused and restarted once the toast becomes visible again.

Callbacks for toast dismissal

There are two callback parameters available on the toast methods:
  • onClose is passed to the “x” button’s onClick, so it is only run when the user activates the button
  • onDismiss is run anytime the toast is removed, including on “x” button click, when toast.dismiss() is run, or when the duration expires on the toast

Using toast actions

The actions property of the toastConfig that is passed to toast creation methods can be used to add one or two Button components to the toast message. The actions property accepts an object with two properties:
type ActionsParameter = { primary: ToastAction; secondary?: ToastAction };
The ToastAction type is used to update some Button props of the action button.
type ToastAction = Pick<ButtonProps, "appearance" | "onClick"> & {
  label: string;
};

Toast event handling

Whenever a toast is added, updated, or dismissed, the Toaster component runs the onToastEvent callback with the following object passed in the event:
type ToastEventType = {
  eventType: "add" | "update" | "dismiss" | "dismiss-all";
  toastCount: number;
  id?: string;
  toasterId?: string;
  toastProps?: ToastProps | ToastUpdateProps;
};
Some of the parameters will only show when certain toast methods are called. For example, toast.dismiss() will not include toastProps, since it affects all of the existing toasts.

MFE Toaster note for toast event handling

The Toaster used in the monolith/host app will take precedence over any loaded by an MFE, so the onToastEvent will generally not work for MFEs that are run in a host app that includes its own Toaster, such as monolith.

React Accessibility

  • When actions are used, do not add a duration so that users have time to decide if they would like to take action.
  • By default, pressing the “t” key on the keyboard while a toast message is visible will shift focus to the toast. This is announced by screen readers to make the messages easily accessible for some users. If the “t” key is used for another hot key, this can be overridden using the focusKey prop of the Toaster.
For further guidance on creating accessible status updates, see accessible status messages and notifications best practices.
Last modified on January 23, 2026