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

Dropdown menu fixes #8564

Merged
merged 2 commits into from
Apr 15, 2016
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
83 changes: 35 additions & 48 deletions docs/pages/dropdown-menu.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,39 @@ To create dropdown menus, nest a new `<ul>` inside an `<li>`. You can nest furth
<li>
<a>Item 1</a>
<ul class="menu">
<li><a href="#">Item 1A</a></li>
<li><a href="#Item-1A">Item 1A</a></li>
<li>
<a href="#">Item 1B</a>
<a href="#Item-1B">Item 1B</a>
<ul class="menu">
<li><a href="#">Item 1B i</a></li>
<li><a href="#">Item 1B ii</a></li>
<li><a href="#Item-1Bi">Item 1B i</a></li>
<li><a href="#Item-1Bii">Item 1B ii</a></li>
<li>
<a href="#">Item 1B iii</a>
<a href="#Item-1Biii">Item 1B iii</a>
<ul class="menu">
<li><a href="#">Item 1B iii alpha</a></li>
<li><a href="#">Item 1B iii omega</a></li>
<li><a href="#Item-1Biiialpha">Item 1B iii alpha</a></li>
<li><a href="#Item-1Biiiomega">Item 1B iii omega</a></li>
</ul>
</li>
<li>
<a href="#">Item 1B iv</a>
<a href="#Item-1Biv">Item 1B iv</a>
<ul class="menu">
<li><a href="#">Item 1B iv alpha</a></li>
<li><a href="#Item-1Bivalpha">Item 1B iv alpha</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Item 1C</a></li>
<li><a href="#Item-1C">Item 1C</a></li>
</ul>
</li>
<li>
<a href="#">Item 2</a>
<a href="#Item-2">Item 2</a>
<ul class="menu">
<li><a href="#">Item 2A</a></li>
<li><a href="#">Item 2B</a></li>
<li><a href="#Item-2A">Item 2A</a></li>
<li><a href="#Item-2B">Item 2B</a></li>
</ul>
</li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 4</a></li>
<li><a href="#Item-3">Item 3</a></li>
<li><a href="#Item-4">Item 4</a></li>
</ul>

---
Expand All @@ -97,54 +97,41 @@ Add the `.vertical` class to the top-level menu to make it vertical. Sub-menus a

<ul class="vertical dropdown menu" data-dropdown-menu style="max-width: 300px;">
<li>
<a href="#">Item 1</a>
<ul class="menu">
<li><a href="#">Item 1A Loooong</a></li>
<li><a href="#">Item 1B</a></li>
</ul>
</li>
<li>
<a href="#">Item 2</a>
<ul class="menu">
<li><a href="#">Item 2A</a></li>
<li><a href="#">Item 2B</a></li>
</ul>
</li>
<li>
<a href="#">Item 3</a>
<ul class="menu">
<li><a href="#">Item 3A</a></li>
<li><a href="#">Item 3B</a></li>
</ul>
</li>
<li>
<a href="#">Item 1</a>
<a>Item 1</a>
<ul class="menu">
<li><a href="#">Item 1A Loooong</a></li>
<li><a href="#Item-1A">Item 1A</a></li>
<li>
<a href="#"> Item 1 sub</a>
<a href="#Item-1B">Item 1B</a>
<ul class="menu">
<li><a href="#">Item 1 subA</a></li>
<li><a href="#">Item 1 subB</a></li>
<li><a href="#Item-1Bi">Item 1B i</a></li>
<li><a href="#Item-1Bii">Item 1B ii</a></li>
<li>
<a href="#"> Item 1 sub</a>
<a href="#Item-1Biii">Item 1B iii</a>
<ul class="menu">
<li><a href="#">Item 1 subA</a></li>
<li><a href="#">Item 1 subB</a></li>
<li><a href="#Item-1Biiialpha">Item 1B iii alpha</a></li>
<li><a href="#Item-1Biiiomega">Item 1B iii omega</a></li>
</ul>
</li>
<li>
<a href="#"> Item 1 sub</a>
<a href="#Item-1Biv">Item 1B iv</a>
<ul class="menu">
<li><a href="#">Item 1 subA</a></li>
<li><a href="#">Item 1 subB</a></li>
<li><a href="#Item-1Bivalpha">Item 1B iv alpha</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Item 1B</a></li>
<li><a href="#Item-1C">Item 1C</a></li>
</ul>
</li>
<li>
<a href="#Item-2">Item 2</a>
<ul class="menu">
<li><a href="#Item-2A">Item 2A</a></li>
<li><a href="#Item-2B">Item 2B</a></li>
</ul>
</li>
<li><a href="#Item-3">Item 3</a></li>
<li><a href="#Item-4">Item 4</a></li>
</ul>

