-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for dark/light theme The browser theme is initially used. A switch in the navbar let the user choose their theme. The selection is saved into localStorage for persistency Fixes #23
- Loading branch information
Showing
5 changed files
with
118 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Get color theme based on previous user choice or browser theme | ||
*/ | ||
export const getThemeMode = () => { | ||
let themeMode = localStorage.getItem("theme-mode"); | ||
|
||
if (themeMode === null) { | ||
const isDark = | ||
window.matchMedia && | ||
window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
themeMode = (isDark && "dark") || "light"; | ||
|
||
localStorage.setItem("theme-mode", themeMode); | ||
} | ||
|
||
return themeMode; | ||
}; | ||
|
||
/** | ||
* Set light or dark theme | ||
*/ | ||
export const updateThemeMode = () => { | ||
if (getThemeMode() === "dark") { | ||
document.body.classList.add("dark-theme"); | ||
document | ||
.getElementById("navbar") | ||
.classList.replace("navbar-light", "navbar-dark"); | ||
document.getElementById("navbar").classList.replace("bg-light", "bg-dark"); | ||
} else { | ||
document.body.classList.remove("dark-theme"); | ||
document | ||
.getElementById("navbar") | ||
.classList.replace("navbar-dark", "navbar-light"); | ||
document.getElementById("navbar").classList.replace("bg-dark", "bg-light"); | ||
} | ||
}; |