-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
style: toggle for dark/light mode integrated
- Loading branch information
1 parent
c66e3a2
commit e761216
Showing
3 changed files
with
121 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
--- | ||
|
||
<div class="theme-switch-container"> | ||
<a href="#" id="color-scheme-toggle">Toggle Color Scheme</a> | ||
</div> | ||
<style> | ||
.theme-switch-container { | ||
position: fixed; | ||
right: 0; | ||
top: 0; | ||
font-size: 0.8rem; | ||
margin: 8px 8px 0 0; | ||
} | ||
/** | ||
* On smaller screens the toggle should be centered on top of the page | ||
*/ | ||
@media only screen and (max-width: 64em) { | ||
.theme-switch-container { | ||
width: 100%; | ||
position: initial; | ||
display: flex; | ||
justify-content: center; | ||
} | ||
} | ||
</style> | ||
<script is:inline> | ||
const darkModeToggle = document.getElementById("color-scheme-toggle"); | ||
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)"); | ||
|
||
let isDarkMode = prefersDarkScheme.matches; | ||
|
||
const toggleDarkMode = (event) => { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
isDarkMode = !isDarkMode; | ||
if (isDarkMode) { | ||
document.body.classList.add("dark-mode"); | ||
document.body.classList.remove("light-mode"); | ||
} else { | ||
document.body.classList.add("light-mode"); | ||
document.body.classList.remove("dark-mode"); | ||
} | ||
}; | ||
|
||
darkModeToggle.addEventListener("click", toggleDarkMode); | ||
|
||
// Initialize based on system preference | ||
if (prefersDarkScheme.matches) { | ||
document.body.classList.remove("light-mode"); | ||
document.body.classList.add("dark-mode"); | ||
} else { | ||
document.body.classList.add("light-mode"); | ||
document.body.classList.remove("dark-mode"); | ||
} | ||
</script> |
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