Config in React Toast component

29 Aug 202324 minutes to read

This section explains on customizing the Toast appearance 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. Title and contents are adaptable in any resolution.

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

[Class-component]

import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
class App extends React.Component {
    toastInstance;
    position = { X: 'Center' };
    toastCreated() {
        this.toastInstance.show();
    }
    toastShow() {
        this.toastInstance.show();
    }
    render() {
        return (<div>
        <ButtonComponent cssClass="e-primary" onClick={this.toastShow = this.toastShow.bind(this)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => this.toastInstance = toast} title="Matt sent you a friend request" content="Hey, wanna dress up as wizards and ride our hoverboards?" position={this.position} created={this.toastCreated = this.toastCreated.bind(this)}/>
      </div>);
    }
}
export default App;
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

class App extends React.Component<{}, {}> {
  public toastInstance: ToastComponent;
  public position = { X: 'Center' };

  public toastCreated(): void {
    this.toastInstance.show();
  }

  public toastShow() {
    this.toastInstance.show();
  }

  public render() {
    return (
      <div>
        <ButtonComponent cssClass="e-primary" onClick={this.toastShow = this.toastShow.bind(this)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => this.toastInstance = toast!} title="Matt sent you a friend request" content="Hey, wanna dress up as wizards and ride our hoverboards?" position={this.position} created={this.toastCreated = this.toastCreated.bind(this)} />
      </div>
    );
  }
}
export default App;

[Functional-component]

import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
function App() {
    let toastInstance;
    let position = { X: 'Center' };
    function toastCreated() {
        toastInstance.show();
    }
    function toastShow() {
        toastInstance.show();
    }
    return (<div>
        <ButtonComponent cssClass="e-primary" onClick={toastShow.bind(this)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => toastInstance = toast} title="Matt sent you a friend request" content="Hey, wanna dress up as wizards and ride our hoverboards?" position={position} created={toastCreated.bind(this)}/>
      </div>);
}
export default App;
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App(){
    let toastInstance: ToastComponent;
    let position = { X: 'Center' };
  
    function toastCreated(): void {
      toastInstance.show();
    }
  
    function toastShow() {
      toastInstance.show();
    }
  
    return (
      <div>
        <ButtonComponent cssClass="e-primary" onClick={toastShow.bind(this)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => toastInstance = toast!} title="Matt sent you a friend request" content="Hey, wanna dress up as wizards and ride our hoverboards?" position={position} created={toastCreated.bind(this)} />
      </div>
    );

  }
export default App;

Specifying custom target

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

Close Button

By default showCloseButton will not enabled. We can enable it by setting true value. Before expiring toast we can use to close or destroy toasts manually.

Progress bar

By default showProgressBar will not enabled. If we enabled it can visually indicate how long time to get toast expires. 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

In default, newly created toasts will append next with existing toast. We can change the Sequence like inserting before the toast, by enabling the newestOnTop.

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

[Class-component]

import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
class App extends React.Component {
    toastInstance;
    position = { X: 'Center' };
    toastCreated() {
        this.toastShow();
    }
    toastShow() {
        setTimeout(() => {
            this.toastInstance.show();
        }, 600);
    }
    render() {
        return (<div>
        <ButtonComponent cssClass="e-primary" onClick={this.toastShow = this.toastShow.bind(this)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => this.toastInstance = toast} title="File Downloading" content="<div class='progress'><span style='width: 80%'></span></div>" position={this.position} showCloseButton='true' target="#toast_target" newestOnTop='true' showProgressBar='true' progressDirection='Ltr' created={this.toastCreated = this.toastCreated.bind(this)}/>
      </div>);
    }
}
export default App;
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

class App extends React.Component<{}, {}> {
  public toastInstance: ToastComponent;
  public position = { X: 'Center' };

  public toastCreated(): void {
    this.toastShow();
  }

  public toastShow() {
    setTimeout(
      () => {
        this.toastInstance.show();
      }, 600);
  }

