{"id":355683,"date":"2021-11-11T07:28:04","date_gmt":"2021-11-11T15:28:04","guid":{"rendered":"https:\/\/css-tricks.com\/?p=355683"},"modified":"2021-11-11T08:43:30","modified_gmt":"2021-11-11T16:43:30","slug":"easy-dark-mode-and-multiple-color-themes-in-react","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/easy-dark-mode-and-multiple-color-themes-in-react\/","title":{"rendered":"Easy Dark Mode (and Multiple Color Themes!) in React"},"content":{"rendered":"\n
I was working on a large React application for a startup, and aside from just wanting some good strategies to keep our styles organized, I wanted to give this whole \u201cdark mode\u201d thing a shot. With the huge ecosystem around React, you might think that there would be a go-to solution for style themes, but a little web searching shows that really isn\u2019t the case.<\/p>\n\n\n\n
There are plenty of different options out there, but many of them tie into very specific CSS strategies, like using CSS Modules, some form of CSS-in-JS, etc. I also found tools specific to certain frameworks, like Gatsby, but not a generic React project. What I was looking for was a basic system that\u2019s easy to set up and work with without jumping through a ton of hoops; something fast, something easy to get a whole team of front-end and full-stack developers onboarded with quickly.<\/p>\n\n\n\n\n\n\n\n
The existing solution I liked the best centered around using CSS variables and data attributes<\/strong>, found in this StackOverflow answer<\/a>. But that also relied on some Fortunately, there is. By combining that general CSS variable strategy with the beautiful <\/a> Let\u2019s pick this up at a very good place to start<\/a>: the beginning.<\/p>\n\n\n\n This guide assumes a basic familiarity with CSS, JavaScript, and React.<\/p><\/blockquote>\n\n\n\n First, make sure you have a recent version of Node and npm installed. Then navigate to whatever folder you want your project to live in, run Swap out Now we\u2019ll open up our brand new project in a code editor. I\u2019m using VS Code for this example, and if you are too, then you can run these commands:<\/p>\n\n\n\n Running And, finally, go ahead and install the use-local-storage package with:<\/p>\n\n\n\n And that\u2019s it for the initial setup of the project!<\/p>\n\n\n Open the useRef<\/code> stuff that felt hack-y. As they say in every infomercial ever, there\u2019s got to be a better way!<\/em><\/p>\n\n\n\nuseLocalStorage<\/a><\/code> hook, we have a powerful, easy-to-use theming system. I\u2019m going to walk through setting this thing up and running it, starting from a brand new React app. And if you stick around to the end, I also show you how to integrate it with react-scoped-css<\/a>, which is what makes this my absolutely preferred way to work with CSS in React.<\/p>\n\n\nProject setup<\/h3>\n\n\n
git bash<\/code> there (or your preferred command line tool), then run:<\/p>\n\n\n\nnpx create-react-app easy-react-themes --template typescript<\/code><\/pre>\n\n\n\neasy-react-themes<\/code> with the name of your project, and feel free to leave off the --template typescript<\/code> if you\u2019d rather work in JavaScript. I happen to like TypeScript but it genuinely makes no difference for this guide, other than files ending in .ts\/.tsx vs .js\/.jsx.<\/p>\n\n\n\ncd easy-react-themes\ncode .<\/code><\/pre>\n\n\n\n
npm start<\/code> next starts your development server, and produces this in a new browser window:<\/p>\n\n\n\n
<\/figure>\n\n\n\nnpm i use-local-storage<\/code><\/pre>\n\n\n\nCode setup<\/h3>\n\n\n
App.tsx<\/code> file and get rid of the stuff we don\u2019t need.<\/p>\n\n\n\n