A place to cache linked articles (think custom and personal wayback machine)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

4 anos atrás
12345678910111213
  1. title: Color Theme Switcher
  2. url: https://mxb.dev/blog/color-theme-switcher/
  3. hash_url: 7a485d9382d6b3138c70cb2b9518fcb1
  4. <p class="lead">Last year, the design gods decided that dark modes were the new hotness. "Light colors are for suckers", they laughed, drinking matcha tea on their fixie bikes or whatever.</p><p>And so every operating system, app and even some websites (mine included) suddenly had to come up with a dark mode. Fortunately though, this coincided nicely with widespread support for CSS <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/--*">custom properties</a> and the introduction of a new <code>prefers-color-scheme</code> media query.</p><p>There’s lots of tutorials on <a href="https://css-tricks.com/dark-modes-with-css/">how to build dark modes</a> already, but why limit yourself to light and dark? Only a Sith deals in absolutes.</p><p>That’s why I decided to build a new feature on my site:<br/><strong>dynamic color themes!</strong> Yes, instead of two color schemes, I now have ten! That’s eight better than the previous website!</p><p>Go ahead and try it, hit that <strong>paintroller-button</strong> in the header.<br/>I’ll wait.</p><p><em>If you’re reading this somewhere else, the effect would look something like this:</em></p><p class="extend"><video poster="/assets/media/theme-switcher/theme-switcher-still.png" preload="metadata" muted="" controls=""><source src="/assets/media/theme-switcher/theme-switcher.webm" type="video/webm"><source src="/assets/media/theme-switcher/theme-switcher.mp4" type="video/mp4"/></source></video></p><p>Nice, right? Let’s look at how to do that!</p>
  5. <h2 id="h-define-color-schemes"><a class="heading-anchor" href="#h-define-color-schemes">#</a> Define Color Schemes</h2><p>First up, we need some data. We need to define our themes in a central location, so they’re easy to access and edit. My site uses <a href="https://www.11ty.dev/">Eleventy</a>, which lets me create a simple JSON file for that purpose:</p><pre class="language-json"><code class="language-json"><br/><span class="token punctuation">[</span><br/> <span class="token punctuation">{</span><br/> <span class="token property">"id"</span><span class="token operator">:</span> <span class="token string">"bowser"</span><span class="token punctuation">,</span><br/> <span class="token property">"name"</span><span class="token operator">:</span> <span class="token string">"Bowser's Castle"</span><span class="token punctuation">,</span><br/> <span class="token property">"colors"</span><span class="token operator">:</span> <span class="token punctuation">{</span><br/> <span class="token property">"primary"</span><span class="token operator">:</span> <span class="token string">"#7f5af0"</span><span class="token punctuation">,</span><br/> <span class="token property">"secondary"</span><span class="token operator">:</span> <span class="token string">"#2cb67d"</span><span class="token punctuation">,</span><br/> <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"#fffffe"</span><span class="token punctuation">,</span><br/> <span class="token property">"border"</span><span class="token operator">:</span> <span class="token string">"#383a61"</span><span class="token punctuation">,</span><br/> <span class="token property">"background"</span><span class="token operator">:</span> <span class="token string">"#16161a"</span><span class="token punctuation">,</span><br/> <span class="token property">"primaryOffset"</span><span class="token operator">:</span> <span class="token string">"#e068fd"</span><span class="token punctuation">,</span><br/> <span class="token property">"textOffset"</span><span class="token operator">:</span> <span class="token string">"#94a1b2"</span><span class="token punctuation">,</span><br/> <span class="token property">"backgroundOffset"</span><span class="token operator">:</span> <span class="token string">"#29293e"</span><br/> <span class="token punctuation">}</span><br/> <span class="token punctuation">}</span><span class="token punctuation">,</span><br/> <span class="token punctuation">{</span>...<span class="token punctuation">}</span><br/><span class="token punctuation">]</span></code></pre><p>Our color schemes are objects in an array, which is now available during build. Each theme gets a <code>name</code>, <code>id</code> and a couple of color definitions. The parts of a color scheme depend on your specific design; In my case, I assigned each theme eight properties.</p><div class="callout callout--tip"><span class="callout__icon"><svg class="icon icon--lightbulb" role="img" aria-hidden="true"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/assets/icons/icons.sprite.svg#icon-lightbulb"/></svg></span><div class="callout__content"><p>It's a good idea to give these properties logical names instead of visual ones like "light" or "muted", as colors vary from theme to theme. I've also found it helpful to define a couple of "offset" colors - these are used to adjust another color on interactions like hover and such.</p></div></div><p>In addition to the “default” and “dark” themes I already had before, I created eight more themes this way. I used a couple of different sources for inspiration; the ones I liked best are <a href="https://color.adobe.com/explore">Adobe Color</a> and <a href="https://www.happyhues.co/">happyhues</a>.</p><p>All my themes are named after Mario Kart 64 race tracks by the way, because why not.</p>
  6. <h2 id="h-transform-to-custom-css-properties"><a class="heading-anchor" href="#h-transform-to-custom-css-properties">#</a> Transform to Custom CSS Properties</h2><p>To actually use our colors in CSS, we need them in a different format. Let’s create a stylesheet and make custom properties out of them. Using Eleventy’s template rendering, we can do that by generating a <code>theme.css</code> file from the data, looping over all themes. We’ll use a macro to output the color definitions for each.</p><p>I wrote this in Nunjucks, the templating engine of my choice - but you can do it in any other language as well.</p><pre class="language-css"><code class="language-css"><br/>---<br/><span class="token property">permalink</span><span class="token punctuation">:</span> <span class="token string">'/assets/css/theme.css'</span><br/><span class="token property">excludeFromSitemap</span><span class="token punctuation">:</span> true<br/>---<br/><br/><span class="token punctuation">{</span>% macro <span class="token function">colorscheme</span><span class="token punctuation">(</span>colors<span class="token punctuation">)</span> %<span class="token punctuation">}</span><br/> <span class="token selector">--color-bg:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.background <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;<br/> --color-bg-offset:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.backgroundOffset <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;<br/> --color-text:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.text <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;<br/> --color-text-offset:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.textOffset <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;<br/> --color-border:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.border <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;<br/> --color-primary:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.primary <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;<br/> --color-primary-offset:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.primaryOffset <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;<br/> --color-secondary:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> colors.secondary <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token selector">;</span><br/><span class="token punctuation">{</span>% endmacro %<span class="token punctuation">}</span><br/><br/><br/><span class="token punctuation">{</span>%- set default = themes|<span class="token function">getTheme</span><span class="token punctuation">(</span><span class="token string">'default'</span><span class="token punctuation">)</span> -%<span class="token punctuation">}</span><br/><span class="token punctuation">{</span>%- set dark = themes|<span class="token function">getTheme</span><span class="token punctuation">(</span><span class="token string">'dark'</span><span class="token punctuation">)</span> -%<span class="token punctuation">}</span><br/><br/><br/><span class="token selector">:root</span> <span class="token punctuation">{</span><br/> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">colorscheme</span><span class="token punctuation">(</span>default.colors<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><br/><span class="token punctuation">}</span><br/><br/><span class="token atrule"><span class="token rule">@media</span><span class="token punctuation">(</span><span class="token property">prefers-color-scheme</span><span class="token punctuation">:</span> dark<span class="token punctuation">)</span></span> <span class="token punctuation">{</span><br/> <span class="token selector">:root</span> <span class="token punctuation">{</span><br/> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">colorscheme</span><span class="token punctuation">(</span>dark.colors<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><br/> <span class="token punctuation">}</span><br/><span class="token punctuation">}</span><br/><br/><br/><span class="token punctuation">{</span>% for theme in themes %<span class="token punctuation">}</span><br/><span class="token selector">html[data-theme='{{ theme.id }}']</span> <span class="token punctuation">{</span><br/> <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">colorscheme</span><span class="token punctuation">(</span>theme.colors<span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><br/><span class="token punctuation">}</span><br/><span class="token punctuation">{</span>% endfor %<span class="token punctuation">}</span><br/></code></pre>
  7. <h2 id="h-using-colors-on-the-website"><a class="heading-anchor" href="#h-using-colors-on-the-website">#</a> Using colors on the website</h2><p>Now for the tedious part - we need to go through all of the site’s styles and replace every color definition with the corresponding custom property. This is different for every site - but your code might look like this if it’s written in SCSS:</p><pre class="language-scss"><code class="language-scss"><span class="token selector">body </span><span class="token punctuation">{</span><br/> <span class="token property">font-family</span><span class="token punctuation">:</span> sans-serif<span class="token punctuation">;</span><br/> <span class="token property">line-height</span><span class="token punctuation">:</span> <span class="token variable">$line-height</span><span class="token punctuation">;</span><br/> <span class="token property">color</span><span class="token punctuation">:</span> <span class="token variable">$gray-dark</span><span class="token punctuation">;</span><br/><span class="token punctuation">}</span></code></pre><p>Replace the static SCSS variable with the theme’s custom property:</p><pre class="language-scss"><code class="language-scss"><span class="token selector">body </span><span class="token punctuation">{</span><br/> <span class="token property">font-family</span><span class="token punctuation">:</span> sans-serif<span class="token punctuation">;</span><br/> <span class="token property">line-height</span><span class="token punctuation">:</span> <span class="token variable">$line-height</span><span class="token punctuation">;</span><br/> <span class="token property">color</span><span class="token punctuation">:</span> <span class="token function">var</span><span class="token punctuation">(</span>--color-text<span class="token punctuation">)</span><span class="token punctuation">;</span><br/><span class="token punctuation">}</span></code></pre><div class="callout callout--warning"><span class="callout__icon"><svg class="icon icon--warning" role="img" aria-hidden="true"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/assets/icons/icons.sprite.svg#icon-warning"/></svg></span><div class="callout__content"><p><strong>Attention:</strong> Custom Properties are supported in <a href="https://caniuse.com/#search=custom%20properties">all modern browsers</a>, but if you need to support IE11 or Opera Mini, be sure to provide a fallback.</p></div></div><p>It’s fine to mix static preprocessor variables and custom properties by the way - they do different things. Our line height is not going to change dynamically.</p><p>Now do this for every instance of <code>color</code>, <code>background</code>, <code>border</code>, <code>fill</code> … you get the idea. Told you it was gonna be tedious.</p>
  8. <h2 id="h-building-the-theme-switcher"><a class="heading-anchor" href="#h-building-the-theme-switcher">#</a> Building the Theme Switcher</h2><p>If you made it this far, congratulations! Your website is now themeable (in theory). We still need a way for people to switch themes without manually editing the markup though, that’s not very user-friendly. We need some sort of UI component for this - a theme switcher.</p>
  9. <h3 id="h-generating-the-markup"><a class="heading-anchor" href="#h-generating-the-markup">#</a> Generating the Markup</h3><p>The switcher structure is pretty straightforward: it’s essentially a list of buttons, one for each theme. When a button is pressed, we’ll switch colors. Let’s give the user an idea what to expect by showing the theme colors as little swatches on the button:</p><figure class="extend"><img src="/assets/media/theme-switcher/theme-buttons.jpg" loading="lazy" alt="a row of buttons, showing the theme name and color swatches"/><figcaption>Fact: All good design is derivative of Mario Kart</figcaption></figure><p>Here’s the template to generate that markup. We’ll use inline style attributes here to display the background, text and accent colors. The button also holds its <code>id</code> in a <code>data-theme-id</code> attribute, we will pick that up with Javascript later.</p><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><br/>{% for theme in themes %}<br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__item<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>button</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__btn<span class="token punctuation">"</span></span> <span class="token attr-name">data-theme-id</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>{{ theme.id }}<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token selector">background-color:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> theme.colors.background <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__name<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token selector">color:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> theme.colors.text <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>{{ theme.name }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__palette<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__hue<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token selector">background-color:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> theme.colors.primary <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>{{ theme.colors.primary }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__hue<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token selector">background-color:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> theme.colors.secondary <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>{{ theme.colors.secondary }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__hue<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token selector">background-color:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> theme.colors.border <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>{{ theme.colors.border }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__hue<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token selector">background-color:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> theme.colors.text <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>{{ theme.colors.text }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>themeswitcher__hue<span class="token punctuation">"</span></span><span class="token style-attr language-css"><span class="token attr-name"> <span class="token attr-name">style</span></span><span class="token punctuation">="</span><span class="token attr-value"><span class="token selector">background-color:</span> <span class="token punctuation">{</span><span class="token punctuation">{</span> theme.colors.textOffset <span class="token punctuation">}</span><span class="token punctuation">}</span></span><span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span>{{ theme.colors.textOffset }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>button</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span><br/>{% endfor %}<br/><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span><br/></code></pre><p>There’s some styling involved as well, but I’ll leave that out for brevity here. If you’re interested in the extended version, you can find all the code in <a href="https://github.com/maxboeck/mxb">my site’s github repo</a>.</p>
  10. <h3 id="h-setting-the-theme"><a class="heading-anchor" href="#h-setting-the-theme">#</a> Setting the Theme</h3><p>The last missing piece is some Javascript to handle the switcher functionality.</p><pre class="language-js"><code class="language-js"><br/><span class="token keyword">class</span> <span class="token class-name">ThemeSwitcher</span> <span class="token punctuation">{</span><br/> <span class="token function">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><br/> <br/> <span class="token keyword">this</span><span class="token punctuation">.</span>activeTheme <span class="token operator">=</span> <span class="token string">'default'</span><br/> <span class="token keyword">this</span><span class="token punctuation">.</span>hasLocalStorage <span class="token operator">=</span> <span class="token keyword">typeof</span> Storage <span class="token operator">!==</span> <span class="token string">'undefined'</span><br/><br/> <br/> <span class="token keyword">this</span><span class="token punctuation">.</span>themeSelectBtns <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">querySelectorAll</span><span class="token punctuation">(</span><span class="token string">'button[data-theme-id]'</span><span class="token punctuation">)</span><br/> <br/> Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>themeSelectBtns<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">btn</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><br/> <span class="token keyword">const</span> id <span class="token operator">=</span> btn<span class="token punctuation">.</span>dataset<span class="token punctuation">.</span>themeId<br/> btn<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'click'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">setTheme</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">)</span><br/> <span class="token punctuation">}</span><span class="token punctuation">)</span><br/> <span class="token punctuation">}</span><br/><span class="token punctuation">}</span><br/><br/><br/><br/><span class="token keyword">if</span> <span class="token punctuation">(</span>window<span class="token punctuation">.</span><span class="token constant">CSS</span> <span class="token operator">&amp;&amp;</span> <span class="token constant">CSS</span><span class="token punctuation">.</span><span class="token function">supports</span><span class="token punctuation">(</span><span class="token string">'color'</span><span class="token punctuation">,</span> <span class="token string">'var(--fake-var)'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><br/> <span class="token keyword">new</span> <span class="token class-name">ThemeSwitcher</span><span class="token punctuation">(</span><span class="token punctuation">)</span><br/><span class="token punctuation">}</span></code></pre><p>When somebody switches themes, we’ll take the theme id and set is as the <code>data-theme</code> attribute on the document. That will trigger the corresponding selector in our <code>theme.css</code> file, and the chosen color scheme will be applied.</p><p>Since we want the theme to persist even when the user reloads the page or navigates away, we’ll save the selected id in <code>localStorage</code>.</p><pre class="language-js"><code class="language-js"><span class="token function">setTheme</span><span class="token punctuation">(</span><span class="token parameter">id</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><br/> <br/> <span class="token keyword">this</span><span class="token punctuation">.</span>activeTheme <span class="token operator">=</span> id<br/> document<span class="token punctuation">.</span>documentElement<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">'data-theme'</span><span class="token punctuation">,</span> id<span class="token punctuation">)</span><br/><br/> <br/> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>hasLocalStorage<span class="token punctuation">)</span> <span class="token punctuation">{</span><br/> localStorage<span class="token punctuation">.</span><span class="token function">setItem</span><span class="token punctuation">(</span><span class="token string">"theme"</span><span class="token punctuation">,</span> id<span class="token punctuation">)</span><br/> <span class="token punctuation">}</span><br/><span class="token punctuation">}</span></code></pre>
  11. <p>On a server-rendered site, we could store that piece of data in a cookie instead and apply the theme id to the html element before serving the page. Since we’re dealing with a static site here though, there is no server-side processing - so we have to do a small workaround.</p><p>We’ll retrieve the theme from <code>localStorage</code> in a tiny additional script in the head, right after the stylesheet is loaded. Contrary to the rest of the Javascript, we want this to execute as early as possible to avoid a FODT (“flash of default theme”).</p><p>OK that’s not actually a real term. I made that up.</p><pre class="language-html"><code class="language-html"><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>link</span> <span class="token attr-name">rel</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>stylesheet<span class="token punctuation">"</span></span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>/assets/css/main.css<span class="token punctuation">"</span></span><span class="token punctuation">&gt;</span></span><br/> <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript"><br/> <br/> localStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">'theme'</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <br/> document<span class="token punctuation">.</span>documentElement<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">'data-theme'</span><span class="token punctuation">,</span> localStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">'theme'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><br/> </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span><br/><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span></code></pre><p>If no stored theme is found, the site uses the default color scheme (either light or dark, depending on the users <a href="https://web.dev/prefers-color-scheme/">system preference</a>).</p>
  12. <h2 id="h-get-creative"><a class="heading-anchor" href="#h-get-creative">#</a> Get creative</h2><p>You can create any number of themes this way, and they’re not limited to flat colors either - with some extra effort you can have patterns, gradients or even GIFs in your design. Although just because you can doesn’t always mean you should, as is evidenced by my site’s new <em>Rainbow Road</em> theme.</p><p>Please don’t use that one.</p>