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

Error when focus to element inside shadow DOM #11

Closed
trungtin opened this issue Jul 16, 2019 · 8 comments · Fixed by #15
Closed

Error when focus to element inside shadow DOM #11

trungtin opened this issue Jul 16, 2019 · 8 comments · Fixed by #15

Comments

@trungtin
Copy link

The error is here:

Failed to execute 'getComputedStyle' on 'Window'

reference: https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot/host
will take a look to create MR later. Help is welcome :D 💃

@theKashey
Copy link
Owner

Not sure what is the real issue and how to reproduce it.

@trungtin
Copy link
Author

here is the reproduce https://codesandbox.io/s/dawn-glitter-n5of7?fontsize=14

@trungtin trungtin changed the title Error when using with shadow DOM Error when focus to element inside shadow DOM Jul 16, 2019
@theKashey
Copy link
Owner

theKashey commented Jul 16, 2019

When going up and checking all parents one should check for the ShadowRoot

node instanceof ShadowRoot ? node.host : node.parentNode

Related - https://github.com/chartjs/Chart.js/pull/5585/files

@emplums
Copy link

emplums commented Sep 11, 2019

I'm dealing with this issue as well, though I'm pretty sure I'm not using the shadow DOM in my application. I'm getting these errors on the document nodes. I noticed that the conditional node === document here doesn't actually resolve to true even though it is the document node: https://github.com/theKashey/focus-lock/blob/master/src/utils/DOMutils.js#L16

image

So when the isVisible gets called on the document element, instead of returning true it tries to evaluate !isElementHidden(window.getComputedStyle(node, null)) an error is thrown because getComputed style expects an Element node and not a Document node.

Maybe a node.nodeType === 9 would work here? https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType

The thing that's so strange about this is that it was working before here: https://primer.style/components/docs/Dialog, in that environment the only usage of focus-lock is in @reach/dialog.

Once we added focus-lock to our project as well, it stopped working: https://primer-components-6nyrsxfvw.now.sh/components/Dialog

Could this have anything to do with competing focus-lock instances?

@theKashey
Copy link
Owner

The problem is with two different documents, and that’s something focus-lock has no powers upon.
However, then more intelligent patch for document detection, or just wrapping check in try/catch should be enough to fix the problem.

@shabailiu
Copy link

shabailiu commented Oct 17, 2019

I'm getting the same issue when using react-focus-on injected into an iframe. As it's traversing up, it's making this check on the document node of the iframe. This is only an issue if there is a focus-able element (like a button) as a child of FocusOn. Here is a demo: https://codesandbox.io/embed/interesting-northcutt-t9whq

@theKashey
Copy link
Owner

Updates has been published to all packages. Not sure if does it solve ShadowDOM issue, but document is now detected via nodeType property

@theKashey theKashey reopened this Oct 17, 2019
@shabailiu
Copy link

That was fast! Appreciate the fix, iframe detection works :)

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

Successfully merging a pull request may close this issue.

4 participants