Skip to content

Commit ede1b4c

Browse files
Added button to switch themes
1 parent dd0a69a commit ede1b4c

7 files changed

Lines changed: 392 additions & 329 deletions

File tree

layouts/partials/header.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<script>
1313
$(document).ready(function() {
1414
$('table').addClass("table table-hover table-striped")
15+
setThemeMode(localStorage.getItem("dark-mode-theme") || "dark");
1516
})
1617
</script>
1718
{{ template "_internal/google_analytics.html" . }}

layouts/partials/header.includes.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<link href="/css/custom.css" rel="stylesheet">
44
<link href="/css/monokai.css" rel="stylesheet">
55
<link href="/css/highlight/solarized_dark.css" rel="stylesheet">
6+
67

78
<script src="/js/highlight.pack.js"></script>
89

@@ -22,3 +23,17 @@
2223
<script defer src="/fontawesome/js/solid.min.js"></script>
2324
<script defer src="/fontawesome/js/brands.min.js"></script>
2425
<script defer src="/fontawesome/js/fontawesome.min.js"></script>
26+
27+
<script>
28+
var head = document.getElementsByTagName('head')[0]
29+
var style = document.createElement('link')
30+
style.type = 'text/css'
31+
style.rel = 'stylesheet'
32+
style.id = "dark-theme"
33+
if (localStorage.getItem("dark-mode-theme") == "dark") {
34+
style.href = '/css/dark.css'
35+
} else {
36+
style.href = '/css/light.css'
37+
}
38+
head.append(style)
39+
</script>

layouts/partials/navbar.html

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
{{ range .Site.Menus.main }}
1515
<li><a href="{{.URL}}">{{ .Name }}</a></li>
1616
{{ end }}
17-
18-
<!--
19-
And here is where you'd add more links to sections, or anywhere you like.
20-
<li><a href="#">About Me</a></li>
21-
-->
17+
<li>
18+
<a id="dark-toggle" class="dark-mode-toggle" aria-label="dark-mode-toggle" style="color: var(--Red); font-size: 20px">
19+
<i class="fas fa-sun"></i>
20+
</a>
21+
</li>
2222
</ul>
2323
<div id="custom-search-input" class="pull-right">
2424
<form class="navbar-form" role="search" method="get" action="https://www.bing.com/search">
@@ -33,6 +33,37 @@
3333
</div>
3434
</form>
3535
</div>
36+
3637
</div>
38+
3739
</div>
3840
</nav>
41+
42+
<script type="text/javascript">
43+
const darkIconClass = 'fas fa-moon'
44+
const lightIconClass = 'fas fa-sun'
45+
var darkCSS = $("#dark-theme");
46+
var darkToggleIcon = $("#dark-toggle i");
47+
48+
// Set theme with the given mode and toggle the theme icon button
49+
// The button won't actually change for some reason on page load, will
50+
// change when its pressed though.
51+
function setThemeMode(mode) {
52+
localStorage.setItem("dark-mode-theme", mode);
53+
if (mode === "dark") {
54+
darkToggleIcon.attr('class', darkIconClass)
55+
} else {
56+
darkToggleIcon.attr('class', lightIconClass)
57+
}
58+
}
59+
60+
// Toggle dark mode when user click on the icon button
61+
$("#dark-toggle").click(function () {
62+
if (darkToggleIcon.attr("class") == lightIconClass) {
63+
setThemeMode("dark")
64+
} else if (darkToggleIcon.attr("class") == darkIconClass) {
65+
setThemeMode("light")
66+
}
67+
location.reload();
68+
});
69+
</script>

0 commit comments

Comments
 (0)