JSFiddle - React, Tailwind, and code Playground

HTML

<div id="leaveWarning">Please dont leave me!</div>

CSS

#leaveWarning {
  background-color: #f00;
  display: none;
  text-align: center;
  top: 0;
  padding: 38px;
  position: fixed;
  width: 100%;
  z-index: 42;
}

JavaScript

window.onbeforeunload = function(e) {
  $('#leaveWarning').show();
  
  // the timer is only to let the message box disappear after the user
  // decides to stay on this page
  // set this to 1ms .. since timers are stopped for this modal  
  setTimeout(function() {
    $('#leaveWarning').hide();
  }, 1);
  
  // 
  return "This message is not relevant in most modern browsers.";
}