Tailwind classes do not compile as expected when using CSS-Modules and :dark prefix #3204
-
Describe the problem:I was updating my website to utilize the tailwind-configuration darkMode: 'class' instead of darkMode: 'media' and noticed an unexpected behavior. In my project, I use TailwindCSS 2.0 and CSS-Modules. When given the following code inside a CSS-module .informationName {
@apply font-black;
@apply text-black;
@apply dark:text-white;
} compiles to the following code when using the darkMode: 'media' configuration .PortraitSection_informationName__1iAOK {
font-weight: 900;
--tw-text-opacity: 1;
color: rgba(0, 0, 0, var(--tw-text-opacity))
}
@media (prefers-color-scheme: dark) {
.PortraitSection_informationName__1iAOK {
--tw-text-opacity: 1;
color: rgba(255, 255, 255, var(--tw-text-opacity))
}
} In this case, the compiled CSS is exactly what one would expect. However with the darkMode: 'class' configuration the following CSS get's compiled .PortraitSection_informationName__1iAOK {
font-weight: 900;
--tw-text-opacity: 1;
color: rgba(0, 0, 0, var(--tw-text-opacity))
}
.PortraitSection_dark__34kkQ .PortraitSection_informationName__1iAOK {
--tw-text-opacity: 1;
color: rgba(255, 255, 255, var(--tw-text-opacity))
} I doubt that this was meant to happen since this breaks the darkMode: 'class' configuration completely when using CSS-Modules It's obvious that this problem is caused by using CSS-Modules but unless there is a way around it CSS-Modules and the darkMode: 'class' configuration are incompatible. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Yeah nothing we can really do about this on our end I'm afraid — CSS modules will automatically scope all CSS inside of it like you're seeing. Whatever solution you come up with here will probably be outside of the scope of Tailwind itself. I don't know how you can write a partially global/partically scoped selector with CSS modules in general, like if you wrote this by hand: .informationName {
font-weight: 900;
color: #000;
}
.dark .informationName {
color: #fff;
} ...the No plans to do anything in Tailwind to change this behavior but will transfer this to our discussions in case others want to chime in with ideas. |
Beta Was this translation helpful? Give feedback.
Yeah nothing we can really do about this on our end I'm afraid — CSS modules will automatically scope all CSS inside of it like you're seeing. Whatever solution you come up with here will probably be outside of the scope of Tailwind itself. I don't know how you can write a partially global/partically scoped selector with CSS modules in general, like if you wrote this by hand:
...the
.dark
class would still get mangled by CSS modules right? So I'm not sure if CSS modules is even compatible with the concept of a distant parent class modifier like this.No plans to do anything in Tailwind to…