Skip to content

Commit ce3bfc3

Browse files
committed
modal.vue
moved onclick handler "onlickOut" from modal to modal-backdrop as it more logical place IMHO dropdown-item.vue link.vue changes to make possible attach @click handlers nav-item-dropdown API changes
1 parent db026d6 commit ce3bfc3

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

lib/components/dropdown-item.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<a :is="componentType" class="dropdown-item" :to="toObject" :href="hrefString">
2+
<a :is="componentType" class="dropdown-item" :to="to" :href="hrefString" @click="click">
33
<slot></slot>
44
</a>
55
</template>

lib/components/link.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<a :is="componentType" :active-class="activeClass" :to="to" :href="hrefString" :exact="exact">
2+
<a :is="componentType" :active-class="activeClass" :to="to" :href="hrefString" :exact="exact" @click="click">
33
<slot></slot>
44
</a>
55
</template>
@@ -33,6 +33,11 @@
3333
type: Boolean,
3434
default: false
3535
}
36+
},
37+
methods: {
38+
click() {
39+
this.$emit('click')
40+
}
3641
}
3742
};
3843
</script>

lib/components/modal.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<div key="modal" :id="id"
1212
v-show="visible"
1313
:class="['modal',{fade :fade}]"
14-
@click="onClickOut($event)"
1514
>
1615

1716
<div :class="['modal-dialog','modal-'+size]">
@@ -43,7 +42,11 @@
4342
</div>
4443
</div>
4544

46-
<div key="modal-backdrop" :class="['modal-backdrop',{fade: fade}]" v-if="visible"></div>
45+
<div key="modal-backdrop"
46+
:class="['modal-backdrop',{fade: fade}]"
47+
v-if="visible"
48+
@click="onClickOut($event)"
49+
></div>
4750
</transition-group>
4851
</div>
4952
</template>

lib/components/nav-item-dropdown.vue

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
2-
<li :class="{'nav-item': true, show: show,dropdown: !dropup, dropup: dropup}">
2+
<li :class="{'nav-item': true, show: visible,dropdown: !dropup, dropup: dropup}">
33
<a @click.stop.prevent="toggle($event)"
44
:class="['nav-link', dropdownToggle]"
55
href="" aria-haspopup="true"
6-
:aria-expanded="show"
6+
:aria-expanded="visible"
77
:disabled="disabled">
88
<slot name="text">{{ text }}</slot>
99
</a>
@@ -14,10 +14,15 @@
1414
</template>
1515

1616
<script>
17+
import clickOut from '../mixins/clickout';
18+
1719
export default {
20+
mixins: [
21+
clickOut
22+
],
1823
data() {
1924
return {
20-
show: false
25+
visible: false
2126
};
2227
},
2328
computed: {
@@ -49,41 +54,39 @@
4954
class: ['class']
5055
},
5156
created() {
57+
// to keep one dropdown opened at page
5258
this.$root.$on('shown::dropdown', el => {
5359
if (el !== this) {
54-
this.clickOut();
60+
this.close()
5561
}
5662
});
5763
},
58-
mounted() {
59-
if (typeof document !== 'undefined') {
60-
document.documentElement.addEventListener('click', this.clickOut);
61-
}
62-
},
63-
destroyed() {
64-
if (typeof document !== 'undefined') {
65-
document.removeEventListener('click', this.clickOut);
66-
}
67-
},
68-
methods: {
69-
setShow(state) {
70-
if (this.show === state) {
64+
watch: {
65+
visible(state, old) {
66+
if (state === old) {
7167
return; // Avoid duplicated emits
7268
}
73-
this.show = state;
7469
75-
if (this.show) {
70+
if (state) {
7671
this.$root.$emit('shown::dropdown', this);
7772
} else {
7873
this.$root.$emit('hidden::dropdown', this);
7974
}
80-
},
75+
}
76+
},
77+
methods: {
8178
toggle() {
82-
this.setShow(!this.show);
79+
this.visible = !this.visible;
80+
},
81+
open() {
82+
this.visible = true
83+
},
84+
close() {
85+
this.visible = false
86+
},
87+
clickOutListener() {
88+
this.close()
8389
},
84-
clickOut() {
85-
this.setShow(false);
86-
}
8790
}
8891
};
8992
</script>

0 commit comments

Comments
 (0)