Giter Site home page Giter Site logo

Comments (7)

mitchheddles avatar mitchheddles commented on August 19, 2024 7

I had a similar issue, but got around it by following this thread.
I delay rendering of the children to avoid FOUC.

Demo:

import React from 'react';
import root from 'react-shadow';
// We need to use StyleSheetManager to catch and styled components and target them a node inside
// of the shadow DOM. Otherwise they will be on the document, therefore excluded.
import { StyleSheetManager } from 'styled-components';

// This component is used to determine if the shadow root children have been mounted,
// we can't assume they are mounted with the parent and using a timer is risky.
const DidMount = ({ onMount }) => {
  React.useEffect(onMount, []);

  return null;
};

export const MyShadowComponent = ({ children }) => {
  const node = React.useRef(null);
  const [mounted, setMounted] = React.useState(false);

  return (
    <root.div>
      <div ref={node}>
        <DidMount onMount={() => setMounted(true)} />
        {mounted && node.current && (
          <>
            <StyleSheetManager target={node.current}>
              <>{children}</>
            </StyleSheetManager>
          </>
        )}
      </div>
    </root.div>
  );
};

Usage:

<MyShadowComponent>
  <MyStyledComponent>Hello</MyStyledComponent>
</MyShadowComponent>

from reactshadow.

Wildhoney avatar Wildhoney commented on August 19, 2024

Sadly styled-components applies all of its stylesheets to the document root, which means they the styles won't pass through the shadow boundaries. At the moment they don't seem to provide a way to re-target the stylesheets.

from reactshadow.

xdumaine avatar xdumaine commented on August 19, 2024

I'm having the same issue with just using react inline styles, which also applies the stylesheet to the root.

from reactshadow.

Wildhoney avatar Wildhoney commented on August 19, 2024

I'm having the same issue with just using react inline styles, which also applies the stylesheet to the root.

Like this? As it seems okay.

from reactshadow.

xdumaine avatar xdumaine commented on August 19, 2024

Ahh sorry, we're using react-native-web with inline styles, so all inline styles get compiled to a single stylesheet appended to the document root, instead of truly inlined.

from reactshadow.

Wildhoney avatar Wildhoney commented on August 19, 2024

Sorry @xdumaine I'm not sure about the library. Maybe others will have an idea.

Awesome solution @mitchheddles – I've implemented that into the core module using your approach starting with v17.2.0. Any time you create a new shadow boundary now, it also creates a new StyleSheetManager context so styled-components should just work without any additional steps.

from reactshadow.

Wildhoney avatar Wildhoney commented on August 19, 2024

@xdumaine please raise a separate ticket if you haven’t yet found a solution πŸ‘

from reactshadow.

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.