forked from dungeon-revealer/dungeon-revealer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add dark mode toggle functionality
- Loading branch information
1 parent
2f56733
commit a9af577
Showing
1 changed file
with
34 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const themeToggleButton = document.getElementById('theme-toggle'); | ||
const themePreferenceKey = 'userThemePreference'; | ||
const darkThemePath = 'wiki/darkmode.css'; | ||
|
||
function applyTheme(theme) { | ||
let link = document.querySelector('link[href="' + darkThemePath + '"]'); | ||
if (theme === 'dark') { | ||
if (!link) { | ||
link = document.createElement('link'); | ||
link.rel = 'stylesheet'; | ||
link.href = darkThemePath; | ||
document.head.appendChild(link); | ||
} | ||
} else { | ||
if (link) { | ||
document.head.removeChild(link); | ||
} | ||
} | ||
} | ||
|
||
function loadTheme() { | ||
const savedTheme = localStorage.getItem(themePreferenceKey); | ||
applyTheme(savedTheme); | ||
} | ||
|
||
function toggleTheme() { | ||
const currentTheme = localStorage.getItem(themePreferenceKey) === 'dark' ? 'light' : 'dark'; | ||
localStorage.setItem(themePreferenceKey, currentTheme); | ||
applyTheme(currentTheme); | ||
} | ||
|
||
themeToggleButton.addEventListener('click', toggleTheme); | ||
|
||
loadTheme(); |