Skip to content

Commit

Permalink
Merge pull request #36 from beeceej/dark-mode
Browse files Browse the repository at this point in the history
Add ability to toggle between light/dark mode
  • Loading branch information
beeceej authored Jul 2, 2024
2 parents 02542c4 + 6fdfc4b commit a59d08d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ TEMPLATE := templates
OUTPUT := output
CONTENT := content
TPL := $(wildcard $(TEMPLATE)/*.css) \
$(shell find templates -name "*.html")
$(shell find templates -name "*.html") \
$(shell find templates -name "*.js")
TPL_OUT := $(patsubst $(TEMPLATE)%, $(OUTPUT)%, $(TPL))
STATIC := $(wildcard $(TEMPLATE)/*.png) \
$(wildcard $(TEMPLATE)/*.webp) \
Expand All @@ -20,6 +21,9 @@ $(OUTPUT)/%.html: beeceej.com $(TPL)
$(OUTPUT)/%.css: beeceej.com $(TPL)
$(TEMPLATIZE) $@

$(OUTPUT)/%.js: beeceej.com $(TPL)
$(TEMPLATIZE) $@

$(OUTPUT)/%.png: beeceej.com $(STATIC)
cp $(TEMPLATE)/$(notdir $@) $@

Expand Down
10 changes: 8 additions & 2 deletions templates/common.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{{define "common_css"}}

body {
--link-text-color: grey;
--background-color: black;

font-size: 24px;
font-family: monospace;
text-align: center;
margin: 0 auto;
width: 80%;

background: var(--background-color)

}
@media only screen and (max-width: 600px) {
body {
Expand All @@ -14,9 +21,8 @@ body {
}

a {
link-decoration: none;
text-decoration: none;
color: #02071f;
color: var(--link-text-color);
}

a:hover {
Expand Down
46 changes: 46 additions & 0 deletions templates/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{define "common-js"}}

const cssMappings = {
dark: {
"--link-text-color": "grey",
"--background-color": "black",
},
light: {
"--link-text-color": "#02071f",
"--background-color": "white",
},
};

const root = document.querySelector(":root");

function updateColors() {
if (JSON.parse(window.localStorage.getItem("dark-mode-enabled"))) {
Object.entries(cssMappings["dark"]).forEach(([key, value]) => {
document.body.style.setProperty(key, value);
});
return;
}
Object.entries(cssMappings["light"]).forEach(([key, value]) => {
document.body.style.setProperty(key, value);
});
}

function toggleDarkMode(event) {
const darkModeEnabled = window.localStorage.getItem("dark-mode-enabled");
if (darkModeEnabled === "null") {
window.localStorage.setItem("dark-mode-enabled", "true");
return;
}
window.localStorage.setItem(
"dark-mode-enabled",
!JSON.parse(window.localStorage.getItem("dark-mode-enabled")),
);
updateColors();
}

updateColors();

document
.getElementById("dark-mode-toggle-button")
.addEventListener("click", toggleDarkMode);
{{end}}
2 changes: 2 additions & 0 deletions templates/footer.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{ define "footer" }}
<footer>
<button id="dark-mode-toggle-button">Light/Dark</button>
<br>
<a href=https://github.com/beeceej/beeceej.com/tree/{{.CommitHash}}>beeceej/beeceej.com @ {{.CommitHash}}</a>
</footer>
{{ end }}
3 changes: 3 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
{{end}}
{{- template "footer" . -}}
</body>
<script>
{{- template "common-js" . -}}
</script>
</html>
{{end}}
{{- template "index" . -}}
5 changes: 3 additions & 2 deletions templates/nav.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ nav {
top: 0;
margin-bottom: 10px; /*Push content pages down a bit */
border-bottom: solid grey;
background-color: white;
background-color: var(--background-color);

}

/* Styles for the Nav Items */
#nav-list {
text-transform: uppercase;
color: grey;
color: var(--link-text-color);
}
{{end}}
2 changes: 1 addition & 1 deletion templates/nav.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{define "nav"}}
<nav >
<nav>
<ul class="no-list-style" id="nav-list">
<li><a href="/about.html"><h3>About</h3></a></li>
<li><a href="/notes/l.html"><h3>Notes</h3></a></li>
Expand Down

0 comments on commit a59d08d

Please sign in to comment.