Config in EJ2 TypeScript Toast control

10 May 202314 minutes to read

This section explains the steps required to customize the appearance of the toast using built-in APIs.

Title and content template

Toast can be created with the notification message. The message contains title and content of the toasts. The title and contents are adaptable in any resolution.

The Title or Content property can be given as HTML element/element ID to a string that can be displayed as a toast.

import {Toast} from '@syncfusion/ej2-notifications';
import { enableRipple } from '@syncfusion/ej2-base';

enableRipple(true);
let toast: Toast = new Toast({
    title: 'Matt sent you a friend request',
    content: 'You have a new friend request yet to accept',
    position: { X: "Center" }
});
toast.appendTo('#element');
toast.show();

document.getElementById('show_toast').onclick = () => {
  toast.show();
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
        <div class='row'> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
        <div id='element'></div>
        <br/><br/>
        <div id='result'></div>
        <div id='templateToast' style="display: none;color:red"> System affected by virus !!! </div>
    </div>
</body>

</html>

Specifying custom target

By default, the toast can be rendered in the document body. You can change the target position for toast rendering using the target property. Based on the target, the position will be updated.

Close button

By default, the showCloseButton is not enabled. You can enable it by setting the true value. Before expiring the toast, you can use this button to close or destroy toasts manually.

Progress bar

By default, the showProgressBar is not enabled. If it is enabled, it can visually indicate when will the toast gets expired. Based on the timeOut property, progress bar will appear.

Progress bar direction

By default, the progressDirection is set to “Rtl” and it will appear from right to left direction. You can change the progressDirection to “Ltr” to make it appear from left to right direction.

Newest on top

By default, the newly created toasts will append next with existing toasts. You can change the sequence like inserting before the toast by enabling the newestOnTop.

Here, The following sample demonstrates the combination of the target, showCloseButton, showProgressBar, and newestOnTop properties in toast.

import {Toast} from '@syncfusion/ej2-notifications';
import { enableRipple } from '@syncfusion/ej2-base';

enableRipple(true);
let toast: Toast = new Toast({
    title: 'File Downloading',
    content: '<div class="progress"><span style="width: 80%"></span></div>',
    showCloseButton: true,
    target: '#toast_target',
    position: { X: "Center" }
    newestOnTop: true,
    showProgressBar: true,
    progressDirection: "Ltr"
});
toast.appendTo('#element');
toast.show();

document.getElementById('show_toast').onclick = () => {
  toast.show();
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="index.css" rel="stylesheet" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/material.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
	<style>
	  #toast_target {
	    height: 200px;
	  }
	</style>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
        <div class='row'> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
        <div id='element'></div>
        <br/><br/>
        <div id='toast_target'></div>
    </div>
</body>

</html>

Width and height

The dimensions of the toast can be set using the width and height properties. This will individually set all toasts. You can create different custom dimension toasts.

By default, the toast can be rendered with 300px width with auto height.

In mobile devices, the default width of the toast gets ‘100%’ width of the page.
When you set toast width as ‘100%’, the toast occupies full width and will be displayed at the top or bottom based on the position Y property.

Both the width and height properties allow setting pixels/numbers/percentage. The number value is considered as pixels.

import {Toast} from '@syncfusion/ej2-notifications';
import { enableRipple } from '@syncfusion/ej2-base';
import { RadioButton, ChangeEventArgs as CheckBoxChange, CheckBox, ChangeEventArgs} from '@syncfusion/ej2-buttons';

enableRipple(true);
let toast: Toast = new Toast({
    title: 'Matt sent you a friend request',
    content: 'You have a new friend request yet to accept',
    position: { X: "Center", Y: "Bottom" },
    width: 400,
    height: 150,
});
toast.appendTo('#element');
toast.show();

let timeDelay = 500;
let radioButton: RadioButton = new RadioButton({ label: 'Top', name: 'toast', value: 'Target', change: checkboxChange });
radioButton.appendTo('#topAlign');

let radioButton2 = new RadioButton({ label: 'Bottom', name: 'toast', value: 'Global', checked: true, change: checkboxChange1 });
radioButton2.appendTo('#bottomAlign');

let checkBoxObj: CheckBox = new CheckBox({ label: '100% Width', change: onChange });
checkBoxObj.appendTo('#fullWidth');

function onChange(e: ChangeEventArgs): void {
  if (e.checked) {
    toast.hide();
    toast.width = "100%";
    toast.title="";
    toast.content="<div class='e-custom'>Take a look at our next generation <b>Javascript</b> <a href='https://ej2.syncfusion.com/home/index.html' target='_blank'>LEARN MORE</a></div>";
    toastShow(timeDelay);
  } else {
    toast.hide();
    toast.width= 300;
    toast.title= 'Matt sent you a friend request',
    toast.content='You have a new friend request yet to accept',
    toastShow(timeDelay);
  }
}

function checkboxChange(e: CheckBoxChange): void {
    if (e.event.target.checked) {
      toast.position.Y = "Top";
      toast.hide();
      toastShow(timeDelay);
    }
}

function checkboxChange1(e: CheckBoxChange): void {
    if (e.event.target.checked) {
      toast.position.Y = "Bottom";
      toast.hide();
      toastShow(timeDelay);
    }
}

function toastShow(timeOutDelay: number): void {
  setTimeout(
    () => {
        toast.show();
    }, timeOutDelay);
}

document.getElementById('show_toast').onclick = () => {
  toast.show();
}
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta name="description" content="Typescript Toolbar Controls" />
    <meta name="author" content="Syncfusion" />
    <link href="https://cdn.syncfusion.com/ej2/27.2.2/material.css" rel="stylesheet" />
    <link href="index.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
    <script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>

<body>
    <div id='loader'>LOADING....</div>
    <div id='container'>
        <div id='element'></div>
            <div class='row'> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
            <div class='row' style="padding-top: 20px" id= "toast_pos_target">
              <table>
                <tr>
                  <td>
                      <div style='padding:25px 0 0 0;'>
                          <input id="topAlign" type="radio">
                      </div>
                  </td>
                  <td>
                      <div style='padding:25px 0 0 0;'>
                          <input id="bottomAlign" type="radio">
                      </div>
                  </td>
                </tr>
                <tr>
                   <td>
                     <div style='padding:25px 0 0 0;'>
                          <input id="fullWidth" type="checkbox">
                      </div>
                   </td>
                </tr>
              </table>
            </div>
        <br/><br/>
        <div id='result'></div>
    </div>
</body>

</html>

See Also