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

sx prop does not correctly apply theme in nested ThemeProviders with ColorMode #1456

Closed
rezavation opened this issue Jan 21, 2021 · 9 comments · Fixed by #1812 or #1838
Closed

sx prop does not correctly apply theme in nested ThemeProviders with ColorMode #1456

rezavation opened this issue Jan 21, 2021 · 9 comments · Fixed by #1812 or #1838
Assignees

Comments

@rezavation
Copy link

rezavation commented Jan 21, 2021

Describe the bug

When having an application that has a nested ThemeProviders with the same theme and color modes but different values, toggling the colorMode changes the colorMode and theme in both the top level and nested theme provider, but when utilizing that theme in the nested component with the sx prop, the wrong colour gets applied.

To Reproduce
Here is a simple codesandbox that reproduces the issue. https://codesandbox.io/s/exciting-banzai-hrjom?file=/src/index.js
Expected behavior

In the example provided, I would expect the inner components background value for light mode to be "darkcyan", and it is if you print out the theme object. But when its applied to the div using sx={{ bg: "background" }} it applies "red".

Screenshot 2021-01-21 at 11 48 01

Kapture 2021-01-21 at 11 54 02

@hasparus
Copy link
Member

Hi rezaabedian 👋
Thanks for the issue!

@hasparus
Copy link
Member

Okay, currently only ColorModeProvider updates its inner emotion context with updated theme, and nested theme providers don't render ColorModeProvider.

image

We should split providing and consuming, and start respecting the color mode in @theme-ui/theme-provider.

@rezavation
Copy link
Author

rezavation commented Jan 22, 2021

Any ideas how we can fix it?

@atanasster
Copy link
Collaborator

Short of rewriting ColorModeProvider I dont see how to fix it. Hope I am missing something as it looks like a significant work, including avoid cross require and keep backward compatibility (to have one ColorModeProvider that can be inside or outside of (multiple) ThemeProvider).

@hasparus ?

@hasparus
Copy link
Member

@hasparus ?

We could split CollorModeProvider into actual React context provider and consumer?
Provide color mode in top level ThemeProvider (from theme-provider package, not core), consume in all ThemeProviders.

@hasparus
Copy link
Member

@atanasster @rezaabedian does one of you guys want to work on this?

@rezavation
Copy link
Author

Happy to try to help, but will probably need some assistance. @hasparus

@hasparus
Copy link
Member

hasparus commented Jan 22, 2021

@rezaabedian Sweet! Thanks! Of course, I'm here to help.

Here's ColorModeProvider: https://github.com/system-ui/theme-ui/blob/develop/packages/color-modes/src/index.tsx#L126-L153
And here's the ThemeProvider: https://github.com/system-ui/theme-ui/blob/develop/packages/theme-provider/src/index.ts#L44-L75

  • ColorModeProvider now both keeps the state and uses it to affect EmotionContext. I think these are two different responsibilities.
  • We'd like nested ThemeProviders respect color mode and affect EmotionContext, but we don't want them to have their own color modes (by default, as nested toggles wouldn't be comfortable).

contributing tips:

As the full repo build takes a while (I'm working on it, but not so trivial) you build only the packages you're changing with

yarn workspace @theme-ui/css prepare

There's just one Jest project in the entire repo, and you run a subset of tests like this:

yarn test css

@rezavation
Copy link
Author

Hi @hasparus , I didn't get an opportunity to look at this yet. Is there any chance one of the existing maintainers of theme-ui will be able to tackle it?

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