Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Bugfix/inside shadow dom #65

Merged
merged 3 commits into from
Mar 19, 2021

Conversation

dsappet
Copy link
Contributor

@dsappet dsappet commented Jan 29, 2021

Fixes #64

If an event is fired from within a component with shadow DOM and listened to outside (like document or window), the event.target will not pierce the shadow DOM and show as the top level component. For example, listening to a click event on the document in the following situation (if foo-bar had a shadow dom)

<foo-bar>
  <duet-date-picker></duet-date-picker>
</foo-bar>

will have foo-bar as the event target! Therefore the condition if (this.dialogWrapperNode.contains(target) || this.datePickerButton.contains(target)) { will not be met because target always returns a parent node!

Enter composedPath to the rescue. https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath
It returns an array of nodes and will contain shadow dom nodes as long as it is open.
I realized that composedPath is not supported in IE (IE doesn't support shadow dom at all), so if composedPath is available it will be used, otherwise it will fallback to target.

@WickyNilliams
Copy link
Contributor

Hey sorry about the delay on this!

I've been doing some testing, and it turns out that stencil polyfills composedPath for older browsers, so no need to feature test.

In which case I think the logic can be:

const isClickOutside = e
  .composedPath()
  .every(node => node !== this.dialogWrapperNode && node !== this.datePickerButton)

if (isClickOutside) {
  this.hide(false)
}

@dsappet
Copy link
Contributor Author

dsappet commented Mar 13, 2021

@WickyNilliams Updated per your suggestion.

@WickyNilliams
Copy link
Contributor

@dsappet awesome, thanks. I'll get this merged and released next week

@WickyNilliams WickyNilliams merged commit d94377b into duetds:master Mar 19, 2021
@WickyNilliams
Copy link
Contributor

Released in v1.3.0

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

Successfully merging this pull request may close these issues.

Interactions do not work within a shadow DOM
2 participants