Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

fix theme applying based on the 'theme' query param (#3639)

RobertJoonas77ca5abbc834
diff --git a/lib/plausible_web/components/layout.ex b/lib/plausible_web/components/layout.ex
index 48b1e84de1..8dac6c784a 100644
--- a/lib/plausible_web/components/layout.ex
+++ b/lib/plausible_web/components/layout.ex
@@ -7,13 +7,13 @@ defmodule PlausibleWeb.Components.Layout do
~H"""
<script>
(function(){
- var userPref = '<%= current_theme(assigns[:current_user]) %>';
+ var themePref = '<%= theme_preference(assigns) %>';
function reapplyTheme() {
var darkMediaPref = window.matchMedia('(prefers-color-scheme: dark)').matches;
var htmlRef = document.querySelector('html');
var hcaptchaRefs = Array.from(document.getElementsByClassName('h-captcha'));
- var isDark = userPref === 'dark' || (userPref === 'system' && darkMediaPref);
+ var isDark = themePref === 'dark' || (themePref === 'system' && darkMediaPref);
if (isDark) {
htmlRef.classList.add('dark')
@@ -31,6 +31,12 @@ defmodule PlausibleWeb.Components.Layout do
"""
end
- defp current_theme(nil), do: "system"
- defp current_theme(user), do: user.theme
+ defp theme_preference(%{theme: theme}) when not is_nil(theme), do: theme
+
+ defp theme_preference(%{current_user: %Plausible.Auth.User{theme: theme}})
+ when not is_nil(theme) do
+ theme
+ end
+
+ defp theme_preference(_assigns), do: "system"
end
diff --git a/lib/plausible_web/templates/layout/app.html.heex b/lib/plausible_web/templates/layout/app.html.heex
index 19caf15f0b..985251d854 100644
--- a/lib/plausible_web/templates/layout/app.html.heex
+++ b/lib/plausible_web/templates/layout/app.html.heex
@@ -45,7 +45,7 @@
<%= Map.get(assigns, :inner_layout) || @inner_content %>
</main>
- <PlausibleWeb.Components.Layout.theme_script current_user={@conn.assigns[:current_user]} />
+ <PlausibleWeb.Components.Layout.theme_script {Map.take(assigns, [:current_user, :theme])} />
<%= if assigns[:embedded] do %>
<div data-iframe-height></div>