Skip to content

Commit

Permalink
Update dark mode (#5)
Browse files Browse the repository at this point in the history
* Update dark mode detection

Updated dark mode detection
 - Watches for browser mode change
 - Watches for toggle change

Either change sets the scheme variable in localStorage

* Move dark mode scheme toggle to dark.css

Move dark mode scheme toggle to dark.css (was in main.css)

* Light mode toggle to not require .light class

Changed the light mode toggle to not require .light class, as the counterpart only applies if html.dark is present.

* Re-enable <html> opening tag with dark mode

Was producing broken HTML _(</html> end tag but no <html> start tag)_

* Enable dark mode toggle on nav

Enabling dark mode toggle on nav

* Missed the color/fill dark class

a.dark svg -> a svg
fill & color add !important

* This element does not exist by default and will NullPointer itself if site.Params.mode is unset

* dark mode (once enabled) renders code blocks unreadable

* Applied changes from jslint

* Updated screenshot
  • Loading branch information
user00265 authored Sep 8, 2022
1 parent 4333872 commit 35dbc78
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 58 deletions.
11 changes: 7 additions & 4 deletions exampleSite/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ copyright = "© Copyright notice"

featherIconsCDN = false

# Site color - dark/light/auto
mode = "auto"

# Enable a table of contents
toc = true

# Max tags
maxTags = 7
maxTags = 7

# Define words per minute to approximate reading time
wpm = 250
# Define words per minute to approximate reading time
wpm = 250

# Stupid footers :)
footers = [
Expand All @@ -35,7 +38,7 @@ copyright = "© Copyright notice"
]

# Github base url
github = "https://github.com/chollinger93/ink-free"
github = "https://github.com/chollinger93/ink-free"


# Nav.
Expand Down
Binary file modified images/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion layouts/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<!-- <html {{- if (eq .Site.Params.mode "dark") -}}class="dark"{{ end }}> -->
<html {{- if (eq .Site.Params.mode "dark") -}}class="dark"{{ end }}>
{{ partial "header.html" . }}
<body>
<div class="container wrapper">
Expand Down
9 changes: 4 additions & 5 deletions layouts/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ <h1 class="site-title"><a href="{{ .Site.BaseURL }}">{{ .Site.Title }}</a></h1>
{{- range $index, $key := .Site.Params.Social -}}
<li><a href="{{ $key.url }}" title="{{ $key.name }}"><i data-feather="{{ $key.icon }}"></i></a></li>
{{- end -}}

{{- if or (eq .Site.Params.mode "auto") (eq .Site.Params.mode "dark") -}}
<span class="scheme-toggle"><a href="#" id="scheme-toggle"></a>
{{- end -}}
</ul>
</nav>
<!-- Disabled
{{- if or (eq .Site.Params.mode "auto") (eq .Site.Params.mode "dark") -}}
<span class="scheme-toggle"><a href="#" id="scheme-toggle"></a>
{{- end -}}
-->
</div>

<nav class="nav">
Expand Down
14 changes: 14 additions & 0 deletions static/css/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,17 @@ html.dark .footer {
html.dark .highlight {
background-color: #333;
}

html.dark .header .site-description .scheme-toggle a svg {
fill: #f8e04f !important;
color: #f8e04f !important;
}

html.dark td pre code {
color: #333;
}

html.dark code:not(pre *) {
color: #ddd;
background-color: #333;
}
10 changes: 1 addition & 9 deletions static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,6 @@ ul {

.header .site-description .scheme-toggle a svg {
max-height: 15px;
}

.header .site-description .scheme-toggle a.dark svg {
fill: #f8e04f;
color: #f8e04f;
}

.header .site-description .scheme-toggle a.light svg {
fill: grey;
color: black;
}
Expand Down Expand Up @@ -521,4 +513,4 @@ code:not(pre *) {
font-size: 1em;
background-color: rgb(239, 241, 243);
font-size: 95%;
}
}
74 changes: 35 additions & 39 deletions static/js/main.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
document.addEventListener("DOMContentLoaded", function(){
var toggle = document.getElementById("scheme-toggle");
"use strict";

var scheme = "light";
var savedScheme = localStorage.getItem("scheme");
function toggleDarkMode() {
var theme = localStorage.getItem("scheme"),
toggle = document.getElementById("scheme-toggle"),
container = document.getElementsByTagName("html")[0];

var container = document.getElementsByTagName("html")[0];
var prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;

if (prefersDark) {
scheme = "dark";
}
if (theme === "dark") {
toggle.innerHTML = feather.icons.sun.toSvg();
container.className = "dark";
} else {
toggle.innerHTML = feather.icons.moon.toSvg();
container.className = "";
}
}

if(savedScheme) {
scheme = savedScheme;
}
document.addEventListener("DOMContentLoaded", function () {
const globalDark = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
const localMode = localStorage.getItem("scheme");
const mode = document.getElementById("scheme-toggle");

if (toggle) {
if(scheme == "dark") {
darkscheme(toggle, container);
} else {
lightscheme(toggle, container);
if (globalDark && (localMode === null)) {
localStorage.setItem("scheme", "dark");
}

toggle.addEventListener("click", () => {
if (toggle.className === "light") {
darkscheme(toggle, container);
} else if (toggle.className === "dark") {
lightscheme(toggle, container);
}
});
}
});
if (mode !== null) {
toggleDarkMode();

window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", function (event) {
if (event.matches) {
localStorage.setItem("scheme", "dark");
} else {
localStorage.setItem("scheme", "light");
}

function darkscheme(toggle, container) {
localStorage.setItem("scheme", "dark");
toggle.innerHTML = feather.icons.sun.toSvg();
toggle.className = "dark";
container.className = "dark";
}
toggleDarkMode();
});

function lightscheme(toggle, container) {
localStorage.setItem("scheme", "light");
toggle.innerHTML = feather.icons.moon.toSvg();
toggle.className = "light";
container.className = "";
}
mode.addEventListener("click", function () {
localStorage.setItem("scheme", document.documentElement.classList.contains('dark') ? "light" : "dark");
toggleDarkMode();
});
}
});

0 comments on commit 35dbc78

Please sign in to comment.