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

Color theme switcher and automatic dark mode based on browser prefers-color-scheme #83

Merged
merged 3 commits into from
Jun 18, 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
50 changes: 50 additions & 0 deletions assets/js/color_mode_switcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";

/**
* Get user preferred theme from their past choice or browser
* @returns {String} User preferred theme
*/
function getPreferredTheme() {
const storedTheme = localStorage.getItem("theme");
if (storedTheme) {
return storedTheme;
}
// Firefox with 'resistFingerprint' activated always returns light
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}

/**
* Update navbar icon to match given theme.
* @param {String} theme - 'dark' or '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);

// On button click, switch
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();
});
});
});
27 changes: 25 additions & 2 deletions assets/scss/includes/utils/_lolight.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,38 @@
}
.ll-pct {
/* operators, punctation */
color: #0550ae;
color: #24292f;
}
.ll-key {
/* keywords */
color: #d73a49;
font-weight: normal;
}
.ll-com {
/* comments */
color: #6a737d;
font-style: italic;
}

[data-bs-theme="dark"] {
.ll-nam {
color: #c9d1d9;
}
.ll-num {
color: #79c0ff;
}
.ll-str {
color: #a5d6ff;
}
.ll-rex {
color: #96cefb;
}
.ll-pct {
color: #c9d1d9;
}
.ll-key {
color: #ff7b72;
}
.ll-com {
color: #8b949e;
}
}
1 change: 1 addition & 0 deletions static/assets/color_mode_switcher.52334129.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion static/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@
"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 All @@ -140,7 +145,7 @@
]
},
"assets/scss/main.scss": {
"file": "assets/main.dad26364.css",
"file": "assets/main.ec6ebc81.css",
"src": "assets/scss/main.scss",
"isEntry": true
}
Expand Down
4 changes: 3 additions & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

{{ Plugins.styles }}

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

<script type="text/javascript">
window.init = {
'urlRoot': "{{ request.script_root }}",
Expand All @@ -22,7 +24,7 @@
'userId': {{ Session.id }},
'userName': {{ User.name | tojson }},
'userEmail': {{ User.email | tojson }},
'teamId': {{ Team.id | tojson }},
'teamId': {{ Team.id | tojson }},
'teamName': {{ Team.name | tojson }},
'start': {{ Configs.start | tojson }},
'end': {{ Configs.end | tojson }},
Expand Down
11 changes: 11 additions & 0 deletions templates/components/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,17 @@
</a>
</li>
{% endif %}
<li class="nav-item">
<button class="nav-link theme-switch" type="button">
<span data-bs-toggle="tooltip" data-bs-placement="bottom" title="{% trans %}Toggle theme{% endtrans %}">
<i class="fas fa-sun d-none d-md-inline"></i>
</span>
<span class="d-sm-inline d-md-none">
<i class="fas fa-sun pe-1"></i>
{% trans %}Toggle theme{% endtrans %}
</span>
</button>
</li>
</ul>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ 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