Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sub-menu for dropdown #1455

Merged
merged 10 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/userGuide/syntax/dropdowns.mbdf
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@
</variable>
</include>

**You can also use create multi-level submenu by nesting Dropdowns.**

<include src="codeAndOutput.md" boilerplate >
<variable name="highlightStyle">html</variable>
<variable name="code">
<!-- Nest the dropdown syntax to create dropdown submenus -->
<dropdown header="*Multi-Level Dropdown*" type="primary">
<li><a href="#dropdown" class="dropdown-item">Item</a></li>
<li><a href="#dropdown" class="dropdown-item">Another item</a></li>
<dropdown header="*Submenu*">
<li><a href="#dropdown" class="dropdown-item">Item</a></li>
<li><a href="#dropdown" class="dropdown-item">Another item</a></li>
</dropdown>
</dropdown>
</variable>
</include>

****Options****

Name | Type | Default | Description
Expand Down
19 changes: 15 additions & 4 deletions packages/vue-components/src/Dropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
</ul>
</slot>
</li>
<submenu v-else-if="isSubmenu" ref="submenu">
<template v-for="(node, name) in $slots" :slot="name">
<slot :name="name"></slot>
</template>
</submenu>
<div v-else ref="dropdown" :class="classes">
<slot name="before"></slot>
<slot name="button">
Expand Down Expand Up @@ -58,6 +63,8 @@ export default {
default: ''
}
},
provide: { hasParentDropdown: true },
inject: ['hasParentDropdown'],
computed: {
btnType () {
return `btn-${this.type}`;
Expand All @@ -72,6 +79,7 @@ export default {
return toBoolean(this.disabled);
},
isLi () { return this.$parent._navbar || this.$parent.menu || this.$parent._tabset },
isSubmenu() { return this.hasParentDropdown },
menu () {
return !this.$parent || this.$parent.navbar
},
Expand Down Expand Up @@ -112,7 +120,7 @@ export default {
showDropdownMenu() {
this.show = true;
$(this.$refs.dropdown).findChildren('ul').each(ul => ul.classList.toggle('show', true));
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's correct these in another PR; we could enable linting for dropdown.vue as well. (the unlinted ones are files that haven't changed much from vue-strap)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright sure will open another PR for this 👍

},
mounted () {
const $el = $(this.$refs.dropdown)
Expand All @@ -122,15 +130,18 @@ export default {
$el.onBlur((e) => { this.hideDropdownMenu() }, false)
$el.findChildren('a,button.dropdown-toggle').on('click', e => {
e.preventDefault()
if (this.disabledBool) { return false }
if (this.disabledBool) { return false; }
if (this.showBool) {
this.hideDropdownMenu();
} else {
this.showDropdownMenu();
}
return false
return false;
})
$el.findChildren('ul').on('click', 'li>a', e => {
if (e.target.classList.contains('submenu-toggle')) { return }
this.hideDropdownMenu();
})
$el.findChildren('ul').on('click', 'li>a', e => { this.hideDropdownMenu() })
},
beforeDestroy () {
const $el = $(this.$refs.dropdown)
Expand Down
23 changes: 17 additions & 6 deletions packages/vue-components/src/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,19 @@ export default {
isExact(url, href) {
return normalizeUrl(url) === normalizeUrl(href);
},
addClassIfDropdown(dropdownLinks, a) {
addClassIfDropdown(dropdownLinks, a, li) {
if (dropdownLinks.includes(a)) {
a.classList.add('dropdown-current');
this.addClassIfSubmenu(a, li);
}
},
addClassIfSubmenu(a, li) {
let el = a.parentElement;
while (el !== li) {
if (el.classList.contains('dropdown-submenu')) {
$(el).findChildren('a').each(a => a.classList.add('dropdown-current'));
}
el = el.parentElement;
}
},
highlightLink(url) {
Expand All @@ -148,7 +158,7 @@ export default {
// terminate early on an exact match
if (this.isExact(url, a.href)) {
li.classList.add('current');
this.addClassIfDropdown(dropdownLinks, a);
this.addClassIfDropdown(dropdownLinks, a, li);
return;
}
}
Expand All @@ -167,19 +177,19 @@ export default {
if (hlMode === 'sibling-or-child') {
if (this.isSibling(url, a.href) || this.isChild(url, a.href)) {
li.classList.add('current');
this.addClassIfDropdown(dropdownLinks, a);
this.addClassIfDropdown(dropdownLinks, a, li);
return;
}
} else if (hlMode === 'sibling') {
if (this.isSibling(url, a.href)) {
li.classList.add('current');
this.addClassIfDropdown(dropdownLinks, a);
this.addClassIfDropdown(dropdownLinks, a, li);
return;
}
} else if (hlMode === 'child') {
if (this.isChild(url, a.href)) {
li.classList.add('current');
this.addClassIfDropdown(dropdownLinks, a);
this.addClassIfDropdown(dropdownLinks, a, li);
return;
}
} else {
Expand Down Expand Up @@ -209,6 +219,7 @@ export default {
});
});
$(this.$el).on('click', 'li:not(.dropdown)>a', (e) => {
if (e.target.classList.contains('submenu-toggle')) { return }
setTimeout(() => { this.collapsed = true; }, 200);
}).onBlur((e) => {
if (!this.$el.contains(e.target)) { this.collapsed = true; }
Expand Down Expand Up @@ -241,7 +252,7 @@ export default {
}

>>> .dropdown-current {
color: #fff;
color: #fff !important;
background: #007bff;
}
</style>
177 changes: 177 additions & 0 deletions packages/vue-components/src/Submenu.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<template>
<li
ref="submenu"
:class="[addClass, 'dropdown-submenu',
{ 'dropright': dropright, 'dropleft': dropleft }]"
>
<slot name="button">
<a
class="submenu-toggle"
role="button"
:class="{disabled: disabled}"
>
<slot name="_header">
<slot name="header"></slot>
</slot>
</a>
</slot>
<slot name="dropdown-menu">
<ul class="dropdown-menu">
<slot></slot>
</ul>
</slot>
</li>
</template>

<script>
import { toBoolean } from './utils/utils';
import $ from './utils/NodeList';
import positionSubmenu from './utils/submenu';

export default {
props: {
addClass: {
type: String,
default: '',
},
disabled: {
type: Boolean,
default: false,
},
},
data() {
return {
show: false,
dropright: true,
dropleft: false,
};
},
computed: {
disabledBool() {
return toBoolean(this.disabled);
},
},
methods: {
hideSubmenu() {
this.show = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use data() for properties that could change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated :)

$(this.$refs.submenu).findChildren('ul').each(ul => ul.classList.toggle('show', false));
this.alignMenuRight();
},
showSubmenu() {
this.show = true;
$(this.$refs.submenu).findChildren('ul').each((ul) => {
ul.classList.toggle('show', true);
if (positionSubmenu.isRightAlign(ul)) {
this.alignMenuRight();
} else {
this.alignMenuLeft();
}
positionSubmenu.preventOverflow(ul);
});
},
alignMenuRight() {
this.dropright = true;
this.dropleft = false;
},
alignMenuLeft() {
this.dropright = false;
this.dropleft = true;
},
},
mounted() {
const $el = $(this.$refs.submenu);
if (this.show) {
this.showSubmenu();
}
$el.onBlur(() => { this.hideSubmenu(); }, false);
$el.findChildren('a,button').on('click', (e) => {
e.preventDefault();
if (e.target !== e.currentTarget) { e.stopPropagation(); }
if (window.innerWidth < 768) {
if (this.disabledBool) { return false; }
if (this.show) {
this.hideSubmenu();
} else {
this.showSubmenu();
}
}
return false;
});
$el.findChildren('a,button').on('mouseover', (e) => {
e.preventDefault();
if (window.innerWidth > 767) {
if (this.disabledBool) { return false; }
e.currentTarget.click();
this.showSubmenu();
}
return false;
});
},
beforeDestroy() {
const $el = $(this.$refs.submenu);
$el.offBlur();
$el.findChildren('a,button').off();
$el.findChildren('ul').off();
},
};
</script>

<style scoped>
.dropdown-submenu {
color: #212529 !important;
padding: 0 !important;
position: relative;
}

.submenu-toggle {
display: inline-block;
width: 100%;
padding: .25rem .75rem .25rem 1.5rem;
}

.dropdown > ul > .dropdown-submenu:last-child > ul,
.btn-group > ul > .dropdown-submenu:last-child > ul {
margin-bottom: -.5rem;
}

@media (min-width: 768px) {
.submenu-toggle:after {
display: inline-block;
width: 0;
height: 0;
vertical-align: .255em;
content: "";
border-top: .3em solid transparent;
border-right: 0;
border-bottom: .3em solid transparent;
border-left: .3em solid;
float: right;
margin-top: .5em;
}
}

@media (max-width: 767px) {
.dropdown-submenu > ul {
padding-bottom: 0;
border-radius: 0;
margin: -.05rem;
position: static;
float: none;
}

.submenu-toggle:after {
display: inline-block;
width: 0;
height: 0;
margin-left: .255em;
vertical-align: .255em;
content: "";
border-top: .3em solid;
border-right: .3em solid transparent;
border-bottom: 0;
border-left: .3em solid transparent;
float: right;
margin-top: .5em;
}
}
</style>
Loading