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

Select2 inside ShadowRoot Broke In 4.0.11 #5682

Closed
pm-michael opened this issue Oct 28, 2019 · 7 comments
Closed

Select2 inside ShadowRoot Broke In 4.0.11 #5682

pm-michael opened this issue Oct 28, 2019 · 7 comments
Assignees
Milestone

Comments

@pm-michael
Copy link

#5584 breaks the usage of Select2 inside a ShadowRoot, because $.contains() returns false, even though the Node is actually connected to the document.

I'd propose changing the following code:

    if ($.contains(document.body, $offsetParent[0])) {
      parentOffset = $offsetParent.offset();
    }

To this:

    if ($offsetParent[0].isConnected) {
      parentOffset = $offsetParent.offset();
    }
@lazka
Copy link

lazka commented Oct 28, 2019

Since 4.0.11 (4.0.10 works) I also get wrong offsets for the popup:

select2-bug

This is also inside a Shadow DOM.

@kevin-brown
Copy link
Member

Probably going to switch this to

    if ($.contains(document.body, $offsetParent[0]) || $offsetParent[0].isConnected) {
      parentOffset = $offsetParent.offset();
    }

Just to handle IE/Edge/older browsers when they aren't using the shadow DOM (because let's be honest, they won't be using the shadow DOM if they're old enough to not support isConnected).

@paxnovem
Copy link
Member

I have made the change locally, looking into how to get proper test coverage for this

@knlshnd
Copy link

knlshnd commented Jan 7, 2021

I am still not able to get it working inside the shadowDOM. I am trying to use it inside an angular element. It gives me an error: Please add Select2 library (js file) to the project. I am using view encapsulation in this. I do not know how to use dropdownParent properly, so have not tried that

@lazka
Copy link

lazka commented Jan 7, 2021

I have it working with a dropdownParent referencing an element in the shadow dom, if that helps.

@knlshnd
Copy link

knlshnd commented Jan 7, 2021

Can you give me a reference as to how exactly to do it?

@sergotail
Copy link

sergotail commented May 7, 2022

Hi, issue still persists for multiple shadow DOM subtrees having select2 elements. I have a custom element, and i initialize select2 for elements inside shadow DOM, in connectedCallback. And when i add second, third and so on custom elements, all select2 forms nested in existing custom elements are become broken.
My version of select2 is 4.0.13.

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

No branches or pull requests

6 participants