Implementing Dark Mode (Part 2)
Tags: opensource, javascript, css, react
Published: 2021-12-09 15:36:38

This is Part 2 of the series on Implementing Dark Mode. This was done in [open-sauced/open-sauced#1020](https://github.com/open-sauced/open-sauced/pull/1020), and handily demonstrated to me the wealth of learning opportunities in contributing to Open Source. I for one have learned a ton! On this one, I got the opportunity for learning in several areas. I was still pretty new to React (and I still am), so I had not yet used the [Context API](https://reactjs.org/docs/context.html).  For a lot of the same reasons, I hadn't used the [styled-components](https://styled-components.com/) library before. 

Throughout the rest of these points, the thing to bear in mind is that for the most part, the app being in dark mode just means that the HTML `body` element has a CSS class name including "dark". 

One implementation detail that I feel was a win was that the only React component that had any kind of interaction with the ThemeContext was a set of buttons that toggle the theme.  I like to think this helps with separation of concerns. Here's a code snippet from the buttons component:
```jsx
import React, {useContext} from "react";
import ThemeContext from "../ThemeContext";
import {FlexCenter} from "../styles/Grid";
import darkMode from "../images/darkMode.svg";
import lightMode from "../images/lightMode.svg";
import themeAuto from "../images/themeAuto.svg";
function ThemeButtonGroup() {
  const [theme, setTheme] = useContext(ThemeContext);
  return (
    <FlexCenter style={{marginRight:"0.5rem"}}>
      <a
        style={{margin:"0 .5rem"}}
        disabled={theme === "dark"}
        onClick={(event) => {
          event.preventDefault();
          setTheme("dark");
        }}>
        <img
          src={darkMode} 
          alt="dark mode" 
          style={{
            backgroundColor:(theme === "dark") 
            ? "#ccc" 
            : "transparent"
          }}/>
      </a>
// ...
    </FlexCenter>
  );
}
```
Another implementation detail was related to coloring of images. Open Sauced makes use of many SVG images, of differing flavors. In the cases where SVG files are in the static assets of Open Sauced (e.g. `<img alt="open sauced" className="svg" src={mortarBoard} />`), the coloring of these is controlled using the `filter` CSS property. On the other hand, we also make use of [@primer/octicons-react](https://github.com/primer/octicons). 

Here's a sample of one of these icons in component code:
```jsx
import {SearchIcon} from "@primer/octicons-react";
// ...
<SearchIcon size="large" verticalAlign="middle" className="svg" />
```
These inject code directly into markup as `<svg>...</svg>`, requiring use of CSS property `fill`.
Finally here's the CSS code snippet where the `<img>` and `<svg>` tags are handled differently.
```css
body.dark img.svg {
  filter: invert();
}
body.dark svg.svg {
  fill: var(--lightestGrey);
}
```
I referred quite a bit to this article: [Color Control of SVGs](https://medium.com/@union_io/swapping-fill-color-on-image-tag-svgs-using-css-filters-fa4818bf7ec6). 

One last fun implementation detail was working with our use of [react-loading-skeleton](https://github.com/dvtng/react-loading-skeleton) (I love this effect, and I feel it really does work in keeping the user engaged and under the impression of the app working while data loads).   To make this effect still work well in dark mode, I took the opportunity to crack open the source, and replicate a few key values as found in this snippet of our CSS.
```scss
body.dark .react-loading-skeleton {
  background-color: var(--backgroundGrey);
  background-image: linear-gradient(
    90deg,
    var(--backgroundGrey),
    var(--grey),
    var(--backgroundGrey)
  );
}
```

Again, working on this PR really cemented my personal belief and experience that contributing to Open Source Software can provide amazing opportunities for learning by doing!