question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Color modes not using CSS custom properties

See original GitHub issue

Looking at the code I see that to set the color mode, it relies on the emotion theme provider:

https://github.com/system-ui/theme-ui/blob/6d9e29369a76c2802a1bb8253efaef072a087fbc/packages/color-modes/src/index.tsx#L111-L117

This generates unnecessary class names in order to change the color even though all the custom props are in DOM:

CleanShot 2020-12-30 at 19 29 59@2x

I’ve build a quick experiment where I’m relying on changing only the className of the body element and it works as expected without any additional re-rendering caused by emotion.

I’m wondering if the current behavior is intentional and what’s the motivation to rely on have JS runtime for changing color mode?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:32 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
atanasstercommented, Jan 6, 2021

@okonet - got and i believe your issue would be fixed by #1327 - where the css custom variables are only updated on the body tag. This way the individual components styles should not change and emotion should not create new classes on color mode change.

we would really appreciate if you can try it out and let us know, i haven’t specifically done the tests that you have

1reaction
okonetcommented, Jan 6, 2021

@atanasster hmm, let me try the latest beta. I’m on the latest stable version: 3.5.0 and this is still an issue.

I do understand why you’d like to rely on the React context here, but I also am confused why is there the need to pre-render all CSS custom properties then. That’s why I raised this question. To me having them in DOM should eliminate the need to rely on the context but it looks like I’m mistaken?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using CSS custom properties (variables) - MDN Web Docs
This example is just like the last one, except we use a custom property. As expected, the browser substitutes the value of --text-color...
Read more >
CSS Custom Properties and Theming
More than one color and property. It's not only colors that can change, a Custom Property can be any valid value. Here's Keith...
Read more >
A Strategy Guide To CSS Custom Properties
If --user-theme-color is not set, the value of #d33a2c will be used. This way, we don't need to provide a fallback every time...
Read more >
I Finally Implemented Dark Mode Using CSS Custom ...
I replaced the majority of color , background-color , and border-color properties with CSS custom properties. And, finally, I added the ability ...
Read more >
Two options for using custom properties - QuirksBlog
We discussed CSS custom properties, and something interesting happened. ... the colour values in the component styles without using custom ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found