Skip to content
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

Hopefully fixes trigger focus restoration on modal close #13627

Merged
merged 1 commit into from
Jun 6, 2014

Conversation

cvrebert
Copy link
Collaborator

Fixes #12364? I admit this is not my forte, but this explanation for the cause of the bug seems to make sense, and the new unit tests pass, so...

  • register the callback before invoking the toggling
  • listen for hidden instead of hide
  • account for the possibility of the associated show event being preventDefault-ed

Adds relevant unit tests.

To: @fat for review

* register the callback before invoking the toggling
* listen for `hidden` instead of `hide`
* account for the possibility of the associated `show` event being preventDefault-ed

Adds relevant unit tests.
@cvrebert cvrebert added the js label May 19, 2014
@cvrebert cvrebert added this to the v3.2.1 milestone May 19, 2014
@cvrebert cvrebert modified the milestones: v3.2.0, v3.2.1 May 19, 2014
$target.one('hide.bs.modal', function () {
$this.is(':visible') && $this.trigger('focus')
$target.one('show.bs.modal', function (showEvent) {
if (showEvent.isDefaultPrevented()) return // only register focus restorer if modal will actually get shown
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't hidden never get called if shown was prevented?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean show or shown?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problematic scenario is:

  1. I click a button to trigger a modal
  2. The show.bs.modal event gets prevented by some code, so the modal doesn't get shown.
  3. I click a different button that triggers the same modal.
  4. The modal gets successfully shown.

Now when I close the modal, we want the latter button to get refocused (and it'd be best not to have the focus jump around multiple times unnecessarily). Hence, this if statement.

@fat
Copy link
Member

fat commented May 22, 2014

unit tests are great, thanks chris 😍

@cvrebert
Copy link
Collaborator Author

cvrebert commented Jun 5, 2014

@fat So, do the explanations I posted make sense?

@fat
Copy link
Member

fat commented Jun 6, 2014

yep, lgtm - thanks!

cvrebert added a commit that referenced this pull request Jun 6, 2014
Hopefully fixes trigger focus restoration on modal close
@cvrebert cvrebert merged commit 3cbbc70 into master Jun 6, 2014
@cvrebert
Copy link
Collaborator Author

cvrebert commented Jun 6, 2014

Thanks for reviewing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

On close, modals should return focus to launching element
2 participants