GithubHelp home page GithubHelp logo

Comments (4)

necolas avatar necolas commented on June 10, 2024

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.

necolas avatar necolas commented on June 10, 2024

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.

chriscoomber avatar chriscoomber commented on June 10, 2024

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.

necolas avatar necolas commented on June 10, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.