parsley/app/javascript/components/AppDropdown.vue

85 lines
1.6 KiB
Vue
Raw Permalink Normal View History

2018-08-28 16:52:56 -05:00
<template>
2018-09-07 21:56:13 -05:00
<div class="dropdown" :class="{'is-active': open, 'is-hoverable': hover}">
2018-08-28 16:52:56 -05:00
<div class="dropdown-trigger">
<slot name="button">
<button type="button" class="button" :class="buttonClass" @click="toggle">
<span>{{ label }}</span>
2018-09-07 21:56:13 -05:00
<app-icon icon="caret-bottom" size="xs"></app-icon>
2018-08-28 16:52:56 -05:00
</button>
</slot>
</div>
<div class="dropdown-menu">
<div class="dropdown-content">
<slot>
Default Content
</slot>
</div>
</div>
</div>
</template>
<script>
export default {
props: {
open: {
2018-09-07 21:56:13 -05:00
required: false,
type: Boolean,
default: false
},
hover: {
required: false,
type: Boolean,
default: false
2018-08-28 16:52:56 -05:00
},
label: {
required: false,
type: String,
default: 'Select'
},
buttonClass: {
required: false,
default: ""
}
},
methods: {
toggle() {
if (this.open) {
this.triggerClose();
} else {
this.triggerOpen();
}
},
triggerOpen() {
this.$emit("open");
},
triggerClose() {
this.$emit("close");
2018-08-29 16:58:07 -05:00
},
handleOutsideClick(evt) {
if (this.open) {
if (!this.$el.contains(evt.target)) {
this.triggerClose();
}
}
2018-08-28 16:52:56 -05:00
}
2018-08-29 16:58:07 -05:00
},
mounted() {
document.addEventListener("click", this.handleOutsideClick);
},
beforeDestroy() {
document.removeEventListener("click", this.handleOutsideClick);
2018-08-28 16:52:56 -05:00
}
}
</script>