-
-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfac516
commit 7405b99
Showing
3 changed files
with
36 additions
and
115 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 |
---|---|---|
@@ -1,38 +1,34 @@ | ||
(function () { | ||
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)') | ||
|
||
function updateToggle (darkMode) { | ||
const checkbox = document.querySelector(".theme-toggle input[type='checkbox']") | ||
if (checkbox) { | ||
checkbox.checked = darkMode | ||
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)') | ||
let initialTheme | ||
if (localStorage.getItem('darkmode-pref-set') === 'true') { | ||
initialTheme = localStorage.getItem('darkMode') === 'true' | ||
} else { | ||
initialTheme = darkModeMediaQuery.matches | ||
} | ||
|
||
document.body.classList.toggle('dark-mode', darkMode) | ||
localStorage.setItem('darkMode', darkMode) | ||
localStorage.setItem('darkmode-pref-set', 'true') | ||
} | ||
function updateToggle (darkMode) { | ||
document.querySelector(".theme-toggle input[type=radio][value='dark']").checked = darkMode | ||
document.querySelector(".theme-toggle input[type=radio][value='light']").checked = !darkMode | ||
|
||
document.body.classList.toggle('dark-mode', darkMode) | ||
localStorage.setItem('darkMode', darkMode) | ||
localStorage.setItem('darkmode-pref-set', 'true') | ||
} | ||
|
||
// Listen for system theme changes | ||
darkModeMediaQuery.addEventListener('change', (e) => { | ||
updateToggle(e.matches) | ||
}) | ||
darkModeMediaQuery.addEventListener('change', (e) => { | ||
const darkModeOn = e.matches | ||
updateToggle(darkModeOn) | ||
}) | ||
|
||
// Set up event listener and initial theme on page load | ||
window.addEventListener('load', function () { | ||
const checkbox = document.querySelector(".theme-toggle input[type='checkbox']") | ||
if (checkbox) { | ||
checkbox.addEventListener('change', function () { | ||
updateToggle(checkbox.checked) | ||
}) | ||
window.addEventListener('load', function () { | ||
const radios = document.querySelectorAll('.theme-toggle input[type=radio]') | ||
radios.forEach((radio) => { | ||
radio.addEventListener('change', function (e) { | ||
updateToggle(e.target.value === 'dark') | ||
}) | ||
}) | ||
|
||
// Initialize theme based on user preference or system preference | ||
let initialTheme | ||
if (localStorage.getItem('darkmode-pref-set') === 'true') { | ||
initialTheme = localStorage.getItem('darkMode') === 'true' | ||
} else { | ||
initialTheme = darkModeMediaQuery.matches | ||
} | ||
updateToggle(initialTheme) | ||
} | ||
}) | ||
updateToggle(initialTheme) | ||
}) | ||
})() |
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