Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline color mode switcher code to reduce flashing #88

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion static/assets/color_mode_switcher.52334129.js

This file was deleted.

5 changes: 0 additions & 5 deletions static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,6 @@
"assets/js/index.js"
]
},
"assets/js/color_mode_switcher.js": {
"file": "assets/color_mode_switcher.52334129.js",
"src": "assets/js/color_mode_switcher.js",
"isEntry": true
},
"_echarts.128204f2.js": {
"file": "assets/echarts.128204f2.js"
},
Expand Down
39 changes: 37 additions & 2 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,43 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="{{ Configs.ctf_small_icon }}" type="image/x-icon">

<script>
// This script is inlined to reduce color flashing issues
function getPreferredTheme() {
const storedTheme = localStorage.getItem("theme");
if (storedTheme) {
return storedTheme;
}
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
function showActiveTheme(theme) {
const activeThemeIcon = document.querySelector(".theme-switch i.fas");
activeThemeIcon.classList.toggle("fa-moon", theme === "dark");
activeThemeIcon.classList.toggle("fa-sun", theme !== "dark");
}
// Change body theme early to prevent flash
let currentTheme = getPreferredTheme();
document.documentElement.setAttribute("data-bs-theme", currentTheme);
// On browser color-scheme change, update
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
currentTheme = getPreferredTheme();
document.documentElement.setAttribute("data-bs-theme", currentTheme);
showActiveTheme(currentTheme);
});
window.addEventListener("load", () => {
showActiveTheme(currentTheme);
document.querySelectorAll(".theme-switch").forEach(e => {
e.addEventListener("click", ev => {
currentTheme = currentTheme === "light" ? "dark" : "light";
document.documentElement.setAttribute("data-bs-theme", currentTheme);
localStorage.setItem("theme", currentTheme);
showActiveTheme(currentTheme);
ev.preventDefault();
});
});
});
</script>

{{ meta | safe }}

{% block stylesheets %}
Expand All @@ -14,8 +51,6 @@

{{ Plugins.styles }}

{{ Assets.js("assets/js/color_mode_switcher.js", defer=False) }}

<script type="text/javascript">
window.init = {
'urlRoot': "{{ request.script_root }}",
Expand Down
1 change: 0 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export default defineConfig({
users_private: resolve(__dirname, "assets/js/users/private.js"),
users_list: resolve(__dirname, "assets/js/users/list.js"),
main: resolve(__dirname, "assets/scss/main.scss"),
color_mode_switcher: resolve(__dirname, "assets/js/color_mode_switcher.js"),
},
},
},
Expand Down
Loading