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

Commit bf63cef

Browse files
committed
fix(dropdown): fix display when using with append-to-body
- Change to add `dropdown` class and open class to the `<body>` element BREAKING CHANGE: This differs from the existing behavior in that it no longer will toggle based on the existing `dropdown` directive element, but on the `body` element instead Closes #4305 Fixes #4240
1 parent 49a0858 commit bf63cef

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/dropdown/dropdown.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
7474
setIsOpen = angular.noop,
7575
toggleInvoker = $attrs.onToggle ? $parse($attrs.onToggle) : angular.noop,
7676
appendToBody = false,
77-
keynavEnabled =false,
78-
selectedOption = null;
77+
keynavEnabled = false,
78+
selectedOption = null,
79+
body = $document.find('body');
7980

8081
this.init = function(element) {
8182
self.$element = element;
@@ -93,7 +94,8 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
9394
keynavEnabled = angular.isDefined($attrs.keyboardNav);
9495

9596
if (appendToBody && self.dropdownMenu) {
96-
$document.find('body').append(self.dropdownMenu);
97+
body.append(self.dropdownMenu);
98+
body.addClass('dropdown');
9799
element.on('$destroy', function handleDestroyEvent() {
98100
self.dropdownMenu.remove();
99101
});
@@ -184,7 +186,9 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
184186
self.dropdownMenu.css(css);
185187
}
186188

187-
$animate[isOpen ? 'addClass' : 'removeClass'](self.$element, openClass).then(function() {
189+
var openContainer = appendToBody ? body : self.$element;
190+
191+
$animate[isOpen ? 'addClass' : 'removeClass'](openContainer, openClass).then(function() {
188192
if (angular.isDefined(isOpen) && isOpen !== wasOpen) {
189193
toggleInvoker($scope, { open: !!isOpen });
190194
}

src/dropdown/test/dropdown.spec.js

+24-5
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,29 @@ describe('dropdownToggle', function() {
224224
expect($document.find('#dropdown-menu').parent()[0]).toBe($document.find('body')[0]);
225225
});
226226

227+
it('adds the dropdown class to the body', function() {
228+
expect($document.find('body').hasClass('dropdown')).toBe(true);
229+
});
230+
227231
it('removes the menu when the dropdown is removed', function() {
228232
element.remove();
229233
$rootScope.$digest();
230234
expect($document.find('#dropdown-menu').length).toEqual(0);
231235
});
236+
237+
it('toggles the open class on body', function() {
238+
var body = $document.find('body');
239+
240+
expect(body.hasClass('open')).toBe(false);
241+
242+
element.find('[dropdown-toggle]').click();
243+
244+
expect(body.hasClass('open')).toBe(true);
245+
246+
element.find('[dropdown-toggle]').click();
247+
248+
expect(body.hasClass('open')).toBe(false);
249+
});
232250
});
233251

234252
describe('integration with $location URL rewriting', function() {
@@ -437,11 +455,12 @@ describe('dropdownToggle', function() {
437455
it('should work with dropdown-append-to-body', function() {
438456
element = $compile('<li dropdown dropdown-append-to-body auto-close="outsideClick"><a href dropdown-toggle></a><ul class="dropdown-menu" id="dropdown-menu"><li><a href>Hello On Body</a></li></ul></li>')($rootScope);
439457
clickDropdownToggle();
440-
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
458+
var body = $document.find('body');
459+
expect(body.hasClass(dropdownConfig.openClass)).toBe(true);
441460
$document.find('#dropdown-menu').find('li').eq(0).trigger('click');
442-
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
461+
expect(body.hasClass(dropdownConfig.openClass)).toBe(true);
443462
$document.click();
444-
expect(element.hasClass(dropdownConfig.openClass)).toBe(false);
463+
expect(body.hasClass(dropdownConfig.openClass)).toBe(false);
445464
});
446465
});
447466

@@ -657,7 +676,7 @@ describe('dropdownToggle', function() {
657676

658677
triggerKeyDown(element, 40);
659678

660-
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
679+
expect($document.find('body').hasClass(dropdownConfig.openClass)).toBe(true);
661680
var focusEl = $document.find('ul').eq(0).find('a');
662681
expect(isFocused(focusEl)).toBe(true);
663682
});
@@ -667,7 +686,7 @@ describe('dropdownToggle', function() {
667686
triggerKeyDown(element, 40);
668687
triggerKeyDown(element, 40);
669688

670-
expect(element.hasClass(dropdownConfig.openClass)).toBe(true);
689+
expect($document.find('body').hasClass(dropdownConfig.openClass)).toBe(true);
671690
var elem1 = $document.find('ul');
672691
var elem2 = elem1.find('a');
673692
var focusEl = $document.find('ul').eq(0).find('a').eq(1);

0 commit comments

Comments
 (0)