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

Dropdown closes on option selection when used in shadow DOM #278

Closed
3 tasks done
silvester-pari opened this issue May 21, 2024 · 4 comments · Fixed by #279
Closed
3 tasks done

Dropdown closes on option selection when used in shadow DOM #278

silvester-pari opened this issue May 21, 2024 · 4 comments · Fixed by #279
Labels
help wanted Extra attention is needed

Comments

@silvester-pari
Copy link
Contributor

Describe the bug

When using this library inside a shadow DOM (e.g. inside a web component), the behavior of the dropdown is different compared to when used in the "normal" DOM. When selecting an option, the dropdown disappears.

Reproduction

Here I created two custom elements, one without shadow, one with shadow DOM. Note the difference when selecting a selection option.

https://stackblitz.com/edit/vitejs-vite-czjnxx?file=filter.js

Expectation

Behavior of the dropdown should ideally be consistent and should work the same inside and outside the shadow DOM.

Environment Info

System:
    OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 4.61 GB / 15.38 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.18.2 - ~/.nvm/versions/node/v18.18.2/bin/node
    Yarn: 1.22.21 - ~/.nvm/versions/node/v18.18.2/bin/yarn
    npm: 9.8.1 - ~/.nvm/versions/node/v18.18.2/bin/npm
  Browsers:
    Chrome: 124.0.6367.201

Validations

Copy link

stackblitz bot commented May 21, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@ghiscoding ghiscoding added the help wanted Extra attention is needed label May 21, 2024
@ghiscoding
Copy link
Owner

ghiscoding commented May 21, 2024

I never work with Shadow DOM, if you know how to fix then please provide a Pull Request because I will most probably not look at fixing this myself. All the code is in MultipleSelectInstance.ts

I also assume that you meant (but forgot to mention) that the problem is for multiple selection? Because a single selection will of course close the drop right after selecting

@silvester-pari
Copy link
Contributor Author

After some digging, it seems that the target in

(e.target === this.dropElm || (findParent(e.target, '.ms-drop') !== this.dropElm && e.target !== this.elm)) &&

behaves differently than with non-shadow DOM. When used inside a shadow DOM, the true event.target is masked and the parent element is returned instead. It is recommended to use event.composedPath()[0] instead of event.target in this case.

See also this Stack Overflow question.

@ghiscoding I'll try creating a PR for this.

@ghiscoding
Copy link
Owner

providing a PR would be best, I also have E2E tests (with Playwright), so if your change is working then all tests should pass in the PR. Just make sure that your solution works for both Shadow and non-Shadow DOM. Thanks

ghiscoding pushed a commit that referenced this issue May 23, 2024
* fix: use `composedPath` instead of `target`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants