From 2d831bcc2aa3b8224770792e16fd9fc0e295718d Mon Sep 17 00:00:00 2001 From: Dolev Dori Date: Wed, 15 Jun 2016 02:02:27 +0300 Subject: [PATCH] fix(dropdown): align position correctly with scrollbar - Fix horizontal alignment independent of the presence of a vertical scrollbar Closes #6008 Fixes #5942 --- src/dropdown/dropdown.js | 10 ++++++++-- src/dropdown/test/dropdown.spec.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/dropdown/dropdown.js b/src/dropdown/dropdown.js index 97ce5f10d6..ab60a2db33 100644 --- a/src/dropdown/dropdown.js +++ b/src/dropdown/dropdown.js @@ -195,7 +195,8 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position']) var pos = $position.positionElements($element, self.dropdownMenu, 'bottom-left', true), css, rightalign, - scrollbarWidth; + scrollbarPadding, + scrollbarWidth = 0; css = { top: pos.top + 'px', @@ -208,7 +209,12 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position']) css.right = 'auto'; } else { css.left = 'auto'; - scrollbarWidth = $position.scrollbarWidth(true); + scrollbarPadding = $position.scrollbarPadding(appendTo); + + if (scrollbarPadding.heightOverflow && scrollbarPadding.scrollbarWidth) { + scrollbarWidth = scrollbarPadding.scrollbarWidth; + } + css.right = window.innerWidth - scrollbarWidth - (pos.left + $element.prop('offsetWidth')) + 'px'; } diff --git a/src/dropdown/test/dropdown.spec.js b/src/dropdown/test/dropdown.spec.js index 3d836d5cbb..7a932f2c37 100644 --- a/src/dropdown/test/dropdown.spec.js +++ b/src/dropdown/test/dropdown.spec.js @@ -702,4 +702,34 @@ describe('uib-dropdown', function() { }); }); }); + + // issue #5942 + describe('using dropdown-append-to-body with dropdown-menu-right class', function() { + function dropdown() { + return $compile('
  • Toggle menu
  • ')($rootScope); + } + + beforeEach(function() { + element = dropdown(); + $document.find('body').append(element); + + var menu = $document.find('#dropdown-menu'); + menu.css('position', 'absolute'); + }); + + afterEach(function() { + element.remove(); + }); + + it('should align the menu correctly when the body has no vertical scrollbar', function() { + var toggle = element.find('[uib-dropdown-toggle]'); + var menu = $document.find('#dropdown-menu'); + toggle.trigger('click'); + + // Get the offsets of the rightmost position of both the toggle and the menu (offset from the left of the window) + var toggleRight = Math.round(toggle.offset().left + toggle.outerWidth()); + var menuRight = Math.round(menu.offset().left + menu.outerWidth()); + expect(menuRight).toBe(toggleRight); + }); + }); });