Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

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

Alessio Cappa2716fad72cdc
diff --git a/app/javascript/controllers/theme_controller.js b/app/javascript/controllers/theme_controller.js
index 10aa7b9a06..361ae95ed9 100644
--- a/app/javascript/controllers/theme_controller.js
+++ b/app/javascript/controllers/theme_controller.js
@@ -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");
}
}
diff --git a/app/views/layouts/_dark_mode_check.html.erb b/app/views/layouts/_dark_mode_check.html.erb
new file mode 100644
index 0000000000..0a0ccac2c2
--- /dev/null
+++ b/app/views/layouts/_dark_mode_check.html.erb
@@ -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>
\ No newline at end of file
diff --git a/app/views/layouts/shared/_head.html.erb b/app/views/layouts/shared/_head.html.erb
index 8a097475b3..26fd2f29c6 100644
--- a/app/views/layouts/shared/_head.html.erb
+++ b/app/views/layouts/shared/_head.html.erb
@@ -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">