Prevent toast close with mobile swipe

17 Feb 20221 minute to read

You can prevent the toast close with mobile swipe action by setting BeforeClose argument cancel value to true while argument type as a swipe. The following code shows how to prevent toast close with mobile swipe.

The following sample demonstrates preventing toast close with mobile swipe element displaying with custom code blocks.

<div class="control-section" style="width:400px;margin:0 auto;">
    @Html.EJS().Toast("element").Title("Matt sent you a friend request").Content("You have a new friend request yet to accept").BeforeClose("beforeClose").Render()
    @Html.EJS().Button("button").Content("Show Toast").CssClass("e-btn").Render()
</div>
<script type="text/javascript">
    setTimeout(() => {
        var toastObj = document.getElementById('element').ej2_instances[0];
        toastObj.target = document.body;
        toastObj.show();
    }, 1000);
    document.getElementById("button").addEventListener('click', function () {
        var toastObj = document.getElementById('element').ej2_instances[0];
        toastObj.show();
    });
    function beforeClose(args: ToastBeforeCloseArgs) {
        if (args.type === "swipe") {
            args.cancel = true;
        }
    }
</script>
public class HomeController : Controller
{

    public ActionResult Index()
    {
        return View();
    }
}