Created
May 31, 2024 14:13
-
-
Save oelna/0bfbbbf30d2de92b6bb33766cd7bd1bf to your computer and use it in GitHub Desktop.
CSS light/dark mode with manual toggle
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
<!doctype html> | |
<html lang="en" data-theme="light"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>dark mode</title> | |
<link rel="stylesheet" href="style.css" /> | |
<script src="script.js" defer></script> | |
</head> | |
<body> | |
<label class="theme-switch" for="cb"> | |
<input type="checkbox" id="cb" /> Toggle Mode | |
</label> | |
</body> | |
</html> |
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
/* manual toggle */ | |
const toggleSwitch = document.querySelector('.theme-switch input'); | |
toggleSwitch.addEventListener('change', function (e) { | |
if (e.target.checked) { | |
document.documentElement.setAttribute('data-theme', 'dark'); | |
localStorage.setItem('theme', 'dark'); | |
} else { | |
document.documentElement.setAttribute('data-theme', 'light'); | |
localStorage.setItem('theme', 'light'); | |
} | |
}, false); | |
/* read localstorage on load */ | |
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null; | |
if (currentTheme) { // localstorage setting exists | |
document.documentElement.setAttribute('data-theme', currentTheme); | |
if (currentTheme === 'dark') { | |
toggleSwitch.checked = true; | |
} | |
} else { // localstorage setting missing, read OS setting | |
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { | |
document.documentElement.setAttribute('data-theme', 'dark'); | |
toggleSwitch.checked = true; | |
} | |
} | |
// react to OS setting change, if no localstorage setting exists | |
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function (event) { | |
if (currentTheme) return; | |
const newColorScheme = event.matches ? 'dark' : 'light'; | |
document.documentElement.setAttribute('data-theme', newColorScheme); | |
if (newColorScheme === 'dark') { | |
toggleSwitch.checked = true; | |
} else { | |
toggleSwitch.checked = false; | |
} | |
}); |
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
/* | |
@media (prefers-color-scheme: light) { | |
:root { | |
} | |
} | |
@media (prefers-color-scheme: dark) { | |
:root { | |
--test: #333; | |
} | |
} | |
*/ | |
:root { | |
color-scheme: light dark; | |
--test: #fff; | |
} | |
[data-theme="dark"] { | |
--test: #333; | |
} | |
html { | |
background: var(--test); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
builds on my previous effort:
https://gist.github.com/oelna/2428a4c468ac96681173f029a38fd723