mirror of
https://github.com/JeremyLikness/vanillajs-deck
synced 2024-11-15 01:54:57 +00:00
29 lines
848 B
JavaScript
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);
|