ui icon indicating copy to clipboard operation
ui copied to clipboard

.dark section not included in tailwind output

Open tonyxiao opened this issue 1 year ago • 4 comments

This is probably not a shadcn problem but I could really use some help figuring it out. For some reason my dark mode does not work because the css variables .dark in global.css that shadcn uses never get included in the final css output of tailwind. If I manually add it to the className of html element then it works, but of course that doesn't work because it prevents switching. Anyone got idea why tailwind would exclude .dark section from output even though the tailwind.config.js uses darkMode: class?

tonyxiao avatar May 31 '23 21:05 tonyxiao

@tonyxiao I had the same problem. Putting the CSS variable definition out of @layer worked. My understanding is that these definitions don't have to be inside @layer .

From

@layer base {
  :root ...
  .dark ...
}

To

:root ...
.dark ...

dninomiya avatar Jun 01 '23 01:06 dninomiya

This is definitely caused by Tailwind CSS's stupid purge engine!

You can fix it by safelist.

https://tailwindcss.com/docs/content-configuration#safelisting-classes

module.exports = {
  darkMode: ["class"],
  content: ["app/**/*.{ts,tsx}", "components/**/*.{ts,tsx}"],
  safelist: ["dark"],
  theme: {
    // ...
  },
  plugins: [require("tailwindcss-animate")],
}

mogeko avatar Jun 04 '23 05:06 mogeko

Great workaround, thanks @mogeko . Do you happen to know why this may happen?

tonyxiao avatar Jun 05 '23 18:06 tonyxiao

Of course! @tonyxiao,

Tailwind CSS will automatically delete those unused CSS to keep the package thin. However, this should not happen when you set darkMode: "class" and use dark variant in your app. I don't know why it happened to you.

I have encountered the same problem for unknown reason before, I used safelist to prevent Tailwind from deleting my code. I hope this can help you.

mogeko avatar Jun 05 '23 19:06 mogeko

I encountered the same problem, and fixed it by moving the CSS out of the layer declaration. As far as I can tell, https://ui.shadcn.com/ uses the CSS variables approach as well, and inspecting the website shows the dark class in the generated CSS. I would be interested in knowing what we are doing differently.

piet-maier avatar Oct 09 '23 11:10 piet-maier

I fixed it by removing darkMode: "class" from the tailwind config.

bigxalx avatar Nov 30 '23 17:11 bigxalx

Doing that actually breaks more things, this is the correct way to fix this

taep96 avatar Nov 30 '23 17:11 taep96

Thanks for the comment, but your referenced solution does not fix it for me. Only thing working so far is what I described above.

bigxalx avatar Nov 30 '23 18:11 bigxalx

This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you.

shadcn avatar Jun 30 '24 23:06 shadcn