forked from django/django
-
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.
Fixed #34670 -- Improved loading of theme in admin.
Thanks Sarah Abderemane for the review.
- Loading branch information
Showing
2 changed files
with
40 additions
and
45 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 |
---|---|---|
@@ -1,56 +1,51 @@ | ||
'use strict'; | ||
{ | ||
window.addEventListener('load', function(e) { | ||
|
||
function setTheme(mode) { | ||
if (mode !== "light" && mode !== "dark" && mode !== "auto") { | ||
console.error(`Got invalid theme mode: ${mode}. Resetting to auto.`); | ||
mode = "auto"; | ||
} | ||
document.documentElement.dataset.theme = mode; | ||
localStorage.setItem("theme", mode); | ||
function setTheme(mode) { | ||
if (mode !== "light" && mode !== "dark" && mode !== "auto") { | ||
console.error(`Got invalid theme mode: ${mode}. Resetting to auto.`); | ||
mode = "auto"; | ||
} | ||
document.documentElement.dataset.theme = mode; | ||
localStorage.setItem("theme", mode); | ||
} | ||
|
||
function cycleTheme() { | ||
const currentTheme = localStorage.getItem("theme") || "auto"; | ||
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
function cycleTheme() { | ||
const currentTheme = localStorage.getItem("theme") || "auto"; | ||
const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; | ||
|
||
if (prefersDark) { | ||
// Auto (dark) -> Light -> Dark | ||
if (currentTheme === "auto") { | ||
setTheme("light"); | ||
} else if (currentTheme === "light") { | ||
setTheme("dark"); | ||
} else { | ||
setTheme("auto"); | ||
} | ||
if (prefersDark) { | ||
// Auto (dark) -> Light -> Dark | ||
if (currentTheme === "auto") { | ||
setTheme("light"); | ||
} else if (currentTheme === "light") { | ||
setTheme("dark"); | ||
} else { | ||
// Auto (light) -> Dark -> Light | ||
if (currentTheme === "auto") { | ||
setTheme("dark"); | ||
} else if (currentTheme === "dark") { | ||
setTheme("light"); | ||
} else { | ||
setTheme("auto"); | ||
} | ||
setTheme("auto"); | ||
} | ||
} else { | ||
// Auto (light) -> Dark -> Light | ||
if (currentTheme === "auto") { | ||
setTheme("dark"); | ||
} else if (currentTheme === "dark") { | ||
setTheme("light"); | ||
} else { | ||
setTheme("auto"); | ||
} | ||
} | ||
} | ||
|
||
function initTheme() { | ||
// set theme defined in localStorage if there is one, or fallback to auto mode | ||
const currentTheme = localStorage.getItem("theme"); | ||
currentTheme ? setTheme(currentTheme) : setTheme("auto"); | ||
} | ||
|
||
function setupTheme() { | ||
// Attach event handlers for toggling themes | ||
const buttons = document.getElementsByClassName("theme-toggle"); | ||
Array.from(buttons).forEach((btn) => { | ||
btn.addEventListener("click", cycleTheme); | ||
}); | ||
initTheme(); | ||
} | ||
function initTheme() { | ||
// set theme defined in localStorage if there is one, or fallback to auto mode | ||
const currentTheme = localStorage.getItem("theme"); | ||
currentTheme ? setTheme(currentTheme) : setTheme("auto"); | ||
} | ||
|
||
setupTheme(); | ||
window.addEventListener('load', function(_) { | ||
const buttons = document.getElementsByClassName("theme-toggle"); | ||
Array.from(buttons).forEach((btn) => { | ||
btn.addEventListener("click", cycleTheme); | ||
}); | ||
}); | ||
|
||
initTheme(); | ||
} |
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