vanillajs-deck/js/keyhandler.js
2019-11-23 12:20:09 -08:00

29 lines
848 B
JavaScript

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);