Skip to content

Commit

Permalink
_onKeyPress method
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewharvey committed Nov 4, 2019
1 parent ff2b3b3 commit 426c04e
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/ui/marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export default class Marker extends Evented {
'_onMove',
'_onUp',
'_addDragHandler',
'_onMapClick'
'_onMapClick',
'_onKeyPress'
], this);

this._anchor = options && options.anchor || 'center';
Expand Down Expand Up @@ -291,22 +292,10 @@ export default class Marker extends Evented {
* @returns {Marker} `this`
*/
setPopup(popup: ?Popup) {
const keypressListener = (e: KeyboardEvent) => {
const code = e.code;
const legacyCode = e.charCode || e.keyCode;

if (
(code === 'Space') || (code === 'Enter') ||
(legacyCode === 32) || (legacyCode === 13) // space or enter
) {
this.togglePopup();
}
};

if (this._popup) {
this._popup.remove();
this._popup = null;
this._element.removeEventListener('keypress', keypressListener);
this._element.removeEventListener('keypress', this._onKeyPress);

if (!this._originalTabIndex) {
this._element.removeAttribute('tabindex');
Expand Down Expand Up @@ -336,12 +325,24 @@ export default class Marker extends Evented {
if (!this._originalTabIndex) {
this._element.setAttribute('tabindex', '0');
}
this._element.addEventListener('keypress', keypressListener);
this._element.addEventListener('keypress', this._onKeyPress);
}

return this;
}

_onKeyPress(e: KeyboardEvent) {
const code = e.code;
const legacyCode = e.charCode || e.keyCode;

if (
(code === 'Space') || (code === 'Enter') ||
(legacyCode === 32) || (legacyCode === 13) // space or enter
) {
this.togglePopup();
}
}

_onMapClick(e: MapMouseEvent) {
const targetElement = e.originalEvent.target;
const element = this._element;
Expand Down

0 comments on commit 426c04e

Please sign in to comment.