Skip to content
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

CSS generation not working with Emotion cache enabled #3071

Closed
goldmont opened this issue Jul 6, 2023 · 3 comments
Closed

CSS generation not working with Emotion cache enabled #3071

goldmont opened this issue Jul 6, 2023 · 3 comments

Comments

@goldmont
Copy link

goldmont commented Jul 6, 2023

Issue description

I'm using react-select library in a shadow DOM.

In my tests I used the following code (no customizations):

const shadowrootCache = createCache({
	key: 'shadowroot-cache',
	container: domElement,
        prepend: true
});

const Root = () => (
	<CacheProvider value={shadowrootCache}>
		<Select
	                options={[
		                { label: 'Test', value: 'test' },
		                { label: 'Test 2', value: 'test-2' },
		                { label: 'Test 3', value: 'test-3' }
	                ]}
                />
	</CacheProvider>
);

ReactDOM.render(<Root/>, domElement);

By the way, the layout is totally broken because apparently Emotion is unable to output the CSS. The class names are set in the class attributes but the classes themselves don't exist.

image

Here is a CodeSandbox reproducing the issue.

Here is another CodeSandbox not using shadow DOM and thus working as expected.

Hoping in a prompt reply. Thanks.

@goldmont
Copy link
Author

goldmont commented Jul 6, 2023

UPDATE:

Apparently there is a problem during CacheProvider first load. After many tests, I had to create my own CacheProvider which renders children only on second rerender.

Here is the code:

CustomEmotionCacheProvider.tsx file:

import { FunctionComponent, useEffect, useState } from 'react';
import { CacheProvider, EmotionCache } from '@emotion/react';

interface Props {
	cache: EmotionCache;
}

const CustomEmotionCacheProvider: FunctionComponent<Props> = ({ cache, children }) => {

	const [ isFirstRender, setIsFirstRender ] = useState(true);

	useEffect(() => setIsFirstRender(false), []);

	return (
		<CacheProvider value={cache}>
			{!isFirstRender && children}
		</CacheProvider>
	);
};

export default CustomEmotionCacheProvider;

index.tsx file:

const shadowrootCache = createCache({
	key: 'shadowroot-cache',
	container: domElement,
        prepend: true
});

const Root = () => (
	<CustomEmotionCacheProvider cache={shadowrootCache}>
		<Select
	                options={[
		                { label: 'Test', value: 'test' },
		                { label: 'Test 2', value: 'test-2' },
		                { label: 'Test 3', value: 'test-3' }
	                ]}
                />
	</CustomEmotionCacheProvider>
);

ReactDOM.render(<Root/>, domElement);

Broken CodeSandbox example using shadow DOM: here
Working CodeSandbox example using shadow DOM: here

@steveoni
Copy link

steveoni commented Jul 8, 2023

@goldmont do you know how this could work for IFrame. using import Frame, { FrameContextConsumer } from 'react-frame-component';

e.g

//App.js

import CreatableSelect from 'react-select/creatable';

function App() => {

return  <CreatableSelect
              options={options}
              value={mainTopic}
              onChange={(newValue) =>{}}
            />
}
//content.js
import React from 'react';
import ReactDOM from 'react-dom';
import Frame, { FrameContextConsumer } from 'react-frame-component';
import App from "./App";


function Main() {
  return (
    <Frame head={[<link type="text/css" rel="stylesheet" href={chrome.runtime.getURL("/static/css/content.css")} ></link>]}>
      <FrameContextConsumer>
        {
          ({ document, window }) => {
            return <App document={document} window={window} isExt={true} />
          }
        }
      </FrameContextConsumer>
    </Frame>
  )
}

fredrikpe added a commit to navikt/mulighetsrommet that referenced this issue Nov 3, 2023
react-select css forsvinner når man bruker shadowDom element. Dette
fikser det.

se f. eks JedWatson/react-select#3680 og
emotion-js/emotion#3071 (comment) for mer info
fredrikpe added a commit to navikt/mulighetsrommet that referenced this issue Nov 3, 2023
react-select css forsvinner når man bruker shadowDom element. Dette
fikser det.

se f. eks JedWatson/react-select#3680 og
emotion-js/emotion#3071 (comment) for mer info
@alexdanilowicz
Copy link

alexdanilowicz commented Feb 2, 2024

This was marked as completed but where is the relevant PR in the emotion-js repo?

I ask because running into something very similar with: chakra-ui/chakra-ui#8241

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants