Cannot focus on input element of modal form after using uib-dropdown
Bug description:
In brief, triggering a modal form after using a uib-dropdown on the page makes it impossible to focus on the form input.
Try the following in the provided plunker:
-
Click button (A) to activate modal form.
-
Click on the input & it focuses
-
Click the uib-dropdown button 'Select' (no need to select menu button (B))
-
Click button (A) again. It is not possible to focus on the input
Link to minimally-working plunker that reproduces the issue:
https://plnkr.co/edit/Aa8zqYgrqd5o0a1yy4EU?p=preview
Version of Angular, UIBS, and Bootstrap
Angular: 1.5.8
UIBS: 2.3.1
Bootstrap: 3.3.7
Other comments
It used to work in UIBS 2.2.0.
Also experiencing the same problem in UIBS 2.3.1, works in 2.2.0.
same experience. v2.3.0 works and 2.3.1 breaks.
assume this is related https://github.com/angular-ui/bootstrap/pull/6295 by @ajpz https://github.com/angular-ui/bootstrap/commit/8a4f625
Same here. After opening the uib-dropdown, inputs lose their focus on mouseup. Input does not to be necessarily on a modal. Any input on same page or even after routing to another page does not accept focus after opening the uib-dropdown. Angular: 1.5.9 angular-bootstrap: 2.3.1
Same problem. Also happens on Getting Started Page: https://angular-ui.github.io/bootstrap/#/dropdown
Drop down element always get focused.
I had the same problem, and after spending half of my working day I found reason. Please look at the source code of dropdown https://github.com/angular-ui/bootstrap/blob/master/src/dropdown/dropdown.js and you can find focusToggleElement function which is invoked untidy when you use uib-dropdown-toggle directive on <a> tag which has href attribute. To workaround this bug just remove href attriibute from link.
This piece of code works for me:
<div uib-dropdown>
<a uib-dropdown-toggle>Open menu</a>
<ul uib-dropdown-menu >
<li>...</li>
</ul>
</div>
Bug for me happened even when focus on a input inside modal so I think you issue is separate
On Wed, Dec 14, 2016, 8:29 AM Andrzej Dybionka [email protected] wrote:
I had the same problem, and after spending half of my working day I found reason. Please look at the source code of dropdown https://github.com/angular-ui/bootstrap/blob/master/src/dropdown/dropdown.js and you can find focusToggleElementfunction which is invoked untidy when you use uib-dropdown-toggle directive on tag which has hrefattribute. To workaround this bug just remove href attriibute from link.
This piece of code works for me:
Open menu
- ...
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular-ui/bootstrap/issues/6364#issuecomment-267033714, or mute the thread https://github.com/notifications/unsubscribe-auth/AAh7gl_1Yirym_zoOmbDeUSEz3oEzKNBks5rH-9CgaJpZM4LLljW .
It is the same reason: focusToggleElement. Just focusToggleElement is invoked in some circumstances but should't. I didn't analyze dropdown menu code deeply, but I found case when focusToggleElement is not invoked when as element with directive uib-dropdown-menu you use <a> tag without href attribute (what of course is invalid html). To test it replace this piece of code:
<button type="button" class="btn btn-primary dropdown-toggle" uib-dropdown-toggle>
<span translate>Select</span>
<span class="caret"></span>
</button>
by this
<a class="btn btn-primary dropdown-toggle" uib-dropdown-toggle>
<span translate>Select</span>
<span class="caret"></span>
</a>
here https://plnkr.co/edit/Aa8zqYgrqd5o0a1yy4EU?p=preview
Of course this is only workaround and bug should be fixed by authors of ui-bootstrap.
@adybionka spot on! Looks like the culprit is in dropdown.js on line 108. The closeDropdown is being invoked on document.click so it focuses the toggleElement (if focusable) every time someone clicks.
I confirm this issue happening on 2.3.1 and not on 2.3.0. Also as @adybionka mentioned, removing the href attribute from the a tags stops the issue from happening on 2.3.1.
Removing the href attribute only works because then the element cannot be focused. The issue still persists with buttons.
This seems to be a duplicate of #6357
Another workaround of this problem is just not use uib-dropdown-toggle to open dropdown menu. focusToggleElement method is only invoked when is a element with uib-dropdown-toggle directive, which is optional. Instead of this, to open dropdown menu just use is-open attribute:
<div uib-dropdown is-open="isOpenedDropdownMenu">
<button ng-click="toggleDropdown($event)">Open dropdown</button>
<ul uib-dropdown-menu>
<li>...</li>
</ul>
</div>
And in controller:
$scope.toggleDropdown = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.isOpenedDropdownMenu = !$scope.isOpenedDropdownMenu;
};
It looks like the root of the problem was identified by @vukers: https://github.com/angular-ui/bootstrap/commit/c824731ae8fed3d56bedaaf88fc8e86a4224e96d#commitcomment-20174042
@mattlewis92 has a pull request with the fix: #6363
seems like #6321 related too (text input focus bug)
Yeah, we are experiencing the same issue. We were developing on 2.3.0 and our deployment today went out with 2.3.1 causing widespread breakages for our drop downs.
e07f9fd appeared to resolve the issue for me.
Looking through the list of issues, these all seem like duplicates: #6321 #6357 #6372
Same issue with 2.3.1 and angular 1.5.9. Reversing to 2.3.0 solve issue. Waiting for fix
I still have to click input fields twice in order to get the focus to move there, once to close the dropdown and again to shift focus (regardless of whether the input is in a modal or not).
(Angular 1.5.10, UIBS 2.4.0)
The same behavior as @mold described for Angular 1.5.7 and UIBS 2.3.0
Dropdown always grabs focus for itself when toggle happens.