Toasts

Materialize is a modern responsive CSS framework based on Material Design by Google.

Materialize provides an easy way for you to send unobtrusive alerts to your users through toasts. These toasts are also placed and sized responsively, try it out by clicking the button below on different device sizes.

To do this, call the M.toast() function programmatically in JavaScript.


  M.toast({text: 'I am a toast!'})
        

One way to add this into your application is to add this as an onclick event to a button.


  <button type="button" onclick="M.toast({text: 'I am a toast'})" class="btn">Toast!</button>
        

Options

You can customize the behavior of each Toast using these options.

Name Type Default Description
text String '' The content of the Toast.
toastId String ''

Id of an HTML element that will be used as tootip content.

displayLength Number 4000 Length in ms the Toast stays before dismissal.
inDuration Number 300 Transition in duration in milliseconds.
outDuration Number 375 Transition out duration in milliseconds.
classes String '' Classes to be added to the toast element.
completeCallback Function null Callback function called when toast is dismissed.
activationPercent Number 0.8 The percentage of the toast's width it takes for a drag to dismiss a Toast.

Properties

Name Type Description
el Element The toast element.
options Object The options the instance was initialized with.
panning Boolean Describes the current pan state of the Toast.
timeRemaining Number The remaining amount of time in ms that the toast will stay before dismissal.

Custom HTML

You can pass in an toastId as the argument as well. This toastId should refer to some element in the HTML that will be used as toast content.


<button type="button" class="btn" onclick="M.toast({toastId: 'my-toast-1'})">Toast 2!</button>
<div id="my-toast-1" style="display: none;">
  This is toast nº1 with a <a href="https://github.com/materializecss">link</a>
</div>

Callback

You can have the toast callback a function when it has been dismissed.


  <button type="button" class="btn" onclick="M.toast({text: 'I am a toast', completeCallback: function(){alert('Your toast was dismissed')}})">Toast!</button>
        

Styling Toasts

We've added the ability to customize your toasts easily. You can pass in classes as an optional parameter into the toast function. We've added a rounded class for you, but you can create your own CSS classes and apply them to toasts. Checkout out our full example below.


  // 'rounded' is the class I'm applying to the toast
  M.toast({text: 'I am a toast!', classes: 'rounded'});
        

Dismiss a Toast Programmatically

To remove a specific toast using JavaScript, access the M_Toast toast HTML element and call the dismiss function


  // Get toast DOM Element, get instance, then call dismiss function
  var toastElement = document.querySelector('.toast');
  var toastInstance = M.Toast.getInstance(toastElement);
  toastInstance.dismiss();
        
Dismiss all Toasts

  M.Toast.dismissAll();