-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Styles are wrong when used inside a shadow root #2612
Comments
You can't use https://codesandbox.io/p/sandbox/react-native-web-shadow-dom-repro-forked-xf696q However, you shouldn't have to reach into the internals to do this, but the exported |
AppRegistry.runApplication uses `ReactDOM.createRoot` by default, but the `render` export uses the legacy `ReactDOM.render`. This patch makes those 2 APIs consistent. It also makes some adjustments to the `createSheet` internals to more reliably implement and test style sheet replication within ShadowRoot's and iframes. Fix #2612
The PR related to this issue is #2615 You can install https://codesandbox.io/p/sandbox/react-native-web-shadow-dom-repro-forked-86y3vd Please try it out in your app, and me know if you encounter any issues, including any issues outside of the shadow DOM. If everything looks good after you've audited your stuff, I can merge the PR and cut a main-line release. Thanks! |
Yep, that seems to work for me! Thanks a lot. I have one question though - it looks like if I define react Contexts outside of the ShadowDomWrapper, they're not available within. Is that to be expected? Is there any way to work around that (aside from re-declaring them all manually in ShadowDomWrapper)? Example: https://codesandbox.io/p/sandbox/react-native-web-shadow-dom-repro-forked-yn52hj |
Yeah that's expected because we're creating a completely new React root. If you want to avoid consuming and re-providing contexts, I think you could render the components into a portal: function ShadowDomWrapper({ children }) {
const shadowHost = useRef();
const shadowRoot = useRef();
const [portal, setPortal] = React.useState(null);
useEffect(() => {
if (shadowHost.current) {
if (!shadowRoot.current) {
shadowRoot.current = shadowHost.current.attachShadow({ mode: 'open' });
// Render dummy content into the shadow DOM for style sheet insertion
render(<div />, shadowRoot.current);
}
setPortal(createPortal(children, shadowRoot.current));
}
}, [children]);
return (
<div ref={shadowHost}>
{portal}
</div>
);
} https://codesandbox.io/p/sandbox/react-native-web-shadow-dom-repro-forked-cfddm3 There will be other differences between |
Is there an existing issue for this?
Describe the issue
I placed a react-native-web component in a shadow root, and the styles were wrong. Specifically, these components have the correct class names, but the style sheet seems to have not been loaded.
looks like
In the chrome debugger, its DOM element looks like:
however Chrome can't resolve any of these classes - only the host's stylesheet is being applied.
Expected behavior
Should look like
Steps to reproduce
Quickstart with Create React App
Obtain a ShadowDom react component (either react-shadow or roll your own).
I had the same issue with react-shadow (which was presumably written by someone who knows what they're doing!), but for the sake of this issue I'm using the following wrapper as it's a simpler repro - please let me know if I'm making a glaring mistake as I'm not an experienced web developer.
Test case
https://codesandbox.io/p/sandbox/react-native-web-shadow-dom-repro-5gy7vw
Additional comments
I saw some related issues, e.g. #1517 and it seems support for the shadow DOM was added with 0.18. It's very possible that I'm just missing a step, but there's no documentation for how to get this working! How does react-native-web provide the stylesheets to the root node of the app? How do I do this for the shadow root?
Why am I trying to do this?
I have a component library written in react-native, for cross-platform compatibility between our react-native app and our next.js app. When rendering components from the library in next.js, they are suddenly affected by CSS stylesheets from the next.js app, which can break the styling. The components are already styled with the
style
prop - I do not need external styling so I want to isolate them from the styling added by the website, hence the use of shadow dom. However, I seem to be losing the RNW styles too!The text was updated successfully, but these errors were encountered: