From 87ad234f329dff421c2fda6b55686e1701a50127 Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Fri, 12 Dec 2025 14:17:58 +0000 Subject: [PATCH] Apply theme preference before body To avoid any visual flashes of the old theme before the Stimulus controllers load, we can apply the saved theme from `` whenever there is a full page load. This applies to both the regular and public views, as we're doing it in the shared head partial. --- app/views/layouts/_theme_preference.html.erb | 6 ++++++ app/views/layouts/shared/_head.html.erb | 1 + 2 files changed, 7 insertions(+) create mode 100644 app/views/layouts/_theme_preference.html.erb diff --git a/app/views/layouts/_theme_preference.html.erb b/app/views/layouts/_theme_preference.html.erb new file mode 100644 index 000000000..0c33750bd --- /dev/null +++ b/app/views/layouts/_theme_preference.html.erb @@ -0,0 +1,6 @@ +<%= javascript_tag nonce: true do %> + const theme = localStorage.getItem("theme") + if (theme && theme !== "auto") { + document.documentElement.dataset.theme = theme + } +<% end %> diff --git a/app/views/layouts/shared/_head.html.erb b/app/views/layouts/shared/_head.html.erb index ecee1abc0..9ef153df2 100644 --- a/app/views/layouts/shared/_head.html.erb +++ b/app/views/layouts/shared/_head.html.erb @@ -15,6 +15,7 @@ <% turbo_refreshes_with method: :morph, scroll: :preserve %> + <%= render "layouts/theme_preference" %> <%= stylesheet_link_tag :app, "data-turbo-track": "reload" %> <%= javascript_importmap_tags %>