<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>darkmode Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/darkmode/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/darkmode/</link>
	<description></description>
	<lastBuildDate>Fri, 10 Dec 2021 22:27:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>darkmode Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/darkmode/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Adding Dark Mode to WordPress</title>
		<link>https://zxi.mytechroad.com/blog/admin/adding-dark-mode-to-wordpress/</link>
					<comments>https://zxi.mytechroad.com/blog/admin/adding-dark-mode-to-wordpress/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 10 Dec 2021 19:15:00 +0000</pubDate>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[darkmode]]></category>
		<category><![CDATA[plugin]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9077</guid>

					<description><![CDATA[<p>Supporting dark mode now becomes an industry standard, I also received several requests from my viewers/readers recently thus I decided to add this feature. First&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/admin/adding-dark-mode-to-wordpress/">Adding Dark Mode to WordPress</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Supporting dark mode now becomes an industry standard, I also received several requests from my viewers/readers recently thus I decided to add this feature. First thing in my mind is a plugin, I found <a href="https://wordpress.org/plugins/wp-dark-mode/">WP Dark Mode</a>, it&#8217;s feature rich and kind of worked until someone told me it didn&#8217;t work with cached pages. Then I found <a href="https://github.com/sandoche/Darkmode.js">Darkmode.js</a>, there&#8217;re several plugin versions but I decided to use the plain js version. It&#8217;s simple but makes the job done. There are only a few options you can play with, like the position, size, color and emoji of the icon, that&#8217;s it. Here&#8217;s mine.</p>



<p>Put the following code in header.php</p>



<pre class="crayon-plain-tag">&lt;script src=&quot;https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
  const options = {
    bottom: '48px', // default: '32px'
    right: '12px', // default: '32px'
    left: 'unset', // default: 'unset'
    time: '0.3s', // default: '0.3s'
    mixColor: '#ddd', // default: '#fff' reduce contrast and lower the brightness.
    backgroundColor: '#fff',  // default: '#fff'
    buttonColorDark: '#100f2c',  // default: '#100f2c'
    buttonColorLight: '#fff', // default: '#fff'
    saveInCookies: true, // default: true,
    label: '<img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f313.png" alt="🌓" class="wp-smiley" style="height: 1em; max-height: 1em;" />', // default: ''
    autoMatchOsTheme: true // default: true
  }

  function addDarkmodeWidget() {
    new Darkmode(options).showWidget();
  }
	
  addDarkmodeWidget(); // No delays.
&lt;/script&gt;</pre>



<p>Basically, it adds an overlay layer that applies a revert color filter (white -&gt; black, blue -&gt; yellow, etc) to <strong>ALL</strong> the content including images and iframes as well as syntax-highlighted code which I decided to ignore later on.</p>



<p>To ignore a few types of content, add the following code to header.php</p>



<pre class="crayon-plain-tag">&lt;style&gt;
.darkmode-layer, .darkmode-toggle {
    z-index: 500;
}

.darkmode--activated img {
    filter: invert(1);
}

.darkmode--activated iframe {
    filter: invert(1);
}	

.darkmode--activated .crayon-syntax {
    filter: invert(1);
}
&lt;/style&gt;</pre>



<p>The next thing is to apply a different color theme for code under darkmode. I plan to use classic for day-mode and sublime-text for darkmode. Although the author says the persistent darkmode setting is in cookies, but turns out it&#8217;s in localstorage. There are two scenarios we need to handle. 1) page loads with darkmode on, 2) user click button to toggle darkmode. We need to change the class for code blocks accordingly.</p>



<p>Put the following code in the footer.php, since we need to let the syntax-highlighter do the work first.</p>



<pre class="crayon-plain-tag">&lt;script&gt;
var darkMode = localStorage.getItem(&quot;darkmode&quot;) == &quot;true&quot;;    

if (darkMode) {
    updateCrayonTheme(darkMode);
}

var button = document.getElementsByClassName(&quot;darkmode-toggle&quot;)[0];

button.addEventListener(&quot;click&quot;, () =&gt; {
    darkMode = localStorage.getItem(&quot;darkmode&quot;) == &quot;true&quot;;
    updateCrayonTheme(darkMode);
});

function updateCrayonTheme(darkMode) {
    var x = document.getElementsByClassName(&quot;crayon-syntax&quot;);
    for (var i = 0; i &lt; x.length; i++) {
      x[i].classList.remove(&quot;crayon-theme-classic&quot;);
      x[i].classList.remove(&quot;crayon-theme-sublime-text&quot;);
      x[i].classList.add(darkMode ? &quot;crayon-theme-sublime-text&quot; : &quot;crayon-theme-classic&quot;);
    }  
}
&lt;/script&gt;</pre>



<script async="" src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2404451723245401" crossorigin="anonymous"></script>
<ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>



<p>Or don&#8217;t forget to preload the darkmode theme since only the default theme will be loaded to reduce requests and bandwidth.</p>



<p><meta charset="utf-8">Put the following code in the header.php &lt;head&gt; section, replace with your domain and plugin version.</p>



<pre class="crayon-plain-tag">&lt;link rel=&quot;stylesheet&quot; id=&quot;crayon-theme-sublime-text-css&quot; href=&quot;https://example.com/wp-content/plugins/crayon-syntax-highlighter-2.8.6/themes/sublime-text/sublime-text.css?ver=_2.7.2_beta&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;</pre>



<p>Finally it looks like below, not perfect but pretty good and easy to config. I hope wordpress could officially add dark mode in the future release such that all the themes and plugins could implement that (more work though).</p>



<div class="wp-container-3 wp-block-columns">
<div class="wp-container-1 wp-block-column">
<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832.png"><img width="473" height="1024" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832-473x1024.png" alt="" class="wp-image-9101" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832-473x1024.png 473w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832-139x300.png 139w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832-768x1662.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832-710x1536.png 710w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832-946x2048.png 946w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2832.png 1170w" sizes="(max-width: 473px) 100vw, 473px" /></a></figure>
</div>



<div class="wp-container-2 wp-block-column">
<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831.png"><img width="473" height="1024" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831-473x1024.png" alt="" class="wp-image-9102" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831-473x1024.png 473w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831-139x300.png 139w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831-768x1662.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831-710x1536.png 710w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831-946x2048.png 946w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/12/IMG_2831.png 1170w" sizes="(max-width: 473px) 100vw, 473px" /></a></figure>
</div>
</div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/admin/adding-dark-mode-to-wordpress/">Adding Dark Mode to WordPress</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/admin/adding-dark-mode-to-wordpress/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
