Add arrow keys navigation for sidebar sections

This commit is contained in:
James Lyne 2021-05-26 17:43:56 +01:00
parent b096240651
commit dca535b826

View File

@ -26,7 +26,7 @@
<SvgIcon name="players"></SvgIcon>
</button>
</header>
<div class="sidebar__content">
<div class="sidebar__content" @keydown="handleKeydown">
<ServerList v-if="serverCount > 1" v-show="currentlyVisible.has('maps')"></ServerList>
<WorldList v-if="mapCount > 1" v-show="currentlyVisible.has('maps')"></WorldList>
<PlayerList v-if="previouslyVisible.has('players')" v-show="currentlyVisible.has('players')"></PlayerList>
@ -90,6 +90,50 @@ export default defineComponent({
messageWorlds,
messagePlayers,
}
},
methods: {
handleKeydown(e: KeyboardEvent) {
if(!e.target || !(e.target as HTMLElement).classList.contains('section__heading')) {
return;
}
const sectionHeadings: HTMLElement[] = Array.from(this.$el.querySelectorAll('.section__heading')),
position = sectionHeadings.indexOf(e.target as HTMLElement);
if(position === -1) {
return;
}
let newPosition;
switch(e.key) {
case 'ArrowDown':
newPosition = position + 1;
break;
case 'ArrowUp':
newPosition = position - 1;
break;
case 'Home':
newPosition = 0;
break;
case 'End':
newPosition = sectionHeadings.length - 1;
break;
default:
return;
}
if(newPosition >= sectionHeadings.length) {
newPosition = 0
} else if(newPosition < 0) {
newPosition = sectionHeadings.length - 1;
}
e.preventDefault();
e.stopImmediatePropagation();
sectionHeadings[newPosition].focus();
}
}
})