Close modals and off-canvas elements with Esc key

An option to also close modals and off-canvas elements with the Esc key.

3 Likes

Would be nice to have something like this. I’m going to add this one to our general backlog but it could be solved with a quick little javascript behavior. Add this in your custom JS somewhere.

window.csGlobal.everinit('[data-x-esc-close]', function(el) {
  var id = el.getAttribute('data-x-toggleable')
  window.addEventListener('keyup', function(e) {
    if (e.key === 'Escape' && window.xToggleGetState(id)) {
      window.xToggleUpdate(id, false)
    }
  })
})

Then inspect the modal then add data-x-esc-close (with no value) as a custom attribute on the modal.

3 Likes