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

Modal focus #2273

Closed
wants to merge 1 commit into from
Closed

Modal focus #2273

wants to merge 1 commit into from

Conversation

tmmueller
Copy link

This is based on pull request #1892 from @mlilli. I rebased and squashed it as @pkozlowski-opensource requested. I also made it re-apply focus to the first autofocus element so that it works if you open the modal more than once.

@mlilli
Copy link
Contributor

mlilli commented May 28, 2014

There is no need to refocus on the auto-focused element since it is already
natively focused. Doing so via JavaScript is not only redundant, but it
causes issues in iOS - autofocusing opens the onscreen keyboard, then by
(re)focusing the element via JS, the keyboard closes defeating the original
purpose of the autofocus.

Please use my pull request as I coded it.

Thanks,

-- Mike

On Wed, May 28, 2014 at 11:08 AM, tmmueller [email protected]:

This is based on pull request #1892https://github.com/angular-ui/bootstrap/pull/1892from
@mlilli https://github.com/mlilli. I rebased and squashed it as
@pkozlowski-opensource https://github.com/pkozlowski-opensourcerequested. I also made it re-apply focus to the first autofocus element so

that it works if you open the modal more than once.

You can merge this Pull Request by running

git pull https://github.com/tmmueller/bootstrap modal-focus

Or view, comment on, or merge it at:

#2273
Commit Summary

  • fix(modal): re-apply autofocus on correct element after showing modal

File Changes

Patch Links:


Reply to this email directly or view it on GitHubhttps://github.com//pull/2273
.

@tmmueller
Copy link
Author

Unfortunately, at least in Chrome and according to MDN, autofocus gets applied only when the page is loaded. So if you open the modal more than once without reloading the page, you have to apply focus manually.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#attr-autofocus

@tmmueller
Copy link
Author

@mlilli: Does this work in iOS? In Chrome, document.activeElement is the button I clicked to open the modal, so it doesn't make any difference.

var autofocusElements = element[0].querySelectorAll('[autofocus]');
if (autofocusElements.length) {
  var autofocusElement = autofocusElements[0];
  if (autofocusElement != document.activeElement) {
    autofocusElement.focus();
  }
} else {
  element[0].focus();
}

@chrisirhc chrisirhc closed this in e62ab94 Jul 8, 2014
jurassic-c pushed a commit to jurassic-c/bootstrap that referenced this pull request Aug 6, 2014
Auto-focusing of a freshly-opened modal element causes any child
elements with the autofocus attribute to loose focus. This is an issue
on touch based devices which will show and then hide the onscreen
keyboard. Attempts to refocus the autofocus element via JavaScript will
not reopen the onscreen keyboard. Fixed by updated the focusing logic to
only autofocus the modal element if the modal does not contain an
autofocus element.

Fixes angular-ui#1696
Closes angular-ui#1892
Closes angular-ui#2273
@sskirby
Copy link

sskirby commented Sep 5, 2014

I don't think this issue is closed. The autofocus attribute only works the first time the modal is opened (in Chrome anyways)

@tmmueller
Copy link
Author

@sskirby Agreed. My pull request fixed that issue, but the accepted one did not. Maybe open a new issue?

@wilg
Copy link

wilg commented Sep 30, 2014

I'm running into this too – did this get fixed anywhere?

OronNadiv pushed a commit to lanetix/bootstrap that referenced this pull request Nov 18, 2014
Auto-focusing of a freshly-opened modal element causes any child
elements with the autofocus attribute to loose focus. This is an issue
on touch based devices which will show and then hide the onscreen
keyboard. Attempts to refocus the autofocus element via JavaScript will
not reopen the onscreen keyboard. Fixed by updated the focusing logic to
only autofocus the modal element if the modal does not contain an
autofocus element.

Fixes angular-ui#1696
Closes angular-ui#1892
Closes angular-ui#2273
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants