cloudflare / cloudflare/react-modal2

Interaction with other uses of Esc

Open
#14 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
275
Forks
16
PR merge metrics
No merged PRs in 30d

Description

I'm using react-modal2 as part of a more complex app that does its own keyboard handling, and I noticed that it doesn't do anything to keep its Esc event from also being seen by other parts of the site. (In my example, I have an Esc handler on my app container, and pressing Esc to close the modal also fires the app's event.)

Possible solutions:
1. Pass useCapture true to `document.addEventListener` and `document.removeEventListener` so that react-modal2's global Esc handler fires before any individual React components'.
2. Move the keyboard handler to the `ref="modal"` component; since it captures the focus, it should see any Esc keypresses.

And whether 1 or 2 is chosen, update `handleDocumentKeydown` to call `event.stopImmediatePropagation()`.

I'm not an expert on DOM events, so I don't know which is better. (2 seems cleaner, but I assume there was some reason for going with document-level events in the current implementation.)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.