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

Enable specifying elements which can be focused #11686

Conversation

dana-shalev
Copy link

A modal dialog does not enable any element which is not a descendant of the dialog to recieve focus.
This makes a lot of sense, of course but it also creates undesriable behavior in extreme cases where controls have elements which are direct descendants of the body element.
For example, drop down elements are usually descendants of the body element and therefore does not behave nicely inside dialogs.

Related issues (drop down inside bootstrap modal dialog does not recieve focus):

I found many discussions on this issue and no final solution.
However, I did find that the same problem existed in JQuery dialog and it solution (described here: https://github.com/ivaynberg/select2/issues/1246).

The idea is that the dialog exposes a method which enables the client to specify whether the element can recieve focus or not.

My change does exactly that: added an allowFocus method which is called from the enforceFocus method. If allowFocus returns true focus will not be enforced by the dialog.

The usage for enabling select2 dropdown to recieve focus is as follows:

$.fn.modal.Constructor.prototype.allowFocus = function (e) {
    return !!$(e.target).closest('.select2-drop');
};

In a modal dialog enable specifying element which can receive focus even
though they are not descendants of the modal dialog. This is a very
common use case with drop down components.
@cvrebert
Copy link
Collaborator

cvrebert commented Dec 3, 2013

The changes fail jshint; see the Travis log.

@fat
Copy link
Member

fat commented Dec 25, 2013

that is undefined in your pr. also you can just turn the enforce focus off by setting Modal.prototype.enforceFocus to a noop

@fat fat closed this Dec 25, 2013
@dana-shalev
Copy link
Author

I don't think this will solve the problem since i do want to enforce focus for all element except a specific one.
As you can see from the references to the issues i added this is a very common problem.

@mkurz
Copy link

mkurz commented Jan 2, 2014

I think this is quite a good approach for fixing the problems mentioned above.
Any chance this will be merged?

@dana-shalev Could you please update the pull request so that.allowFocus becomes this.allowFocus?
Like this:

if (this.$element[0] !== e.target && !this.$element.has(e.target).length && !this.allowFocus(e)) {
    this.$element.focus()
}

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

Successfully merging this pull request may close these issues.

4 participants