  public render() {
    return (
      <div>
        <ButtonComponent cssClass="e-primary" onClick={this.toastShow = this.toastShow.bind(this)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => this.toastInstance = toast!} title="File Downloading" content="<div class='progress'><span style='width: 80%'></span></div>" position={this.position} showCloseButton='true' target="#toast_target" newestOnTop='true' showProgressBar='true' progressDirection='Ltr' created={this.toastCreated = this.toastCreated.bind(this)} />
      </div>
    );
  }
}
export default App;

[Functional-component]

import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
function App() {
    let toastInstance;
    let position = { X: 'Center' };
    function toastCreated() {
        toastShow();
    }
    function toastShow() {
        setTimeout(() => {
            toastInstance.show();
        }, 600);
    }
    return (<div>
            <ButtonComponent cssClass="e-primary" onClick={toastShow.bind(this)}> Show Toast </ButtonComponent>
            <ToastComponent ref={toast => toastInstance = toast} title="File Downloading" content="<div class='progress'><span style='width: 80%'></span></div>" position={position} showCloseButton={true} target="#toast_target" newestOnTop={true} showProgressBar={true} progressDirection='Ltr' created={toastCreated.bind(this)}/>
        </div>);
}
export default App;
import { ButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
    let toastInstance: ToastComponent;
    let position = { X: 'Center' };
  
    function toastCreated(): void {
      toastShow();
    }
  
    function toastShow() {
      setTimeout(
        () => {
          toastInstance.show();
        }, 600);
    }
  
    return (
        <div>
            <ButtonComponent cssClass="e-primary" onClick={toastShow.bind(this)}> Show Toast </ButtonComponent>
            <ToastComponent ref={toast => toastInstance = toast!} title="File Downloading" content="<div class='progress'><span style='width: 80%'></span></div>" position={position} showCloseButton={true} target="#toast_target" newestOnTop={true} showProgressBar={true} progressDirection='Ltr' created={toastCreated.bind(this)} />
        </div>
    );
}
export default App;

Width and height

we can set toast dimensions through width and height property. The height and width can be applied for specific set of toasts. So you can create different toasts with custom dimension.

In default toast can be rendered with ‘300px’ width with ‘auto’ height

In mobile device toast default width gets ‘100%’ width of the page.
When we sets toast width as ‘100%’ toast will occupies full width and displayed top or bottom based on position Y property.

Both width and height property allows setting pixels/numbers/percentage. The number value is considered as pixels.

[Class-component]

