Fix dark light mode flicker

This commit is contained in:
Nicky van Urk
2020-09-14 18:42:29 +02:00
parent fefff75774
commit 484d30b622
6 changed files with 94 additions and 19 deletions

View File

@@ -6,7 +6,27 @@ layout: compress
<html lang="{{ page.lang | default: site.lang | default: " en " }}"> <html lang="{{ page.lang | default: site.lang | default: " en " }}">
{% include header.html %} {% include header.html %}
<body> <body data-theme="{{ site.mode }}" class="notransition">
<script>
const body = document.body;
const data = body.getAttribute("data-theme");
const initTheme = (state) => {
if (state === "dark") {
body.setAttribute("data-theme", "dark");
} else if (state === "light") {
body.removeAttribute("data-theme");
} else {
localStorage.setItem("theme", data);
}
};
initTheme(localStorage.getItem("theme"));
setTimeout(() => body.classList.remove("notransition"), 75);
</script>
{% include navbar.html %} {% include navbar.html %}
<div class="wrapper"> <div class="wrapper">
<main aria-label="Content"> <main aria-label="Content">

View File

@@ -5,7 +5,27 @@ layout: compress
<html lang="{{ page.lang | default: site.lang | default: " en " }}"> <html lang="{{ page.lang | default: site.lang | default: " en " }}">
{% include header.html %} {% include header.html %}
<body data-theme="{{ site.mode }}"> <body data-theme="{{ site.mode }}" class="notransition">
<script>
const body = document.body;
const data = body.getAttribute("data-theme");
const initTheme = (state) => {
if (state === "dark") {
body.setAttribute("data-theme", "dark");
} else if (state === "light") {
body.removeAttribute("data-theme");
} else {
localStorage.setItem("theme", data);
}
};
initTheme(localStorage.getItem("theme"));
setTimeout(() => body.classList.remove("notransition"), 75);
</script>
{% include navbar.html %} {% include navbar.html %}
<div class="wrapper"> <div class="wrapper">
{% include author.html %} {% include author.html %}

View File

@@ -7,7 +7,27 @@ layout: compress
{% include header.html %} {% include header.html %}
<body data-theme="{{ site.mode }}"> <body data-theme="{{ site.mode }}" class="notransition">
<script>
const body = document.body;
const data = body.getAttribute("data-theme");
const initTheme = (state) => {
if (state === "dark") {
body.setAttribute("data-theme", "dark");
} else if (state === "light") {
body.removeAttribute("data-theme");
} else {
localStorage.setItem("theme", data);
}
};
initTheme(localStorage.getItem("theme"));
setTimeout(() => body.classList.remove("notransition"), 75);
</script>
{% include navbar.html %} {% include navbar.html %}
<div class="wrapper"> <div class="wrapper">
<header class="header"> <header class="header">

View File

@@ -7,7 +7,27 @@ layout: compress
{% include header.html %} {% include header.html %}
<body data-theme="{{ site.mode }}"> <body data-theme="{{ site.mode }}" class="notransition">
<script>
const body = document.body;
const data = body.getAttribute("data-theme");
const initTheme = (state) => {
if (state === "dark") {
body.setAttribute("data-theme", "dark");
} else if (state === "light") {
body.removeAttribute("data-theme");
} else {
localStorage.setItem("theme", data);
}
};
initTheme(localStorage.getItem("theme"));
setTimeout(() => body.classList.remove("notransition"), 75);
</script>
{% include navbar.html %} {% include navbar.html %}
<div class="wrapper post"> <div class="wrapper post">
<main class="page-content" aria-label="Content"> <main class="page-content" aria-label="Content">
@@ -55,7 +75,7 @@ layout: compress
</article> </article>
{% if page.comments %} {% if page.comments %}
{% include comments.html%} {% include comments.html %}
{% endif %} {% endif %}
</main> </main>
@@ -71,4 +91,4 @@ layout: compress
</div> </div>
</body> </body>
</html> </html>

View File

@@ -7,6 +7,14 @@
transition: background-color 75ms ease-in, border-color 75ms ease-in; transition: background-color 75ms ease-in, border-color 75ms ease-in;
} }
.notransition {
-webkit-transition: none;
-moz-transition: none;
-ms-transition: none;
-o-transition: none;
transition: none;
}
html { html {
overflow-x: hidden; overflow-x: hidden;
width: 100%; width: 100%;

View File

@@ -2,17 +2,6 @@
// Theme switch // Theme switch
const body = document.body; const body = document.body;
const lamp = document.getElementById("mode"); const lamp = document.getElementById("mode");
const data = body.getAttribute("data-theme");
const initTheme = (state) => {
if (state === "dark") {
body.setAttribute("data-theme", "dark");
} else if (state === "light") {
body.removeAttribute("data-theme");
} else {
localStorage.setItem("theme", data);
}
};
const toggleTheme = (state) => { const toggleTheme = (state) => {
if (state === "dark") { if (state === "dark") {
@@ -26,8 +15,6 @@
} }
}; };
initTheme(localStorage.getItem("theme"));
lamp.addEventListener("click", () => lamp.addEventListener("click", () =>
toggleTheme(localStorage.getItem("theme")) toggleTheme(localStorage.getItem("theme"))
); );