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 %>