import { ButtonComponent, CheckBoxComponent, RadioButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
class App extends React.Component {
    toastInstance;
    checkboxObj;
    constructor(props) {
        super(props);
        this.state = {
            content: 'Hey, wanna dress up as wizards and ride our hoverboards?',
            position: { X: 'Center', Y: "Bottom" },
            title: 'Matt sent you a friend request',
            width: 300
        };
    }
    toastCreated() {
        this.toastShow(600);
    }
    toastShow(timeOutDelay) {
        setTimeout(() => {
            this.toastInstance.show();
        }, timeOutDelay);
    }
    checkboxChange(e) {
        if (e.event.target.checked) {
            this.setState({ position: { Y: "Top" } });
            this.toastInstance.hide('All');
            this.toastShow(700);
        }
    }
    checkboxChange1(e) {
        if (e.event.target.checked) {
            this.setState({ position: { Y: "Bottom" } });
            this.toastInstance.hide('All');
            this.toastShow(700);
        }
    }
    onChange(e) {
        if (e.checked) {
            this.toastInstance.hide('All');
            this.setState({
                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>",
                width: '100%',
                title: ''
            });
            this.toastShow(500);
        }
        else {
            this.toastInstance.hide('All');
            this.setState({
                content: 'Hey, wanna dress up as wizards and ride our hoverboards?',
                width: 300,
                title: 'Matt sent you a friend request'
            });
            this.toastShow(500);
        }
    }
    render() {
        return (<div id="container">
        <div id="toast_full_width" className='row'>
          <table>
            <tbody>
            <tr>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Top' name="toast" change={this.checkboxChange = this.checkboxChange.bind(this)}/>
                </div>
              </td>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Bottom' name="toast" change={this.checkboxChange1 = this.checkboxChange1.bind(this)}/>
                </div>
              </td>
            </tr>
            <tr>
              <td>
                <div>
                  <CheckBoxComponent label='100% Width' ref={scope => this.checkboxObj = scope} change={this.onChange = this.onChange.bind(this)}/>
                </div>
              </td>
            </tr>
            </tbody>
          </table>
        </div>
        <ButtonComponent cssClass="e-primary" onClick={this.toastShow = this.toastShow.bind(this, 500)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => this.toastInstance = toast} width={this.state.width} title={this.state.title} content={this.state.content} position={this.state.position} created={this.toastCreated = this.toastCreated.bind(this)}/>
      </div>);
    }
}
export default App;
import { ButtonComponent, ChangeEventArgs, CheckBoxComponent, RadioButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

class App extends React.Component<{}, {}> {
  public toastInstance: ToastComponent;
  public checkboxObj: CheckBoxComponent;

  constructor(props: any) {
    super(props);
    this.state = {
      content: 'Hey, wanna dress up as wizards and ride our hoverboards?',
      position: { X: 'Center', Y: "Bottom" },
      title: 'Matt sent you a friend request',
      width: 300
    }
  }

  public toastCreated(): void {
    this.toastShow(600);
  }

  public toastShow(timeOutDelay: number) {
    setTimeout(
      () => {
        this.toastInstance.show();
      }, timeOutDelay);
  }

  public checkboxChange(e: any): void {
    if (e.event.target.checked) {
      this.setState({ position: { Y: "Top" } });
      this.toastInstance.hide('All');
      this.toastShow(700);
    }
  }

  public checkboxChange1(e: any): void {
    if (e.event.target.checked) {
      this.setState({ position: { Y: "Bottom" } });
      this.toastInstance.hide('All');
      this.toastShow(700);
    }
  }

  public onChange(e: ChangeEventArgs): void {
    if (e.checked) {
      this.toastInstance.hide('All');
      this.setState({
        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>",
        width: '100%',
        title: ''
      });
      this.toastShow(500);
    } else {
      this.toastInstance.hide('All');
      this.setState({
        content: 'Hey, wanna dress up as wizards and ride our hoverboards?',
        width: 300,
        title: 'Matt sent you a friend request'
      });
      this.toastShow(500);
    }
  }

  public render() {
    return (
      <div id="container">
        <div id="toast_full_width" className='row'>
          <table>
            <tr>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Top' name="toast" change={this.checkboxChange = this.checkboxChange.bind(this)} />
                </div>
              </td>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Bottom' name="toast" change={this.checkboxChange1 = this.checkboxChange1.bind(this)} />
                </div>
              </td>
            </tr>
            <tr>
              <td>
                <div>
                  <CheckBoxComponent label='100% Width' ref={scope => this.checkboxObj = scope!} change={this.onChange = this.onChange.bind(this)} />
                </div>
              </td>
            </tr>
          </table>
        </div>
        <ButtonComponent cssClass="e-primary" onClick={this.toastShow = this.toastShow.bind(this, 500)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => this.toastInstance = toast!} width={this.state.width} title={this.state.title} content={this.state.content} position={this.state.position} created={this.toastCreated = this.toastCreated.bind(this)} />
      </div>
    );
  }
}
export default App;

[Functional-component]

import { ButtonComponent, CheckBoxComponent, RadioButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
function App() {
    let toastInstance;
    let checkboxObj;
    const [status, setStatus] = React.useState({ content: 'Hey, wanna dress up as wizards and ride our hoverboards?', title: 'Matt sent you a friend request', width: '600', position: { X: 'Center', Y: "Bottom" } });
    function toastCreated() {
        toastShow(600);
    }
    function toastShow(timeOutDelay) {
        setTimeout(() => {
            toastInstance.show();
        }, timeOutDelay);
    }
    function checkboxChange(e) {
        if (e.event.target.checked) {
            setStatus({
                content: '',
                width: '',
                title: '',
                position: { Y: "Top", X: '' }
            });
            toastInstance.hide('All');
            toastShow(700);
        }
    }
    function checkboxChange1(e) {
        if (e.event.target.checked) {
            setStatus({
                content: '',
                width: '',
                title: '',
                position: { Y: "Bottom", X: '' }
            });
            toastInstance.hide('All');
            toastShow(700);
        }
    }
    function onChange(e) {
        if (e.checked) {
            toastInstance.hide('All');
            setStatus({
                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>",
                width: '100%',
                title: '',
                position: { X: '', Y: '' }
            });
            toastShow(500);
        }
        else {
            toastInstance.hide('All');
            setStatus({
                content: 'Hey, wanna dress up as wizards and ride our hoverboards?',
                width: '300',
                title: 'Matt sent you a friend request',
                position: { X: '', Y: '' }
            });
            toastShow(500);
        }
    }
    return (<div id="container">
        <div id="toast_full_width" className='row'>
          <table>
            <tr>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Top' name="toast" change={checkboxChange.bind(this)}/>
                </div>
              </td>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Bottom' name="toast" change={checkboxChange1.bind(this)}/>
                </div>
              </td>
            </tr>
            <tr>
              <td>
                <div>
                  <CheckBoxComponent label='100% Width' ref={scope => checkboxObj = scope} change={onChange.bind(this)}/>
                </div>
              </td>
            </tr>
          </table>
        </div>
        <ButtonComponent cssClass="e-primary" onClick={toastShow.bind(this, 500)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => toastInstance = toast} width={status.width} title={status.title} content={status.content} position={status.position} created={toastCreated.bind(this)}/>
      </div>);
}
export default App;
import { ButtonComponent, ChangeEventArgs, CheckBoxComponent, RadioButtonComponent } from '@syncfusion/ej2-react-buttons';
import { ToastComponent } from '@syncfusion/ej2-react-notifications';
import * as React from 'react';
import * as ReactDOM from 'react-dom';

function App() {
  let toastInstance: ToastComponent;
  let checkboxObj: CheckBoxComponent;
  const [status, setStatus] = React.useState({ content: 'Hey, wanna dress up as wizards and ride our hoverboards?' , title: 'Matt sent you a friend request' ,width:'600' , position: { X: 'Center', Y: "Bottom" } });

  function toastCreated(): void {
    toastShow(600);
  }

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

  function checkboxChange(e: any): void {
    if (e.event.target.checked) {
      setStatus({
          content: '',
          width: '',
          title: '',
          position: { Y: "Top", X:''}
       });
      toastInstance.hide('All');
      toastShow(700);
    }
  }

  function checkboxChange1(e: any): void {
    if (e.event.target.checked) {
      setStatus({
        content: '',
          width: '',
          title: '',
          position: { Y: "Bottom", X:''}
       });
      toastInstance.hide('All');
      toastShow(700);
    }
  }

  function onChange(e: ChangeEventArgs): void {
    if (e.checked) {
      toastInstance.hide('All');
      setStatus({
        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>",
        width: '100%',
        title: '',
        position: {X:'',Y:''}
      });
      toastShow(500);
    } else {
      toastInstance.hide('All');
      setStatus({
        content: 'Hey, wanna dress up as wizards and ride our hoverboards?',
        width: '300',
        title: 'Matt sent you a friend request',
        position: {X:'',Y:''}
      });
      toastShow(500);
    }
  }

    return (
      <div id="container">
        <div id="toast_full_width" className='row'>
          <table>
            <tr>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Top' name="toast" change={ checkboxChange.bind(this)} />
                </div>
              </td>
              <td>
                <div>
                  <RadioButtonComponent checked={true} label='Bottom' name="toast" change={checkboxChange1.bind(this)} />
                </div>
              </td>
            </tr>
            <tr>
              <td>
                <div>
                  <CheckBoxComponent label='100% Width' ref={scope => checkboxObj = scope!} change={onChange.bind(this)} />
                </div>
              </td>
            </tr>
          </table>
        </div>
        <ButtonComponent cssClass="e-primary" onClick={toastShow.bind(this, 500)}> Show Toast </ButtonComponent>
        <ToastComponent ref={toast => toastInstance = toast!} width={status.width} title={status.title} content={status.content} position={status.position} created={ toastCreated.bind(this)} />
      </div>
    );
}
export default App;

See Also

Prevent duplicate toasts
Customize the progress bar