vanillajs-deck/js/keyhandler.js

29 lines
848 B
JavaScript
Raw Normal View History

2019-11-23 20:20:09 +00:00
class KeyHandler extends HTMLElement {
constructor() {
super();
this._deck = null;
}
static get observedAttributes() {
return ["deck"];
}
async attributeChangedCallback(attrName, oldVal, newVal) {
if (attrName === "deck") {
if (oldVal !== newVal) {
this._deck = document.getElementById(newVal);
this._deck.parentElement.addEventListener("keydown", key => {
if (key.keyCode == 39 || key.keyCode == 32) {
this._deck.next();
}
else if (key.keyCode == 37) {
this._deck.previous();
}
});
}
}
}
}
export const registerKeyHandler = () => customElements.define('key-handler', KeyHandler);