How to add dark mode to your website

How to add dark mode to your website

Following the trend of dark mode, I thought this would be a good challenge to create my first library and help developers add dark mode themes to their websites.

At Remote, specially in our blog, we thought it would be nice to add dark mode and so we implemented an initial version. The problem was that our initial implementation didn’t fit all our future use cases and because we’ve updated the project in Gatsby to use the new version of react, our implementation became stale.

Instead of taking time to implement something specifically for Gatsby and worrying about SSR, we decided to find and use a library that would take care of that for us. If you want to know where we’re using our time at Remote, check https://blog.remote.com/solving-global-employment-through-remote/ 😛.

But to use this technique in all React-based websites and using the Context API that allows us to access the state easily in all components was not possible . That’s were I’ve found a good niche to contribute.

So I built a library based on the Context API and React hooks which allows you to easily implement dark mode.

https://github.com/luisgserrano/react-dark-mode

Continue reading for some examples.

Accessing state inside React components

As I said, this library uses the Context API so you should have at least React 16.3.0.

Changing the logo src based on the theme

To help you out with styling your app, we detect if the user has requested the system to use a light or dark color theme (by using the prefers-color-scheme CSS media feature) and set this theme as a class of the body tag like <body class="dark">.

By having this, you can create different styles when the body tag as the css class dark or light.

Since we’re using the Context API you should add our provider on top of your top level component so that all the child components have access to this state.

You can use the context in any child component of the <Provider> and this context exposes the mode variable, which is the theme that can be light or dark and the toggleMode function which toggles the theme. When the mode changes, this component will re-render.

  import Provider, { useDarkmodeContext } from 'react-use-dark-mode';

  // Images
  import BlackLogo from './blackLogo.svg';
  import WhiteLogo from './WhiteLogo.svg';

  const Header = () => {
    const { mode, toggleMode } = useDarkmodeContext();

    function getLogo(theme) {
      return theme === "light" ? BlackLogo : WhiteLogo;
    }

    return (
      <header>
        <img src={getLogo(mode)} alt="Logo" />
        <button type="button" onClick={toggleMode}>Toggle darkmode</button>
      </header>
    );
  };

  ReactDOM.render(
    <Provider>
      <Header />
    </Provider>,
    document.getElementById('header')
  );

Using CSS variables

  :root {
    --textPrimaryColor: #000;
    --backgroundPrimaryColor: #fff;
  }

  body {
    color: var(--textPrimaryColor);
    background-color: var(--backgroundPrimaryColor);
  }

  body.dark {
    --textPrimaryColor: #fff;
    --backgroundPrimaryColor: #000;
  }

  .block__element {
    color: var(--textPrimaryColor);
  }

As you can see, you only need to add the CSS based on the theme and add a button to toggle the theme.

Conclusion

The benefit of this library is if you need to change something that you can’t with CSS and you’re using React, like in the example above where we had to change the src of an image based on the theme, you can now easily subscribe to the theme change and apply your custom behaviour.

Overall, this has been an excellent experience to start in the world of open source, the feeling that someone is using your work and you’re helping someone building products with it is awesome.