-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Do you render the Container below or above the ThemeWrapper in the React tree? (I deleted the issue, because it was basically a duplicate. If we find that this is truly a bug, we can create an issue describing actionable steps forward.) |
Beta Was this translation helpful? Give feedback.
-
So both the ThemeWrapper and Container are used in the host app. The Button works, so the issue should not be connected to Module Federation. Weird. Would you be able to provide a reproduction on CodeSandbox or a GitHub repo? |
Beta Was this translation helpful? Give feedback.
-
Hey @daenash, I've spent some time fiddling with this issue, and unfortunately I can only offer you a workaround and my reasoning about the cause of the problem. So, Theme UI uses React Context, and depends on Emotion that uses React Context. React Contexts are effectively singletons, they won't work as expected if you have a consumer at one version and a provider from another. I added some more properties to the theme theme={{
colors: { "almost-white": "#f0f0f0" },
space: { large: "100px" },
breakpoints: ["900px", "1000px", "1100px"],
}} Then, I changed the contents of Container.js into this /** @jsxImportSource @emotion/react */
import { css, useThemeUI } from "theme-ui";
const Container = ({ children }) => {
const theme = useThemeUI();
const styles = css({
background: "almost-white",
p: "large",
width: ["500px", "600px", "700px", "900px"],
margin: "auto",
})(theme);
console.log({ theme, styles });
return <div css={styles}>{children}</div>;
}; And I noticed that if imported manually, we access the theme we want, and the styles are transformed successfuly. Using Box component without import { Box } from "theme-ui";
const Container = ({ children }) => {
return (
<Box
sx={{
background: "almost-white",
p: "large",
width: ["500px", "600px", "700px", "900px"],
margin: "auto",
}}
>
{children}
</Box>
);
};
export default Container; Both Theme UI's /** @jsxImportSource theme-ui */
const Container = ({ children }) => {
return (
<div
css={(theme) => {
console.log(theme, "in css");
return {};
}}
sx={(theme) => {
console.log(theme, "in sx");
return {
background: "almost-white",
p: "large",
width: ["500px", "600px", "700px", "900px"],
margin: "auto",
};
}}
>
{children}
</div>
);
}; After logging both themes to console, we can see that the theme from perspective of Compare this to results of similar code using Box without import { Box } from "theme-ui";
const Container = ({ children }) => {
return (
<Box
css={(theme) => {
console.log(theme, "in css");
return {};
}}
sx={(theme) => {
console.log(theme, "in sx");
return {
background: "almost-white",
p: "large",
width: ["500px", "600px", "700px", "900px"],
margin: "auto",
};
}}
>
{children}
</Box>
);
}; ⬆️ That's our theme object, right? I tried to enforce using single version of some of the libraries involved in modulefederation.config.js, but it didn't help 😢 // modulefederation.config.js
const { dependencies } = require("./package.json");
module.exports = {
name: "host",
remotes: {
remote: "remote@http://localhost:3001/remoteEntry.js",
},
shared: {
...dependencies,
react: {
singleton: true,
requiredVersion: dependencies["react"],
},
"react-dom": {
singleton: true,
requiredVersion: dependencies["react-dom"],
},
"@emotion/react": {
singleton: true,
requiredVersion: dependencies["@emotion/react"],
},
"theme-ui": {
singleton: true,
requiredVersion: dependencies["theme-ui"],
},
"@theme-ui/css": {
singleton: true,
},
"@theme-ui/core": {
singleton: true,
},
"@theme-ui/parse-props": {
singleton: true,
},
"@emotion/react/jsx-dev-runtime": {
singleton: true,
},
},
}; I'm sorry I couldn't help you more, but I'm far from a module federation expert, and why the reasons why TLDR: Avoid |
Beta Was this translation helpful? Give feedback.
Hey @daenash, I've spent some time fiddling with this issue, and unfortunately I can only offer you a workaround and my reasoning about the cause of the problem.
So, Theme UI uses React Context, and depends on Emotion that uses React Context. React Contexts are effectively singletons, they won't work as expected if you have a consumer at one version and a provider from another.
I added some more properties to the theme
Then, I changed the contents of Container.js into this