Skip to content

Commit 6118082

Browse files
author
Pooya Parsa
committed
prevent multi dropdowns showing at same time
1 parent 95bee77 commit 6118082

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

components/dropdown.vue

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,27 @@
7878
document.documentElement.addEventListener('click', this.clickOut);
7979
}
8080
},
81+
created() {
82+
this.$root.$on('shown::dropdown', el => {
83+
if (el !== this) {
84+
this.clickOut();
85+
}
86+
});
87+
},
8188
methods: {
8289
toggle() {
8390
this.setShow(!this.show);
8491
},
8592
setShow(state) {
8693
if (this.show === state) {
87-
return;
88-
} // Avoid duplicated emits
94+
return; // Avoid duplicated emits
95+
}
8996
this.show = state;
9097
9198
if (this.show) {
92-
this.$emit('shown');
99+
this.$root.$emit('shown::dropdown', this);
93100
} else {
94-
this.$emit('hidden');
101+
this.$root.$emit('hidden::dropdown', this);
95102
}
96103
},
97104
clickOut() {

components/nav-item-dropdown.vue

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@
5252
methods: {
5353
setShow(state) {
5454
if (this.show === state) {
55-
return;
56-
} // Avoid duplicated emits
55+
return; // Avoid duplicated emits
56+
}
5757
this.show = state;
5858
5959
if (this.show) {
60-
this.$root.$emit('shown::dropdown');
60+
this.$root.$emit('shown::dropdown', this);
6161
} else {
62-
this.$root.$emit('hidden::dropdown');
62+
this.$root.$emit('hidden::dropdown', this);
6363
}
6464
},
6565
toggle() {
@@ -70,14 +70,15 @@
7070
}
7171
},
7272
created() {
73-
const hub = this.$root;
74-
hub.$on('hide::dropdown', () => {
75-
this.show = false;
73+
this.$root.$on('shown::dropdown', el => {
74+
if (el !== this) {
75+
this.clickOut();
76+
}
7677
});
7778
},
7879
mounted() {
7980
if (typeof document !== 'undefined') {
80-
document.documentElement.addEventListener('click', this.clickOut, true);
81+
document.documentElement.addEventListener('click', this.clickOut);
8182
}
8283
}
8384
};

0 commit comments

Comments
 (0)