dimsemenov / Magnific-Popup

Light and responsive lightbox script with focus on performance.
http://dimsemenov.com/plugins/magnific-popup/
MIT License
11.38k stars 3.49k forks source link

problem when using jquery.select2 in a popup #280

Closed clime closed 10 years ago

clime commented 10 years ago

Hey,

when using jquery.select2 inside a popup, you can't type into the select2's searchbox. There seems to be some kind of focus/blur loop. I have described the issue in more detail here: http://stackoverflow.com/questions/19990191/select2-in-magnific-popup-search-input-cannot-be-focused. Here is jsfiddle that shows the problem: http://jsfiddle.net/clime/qweWa/15/. I have tracked the problem to the following code in magnific popup:

        setTimeout(function() {

            if(mfp.content) {
                mfp._addClassToMFP(READY_CLASS);
                _setFocus();
            } else {
                // if content is not defined (not loaded e.t.c) we add class only for BG
                mfp.bgOverlay.addClass(READY_CLASS);
            }

            // Trap the focus in popup
            _document.on('focusin' + EVENT_NS, function (e) {
                if( e.target !== mfp.wrap[0] && !$.contains(mfp.wrap[0], e.target) ) {
                    _setFocus();
                    return false;
                }
            });

        }, 16);

_setFocus is called indefinietly when select2 is clicked on in a popup.

dimsemenov commented 10 years ago

Ok, so the problem is that select2 "dropdown" input field is added directly to the body, instead of inside current select2 DOM element. Which makes popup think that it's outside of it.

magnific popup responsive jquery lightbox plugin 2013-11-15 11-04-55

When tab focus changes, the popup checks if the new focused element is inside the popup. If it is outside, popup sets focus back to the root element of popup. This is done to "lock" the tab focus in popup.

I don't think that disabling such feature or removing "focus" from select2 is a good idea, so I'd recommend to override _onFocusIn event handler and add an additional check:

$.magnificPopup.instance._onFocusIn = function(e) {
          // Do nothing if target element is select2 input
          if( $(e.target).hasClass('select2-input') ) {
            return true;
          } 
          // Else call parent method
          $.magnificPopup.proto._onFocusIn.call(this,e);
};

The updated fiddle: http://jsfiddle.net/qweWa/21/

Important note, I've added function _onFocusIn in 0.9.9 update, so make sure that latest version of popup JS file is used. https://rawgithub.com/dimsemenov/Magnific-Popup/master/dist/jquery.magnific-popup.js

dimsemenov commented 10 years ago

If you'll find any issues let me know...

clime commented 10 years ago

It works well. Thanks.

nestordeharo commented 10 years ago

Great!...that solution solves my painful problem...

s0w4 commented 10 years ago

Небольшая опечатка: image

JamoCA commented 8 years ago

The sample code should be updated for the new Select2 v4.x className.

$.magnificPopup.instance._onFocusIn = function(e) {
    // Do nothing if target element is select2 input
    if( $(e.target).hasClass('select2-search__field') ) {
        return true;
    } 
    // Else call parent method
    $.magnificPopup.proto._onFocusIn.call(this,e);
};

A more future-proof solution may involve searching for "select2" anywhere in the class attribute.

if( $(e.target).attr('class').indexOf('select2') >= 0 ) {
ketancmarix commented 7 years ago

It works. Thanks

swapnil-spyryox commented 6 years ago

Simply add following code

$(function () { $('.class-name').magnificPopup({ callbacks: { open: function(){ $('.mfp-wrap').css("overflow", "initial") $('.mfp-wrap').removeAttr('tabindex') }, }, }); });

chachapara commented 8 months ago

If you'll find any issues let me know...

bro in popup video in video link is not working when open the link video is not play