Comments (4)
You can't use createRoot
directly from react-dom/client
, as that doesn't allow the library to insert the style sheet into the shadow DOM. You have to use the render
function from RNW
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 render
wasn't updated to use React 18's createRoot
. I also encountered a couple of other bugs related to this functionality that I'll fix in a patch. I'll post it here for you to try once it's up. Thanks!
from react-native-web.
The PR related to this issue is #2615
You can install 0.0.0-dee1467a3
to try it out - it's just the PR stacked on top of 0.19.9. Here's an updated code example:
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!
from react-native-web.
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
from react-native-web.
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 render
and createPortal
implementations, e.g., getting event targets propagating from within the shadow dom. React doesn't really support this use case very well, and events-from-within portals are a bit funky.
from react-native-web.
Related Issues (20)
- TextInput not forwarding type HOT 3
- Pressable triggers onPress twice, when activated via Enter key and set to disabled HOT 1
- Pointer event is not applied for grand-children
- Module build failed HOT 2
- `accessibilityRole="search"` should map to element `<search>`
- In webview Input box is not working As expected HOT 1
- react native bottom tab not found in react native web no render
- Breaks with libraries that use NativeModules, like react-native-randombytes HOT 3
- PanResponder is not working with scale/zoom
- No documentation about shadow* style props being deprecated HOT 2
- Wrong measureInWindow value while using transform
- Avoid react updates on each animation frame HOT 2
- react-native-web on webOS HOT 2
- Confusion with `@react-native/normalize-colors` vs. `@react-native/normalize-color` HOT 1
- ColumnWrapperStyle not supported for single column lists
- react-native-web Server API Support HOT 1
- Revert "Remove setNativeProps API" change HOT 4
- Text component inline style overrides global style [color] HOT 3
- Is 50ms press delay really necessary? Can't be configurable? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-web.