Skip to content

Commit

Permalink
Support switching theme style on mobile devices
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
steffen-wilke committed May 30, 2021
1 parent 9f64338 commit 745abc4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
1 change: 0 additions & 1 deletion darkfx/partials/affix.tmpl.partial
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<div class="icon">
<i aria-hidden="true">☾</i>
</div>
<script type="text/javascript" src="{{_rel}}styles/toggle-theme.js"></script>
</div>

<nav class="bs-docs-sidebar hidden-print hidden-xs hidden-sm affix" id="affix">
Expand Down
29 changes: 29 additions & 0 deletions darkfx/partials/footer.tmpl.partial
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{!Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license. See LICENSE file in the project root for full license information.}}

<footer>
<div class="grad-bottom"></div>
<div class="footer">
<div class="container">
<span class="pull-right">
<a href="#top">Back to top</a>
</span>
<div class="pull-left">
{{{_appFooter}}}
{{^_appFooter}}<span>Generated by <strong>DocFX</strong></span>{{/_appFooter}}
</div>
<div class="toggle-mode pull-right visible-sm visible-xs">
<div class="icon">
<i aria-hidden="true">☀</i>
</div>
<label class="switch">
<input type="checkbox" id="switch-style-m">
<span class="slider round"></span>
</label>
<div class="icon">
<i aria-hidden="true">☾</i>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="{{_rel}}styles/toggle-theme.js"></script>
</footer>
41 changes: 31 additions & 10 deletions darkfx/styles/toggle-theme.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
const sw = document.getElementById("switch-style"), b = document.body;
if (sw && b) {
sw.checked = !window.localStorage || !window.localStorage.getItem("theme") || window.localStorage && localStorage.getItem("theme") === "dark-theme";
b.classList.toggle("dark-theme", sw.checked)
b.classList.toggle("light-theme", !sw.checked)

sw.addEventListener("change", function (){
b.classList.toggle("dark-theme", this.checked)
b.classList.toggle("light-theme", !this.checked)
const sw = document.getElementById("switch-style"), sw_mobile = document.getElementById("switch-style-m"), b = document.body;
if (b) {
function toggleTheme(target, dark) {
target.classList.toggle("dark-theme", dark)
target.classList.toggle("light-theme", !dark)
}

function switchEventListener() {
toggleTheme(b, this.checked);
if (window.localStorage) {
this.checked ? localStorage.setItem("theme", "dark-theme") : localStorage.setItem("theme", "light-theme")
}
})
}

var isDarkTheme = !window.localStorage || !window.localStorage.getItem("theme") || window.localStorage && localStorage.getItem("theme") === "dark-theme";

if(sw && sw_mobile){
sw.checked = isDarkTheme;
sw_mobile.checked = isDarkTheme;

sw.addEventListener("change", switchEventListener);
sw_mobile.addEventListener("change", switchEventListener);

// sync state between switches
sw.addEventListener("change", function() {
sw_mobile.checked = this.checked;
});

sw_mobile.addEventListener("change", function() {
sw.checked = this.checked;
});
}

toggleTheme(b, isDarkTheme);
}

0 comments on commit 745abc4

Please sign in to comment.