Fix darkmode flash (#3625)

* Inline theme script to layout

* Extract theme_script as Heex component

* Remove unused function

* Reference assigns directly

* Clean up current user

* Clean up theme javascript

* Use new theme script in focus.html layout

* Use `assigns` instead of `@conn.assigns` in `focus.html.heex` template too

---------

Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
pull/3636/head r2023.12.14-288
Uku Taht 3 years ago committed by GitHub
parent ab54a66a43
commit 17ba44e8db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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"

@ -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();
};

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

@ -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 !@conn.assigns[:embedded], do: "height: 100%;" %> <%= if @conn.assigns[:background], do: "background-color: #{@conn.assigns[:background]}" %>">
<%= if !@conn.assigns[: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>

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

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

@ -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>
Loading…
Cancel
Save