Add keyboard shortcuts to menu

This commit is contained in:
Marcos Moura 2016-10-24 22:04:00 -02:00
parent 10d329a220
commit a929aafda9
3 changed files with 45 additions and 2 deletions

View file

@ -83,7 +83,9 @@ $menu-base-width: 56px;
text-overflow: ellipsis;
white-space: nowrap;
&:hover {
&:hover,
&:focus,
&.md-highlighted {
background-color: rgba(#000, .12);
transition: $swift-ease-out;
}

View file

@ -3,6 +3,10 @@
class="md-menu-content"
@keydown.esc.prevent="close"
@keydown.tab.prevent="close"
@keydown.up.prevent="highlightItem(highlighted - 1)"
@keydown.down.prevent="highlightItem(highlighted + 1)"
@keydown.enter.prevent="fireClick"
@keydown.space.prevent="fireClick"
tabindex="-1">
<slot></slot>
</div>
@ -10,9 +14,28 @@
<script>
export default {
data() {
return {
highlighted: false,
itemsAmount: 0
};
},
methods: {
close() {
this.highlighted = false;
this.$parent.close();
},
highlightItem(factor) {
if (factor >= 1 && factor <= this.itemsAmount) {
this.highlighted = factor;
} else {
this.highlighted = 1;
}
},
fireClick() {
if (this.highlighted > 0) {
this.$children[this.highlighted - 1].$el.click();
}
}
},
mounted() {

View file

@ -1,5 +1,8 @@
<template>
<div class="md-menu-item" @click="close">
<div
class="md-menu-item"
:class="classes"
@click="close">
<div class="md-menu-item-content">
<slot></slot>
</div>
@ -8,6 +11,18 @@
<script>
export default {
data() {
return {
index: 0
};
},
computed: {
classes() {
return {
'md-highlighted': this.index === this.$parent.highlighted
};
}
},
methods: {
close() {
this.$emit('click');
@ -20,6 +35,9 @@
throw new Error('You must wrap the md-menu-item in a md-menu-content');
}
this.$parent.itemsAmount++;
this.index = this.$parent.itemsAmount;
}
};
</script>