---
Expand Down
59 changes: 34 additions & 25 deletions js/foundation.dropdownMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,29 +69,32 @@ class DropdownMenu {
hasTouch = 'ontouchstart' in window || (typeof window.ontouchstart !== 'undefined'),
parClass = 'is-dropdown-submenu-parent';

if (this.options.clickOpen || hasTouch) {
this.$menuItems.on('click.zf.dropdownmenu touchstart.zf.dropdownmenu', function(e) {
var $elem = $(e.target).parentsUntil('ul', `.${parClass}`),
hasSub = $elem.hasClass(parClass),
hasClicked = $elem.attr('data-is-click') === 'true',
$sub = $elem.children('.is-dropdown-submenu');
// used for onClick and in the keyboard handlers
var handleClickFn = function(e) {
var $elem = $(e.target).parentsUntil('ul', `.${parClass}`),
hasSub = $elem.hasClass(parClass),
hasClicked = $elem.attr('data-is-click') === 'true',
$sub = $elem.children('.is-dropdown-submenu');

if (hasSub) {
if (hasClicked) {
if (!_this.options.closeOnClick || (!_this.options.clickOpen && !hasTouch) || (_this.options.forceFollow && hasTouch)) { return; }
else {
e.stopImmediatePropagation();
e.preventDefault();
_this._hide($elem);
}
} else {
e.preventDefault();
if (hasSub) {
if (hasClicked) {
if (!_this.options.closeOnClick || (!_this.options.clickOpen && !hasTouch) || (_this.options.forceFollow && hasTouch)) { return; }
else {
e.stopImmediatePropagation();
_this._show($elem.children('.is-dropdown-submenu'));
$elem.add($elem.parentsUntil(_this.$element, `.${parClass}`)).attr('data-is-click', true);
e.preventDefault();
_this._hide($elem);
}
} else { return; }
});
} else {
e.preventDefault();
e.stopImmediatePropagation();
_this._show($elem.children('.is-dropdown-submenu'));
$elem.add($elem.parentsUntil(_this.$element, `.${parClass}`)).attr('data-is-click', true);
}
} else { return; }
};

if (this.options.clickOpen || hasTouch) {
this.$menuItems.on('click.zf.dropdownmenu touchstart.zf.dropdownmenu', handleClickFn);
}

if (!this.options.disableHover) {
Expand Down Expand Up @@ -135,36 +138,42 @@ class DropdownMenu {
});

var nextSibling = function() {
if (!$element.is(':last-child')) $nextElement.children('a:first').focus();
if (!$element.is(':last-child')) {
$nextElement.children('a:first').focus();
e.preventDefault();
}
}, prevSibling = function() {
$prevElement.children('a:first').focus();
e.preventDefault();
}, openSub = function() {
var $sub = $element.children('ul.is-dropdown-submenu');
if ($sub.length) {
_this._show($sub);
$element.find('li > a:first').focus();
e.preventDefault();
} else { return; }
}, closeSub = function() {
//if ($element.is(':first-child')) {
var close = $element.parent('ul').parent('li');
close.children('a:first').focus();
_this._hide(close);
close.children('a:first').focus();
_this._hide(close);
e.preventDefault();
//}
};
var functions = {
open: openSub,
close: function() {
_this._hide(_this.$element);
_this.$menuItems.find('a:first').focus(); // focus to first element
e.preventDefault();
},
handled: function() {
e.preventDefault();
e.stopImmediatePropagation();
}
};

if (isTab) {
if (_this.vertical) { // vertical menu
if (_this.$element.hasClass(_this.options.verticalClass)) { // vertical menu
if (_this.options.alignment === 'left') { // left aligned
$.extend(functions, {
down: nextSibling,
Expand Down
10 changes: 5 additions & 5 deletions scss/components/_dropdown-menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ $dropdownmenu-border-width: nth($dropdownmenu-border, 1);
}

&.opens-left {
.is-dropdown-submenu {
> .is-dropdown-submenu {
left: auto;
right: 100%;
}
}

&.opens-right {
.is-dropdown-submenu {
> .is-dropdown-submenu {
right: auto;
left: 100%;
}
Expand Down Expand Up @@ -164,11 +164,11 @@ $dropdownmenu-border-width: nth($dropdownmenu-border, 1);
margin-top: -2px;
}

&.opens-inner .is-dropdown-submenu {
&.opens-inner > .is-dropdown-submenu {
top: 100%;
}

&.opens-left .is-dropdown-submenu {
&.opens-left > .is-dropdown-submenu {
left: auto;
right: 100%;
}
Expand Down Expand Up @@ -202,7 +202,7 @@ $dropdownmenu-border-width: nth($dropdownmenu-border, 1);

// [TODO] Cut back specificity
// scss-lint:disable SelectorDepth
&:not(.js-dropdown-nohover) > .is-dropdown-submenu-parent:hover > &,
//&:not(.js-dropdown-nohover) > .is-dropdown-submenu-parent:hover > &, // why is this line needed? Opening is handled by JS and this causes some ugly flickering when the sub is re-positioned automatically...
&.js-dropdown-active {
display: block;
}
Expand Down
Loading