| 1 | # 1. Use CSS custom properties for night mode |
| 2 | |
| 3 | Date: 2024-01-29 |
| 4 | |
| 5 | ## Status |
| 6 | |
| 7 | Accepted. |
| 8 | |
| 9 | ## Context |
| 10 | |
| 11 | During the Night Mode Project, there was considerable debate as to how to |
| 12 | technically achieve night mode given the ecosystem and infrastructure |
| 13 | complexities of the Wikimedia ecosystem. |
| 14 | |
| 15 | ### Considered approaches |
| 16 | |
| 17 | **Use the existing DarkMode gadget** - This approach relies on using a CSS |
| 18 | invert "hack" to invert all the colors on the rendered page and change them from |
| 19 | light to dark. This was the initially proposed strategy, but it was |
| 20 | later dropped for the following reasons: |
| 21 | |
| 22 | - **Semantic color distortion**: Inverting all colors on the page would make certain |
| 23 | colors incorrect. This would negatively impact Wikipedia content in a way that |
| 24 | may make it wrong or confusing (e.g. inverting [political party](https://en.wikipedia.org/wiki/Congress_of_the_Philippines)/sports team |
| 25 | colors or [articles about colors](https://en.wikipedia.org/wiki/Pantone#Color_of_the_Year) ). It was deemed that in principle, the |
| 26 | Wikimedia foundation should not make these kinds of potentially negative |
| 27 | changes to Wikimedia project content. |
| 28 | - **Inability to measure impact**: When inverting colors, we would have no way of |
| 29 | measuring the amount of content that has been negatively impacted, and |
| 30 | therefor would not be able to identify which content needs fixing. |
| 31 | - **Lack of color palette customization**: Inverting colors would not provide |
| 32 | any flexibility for tweaking or modifying the night mode design. The resulting |
| 33 | night mode would be an inverse of the light-mode, instead of a more nuanced |
| 34 | design. Visual elements such as colors or white drop-shadows would be technically |
| 35 | difficult to remove or modify. |
| 36 | |
| 37 | **Modify the existing Less variables** - This approach would involve changing the |
| 38 | values of the existing color variables in the Less CSS preprocessor based on |
| 39 | night mode being enabled or disabled. Since Codex variables are widely used and |
| 40 | overrideable by the skin, it would have broad ecosystem support. It was |
| 41 | not pursued for the following reasons: |
| 42 | |
| 43 | - **Support for anonymous users**: In order to serve night mode to anonymous |
| 44 | users, we would have to generate a new night mode stylesheet, then serve that |
| 45 | stylesheet conditionally for anonymous users based on whether they or not |
| 46 | have night mode enabled. Due to our aggressive caching strategy, we are |
| 47 | unable to vary which stylesheets we serve to anonymous users. |
| 48 | |
| 49 | **Use CSS custom properties for colors** - This approach involves replacing the |
| 50 | Less color variables required for night mode with CSS custom properties. |
| 51 | Conditional code is then added to the stylesheet (either by media-query or |
| 52 | class) that changes the value of these custom properties for night mode. Due to |
| 53 | the constrains mentioned above, this is the preferred approach. |
| 54 | |
| 55 | ## Decision |
| 56 | |
| 57 | In order to create a night mode for the Minerva skin, the Web Team has decided to |
| 58 | use CSS custom properties to achieve a customizable color palette. |
| 59 | |
| 60 | We will replace the Less variables required for night mode with CSS custom |
| 61 | properties. The values of these custom properties will remain unchanged for |
| 62 | light-mode, but will be customized for night mode. |
| 63 | |
| 64 | We agree that maintaining a single source of truth for Codex token values is |
| 65 | important, so we will maintain the Codex naming convention for CSS custom |
| 66 | properties and use values derived from Codex as much as possible. |
| 67 | |
| 68 | ## Consequences |
| 69 | |
| 70 | ### Benefits |
| 71 | |
| 72 | - **Performance** - Night mode can be implemented by changing a |
| 73 | relatively small set of CSS values. |
| 74 | - **Better alignment with web standards** - CSS custom properties are a |
| 75 | standards-based solution with broad browser support. |
| 76 | - **Better traceability & tooling**. Unlike Less variables, changes to CSS custom |
| 77 | properties are easily traceable via the browser console. |
| 78 | - **Better support for system-level settings** - CSS custom properties can |
| 79 | respond to the `prefers-color-scheme` media-query, which will allow us to |
| 80 | enable/disable night mode based on system-level settings. *In future, we can |
| 81 | also respond to other accessibility media-queries such `prefers-contrast`*. |
| 82 | |
| 83 | ### Drawbacks |
| 84 | |
| 85 | Although the Minerva skin can implement CSS custom properties with relative ease, |
| 86 | we acknowledge that the Mediawiki ecosystem depends on on a broad range of |
| 87 | extensions, and for these to function well in night mode, they too will have to |
| 88 | adopt the same CSS custom properties as the Minerva skin. We commit to helping |
| 89 | these migration by providing strategies, documentation and tooling around the |
| 90 | expect use of CSS custom properties for night mode. |
| 91 | |
| 92 | Similarly, we acknowledge that templates will also have to be modified to work |
| 93 | with this night mode strategy. We commit to creating plans or strategies to |
| 94 | help template authors with these modifications. |