Close modals with Esc key

When you click on the images on this page a modal opens with a larger preview of the image. It works fine.

I would like to be able to use the ESC key to close these modals. I have followed this guide:


But it is not working … any ideas?

1 Like

Yeah, a native option for this would be great. Maybe they’ll consider adding it in.

1 Like

Yes, but until then a custom JS (that works) would also be fine. @JvP did you manage to get the custom JS from this post to work?

Hi @JesperF,

The code in the reference thread works properly. On the other hand, I will check with our development team on this and will add this as a feature request for future consideration.

Thanks

1 Like

Yes it still works here.

Thanks for adding a feature request for it :+1:t2:

@tristup But I have used the code from this post and it is not working. Can you see what I’m doing wrong?

Did you add this as well?

Yes:


The modal is a Component … perhaps that complicates things. But let´s see what Themeco support says.

But thanks :slight_smile: :pray:

1 Like

The Frontend API has changed since that post. See the below for an updated version. We’re also just going to add this feature to our main codebase in Pro 6.4. Have a great weekend!

window.csGlobal.rivet.attach('[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)
    }
  }, { passive: true })
});
1 Like

Fantastic, thanks and you have a great weekend too!

1 Like

You are most welcome.

@charlie Thanks for the updated js code. I’ve added it as Global JS code on our site, but the ESC key is still not working :sob:

But it’s good to hear that a native ESC key solution will be added to the main codebase in Pro 6.4.

I guess I’ll just have to wait for this :blush:

Thanks again :metal:

1 Like

No problem, @JesperF.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.