fix: Check user's theme preference during page load (#156)

This commit is contained in:
Alessio Cappa
2025-09-23 15:43:36 +02:00
committed by GitHub
parent d2f631ec5c
commit 2716fad72c
3 changed files with 8 additions and 0 deletions

View File

@@ -42,8 +42,10 @@ export default class extends Controller {
// Sets or removes the data-theme attribute
setTheme(isDark) {
if (isDark) {
localStorage.theme = "dark";
document.documentElement.setAttribute("data-theme", "dark");
} else {
localStorage.theme = "light";
document.documentElement.setAttribute("data-theme", "light");
}
}

View File

@@ -0,0 +1,5 @@
<script>
if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.setAttribute("data-theme", "dark");
}
</script>

View File

@@ -10,6 +10,7 @@
<%= combobox_style_tag %>
<%= javascript_importmap_tags %>
<%= render "layouts/dark_mode_check" %>
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, viewport-fit=cover">