<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alexander Cerutti on Medium]]></title>
        <description><![CDATA[Stories by Alexander Cerutti on Medium]]></description>
        <link>https://medium.com/@cerutti.alexander?source=rss-b5d641eaa7d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*zLmQCgVbea96Lo4VGFNxJg.jpeg</url>
            <title>Stories by Alexander Cerutti on Medium</title>
            <link>https://medium.com/@cerutti.alexander?source=rss-b5d641eaa7d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 15:45:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cerutti.alexander/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A (mostly complete) guide to theme switching in CSS and JS]]></title>
            <link>https://medium.com/@cerutti.alexander/a-mostly-complete-guide-to-theme-switching-in-css-and-js-c4992d5fd357?source=rss-b5d641eaa7d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4992d5fd357</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[dark-mode]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Alexander Cerutti]]></dc:creator>
            <pubDate>Wed, 30 Oct 2024 21:43:34 GMT</pubDate>
            <atom:updated>2024-10-30T21:43:34.432Z</atom:updated>
            <content:encoded><![CDATA[<p>In the beginning, introducing support to dark mode / light mode was tricky as there were no CSS custom properties. Every logic was delegated to JS and to an HTML toggle button.</p><p>Then, around 2016, CSS custom properties arrived and changed the game, so we could apply styles by <strong>just changing variables</strong> <strong>values</strong>, based on a higher selector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qz10NCVq_05eqIBLzrrolQ.png" /></figure><p>However, in 2019 the @media query prefers-color-scheme arrived to allows handling colors based on the user’s system preferences. Things got a little bit more complicated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6XTC80ecvCjuT4QiVP3LdQ.png" /></figure><p>So, which way to choose should a website support? System preferences way or stick to the toggle button way?</p><p>The answer here can be only one: <em>“Yes.”</em> (🤪)</p><p>I’ll show below a way I used, collected from several resources around the web to support both ways.</p><p>The only JS we are going to use, is the one to read and update the current state of user system preferences.</p><p>What we need is to set a valid selector somewhere on top of everything else, so most likely on the html element. We can exploit the dataset property to set a data-theme attribute we’ll be using later.</p><p>A class can be used as well. Your choice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Y2SHF4yBK7uCx9rW1XiRQ.png" /></figure><p>We can do this because matchMedia returns a <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList"><em>MediaQueryList</em></a>, which extends the <em>EventTarget</em> interface.</p><p>A note about this: <strong>this code should be global</strong> and <strong>should be executed only once</strong> when the website (or Electron app) is run. So, yeah, before any React (or whatever framework is being used) component is executed. <strong>Running this on a “mount event” will probably be already too late and might create a content colors / properties flashing.</strong></p><blockquote>This code can be easily wrapped in a if-else that uses localStorage to save the last preference of the user. <strong>The implementation of this is left to the reader</strong> as it might require few toggling when the last saved setting was dark due to the user system changing dynamically, but the system is now in light mode.</blockquote><p>Then, we’ll have a button somewhere that updates these values on click . For the sake of brevity, <strong>I’ll write only the function to execute</strong> when click event is fired.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c8nHTWPK5N-nG0qvA4XBdQ.png" /></figure><p>Next part is the CSS.</p><blockquote>Now, using @media (prefers-color-scheme: dark | light) in CSS code is <strong>completely optional</strong>, as we can use exclusively the newly created [data-theme] attribute.</blockquote><blockquote>If you still want to use it, you should be prepared to handle all the four cases: system:light-setting:light , system:light-setting:dark , system:dark-setting:dark , system:dark-setting-light .</blockquote><p>We’ll use :root to contain the shared CSS properties and then we’ll be specify which :root will be used, based on the data-theme we defined above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MaBoLwPiezJ4kJFxAl7Iug.png" /></figure><p>And that’s all it takes. Now you can create your HTML Button to toggle theme.</p><p>I know, it feels like like wrong to not use @media (prefers-color-scheme: dark | light) in CSS, however we are still using this feature in a different way.</p><p>However, for how wonderful this way this, implementing such system in a component that can be used in several contexts, can be difficult as not every context might be using the code above to find the user settings.</p><p>So the first solution that comes to my mind, is exposing a CSS property for each customizable component element.</p><p>You will be selecting a default theme, wether it is dark or light and use the fallback value of the property as the default theme value.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6PS0AF36haA-Uzid9P91Pw.png" /></figure><p>Definitely not the best solution, as the integrator could choose the color it wants. However the wider can be “audience” that can use your component, the weirdest use cases you can expect and the less specific contracts can be.</p><p>About specific contracts, if you are brave enough, you could even think of a <em>checkbox-based toggle</em> that will save you the usage of the togglePreferredTheme function and do (almost) everything in CSS 😬.</p><p>As this is my very first article, I’m open to all kind of feedbacks. I hope you enjoyed it ❤.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4992d5fd357" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>