-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autofocus doesn't work in modals #938
Comments
Hmm, indeed. At this point I would assume that this is an issue on our side that will be fixed. But it needs investigation as at this point I'm not sure what is going on. Thnx for reporting! |
So it seems that my naive implementation can't really work as the Given the above the only real solution is to query for elements with |
BTW: I'm marking it as a feature since Bootstrap 4 doesn't support it. |
You're right, Chrome only autofocuses the first element. Safari seems to autofocus any newly-created element - not sure why this wouldn't work with modals, but it doesn't. Even so, this doesn't help, because browsers are behaving inconsistently. Would a PR help with this? If so, any tips on where to hook this in? |
Has there been any development on this? I'm running into this problem as well |
Solution to this is rather simple. Your own focus directive. I even do not understand why this directive is not default in Angular core. http://plnkr.co/edit/2euC1MDHup7x4ShJcXQl?p=preview
|
@laserus |
@kohoutjosef you mean you want to fix the scrollTop? |
It most likely is done because the new modal first is created at bottom, and when styles applied it becomes overlay. So it scrolls to position of input at the moment of creation of modal, but just before it changes position to absolute? It is just wild guess, I do not know the reason, probably related: But issue is not with directive itself but how the focus working. |
yes the directive is OK but the problem is its application onto modal. |
a little bit hack, but functional (i hope so):
i would like to a better solution |
@kohoutjosef Less hacky might be to use a setTimeout, wrapped around the @pkozlowski-opensource Using Note: My issue is not related to autofocus, but calling focus() in general inside a modal. I wasn't sure if this comment would be better here or at #1776 |
I'm having a similar issue. I set |
Based on https://getbootstrap.com/docs/4.1/components/modal/:
Given the above I wonder if we should work-around it here. The "solution" would basically boil down to:
@benouat since you've been working on focus in modals - what is your opinion on this? |
For anything related to focus like that I would recommend to use the zone for that. /!\ disclaimer, this might be scary code :) this.ngZone.onStable.asObservable().pipe(take(1)).subscribe(() => {
const element = this.element.nativeElement.querySelector('[autofocus]');
if (element) {
element.focus();
}
}); |
@benouat the provided workaround doesn't works only the first time the modal is open. const elem: any = this.element.nativeElement.querySelector('[autofocus]');
if (elem) {
elem.focus();
} |
As I mentioned:
So to me, the idea here remains the same as @pkozlowski-opensource described in his last comment, you have to create a directive that will do:
|
Closing in favour of #2728 |
Feature description:
I've got an input in my modal. I'd like it to autofocus when the modal opens. I've added the
autofocus
attribute and this works the first time the modal is opened in Google Chrome, but not subsequently. It doesn't seem to work at all in Safari. I haven't tested other browsers.As a workaround, I've created an attribute directive that focuses the input during
ngAfterViewInit
, but this seems like using a sledge hammer to crack a nut (and it could create unexpected behaviour elsewhere.)Link to minimally-working plunker that reproduces the issue:
http://plnkr.co/edit/At2FNwl9lKGF3sk2gTbt?p=preview
Version of Angular, ng-bootstrap, and Bootstrap:
Angular: v2.0.1
ng-bootstrap: v1.0.0-alpha.5
Bootstrap: v4.0.0-alpha.4
The text was updated successfully, but these errors were encountered: