Implement a switch for switching between light and dark mode

This commit is contained in:
Philipp Oppermann
2020-12-28 20:47:17 +01:00
parent eb767523a5
commit 300a6f452a
5 changed files with 81 additions and 3 deletions

View File

@@ -88,6 +88,13 @@ html {
--post-title-color: #c8c8ff;
}
@mixin light-switch-light {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23004' class='bi bi-moon' viewBox='0 0 16 16'%3E%3Cpath fill-rule='evenodd' d='M14.53 10.53a7 7 0 0 1-9.058-9.058A7.003 7.003 0 0 0 8 15a7.002 7.002 0 0 0 6.53-4.47z'/%3E%3C/svg%3E");
}
@mixin light-switch-dark {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='%23ff9' class='bi bi-brightness-high-fill' viewBox='0 0 16 16'%3E%3Cpath d='M12 8a4 4 0 1 1-8 0 4 4 0 0 1 8 0zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z'/%3E%3C/svg%3E");
}
body {
@include set-colors-light();
@@ -97,6 +104,14 @@ body.dark {
@include set-colors-dark();
}
.light-switch {
@include light-switch-light();
}
body.dark .light-switch {
@include light-switch-dark();
}
/* Styles for users who prefer dark mode at the OS level */
@media (prefers-color-scheme: dark) {
@@ -108,8 +123,15 @@ body.dark {
body.light {
@include set-colors-light();
}
}
.light-switch {
@include light-switch-dark();
}
body.light .light-switch {
@include light-switch-light();
}
}
body {
color: var(--text-color);
@@ -942,4 +964,21 @@ a strong {
.left-to-right, .right-to-left pre {
direction: ltr;
}
}
.light-switch {
position: fixed;
left: 2rem;
bottom: 2rem;
background-repeat: no-repeat;
width: 2rem;
height: 2rem;
cursor: pointer;
opacity: 0.6;
}
.light-switch:hover {
transform: scale(1.3);
transition: 200ms ease-out;
opacity: 1;
}

View File

@@ -61,3 +61,29 @@ function toc_scroll_position(container) {
current_toc_item.classList.add("active");
}
}
function toggle_lights() {
var body = document.querySelector("body");
var comment_form = document.querySelector("iframe.utterances-frame");
if (body != null) {
if (body.classList.contains("dark")) {
body.classList.replace("dark", "light");
if (comment_form != null) {
comment_form.contentWindow.postMessage(
{ type: "set-theme", theme: "github-light" },
"https://utteranc.es/"
)
}
} else {
body.classList.remove("light");
body.classList.add("dark");
if (comment_form != null) {
comment_form.contentWindow.postMessage(
{ type: "set-theme", theme: "github-dark" },
"https://utteranc.es/"
)
}
}
console.log(body)
}
}

View File

@@ -41,6 +41,8 @@
</footer>
</div>
<div class="light-switch" onclick="toggle_lights()"></div>
<!-- Fathom - simple website analytics - https://github.com/usefathom/fathom -->
<script>
(function(f, a, t, h, o, m){

View File

@@ -98,7 +98,7 @@
</p>
{% endif %}
{{ macros::utterances() }}
{{ macros::utterances_edition_3() }}
</section>
<aside class="page-aside-right">

View File

@@ -54,6 +54,17 @@
</script>
{% endmacro utterances %}
{% macro utterances_edition_3() %}
<script src="https://utteranc.es/client.js"
data-repo="phil-opp/blog_os"
data-issue-term="url"
data-label="comments"
crossorigin="anonymous"
theme="preferred-color-scheme"
async>
</script>
{% endmacro utterances_edition_3 %}
{% macro toc(toc) %}
<details id = "toc-inline">
<summary><b>Table of Contents</b></summary>