JavaScript/jQuery Toast - Show and Hide the Toast

API

NOTE
In this article, the Button UI component is used to demonstrate how to show and hide the Toast. This choice is made for purely demonstrational purposes, and you can do the same operations using another UI component following the same guidelines.

To show or hide the Toast programmatically, call the show() or hide() method. The same thing can be done using the toggle(showing) method. Pass true or false to this method to show or hide the Toast, respectively.

JavaScript
$(function() {
    $("#toastContainer").dxToast({
        message: "Connection problem",
        type: "error"
    });

    $("#buttonContainer").dxButton({
        text: "Show the Toast", 
        onClick: function () {
            $("#toastContainer").dxToast("show");
            // ---------- or ----------
            $("#toastContainer").dxToast("toggle", true);
        } 
    });
});

With Angular, Vue, or React, use a different technique. Bind the visible property of the Toast UI component to a component property. After that, change this property, and the Toast will appear or disappear.

Angular
HTML
TypeScript
<dx-toast
    [(visible)]="isVisible"
    type="error"
    message="Connection problem">
</dx-toast>
<dx-button
    text="Show the Toast"
    (onClick)="isVisible = true">
</dx-button>
import { DxToastModule, DxButtonModule } from "devextreme-angular";
// ...
export class AppComponent {
    isVisible: boolean = false;
}
@NgModule({
     imports: [
         DxButtonModule,
         DxToastModule,
         // ...
     ],
     // ...
 })
Vue
<template>
    <div>
        <DxToast
            :visible.sync="isVisible"
            message="Connection problem"
            type="error"
        />
        <DxButton
            text="Show the Toast"
            @click="onClick"
        />
    </div>
</template>

<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { DxToast } from 'devextreme-vue/toast';
import { DxButton } from 'devextreme-vue/button';

export default {
    components: {
        DxToast,
        DxButton
    },
    data() {
        return {
            isVisible: false
        };
    },
    methods: {
        onClick() {
            this.isVisible = true;
        }
    }
}
</script>
React
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { Toast } from 'devextreme-react/toast';
import { Button } from 'devextreme-react/button';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            isVisible: false
        };

        this.onClick = this.onClick.bind(this);
        this.onHiding = this.onHiding.bind(this);
    }

    onClick() {
        this.setState({
            isVisible: true
        });
    }

    onHiding() {
        this.setState({
            isVisible: false
        });
    }

    render() {
        return (
            <div>
                <Toast
                    visible={this.state.isVisible}
                    message="Connection problem"
                    type="error"
                    onHiding={this.onHiding}
                />
                <Button
                    text="Show the Toast"
                    onClick={this.onClick}
                />
            </div>
        );
    }
}

export default App;

User Interaction

Because the Toast is supposed to notify a user when something happens, it cannot be invoked from the UI. However, it may be hidden from the UI in many different ways. To decide which of them are available to the user, specify the following properties.

Property Description
closeOnClick Hides the Toast when a user clicks/presses it.
closeOnOutsideClick Hides the Toast when a user clicks/presses outside of it.
closeOnSwipe Hides the Toast when a user swipes it out of the screen.

Events

To execute certain commands before or after the Toast was shown/hidden, handle the showing, shown, hiding or hidden event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the corresponding onEventName property:

jQuery
JavaScript
$(function() {
    $("#toastContainer").dxToast({
        // ...
        onShowing: function (e) {
            // Handler of the "showing" event
        },
        onShown: function (e) {
            // Handler of the "shown" event
        },
        onHiding: function (e) {
            // Handler of the "hiding" event
        },
        onHidden: function (e) {
            // Handler of the "hidden" event
        }
    });
});
Angular
HTML
TypeScript
<dx-toast ...
    (onShowing)="onShowing($event)"
    (onShown)="onShown($event)"
    (onHiding)="onHiding($event)"
    (onHidden)="onHidden($event)">
</dx-toast>
import { DxToastModule, DxButtonModule } from "devextreme-angular";
// ...
export class AppComponent {
    onShowing (e) {
        // Handler of the "showing" event
    },
    onShown (e) {
        // Handler of the "shown" event
    },
    onHiding (e) {
        // Handler of the "hiding" event
    },
    onHidden (e) {
        // Handler of the "hidden" event
    }
}
@NgModule({
     imports: [
         DxButtonModule,
         DxToastModule,
         // ...
     ],
     // ...
 })
Vue
<template>
    <DxToast ...
        @showing="onShowing"
        @shown="onShown"
        @hiding="onHiding"
        @hidden="onHidden"
    />
</template>

<script>
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { DxToast } from 'devextreme-vue/toast';
import { DxButton } from 'devextreme-vue/button';

export default {
    components: {
        DxToast,
        DxButton
    },
    methods: {
        onShowing(e) {
            // Handler of the 'showing' event
        },
        onShown(e) {
            // Handler of the 'shown' event
        },
        onHiding(e) {
            // Handler of the 'hiding' event
        },
        onHidden(e) {
            // Handler of the 'hidden' event
        }
    }
}
</script>
React
import React from 'react';
import 'devextreme/dist/css/dx.common.css';
import 'devextreme/dist/css/dx.light.css';

import { Toast } from 'devextreme-react/toast';
import { Button } from 'devextreme-react/button';

class App extends React.Component {
    onShowing(e) {
        // Handler of the 'showing' event
    }

    onShown(e) {
        // Handler of the 'shown' event
    }

    onHiding(e) {
        // Handler of the 'hiding' event
    }

    onHidden(e) {
        // Handler of the 'hidden' event
    }

    render() {
        return (
            <Toast ...
                onShowing={this.onShowing}
                onShown={this.onShown}
                onHiding={this.onHiding}
                onHidden={this.onHidden}
            />
        );
    }
}

export default App;

If you are going to change event handlers at runtime, or if you need to attach several handlers to a single event, subscribe to the events using the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
const hiddenEventHandler1 = function (e) {
    // First handler of the "hidden" event
};

const hiddenEventHandler2 = function (e) {
    // Second handler of the "hidden" event
};

$("#toastContainer").dxToast("instance")
    .on("hidden", hiddenEventHandler1)
    .on("hidden", hiddenEventHandler2);
See Also