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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
FocusScope not working when used inside shadowRoot #1472
Comments
Shadow DOM support is definitely something we're interested in. I'm sure quite a few things are broken at the moment beyond just FocusScope. If you're interested in contributing, that would be amazing! A few other places I can think of that will need work:
There are probably others as well. In general, is it safe to assume that we can traverse into a shadow root though? What about closed shadow roots? Is it ok to not support those you think? Or assume that the closed shadow roots will forward focus internally as necessary, for example? |
I love this project, I will contribute to adding support for shadow DOM. Any pointers on what should I keep in mind while working on it? |
Thank you! I think just the things @devongovett mentioned in the previous comment to start. Otherwise, make sure to check out the contribution page on our docs site https://react-spectrum.adobe.com/contribute.html |
Ok sure, I will check the contribution guide and work on the initial RFC. |
I would love to use this fix as well. I too need this for implementing micro-frontends in our application. Is there any pending work that needs an extra helping hand? |
If you want to have a look at it, that'd be a great help! We don't have any current work in the team going on for it and I don't think we've heard from the other people in this thread in a long while. |
@theomessin
If this is something you would be interested in, we would be open to discussing how you think a shadow dom solution in React Aria would work before committing any individual patches. |
I ran into the same issue where keyboard focus doesn't work when an app that uses |
@snowystinger I know that the scope of I'm wondering if a similar approach could be followed, but maybe using context instead (similar to Just an idea - I don't know my way around this codebase, so maybe there are problems with following a similar pattern? |
@snowystinger Shouldn't this be easier now that we already support dynamic iframe starting next release ? I guess the change needed will be mostly in |
Possibly? I do not know much about ShadowDOM or ShadowRoot. Here's a list of things we identified as potentially problematic with iframes. I'm not sure if it holds for ShadowDom as well. The biggest help for support would be unit tests demonstrating real life use cases and expected behaviors. Or an example minimal app. Something so we can discuss the approach here. If it's just FocusScope, we can also determine how much would be required to support it more easily this way. |
Thanks for the list. This is very helpful. We will be soon implementing react-aria inside shadow DOM so we will be able to look into this more. One good starting point will be to support iframes for all react aria hooks. It will make sure that the document extraction logic is at the same place, i.e Do you see any problem in replacing all instances of |
馃悰 Bug Report
When used inside a
shadowRoot
theFocusScope
currently has some issues:document.activeElement
will refer to the parent node of the shadowRoot the currently focused element is in. The activeElement needs to be determined recursively traversing (open) shadowRoots (document.activeElement.shadowRoot.activeElement.shadowRoot.activeElement......). This leads to invalid detection whether an element is in scope and also restores focus wrongly.e.target
in focus/blur events is referring to the parent node of the shadowRoot of the focused element.e.composedPath()[0]
can be used to determine the actually focused element inside the custom element. ('composedPath' in e ? e.composedPath()[0] : e.target
)TypeError: Cannot read property 'focus' of null
in theonBlur
ofuseFocusContainment
due toe.target
being null inside the rAF (not sure this is really related to custom element / shadowRoot or a general bugI patched this in our libs using
patch-package
so I'm happy to file a PR if you'd like to support custom elements. I'm not sure how this affects other packages of the spectrum ecosystem and thus wanted to check in prior to opening a PR.馃 Expected Behavior
FocusRing
can be used with custom elements / shadowRoots馃槸 Current Behavior
See Bug Report above
馃拋 Possible Solution
document.activeElement
useactiveElement()
e.target
use'composedPath' in e ? e.composedPath()[0] : e.target
馃敠 Context
In our Microfrontend Framework we encapsulate modules inside custom elements. To escape them we use a
portal-root
custom element in the body where all modals will be rendered into. Inside this we useFocusScope
to contain focus.馃捇 Code Sample
https://codesandbox.io/s/vigilant-hofstadter-3wf4i?file=/src/index.js
馃實 Your Environment
馃Б Your Company/Team
Undisclosed
馃暦 Tracking Issue (optional)
n/a
The text was updated successfully, but these errors were encountered: