ui icon indicating copy to clipboard operation
ui copied to clipboard

Simpler dark mode setup

Open xixixao opened this issue 2 years ago • 6 comments

Might be worth mentioning that with CSS variables you can get simple dark mode by replacing

.dark {

in the global CSS file (perhaps app/tailwind.css)

with

@media (prefers-color-scheme: dark) {
    :root {

and add the second closing brace below the variable names.

This doesn't require any React and works with server rendering (if you do want to upgrade to having a theme switcher, revert this change).

xixixao avatar Aug 06 '23 04:08 xixixao

Hi @xixixao, the .dark class is used to make it work with TailwindCSS since the darkMode is set to class in the tailwind.config.js. If you want you change it to media and use the snippet you provided instead of the class-based one.

dan5py avatar Aug 08 '23 19:08 dan5py

@dan5py Since I couldn't find any description for darkMode: ['class'] in the documentation, are the only two options class and media for darkMode?

RulerChen avatar Aug 09 '23 11:08 RulerChen

are the only two options class and media for darkMode?

Yes, you can check the TailwindCSS Docs for further info.

dan5py avatar Aug 09 '23 11:08 dan5py

Hey @dan5py, you only need to change the tailwind config’s “darkMode” if you’re using the dark: modifier, whereas I was suggesting an approach with CSS variables.

xixixao avatar Aug 09 '23 14:08 xixixao

In that way every component would change when you change the color scheme. Using the dark: selector lets you decide whether a component should have dark mode or not.

dan5py avatar Aug 09 '23 15:08 dan5py

Agree this is absolutely worth documenting on the site in the dark mode section, would have saved a lot of time to find this earlier. Perfect for people doing SSR or SSG because there will be no flicker as opposed to a fully JS impl that only runs on the front end

ivangreene avatar Feb 03 '24 23:02 ivangreene

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 12 '24 23:06 shadcn