Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing Popup by pressing Escape doesn't work in all cases #6551

Open
5 tasks done
Schleuse opened this issue Mar 11, 2019 · 4 comments
Open
5 tasks done

Closing Popup by pressing Escape doesn't work in all cases #6551

Schleuse opened this issue Mar 11, 2019 · 4 comments
Labels
accessibility Anything related to ensuring no barriers exist that prevent interactions or information access bug

Comments

@Schleuse
Copy link
Contributor

Schleuse commented Mar 11, 2019

Description

The keyboard handler adds the keydown-Listener to the whole document and not to the map-container element.

In order not to interfere with other keyboard-events, the keydown-listener gets registered when the map-container receives focus and gets removed when a blur event on the map-container occurs.

this._map.on({
focus: this._addHooks,
blur: this._removeHooks
}, this);

this._map.off({
focus: this._addHooks,
blur: this._removeHooks
}, this);

_addHooks: function () {
on(document, 'keydown', this._onKeyDown, this);
},
_removeHooks: function () {
off(document, 'keydown', this._onKeyDown, this);
},

When the marker gets clicked, it receives focus, therefore the map container looses focus and triggers a blur event. The blur event disables the Keyboard-Handler and the opened popup can't be closed by pressing Esc because the Keyboard-Handler is disabled.

  • I've looked at the documentation to make sure the behavior is documented and expected
  • I'm sure this is a Leaflet code issue, not an issue with my own code nor with the framework I'm using (Cordova, Ionic, Angular, React…)
  • I've searched through the issues to make sure it's not yet reported

Steps to reproduce
Steps to reproduce the behavior:

  • Add a marker to a map
  • Bind a popup to that marker
  • Click the marker to open the popup
  • Try to close the opened popup by pressing Esc

Expected behavior
The opened popup should be closed then the Esc-Key is pressed

Current behavior
The popup get's only closed when the layer the popup is bound to is not focusable

Environment

  • Leaflet version: 1.4
  • Browser (with version): Chrome 73
  • OS/Platform (with version): Windows 10

Minimal example reproducing the issue

https://next.plnkr.co/edit/Soh1otCSsfdBBCi0

  • this example is as simple as possible
  • this example does not rely on any third party code
@Schleuse
Copy link
Contributor Author

Schleuse commented Mar 11, 2019

I'm wondering why the keydown listeners are added to the document and not the map-container.
Is it necessary for some legacy browser?

I've made a branch where the KeyBoard-Handler adds the listeners to container and not the whole document. By doing that it is not necessary to add and remove the listeners on focus and blur and it seems somewhat simpler and more robust

@IvanSanchez
Copy link
Member

Because keyboard events only fire/bubble on HTMLElements that the browser deems focusable. Let me quote from https://w3c.github.io/uievents/#events-keyboard-event-order , emphasis mine:

The event target of a key event is the currently focused element which is processing the keyboard activity. This is often an HTML input element or a textual element which is editable, but MAY be an element defined by the host language to accept keyboard input for non-text purposes, such as the activation of an accelerator key or trigger of some other behavior. If no suitable element is in focus, the event target will be the HTML body element if available, otherwise the root element.

Because a Leaflet map container is often a non-focusable-by-default <div>, markers can have non-focusable DivIcons and pop-ups have non-focusable containers, and on top of that there's multiple browser implementations, the only reliable way of handling keyboard input is to attach the event handlers to the document.

The current code attaches focus/blur and keyboard event handlers to the map container at ...
https://github.com/Leaflet/Leaflet/blob/master/src/map/Map.js#L1326-L1327
...since #6421, but anyway this is one of the areas where I must ask for testing in several browsers and environments.

@Malvoz
Copy link
Member

Malvoz commented Feb 8, 2022

@Schleuse's #6551 (comment):

When the marker gets clicked, it receives focus, therefore the map container looses focus and triggers a blur event. The blur event disables the Keyboard-Handler and the opened popup can't be closed by pressing Esc because the Keyboard-Handler is disabled.

@IvanSanchez's #6551 (comment):

Because keyboard events only fire/bubble on HTMLElements that the browser deems focusable

Because [...] pop-ups have non-focusable containers

#8115 as proposed requires the popup's container to be programmatically focusable via tabindex="-1", perhaps that'd be sufficient to have the Esc key close popups more reliably? Just an observation on the potential relation.

@Malvoz Malvoz added the accessibility Anything related to ensuring no barriers exist that prevent interactions or information access label Apr 20, 2022
@joeyskuo
Copy link

joeyskuo commented May 15, 2022

hello, I've modified the Popup class's Layer to include a keydown eventListener to close the popups. ( PR #8239 )

Edited Plunkrs for demonstration
https://plnkr.co/edit/pkOzSCsI5QvtAcAe
https://plnkr.co/edit/4hXgn7EMinn9Wwn6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessibility Anything related to ensuring no barriers exist that prevent interactions or information access bug
Projects
None yet
Development

No branches or pull requests

5 participants