Offcanvas and preventing closing if the condition is not met

I have the form on offcanvas and I will try to prevent closing this offcanvas, if the data in the form is changed. I have custom function attached to “close.zf.offCanvas” event. This function is called, but closing offcanvas is not interupted.

$('#offCanvas').on('close.zf.offCanvas', function(event) {
            if (dataChanged) {
                event.stopImmediatePropagation();
                event.preventDefault();
                event.stopPropagation();
                console.log("stop here");
            }
        });

What is the best way to prevent the closing of offcanvas,
if I want to keep the closing with the pressing of ESC or the clicking outside the offcanvas, but only if the data in the form was not changed?

Nobody knows? Is it even possible?