Skip to content

Commit

Permalink
Add dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
palinkiewicz committed Aug 5, 2024
1 parent 9c18aaf commit f18b4b4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
30 changes: 29 additions & 1 deletion classes/DOMHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,18 @@ class DOMHandler {
/** @type {Element} */
this.songImage = document.querySelector(".song-image");

/** @type {Element} */
this.toggleDarkMode = document.querySelector("#dark-mode-toggle");

this.populateColorSelection();
this.setListeners();

this.setBase64Image(SPOTIFY_LOGO, ".song-image > .spotify > img", 0);
this.setTheme(
localStorage.getItem("theme") ??
(window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light")
);
}

/**
Expand Down Expand Up @@ -159,6 +167,12 @@ class DOMHandler {
);
});
});

this.toggleDarkMode.addEventListener("click", () => {
this.setTheme(
document.body.classList.contains("dark-mode") ? "light" : "dark"
);
});
}

/**
Expand Down Expand Up @@ -625,4 +639,18 @@ class DOMHandler {
}
});
}

/**
* Sets color theme
* @param {string} theme
*/
setTheme(theme) {
if (theme === "dark") {
document.body.classList.add("dark-mode");
localStorage.setItem("theme", "dark");
} else {
document.body.classList.remove("dark-mode");
localStorage.setItem("theme", "light");
}
}
}
13 changes: 10 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<link rel="stylesheet" href="styles/song-image.css">
<title>Lyrics Image Generator</title>
</head>
<body>
<body class="dark-mode">
<main>
<section class="lyrics-image-screen search-form" data-number="1">
<div class="screen-wrapper">
Expand Down Expand Up @@ -97,8 +97,15 @@ <h2>Song image</h2>
</section>
</main>
<footer class="surface">
Made by <a href="https://github.com/palinkiewicz" target="_blank">palinkiewicz</a>
| <a href="https://ko-fi.com/palinkiewicz">Buy me a coffee</a>
<div>
Made by
<a href="https://github.com/palinkiewicz" target="_blank">palinkiewicz</a>
|
<a href="https://ko-fi.com/palinkiewicz">Ko-Fi</a>
</div>
<div id="dark-mode-toggle">
<span class="material-symbols-outlined"></span>
</div>
</footer>
<script src="classes/data/Artist.js"></script>
<script src="classes/data/Lyric.js"></script>
Expand Down
31 changes: 30 additions & 1 deletion styles/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,35 @@ header > .go-to-screen:hover, #download:hover, #last-go-back:hover {
footer {
text-align: center;
padding: 2.1rem;
height: 6rem;
height: 8rem;
font-size: 1.2rem;
}

#dark-mode-toggle {
display: flex;
justify-content: center;
align-items: center;
gap: 0.3rem;
font-weight: 600;
padding-top: 0.5rem;
}

#dark-mode-toggle:hover {
cursor: pointer;
}

#dark-mode-toggle::before {
content: "Switch to dark";
}

.dark-mode #dark-mode-toggle::before {
content: "Switch to light";
}

#dark-mode-toggle > span::after {
content: "dark_mode";
}

.dark-mode #dark-mode-toggle > span::after {
content: "light_mode";
}
2 changes: 1 addition & 1 deletion styles/wizard.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

.screen-wrapper {
width: 100vw;
min-height: calc(100vh - 6rem);
min-height: calc(100vh - 8rem);
display: flex;
flex-direction: column;
justify-content: center;
Expand Down

0 comments on commit f18b4b4

Please sign in to comment.