2021-03-20 07:38:56 +00:00
|
|
|
<template>
|
|
|
|
<ul
|
2021-08-09 07:38:52 +00:00
|
|
|
ref="dropdownMenu"
|
2021-03-20 07:38:56 +00:00
|
|
|
class="dropdown menu vertical"
|
|
|
|
:class="[placement && `dropdown--${placement}`]"
|
|
|
|
>
|
|
|
|
<slot></slot>
|
|
|
|
</ul>
|
|
|
|
</template>
|
|
|
|
<script>
|
2021-08-09 07:38:52 +00:00
|
|
|
import eventListenerMixins from 'shared/mixins/eventListenerMixins';
|
|
|
|
import {
|
|
|
|
hasPressedArrowUpKey,
|
|
|
|
hasPressedArrowDownKey,
|
|
|
|
} from 'shared/helpers/KeyboardHelpers';
|
2021-03-20 07:38:56 +00:00
|
|
|
export default {
|
|
|
|
name: 'WootDropdownMenu',
|
|
|
|
componentName: 'WootDropdownMenu',
|
|
|
|
|
2021-08-09 07:38:52 +00:00
|
|
|
mixins: [eventListenerMixins],
|
|
|
|
|
2021-03-20 07:38:56 +00:00
|
|
|
props: {
|
|
|
|
placement: {
|
|
|
|
type: String,
|
|
|
|
default: 'top',
|
|
|
|
},
|
|
|
|
},
|
2021-08-09 07:38:52 +00:00
|
|
|
|
|
|
|
mounted() {
|
|
|
|
this.focusItem();
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
focusItem() {
|
|
|
|
this.$refs.dropdownMenu
|
|
|
|
.querySelector('ul.dropdown li.dropdown-menu__item .button')
|
|
|
|
.focus();
|
|
|
|
},
|
|
|
|
handleKeyEvents(e) {
|
|
|
|
if (hasPressedArrowUpKey(e)) {
|
|
|
|
const items = this.$refs.dropdownMenu.querySelectorAll(
|
|
|
|
'ul.dropdown li.dropdown-menu__item .button'
|
|
|
|
);
|
|
|
|
const focusItems = this.$refs.dropdownMenu.querySelector(
|
|
|
|
'ul.dropdown li.dropdown-menu__item .button:focus'
|
|
|
|
);
|
|
|
|
const activeElementIndex = [...items].indexOf(focusItems);
|
|
|
|
const lastElementIndex = items.length - 1;
|
|
|
|
|
|
|
|
if (activeElementIndex >= 1) {
|
|
|
|
items[activeElementIndex - 1].focus();
|
|
|
|
} else {
|
|
|
|
items[lastElementIndex].focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hasPressedArrowDownKey(e)) {
|
|
|
|
const items = this.$refs.dropdownMenu.querySelectorAll(
|
|
|
|
'li.dropdown-menu__item .button'
|
|
|
|
);
|
|
|
|
const focusItems = this.$refs.dropdownMenu.querySelector(
|
|
|
|
'li.dropdown-menu__item .button:focus'
|
|
|
|
);
|
|
|
|
const activeElementIndex = [...items].indexOf(focusItems);
|
|
|
|
const lastElementIndex = items.length - 1;
|
|
|
|
|
|
|
|
if (activeElementIndex === lastElementIndex) {
|
|
|
|
items[0].focus();
|
|
|
|
} else {
|
|
|
|
items[activeElementIndex + 1].focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2021-03-20 07:38:56 +00:00
|
|
|
};
|
|
|
|
</script>
|