Skip to content

Commit f69c401

Browse files
author
Jakub Strebeyko
committed
fix Navbar so it closes on a navlink click
1 parent 628bf89 commit f69c401

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

src/components/Navbar.vue

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
2-
<component :class="navClass" :is="tag" :style="navStyles">
2+
<component :class="navClass" :is="tag" :style="navStyles" @click="close">
33
<button :class="navTogglerClass" type="button" data-toggle="collapse" :data-target="target" aria-controls="navbarSupportedContent"
4-
aria-expanded="false" aria-label="Toggle navigation" @click="onClick">
4+
aria-expanded="false" aria-label="Toggle navigation" @click.stop="onClick">
55
<div v-if="animated" ref="animatedIcon" :class="'animated-icon' + animation"><span></span><span></span><span></span><span v-if="animation === '3'"></span></div>
66
<span v-else :class="navTogglerIcon">
77
<i v-if="hamburger" class="fa fa-bars fa-1x"/>
@@ -33,7 +33,7 @@ const Navbar = {
3333
},
3434
target: {
3535
type: String,
36-
default: '#navbarSupportedContent'
36+
default: 'navbarSupportedContent'
3737
},
3838
scrolling: {
3939
type: Boolean,
@@ -164,11 +164,33 @@ const Navbar = {
164164
if (e.target.classList.contains('navbar-toggler') || e.target.parentNode.classList.contains('navbar-toggler')) {
165165
this.toggle();
166166
}
167+
},
168+
searchForCollapseContent(node) {
169+
if ((typeof node.attributes === 'undefined') || (typeof node.attributes.id === 'undefined')) return;
170+
if (node.id === this.target) {
171+
this.collapse = node;
172+
this.collapse.classList.add('collapse');
173+
}
167174
}
168175
},
169176
mounted() {
170-
this.collapse = this.$el.children.navbarSupportedContent;
171-
this.collapse.classList.add('collapse');
177+
this.$slots.default.forEach(child => {
178+
if (child.elm.id === this.target) {
179+
this.collapse = child.elm;
180+
this.collapse.classList.add('collapse');
181+
} else {
182+
this.children = child.elm.childNodes;
183+
this.children.forEach(nextChild => {
184+
this.searchForCollapseContent(nextChild);
185+
nextChild.childNodes.forEach(nextChild2 => {
186+
this.searchForCollapseContent(nextChild2);
187+
nextChild2.childNodes.forEach(nextChild3 => {
188+
this.searchForCollapseContent(nextChild3);
189+
});
190+
});
191+
});
192+
}
193+
});
172194
},
173195
created() {
174196
window.addEventListener('scroll', this.handleScroll);

0 commit comments

Comments
 (0)