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

Commit cce0097

Browse files
committed
feat(dropdown): focus toggle on close from click
- Focus the toggle element when the click causes the dropdown menu to close BREAKING CHANGE: This changes the focus behavior of the dropdown slightly, and potentially may break code built around current usage Closes #5934 Fixes #5782
1 parent 23b91d9 commit cce0097

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/dropdown/dropdown.js

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ angular.module('ui.bootstrap.dropdown', ['ui.bootstrap.position'])
5050
}
5151

5252
openScope.isOpen = false;
53+
openScope.focusToggleElement();
5354

5455
if (!$rootScope.$$phase) {
5556
openScope.$apply();

src/dropdown/test/dropdown.spec.js

+27
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,28 @@ describe('uib-dropdown', function() {
220220

221221
beforeEach(function() {
222222
element = dropdown();
223+
$document.find('body').append(element);
224+
});
225+
226+
afterEach(function() {
227+
element.remove();
223228
});
224229

225230
it('adds the menu to the body', function() {
226231
expect($document.find('#dropdown-menu').parent()[0]).toBe($document.find('body')[0]);
227232
});
228233

234+
it('focuses the dropdown element on close', function() {
235+
var toggle = element.find('[uib-dropdown-toggle]');
236+
var menu = $document.find('#dropdown-menu a');
237+
toggle.trigger('click');
238+
menu.focus();
239+
240+
menu.trigger('click');
241+
242+
expect(document.activeElement).toBe(toggle[0]);
243+
});
244+
229245
it('removes the menu when the dropdown is removed', function() {
230246
element.remove();
231247
$rootScope.$digest();
@@ -268,6 +284,17 @@ describe('uib-dropdown', function() {
268284
expect(container).not.toHaveClass('uib-dropdown-open');
269285
});
270286

287+
it('focuses the dropdown element on close', function() {
288+
var toggle = element.find('[uib-dropdown-toggle]');
289+
var menu = $document.find('#dropdown-menu a');
290+
toggle.trigger('click');
291+
menu.focus();
292+
293+
menu.trigger('click');
294+
295+
expect(document.activeElement).toBe(toggle[0]);
296+
});
297+
271298
it('removes the menu when the dropdown is removed', function() {
272299
element.remove();
273300
$rootScope.$digest();

0 commit comments

Comments
 (0)