aboutsummaryrefslogtreecommitdiff
path: root/build/posts/quick-dirty-theme-switcher/index.html
diff options
context:
space:
mode:
authorBradley Taunt <bt@btxx.org>2024-07-02 14:22:21 -0400
committerBradley Taunt <bt@btxx.org>2024-07-02 14:22:21 -0400
commit3f6a9546ec13063d0d5bdf21d30a93d3e8aa6050 (patch)
tree947985c4eda1bceb1910bc01739c32fd0baad181 /build/posts/quick-dirty-theme-switcher/index.html
parent14074019d62d98885c4c764401a9e7e1fd129f79 (diff)
Rebuild changes based off latest barfHEADmaster
Diffstat (limited to 'build/posts/quick-dirty-theme-switcher/index.html')
-rw-r--r--build/posts/quick-dirty-theme-switcher/index.html138
1 files changed, 0 insertions, 138 deletions
diff --git a/build/posts/quick-dirty-theme-switcher/index.html b/build/posts/quick-dirty-theme-switcher/index.html
deleted file mode 100644
index a627be4..0000000
--- a/build/posts/quick-dirty-theme-switcher/index.html
+++ /dev/null
@@ -1,138 +0,0 @@
-<!doctype html>
-<html lang="en">
-<head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <meta name="color-scheme" content="dark light">
- <link rel="icon" href="data:,">
- <title>Quick and Dirty Theme Switcher</title>
- <link href="/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
- <link href="/rss.xml" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" />
-<style>*{box-sizing:border-box;}body{font-family:sans-serif;line-height:1.33;margin:0 auto;max-width:650px;padding:1rem;}blockquote{background:rgba(0,0,0,0.1);border-left:4px solid;padding-left:5px;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.footnotes{font-size:90%;}</style>
-</head>
-
-<nav>
- <a href="#menu">Menu &darr;</a>
-</nav>
-
-<main>
-<h1 id="quick-and-dirty-theme-switcher">Quick and Dirty Theme Switcher</h1>
-<p>2020-06-04</p>
-<p><strong>Update</strong>: This article is no longer relevant since my blog design has changed. I&#8217;m keeping this post up since it will still be useful for those wanting to implement a theme switcher on their own site.</p>
-<hr/>
-<p><em>I recently added a fairly straightforward color scheme (theme) switcher</em> to my personal website. You can toggle this simple color switcher in the footer of the site to see it in action. In case anyone else had the desire to add such functionality to their own sites&#47;projects, I figured I&#8217;d write up a quick post explaining how to do so. Let&#8217;s get into it.</p>
-<h2 id="the-html">The HTML</h2>
-<p>First we need to include the &#8220;buttons&#8221; that will trigger the theme to switch based on which one is selected. (Note: you could always render these as <code>options</code> in a <code>select</code> element if you preferred that method)</p>
-<pre><code>&#60;div class="color-select"&#62;
- &#60;button onclick="toggleDefaultTheme()"&#62;&#60;&#47;button&#62;
- &#60;button onclick="toggleSecondTheme()"&#62;&#60;&#47;button&#62;
- &#60;button onclick="toggleThirdTheme()"&#62;&#60;&#47;button&#62;
-&#60;&#47;div&#62;
-</code></pre>
-<p>That&#8217;s it! Don&#8217;t worry too much about the <code>onclick</code> parameter right now, we&#8217;ll come back to that when adding our JavaScript. The only remaining item is adding a default theme class to our <code>html</code> element, like so:</p>
-<pre><code>&#60;html class="theme-default"&#62;
-</code></pre>
-<h2 id="the-css">The CSS</h2>
-<p>Next we need to style both the <code>color-select</code> buttons, along with the custom color schemes that will alter the entire website. We will start with the color schemes.</p>
-<p>For these themes to swap seamlessly between each other, we will be setting our altering color sets as CSS variables:</p>
-<pre><code>.theme-default {
---accent-color: #72f1b8;
---font-color: #34294f;
-}
-
-.theme-second {
- --accent-color: #FFBF00;
- --font-color: #59316B;
-}
-
-.theme-third {
- --accent-color: #d9455f;
- --font-color: #303960;
-}
-
-body {
- background-color: var(--accent-color);
- color: var(--font-color);
-}
-</code></pre>
-<p>Finally, we style the user-facing color swatches:</p>
-<pre><code>.color-select button {
- -moz-appearance: none;
- appearance: none;
- border: 2px solid;
- border-radius: 9999px;
- cursor: pointer;
- height: 20px;
- margin: 0 0.8rem 0.8rem 0;
- outline: 0;
- width: 20px;
-}
-
-&#47;* Style each swatch to match the corresponding theme *&#47;
-.color-select button:nth-child(1) { background: #72f1b8; border-color: #34294f; }
-.color-select button:nth-child(2) { background: #FFBF00; border-color: #59316B; }
-.color-select button:nth-child(3) { background: #d9455f; border-color: #303960; }
-</code></pre>
-<h2 id="the-javascript">The JavaScript</h2>
-<p>We need to have each color swatch button trigger it&#8217;s corresponding theme and swap out the <code>theme-default</code> class that we have originally attached to the main <code>html</code> element. We also need to store what the user has selected into <code>localStorage</code>, so their choice persists when reloading or navigating to other pages.</p>
-<pre><code>&#47;&#47; Set a given theme&#47;color-scheme
-function setTheme(themeName) {
- localStorage.setItem(&#39;theme&#39;, themeName);
- document.documentElement.className = themeName;
-}
-
-&#47;&#47; Toggle between color themes
-function toggleDefaultTheme() {
- if (localStorage.getItem(&#39;theme&#39;) !== &#39;theme-default&#39;){
- setTheme(&#39;theme-default&#39;);
- }
-}
-function toggleSecondTheme() {
- if (localStorage.getItem(&#39;theme&#39;) !== &#39;theme-second&#39;){
- setTheme(&#39;theme-second&#39;);
- }
-}
-function toggleThirdTheme() {
- if (localStorage.getItem(&#39;theme&#39;) !== &#39;theme-third&#39;){
- setTheme(&#39;theme-third&#39;);
- }
-}
-
-&#47;&#47; Immediately set the theme on initial load
-(function () {
- if (localStorage.getItem(&#39;theme&#39;) === &#39;theme-default&#39;) {
- setTheme(&#39;theme-default&#39;);
- }
- if (localStorage.getItem(&#39;theme&#39;) === &#39;theme-second&#39;) {
- setTheme(&#39;theme-second&#39;);
- }
- if (localStorage.getItem(&#39;theme&#39;) === &#39;theme-third&#39;) {
- setTheme(&#39;theme-third&#39;);
- }
-})();
-</code></pre>
-<p>And that&#8217;s it! Now it just depends on how custom you want each individual theme style to be. The possibilities are endless!</p>
-<h2 id="extra-improvements">Extra Improvements</h2>
-<p>You could improve this concept even further hiding the <code>color-select</code> item if the user has JavaScript disabled. For my needs, I felt it was a fine trade-off to keep the non-functioning color swatch pickers if JavaScript was disabled. However, your project&#47;site might need better fallbacks.</p>
-<footer role="contentinfo">
- <h2>Menu Navigation</h2>
- <ul id="menu">
- <li><a href="/">Home</a></li>
- <li><a href="/projects">Projects</a></li>
- <li><a href="/uses">Uses</a></li>
- <li><a href="/wiki">Wiki</a></li>
- <li><a href="/resume">Resume</a></li>
- <li><a href="/colophon">Colophon</a></li>
- <li><a href="/now">Now</a></li>
- <li><a href="/donate">Donate</a></li>
- <li><a href="/atom.xml">RSS</a></li>
- <li><a href="#top">&uarr; Top of the page</a></li>
- </ul>
- <small>
- Built with <a href="https://barf.btxx.org">barf</a>. <br>
- Feeds: <a href="/atom.xml">Atom</a> & <a href="/rss.xml">RSS</a><br>
- Maintained with ♥ for the web. <br>
- Proud supporter of <a href="https://usefathom.com/ref/DKHJVX">Fathom</a> &amp; <a href="https://nextdns.io/?from=74d3p3h8">NextDNS</a>. <br>
- The content for this site is <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.<br> The <a href="https://git.sr.ht/~bt/bt.ht">code for this site</a> is <a href="https://git.sr.ht/~bt/bt.ht/tree/master/item/LICENSE">MIT</a>.
- </small>
-</footer> \ No newline at end of file