- Implementation
- Toaster Props
- Toast Methods
Common Examples
Toast methods
There are seventoast methods available in Anvil2. Four of them are used to create standard toasts with varying styles:toast.info()toast.success()toast.warning()toast.danger()
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()
Toasters
To use thetoast 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.Multiple toasters
In order to avoid double-rendering toast messages, theToaster 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, theToaster component should handle most of the config options for toast messages within an app. The following props can be added: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:Notes for basic toast methods
- Toasts with
actionswill 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
Toastercomponent.
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.Notes for promise toasts
- The first parameter of the
toast.promise()method should be aPromisethat either resolves or rejects. Otherwise, the loading state will render indefinitely.
Updating toasts
Notes for updating toasts
- An
idfor the toast message to be updated is required as the first parameter of thetoast.update()method. Thisidis provided as the return value of any of the toast creation methods. - Any of the
toastPropscan be updated using this method.
Dismissing toasts
When toasts are dismissed
Toasts can be dismissed in four ways:- By pressing the “x” button on the toast message.
- By swiping the toast message off the screen on touch screens.
- Automatically after the
durationexpires, if supplied on theToasteror in thetoastmethod. - Programmatically using the
toast.dismiss()method.
Notes for dismissing toasts
- The
toast.dismiss()method accepts an optionalidparameter to dismiss a specific toast message. Thisidis given as the return value of any of the toast creation methods. - If no
idis provided, all visible toast messages will be dismissed. - If a
durationis 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:onCloseis passed to the “x” button’sonClick, so it is only run when the user activates the buttononDismissis run anytime the toast is removed, including on “x” button click, whentoast.dismiss()is run, or when thedurationexpires on the toast
Using toast actions
Theactions 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:ToastAction type is used to update some Button props of the action button.Toast event handling
Whenever a toast is added, updated, or dismissed, theToaster component runs the onToastEvent callback with the following object passed in the event:toast.dismiss() will not include toastProps, since it affects all of the existing toasts.MFE Toaster note for toast event handling
TheToaster 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
actionsare used, do not add adurationso 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
focusKeyprop of theToaster.




