Skip to content

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

Fix darkmode flash (#3625)

Uku Taht17ba44e8db80
diff --git a/assets/js/app.js b/assets/js/app.js
index acd0e4d4ce..3e344c2aa9 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -1,5 +1,4 @@
import "./polyfills/closest"
-import "./apply-theme"
import 'abortcontroller-polyfill/dist/polyfill-patch-fetch'
import Alpine from 'alpinejs'
import "./liveview/live_socket"
diff --git a/assets/js/apply-theme.js b/assets/js/apply-theme.js
deleted file mode 100644
index f715f55800..0000000000
--- a/assets/js/apply-theme.js
+++ /dev/null
@@ -1,37 +0,0 @@
-var pref = document.currentScript.dataset.pref;
-
-function reapplyTheme() {
- var userPref = pref || "system";
- var mediaPref = window.matchMedia('(prefers-color-scheme: dark)').matches;
- var htmlRef = document.querySelector('html');
- var hcaptchaRefs = document.getElementsByClassName('h-captcha');
-
- htmlRef.classList.remove('dark');
- for (let i = 0; i < hcaptchaRefs.length; i++) {
- hcaptchaRefs[i].dataset.theme = "light";
- }
-
- switch (userPref) {
- case "dark":
- htmlRef.classList.add('dark');
- for (let i = 0; i < hcaptchaRefs.length; i++) {
- hcaptchaRefs[i].dataset.theme = "dark";
- }
- break;
- case "system":
- if (mediaPref) {
- htmlRef.classList.add('dark');
- for (let i = 0; i < hcaptchaRefs.length; i++) {
- hcaptchaRefs[i].dataset.theme = "dark";
- }
- }
- break;
- }
-}
-
-reapplyTheme();
-window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', reapplyTheme);
-
-window.onload = function() {
- reapplyTheme();
-};
diff --git a/lib/plausible_web/components/layout.ex b/lib/plausible_web/components/layout.ex
new file mode 100644
index 0000000000..48b1e84de1
--- /dev/null
+++ b/lib/plausible_web/components/layout.ex
@@ -0,0 +1,36 @@
+defmodule PlausibleWeb.Components.Layout do
+ @moduledoc false
+
+ use Phoenix.Component
+
+ def theme_script(assigns) do
+ ~H"""
+ <script>
+ (function(){
+ var userPref = '<%= current_theme(assigns[:current_user]) %>';
+ 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);
+
+ if (isDark) {
+ htmlRef.classList.add('dark')
+ hcaptchaRefs.forEach(function(ref) { ref.dataset.theme = "dark"; });
+ } else {
+ htmlRef.classList.remove('dark');
+ hcaptchaRefs.forEach(function(ref) { ref.dataset.theme = "light"; });
+ }
+ }
+
+ reapplyTheme();
+ window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', reapplyTheme);
+ })()
+ </script>
+ """
+ end
+
+ defp current_theme(nil), do: "system"
+ defp current_theme(user), do: user.theme
+end
diff --git a/lib/plausible_web/templates/layout/app.html.eex b/lib/plausible_web/templates/layout/app.html.eex
deleted file mode 100644
index 81c5bc9ecc..0000000000
--- a/lib/plausible_web/templates/layout/app.html.eex
+++ /dev/null
@@ -1,40 +0,0 @@
-<!DOCTYPE html>
-<html lang="en" class="h-full">
- <head>
- <meta charset="utf-8"/>
- <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
- <meta name="description" content="Plausible is a lightweight and open-source web analytics tool. Your website data is 100% yours and the privacy of your visitors is respected."/>
- <%= if @conn.assigns[:connect_live_socket] do %>
- <meta name="csrf-token" content="<%= Plug.CSRFProtection.get_csrf_token() %>" />
- <meta name="websocket-url" content="<%= websocket_url() %>" />
- <% end %>
- <meta name="robots" content="<%= @conn.private.robots %>" />
- <link rel="icon" type="image/png" sizes="32x32" href="<%= PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_favicon.png") %>">
- <link rel="apple-touch-icon" href="/images/icon/apple-touch-icon.png">
- <title><%= assigns[:title] || "Plausible · Simple, privacy-friendly alternative to Google Analytics" %></title>
- <link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
- <%= render("_tracking.html", assigns) %>
- </head>
- <body class="flex flex-col bg-gray-50 dark:bg-gray-850" style="<%= if [email protected][:embedded], do: "height: 100%;" %> <%= if @conn.assigns[:background], do: "background-color: #{@conn.assigns[:background]}" %>">
- <%= if [email protected][:embedded] do %>
- <%= render("_header.html", assigns) %>
- <%= render("_notice.html", assigns) %>
- <% end %>
-
- <main class="flex-1">
- <%= Map.get(assigns, :inner_layout) || @inner_content %>
- </main>
-
- <%= if @conn.assigns[:embedded] do %>
- <div data-iframe-height></div>
- <script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/embed.content.js") %>"></script>
- <% else %>
- <%= render("_footer.html", assigns) %>
- <% end %>
- <script type="text/javascript" data-pref="<%= @conn.assigns[:theme] || (@conn.assigns[:current_user] && @conn.assigns[:current_user].theme) %>" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
- <%= if @conn.assigns[:load_dashboard_js] do %>
- <script type="text/javascript" src="<%= Routes.static_path(@conn, "/js/dashboard.js") %>"></script>
- <% end %>
- </body>
-</html>
diff --git a/lib/plausible_web/templates/layout/app.html.heex b/lib/plausible_web/templates/layout/app.html.heex
new file mode 100644
index 0000000000..19caf15f0b
--- /dev/null
+++ b/lib/plausible_web/templates/layout/app.html.heex
@@ -0,0 +1,64 @@
+<!DOCTYPE html>
+<html lang="en" class="h-full">
+ <head>
+ <meta charset="utf-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <meta
+ name="description"
+ content="Plausible is a lightweight and open-source web analytics tool. Your website data is 100% yours and the privacy of your visitors is respected."
+ />
+ <%= if assigns[:connect_live_socket] do %>
+ <meta name="csrf-token" content={Plug.CSRFProtection.get_csrf_token()} />
+ <meta name="websocket-url" content={websocket_url()} />
+ <% end %>
+ <meta name="robots" content={@conn.private.robots} />
+ <link
+ rel="icon"
+ type="image/png"
+ sizes="32x32"
+ href={PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_favicon.png")}
+ />
+ <link rel="apple-touch-icon" href="/images/icon/apple-touch-icon.png" />
+ <title>
+ <%= assigns[:title] ||
+ "Plausible · Simple, privacy-friendly alternative to Google Analytics" %>
+ </title>
+ <link rel="stylesheet" href={Routes.static_path(@conn, "/css/app.css")} />
+ <%= render("_tracking.html", assigns) %>
+ </head>
+ <body
+ class={[
+ "flex flex-col bg-gray-50 dark:bg-gray-850",
+ if !assigns[:embedded] do
+ "h-full"
+ end
+ ]}
+ style={if assigns[:background], do: "background-color: #{assigns[:background]}"}
+ >
+ <%= if !assigns[:embedded] do %>
+ <%= render("_header.html", assigns) %>
+ <%= render("_notice.html", assigns) %>
+ <% end %>
+
+ <main class="flex-1">
+ <%= Map.get(assigns, :inner_layout) || @inner_content %>
+ </main>
+
+ <PlausibleWeb.Components.Layout.theme_script current_user={@conn.assigns[:current_user]} />
+
+ <%= if assigns[:embedded] do %>
+ <div data-iframe-height></div>
+ <script type="text/javascript" src={Routes.static_path(@conn, "/js/embed.content.js")}>
+ </script>
+ <% else %>
+ <%= render("_footer.html", assigns) %>
+ <% end %>
+ <script type="text/javascript" src={Routes.static_path(@conn, "/js/app.js")}>
+ </script>
+ <%= if assigns[:load_dashboard_js] do %>
+ <script type="text/javascript" src={Routes.static_path(@conn, "/js/dashboard.js")}>
+ </script>
+ <% end %>
+ </body>
+</html>
diff --git a/lib/plausible_web/templates/layout/focus.html.eex b/lib/plausible_web/templates/layout/focus.html.eex
deleted file mode 100644
index b28e83c53f..0000000000
--- a/lib/plausible_web/templates/layout/focus.html.eex
+++ /dev/null
@@ -1,38 +0,0 @@
-<!DOCTYPE html>
-<html lang="en" class="h-full">
- <head>
- <meta charset="utf-8"/>
- <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
- <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
- <meta name="description" content="A lightweight, non-intrusive alternative to Google Analytics."/>
- <meta name="robots" content="<%= @conn.private.robots %>" />
- <%= if @conn.assigns[:connect_live_socket] do %>
- <meta name="csrf-token" content="<%= Plug.CSRFProtection.get_csrf_token() %>" />
- <meta name="websocket-url" content="<%= websocket_url() %>" />
- <% end %>
- <link rel="icon" type="image/png" sizes="32x32" href="<%= PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_favicon.png") %>">
- <title><%= assigns[:title] || "Plausible · Web analytics" %></title>
- <link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/>
- <%= render("_tracking.html", assigns) %>
- </head>
- <body class="flex flex-col h-full bg-gray-100 dark:bg-gray-900">
- <div class="w-full my-8 text-center">
- <a href="<%= home_dest(@conn) %>">
- <%= img_tag(PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_logo_dark.png"), class: "hidden dark:inline", style: "height: 2.5rem;", alt: "Plausible logo") %>
- <%= img_tag(PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_logo.png"), class: "inline dark:hidden", style: "height: 2.5rem;", alt: "Plausible logo")%>
- </a>
- </div>
-
- <%= if assigns[:flash] do %>
- <%= render("_flash.html", assigns) %>
- <% end %>
- <%= @inner_content %>
-
-
- <p class="text-center text-gray-500 text-xs py-8">
- © <%= DateTime.utc_now().year() %> Plausible Analytics. All rights reserved.
- </p>
-
- <script type="text/javascript" data-pref="<%= @conn.assigns[:theme] || (@conn.assigns[:current_user] && @conn.assigns[:current_user].theme) %>" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script>
- </body>
-</html>
diff --git a/lib/plausible_web/templates/layout/focus.html.heex b/lib/plausible_web/templates/layout/focus.html.heex
new file mode 100644
index 0000000000..16f90460a2
--- /dev/null
+++ b/lib/plausible_web/templates/layout/focus.html.heex
@@ -0,0 +1,58 @@
+<!DOCTYPE html>
+<html lang="en" class="h-full">
+ <head>
+ <meta charset="utf-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <meta
+ name="description"
+ content="A lightweight, non-intrusive alternative to Google Analytics."
+ />
+ <meta name="robots" content={@conn.private.robots} />
+ <%= if assigns[:connect_live_socket] do %>
+ <meta name="csrf-token" content={Plug.CSRFProtection.get_csrf_token()} />
+ <meta name="websocket-url" content={websocket_url()} />
+ <% end %>
+ <link
+ rel="icon"
+ type="image/png"
+ sizes="32x32"
+ href={PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_favicon.png")}
+ />
+ <title><%= assigns[:title] || "Plausible · Web analytics" %></title>
+ <link rel="stylesheet" href={Routes.static_path(@conn, "/css/app.css")} />
+ <%= render("_tracking.html", assigns) %>
+ </head>
+ <body class="flex flex-col h-full bg-gray-100 dark:bg-gray-900">
+ <div class="w-full my-8 text-center">
+ <a href={home_dest(@conn)}>
+ <%= img_tag(
+ PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_logo_dark.png"),
+ class: "hidden dark:inline",
+ style: "height: 2.5rem;",
+ alt: "Plausible logo"
+ ) %>
+ <%= img_tag(
+ PlausibleWeb.Router.Helpers.static_path(@conn, "/images/icon/plausible_logo.png"),
+ class: "inline dark:hidden",
+ style: "height: 2.5rem;",
+ alt: "Plausible logo"
+ ) %>
+ </a>
+ </div>
+
+ <%= if assigns[:flash] do %>
+ <%= render("_flash.html", assigns) %>
+ <% end %>
+ <%= @inner_content %>
+
+ <p class="text-center text-gray-500 text-xs py-8">
+ © <%= DateTime.utc_now().year() %> Plausible Analytics. All rights reserved.
+ </p>
+
+ <PlausibleWeb.Components.Layout.theme_script current_user={assigns[:current_user]} />
+
+ <script type="text/javascript" src={Routes.static_path(@conn, "/js/app.js")}>
+ </script>
+ </body>
+</html>