Skip to content

Commit 7c490b2

Browse files
committed
Retain keyboard focus after closing the dropdown
Fixes angular-ui#46. The fix does two things. First, it actively sets focus back to the button after closing the dropdown. To do this, it uses $timeout with a zero timeout value to give angular time to show the button before changing focus to it. This is the same as was done previously when showing the dropdown. There is however a second problem: bootstrap defines a transition for form_control. This means that it takes some time before the button is shown. This means that when the timeout fires and focus() is called, the button is still hidden and thus cannot be focused. To work around the second problem, we can either use a slightly longer timeout (a few 100ms), or we can disable the transitions. I have chosen to disable the transitions, since the timeout feels "ugly". This is done in the patch to select.css.
1 parent a873eb0 commit 7c490b2

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/select.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@
5555
max-height: 200px;
5656
overflow-x: hidden;
5757
}
58+
59+
.ui-select-bootstrap .form-control {
60+
-webkit-transition: none;
61+
transition: none;
62+
}

src/select.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ angular.module('ui.select', [])
199199
if (ctrl.open) {
200200
_resetSearchInput();
201201
ctrl.open = false;
202+
203+
// Give the button element time to appear again before returning focus to it
204+
$timeout(function () {
205+
var _selectButton = $element.querySelectorAll("button.ui-select-match");
206+
_selectButton[0].focus();
207+
});
202208
}
203209
};
204210

0 commit comments

Comments
 (0)