<?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 Harish V on Medium]]></title>
        <description><![CDATA[Stories by Harish V on Medium]]></description>
        <link>https://medium.com/@harishv6665_49536?source=rss-3fe273f3b382------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*D91WFF0LlGahLA_F.</url>
            <title>Stories by Harish V on Medium</title>
            <link>https://medium.com/@harishv6665_49536?source=rss-3fe273f3b382------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 10:20:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harishv6665_49536/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[Theming with React.js or any in minutes with CSS Variables]]></title>
            <link>https://medium.com/@harishv6665_49536/theming-with-react-js-or-any-in-minutes-with-css-variables-11d4ebecdb41?source=rss-3fe273f3b382------2</link>
            <guid isPermaLink="false">https://medium.com/p/11d4ebecdb41</guid>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[css]]></category>
            <category><![CDATA[themes]]></category>
            <dc:creator><![CDATA[Harish V]]></dc:creator>
            <pubDate>Mon, 03 Sep 2018 04:06:17 GMT</pubDate>
            <atom:updated>2019-05-09T12:59:40.119Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KXnaCZhUU84b9VBYvwXy0A.png" /></figure><p>After being a Web Developer for a couple of years and being the person responsible for UI, I’ve learnt a lot of things about styling and theming. Theming in Web applications is always challenging and interesting task.</p><p>Earlier(before CSS Variables) we had SASS and LESS CSS preprocessors to define variables in which it would dynamically compile and set inline colors.</p><p>Now that we have CSS Variables, supported natively, we don’t have to worry about it anymore! Alongside we should think about the support for CSS Variables as well. Check the image below for the browser support:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xtz-X5p0ZIN6ldyFVvUR7g.png" /><figcaption>CSS Variables support</figcaption></figure><h3>What are CSS Variables?</h3><blockquote>CSS variables are entities defined by CSS authors that contain specific values to be reused throughout a document.</blockquote><p>Reference: <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_variables">MDN</a></p><h4>Before:</h4><pre>.block1 {<br>    background-color: #ffffff;<br>    color: #000000;<br>}</pre><pre>.block2 {<br>    color: #000000;<br>}</pre><h4>Now: (with CSS Variables)</h4><pre>html {<br>    --primaryBackground: #ffffff;<br>    --primaryTextColor: #000000;<br>}</pre><pre>.block1 {<br>    background-color: var(--primaryBackground);<br>    color: var(--primaryTextColor);<br>}</pre><pre>.block2 {<br>    color: var(--primaryTextColor);<br>}</pre><p>Pretty simple huh?</p><h4>Naming Variables</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*yY_-K-Tl7IhVxuQBktPWVA.jpeg" /></figure><p>Before we do anything, we need to figure out how we name the colors. I think this diagram is pretty fun because, well its kinda true.</p><p>So for naming the colors for theme, I prefer it to be based on the content.</p><p>Check this for more info <a href="https://material.io/design/color/the-color-system.html">Material Design</a></p><p>Now that we know how to define and set the color variables, let’s put it in action.</p><h3>Setting up themes</h3><p>Define all your theme colors in html tag selector in css.<br>Use <strong>data-theme</strong> attribute to set the selected theme.</p><pre>// default theme(light)</pre><pre>html {<br>    --primaryBackground: #ffffff;<br>    --primaryTextColor: #000000;<br>}</pre><pre><strong><br></strong>// dark theme</pre><pre>html[data-theme=&quot;dark&quot;] {<br>    --primaryBackground: #000000;<br>    --primaryTextColor: #ffffff;<br>}</pre><h3>Switching Theme</h3><pre><strong>Invoke from Javascript</strong></pre><pre>// default or light theme<br>document.documentElement.setAttribute(&#39;data-theme&#39;, &#39;light&#39;);</pre><pre>// dark theme<br>document.documentElement.setAttribute(&#39;data-theme&#39;, &#39;dark&#39;);</pre><blockquote>Note: The default theme is defined without any value for data-theme attribute. So the above code will switch to dark theme only if the data-theme attribute value is set to “dark”.</blockquote><h4>Switching Theme in React.js</h4><p>Theme switching should happen in the root level of the application. Usually it will be App Component(App.js).</p><pre><strong>// App.js</strong></pre><pre>constructor(props) {<br>    super(props);<br>    this.state = {<br>        theme: &#39;light&#39;,<br>    };<br>    this.toggleTheme = this.toggleTheme.bind(this);<br>}<br></pre><pre>toggleTheme() {<br>    const theme = this.state.theme === &#39;dark&#39; ? &#39;light&#39; : &#39;dark&#39;;<br>    this.setState({ theme });<br>    document.documentElement.setAttribute(&quot;data-theme&quot;, theme);<br>}</pre><h4>Switching Theme in other frameworks</h4><p>Similarly if you are using any other framework or library just keep the logic at the root level of the application and trigger the theme switch function based on the user action.</p><p>Got code snippets for other frameworks? Please suggest and I’ll add them!</p><h3>Switching Theme like a pro</h3><p>Now that we can switch the theme we can make the color change smoother with the help of CSS transition.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*FPMbi-g8pDqHmfgDLKsAdg.gif" /></figure><p>So cool, right? I could just keep on toggling it the whole day and feel so good.</p><h3>Setup transition for smooth switching</h3><pre><strong>// CSS</strong></pre><pre>html.theme-transition,<br>html.theme-transition *,<br>html.theme-transition *:before,<br>html.theme-transition *:after {<br>  transition: all 750ms !important;<br>  transition-delay: 0 !important;<br>}<br></pre><pre><strong>// JS</strong></pre><pre>document.documentElement.classList.add(&#39;theme-transition&#39;)<br>document.documentElement.setAttribute(&#39;data-theme&#39;, theme)<br>window.setTimeout(function() {<br>  document.documentElement.classList.remove(&#39;theme-transition&#39;)<br>}, 1000)</pre><p>And with these simple few lines of code we can change the way the theme switching happens. Pretty elegant, huh?</p><p>I have created a simple application using React.js on CodeSandbox.</p><h4>Demo:</h4><p><a href="https://codesandbox.io/embed/qlqlv9376w?view=preview">CodeSandbox</a></p><h4>That’s it!</h4><p>Hope this helps. Be sure to leave comments if you have any thoughts on this article or have any other use case to show. And if you find it helpful, give me a clap 👏 and share it. Thanks for reading!</p><p>PS: I’m human and make mistakes — so if you find any mistake I’d love for you to let me know!</p><p>Special thanks to <a href="https://dribbble.com/akhiltv">Akhil TV</a> for getting these awesome images in my blog and the demo application design.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11d4ebecdb41" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>