Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 2d831bc

Browse files
dolevdwesleycho
authored andcommitted
fix(dropdown): align position correctly with scrollbar
- Fix horizontal alignment independent of the presence of a vertical scrollbar Closes #6008 Fixes #5942
1 parent d846e2d commit 2d831bc

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

Diff for: src/dropdown/dropdown.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
195195
var pos = $position.positionElements($element, self.dropdownMenu, 'bottom-left', true),
196196
css,
197197
rightalign,
198-
scrollbarWidth;
198+
scrollbarPadding,
199+
scrollbarWidth = 0;
199200

200201
css = {
201202
top: pos.top + 'px',
@@ -208,7 +209,12 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
208209
css.right = 'auto';
209210
} else {
210211
css.left = 'auto';
211-
scrollbarWidth = $position.scrollbarWidth(true);
212+
scrollbarPadding = $position.scrollbarPadding(appendTo);
213+
214+
if (scrollbarPadding.heightOverflow && scrollbarPadding.scrollbarWidth) {
215+
scrollbarWidth = scrollbarPadding.scrollbarWidth;
216+
}
217+
212218
css.right = window.innerWidth - scrollbarWidth -
213219
(pos.left + $element.prop('offsetWidth')) + 'px';
214220
}

Diff for: src/dropdown/test/dropdown.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -702,4 +702,34 @@ describe('uib-dropdown', function() {
702702
});
703703
});
704704
});
705+
706+
// issue #5942
707+
describe('using dropdown-append-to-body with dropdown-menu-right class', function() {
708+
function dropdown() {
709+
return $compile('<li style="float: right;" uib-dropdown dropdown-append-to-body><a href uib-dropdown-toggle>Toggle menu</a><ul uib-dropdown-menu class="dropdown-menu-right" id="dropdown-menu"><li><a href>Hello On Body</a></li></ul></li>')($rootScope);
710+
}
711+
712+
beforeEach(function() {
713+
element = dropdown();
714+
$document.find('body').append(element);
715+
716+
var menu = $document.find('#dropdown-menu');
717+
menu.css('position', 'absolute');
718+
});
719+
720+
afterEach(function() {
721+
element.remove();
722+
});
723+
724+
it('should align the menu correctly when the body has no vertical scrollbar', function() {
725+
var toggle = element.find('[uib-dropdown-toggle]');
726+
var menu = $document.find('#dropdown-menu');
727+
toggle.trigger('click');
728+
729+
// Get the offsets of the rightmost position of both the toggle and the menu (offset from the left of the window)
730+
var toggleRight = Math.round(toggle.offset().left + toggle.outerWidth());
731+
var menuRight = Math.round(menu.offset().left + menu.outerWidth());
732+
expect(menuRight).toBe(toggleRight);
733+
});
734+
});
705735
});

0 commit comments

Comments
 (0)