From d2900593e8747f83038e5246529d2bef46fa9775 Mon Sep 17 00:00:00 2001 From: Matias Grunberg Date: Thu, 29 Aug 2024 23:15:51 -0300 Subject: [PATCH] move theme setup script into a partial Keep the logic to intialize the them "private". Users don't need to know the JS we use when they install views. Simplify the update process for users if we ever make any change to that. --- app/views/active_admin/_html_head.html.erb | 10 ++-------- app/views/active_admin/shared/_theme_setup.erb | 8 ++++++++ 2 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 app/views/active_admin/shared/_theme_setup.erb diff --git a/app/views/active_admin/_html_head.html.erb b/app/views/active_admin/_html_head.html.erb index afbfd32ae28..9fc69f03eea 100644 --- a/app/views/active_admin/_html_head.html.erb +++ b/app/views/active_admin/_html_head.html.erb @@ -2,12 +2,6 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> -<% # On page load or when changing themes, best to add inline in `head` to avoid FOUC %> -<%= javascript_tag nonce: true do %> - if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { - document.documentElement.classList.add('dark') - } else { - document.documentElement.classList.remove('dark') - } -<% end %> + +<%= render 'active_admin/shared/theme_setup' %> <%= javascript_importmap_tags "active_admin", importmap: ActiveAdmin.importmap %> diff --git a/app/views/active_admin/shared/_theme_setup.erb b/app/views/active_admin/shared/_theme_setup.erb new file mode 100644 index 00000000000..5f1feda2d9b --- /dev/null +++ b/app/views/active_admin/shared/_theme_setup.erb @@ -0,0 +1,8 @@ +<% # On page load or when changing themes, best to add inline in `head` to avoid FOUC %> +<%= javascript_tag nonce: true do %> + if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { + document.documentElement.classList.add('dark') + } else { + document.documentElement.classList.remove('dark') + } +<% end %>