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] Display sub menu on the left when if cannot fit screen #4211

Merged
merged 13 commits into from
Jul 11, 2017
26 changes: 26 additions & 0 deletions examples/components/menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,32 @@
<button class="ui button">Submit</button>
</div>
</div>
<div class="ui dropdown item">
Dropdown Left<i class="dropdown icon"></i>
<div class="menu">
<a class="item">Link</a>
<a class="item">Link</a>
<div class="divider"></div>
<div class="header">Header</div>
<div class="item">
<i class="dropdown icon"></i>
Sub Menu
<div class="menu">
<a class="item">Link</a>
<div class="item">
<i class="dropdown icon"></i>
Sub Sub Menu
<div class="menu">
<a class="item">Link</a>
<a class="item">Link</a>
</div>
</div>
<a class="item">Link</a>
</div>
</div>
<a class="item">Link</a>
</div>
</div>
<a class="item">Link</a>
</div>
</div>
Expand Down
44 changes: 43 additions & 1 deletion src/definitions/modules/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2374,6 +2374,12 @@ $.fn.dropdown = function(parameters) {
else {
module.set.upward($menu);
}
if(module.is.fitWidth($menu)) {
module.remove.leftward($menu);
}
else {
module.set.leftward($menu);
}
}
else if(settings.direction == 'upward') {
module.set.upward($menu);
Expand All @@ -2383,6 +2389,10 @@ $.fn.dropdown = function(parameters) {
var $element = $menu || $module;
$element.addClass(className.upward);
},
leftward: function($menu) {
var $element = $menu || $module;
$element.addClass(className.leftward);
},
value: function(value, text, $selected) {
var
escapedValue = module.escape.value(value),
Expand Down Expand Up @@ -2734,6 +2744,10 @@ $.fn.dropdown = function(parameters) {
var $element = $menu || $module;
$element.removeClass(className.upward);
},
leftward: function($menu) {
var $element = $menu || $module;
$element.removeClass(className.leftward);
},
visible: function() {
$module.removeClass(className.visible);
},
Expand Down Expand Up @@ -3096,6 +3110,28 @@ $.fn.dropdown = function(parameters) {
$currentMenu.removeClass(className.loading);
return canOpenDownward;
},
fitWidth: function($subMenu) {
var
$currentMenu = $subMenu || $menu,
canOpenRightward = true,
isOutsideScreen = false,
calculations
;
$currentMenu.addClass(className.loading);
calculations = {
contextWidth: $context.outerWidth(),
menuOffset: $currentMenu.offset().left,
menuWidth: $currentMenu.outerWidth()
};
isOutsideScreen = (calculations.contextWidth < calculations.menuOffset + calculations.menuWidth) || (calculations.menuOffset - $menu.offset().left < 0);

if(isOutsideScreen) {
module.verbose('Dropdown cannot fit in context rightward', isOutsideScreen);
canOpenRightward = false;
}
$currentMenu.removeClass(className.loading);
return canOpenRightward;
},
inObject: function(needle, object) {
var
found = false
Expand Down Expand Up @@ -3276,7 +3312,12 @@ $.fn.dropdown = function(parameters) {
onStart : start,
onComplete : function() {
if(settings.direction == 'auto') {
module.remove.upward($subMenu);
if ($currentMenu.hasClass(className.leftward)) {
module.remove.leftward($subMenu);
}
else {
module.remove.upward($subMenu);
}
}
callback.call(element);
}
Expand Down Expand Up @@ -3707,6 +3748,7 @@ $.fn.dropdown.settings = {
selected : 'selected',
selection : 'selection',
upward : 'upward',
leftward : 'left',
visible : 'visible'
}

Expand Down
1 change: 1 addition & 0 deletions src/definitions/modules/dropdown.less
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,7 @@ select.ui.dropdown {
/* Left Flyout Menu */
.ui.dropdown > .left.menu .menu,
.ui.dropdown .menu .left.menu {
display: block !important;
left: auto !important;
right: 100% !important;
border-radius: @leftSubMenuBorderRadius !important;
Expand Down