Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ const AutoDismissFlash = {

// Theme switching logic
function setTheme(theme) {
if (!['system', 'light', 'dark'].includes(theme)) {
theme = 'system'
}

localStorage.setItem('theme', theme)

let appliedTheme = theme
Expand Down Expand Up @@ -79,6 +83,13 @@ window.addEventListener('phx:set-theme', (e) => {
}
})

// Keep the theme in sync when another browser window changes it.
window.addEventListener('storage', (e) => {
if (e.key === 'theme') {
setTheme(e.newValue || 'system')
}
})

// Watch for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
const currentTheme = localStorage.getItem('theme')
Expand Down
4 changes: 2 additions & 2 deletions lib/aludel/web/components/layouts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ defmodule Aludel.Web.Layouts do
assigns = assign(assigns, :active, active)

~H"""
<a href={@href} class={"aludel-nav-link #{@active}"}>
<.link navigate={@href} class={"aludel-nav-link #{@active}"}>
{render_slot(@inner_block)}
</a>
</.link>
"""
end
end
33 changes: 16 additions & 17 deletions lib/aludel/web/components/layouts/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,27 @@
{assigns[:page_title] || "Dashboard"}
</.live_title>
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<link rel="stylesheet" href={asset_path(@conn, :css)} />
<script defer src={asset_path(@conn, :js)}>
</script>
<script>
(() => {
const setTheme = (theme) => {
if (theme === "system") {
localStorage.removeItem("phx:theme");
document.documentElement.removeAttribute("data-theme");
} else {
localStorage.setItem("phx:theme", theme);
document.documentElement.setAttribute("data-theme", theme);
}
};
if (!document.documentElement.hasAttribute("data-theme")) {
setTheme(localStorage.getItem("phx:theme") || "system");
let savedTheme = "system";

try {
savedTheme = localStorage.getItem("theme") || "system";
} catch (_error) {
// Storage can be unavailable in privacy-restricted browsing contexts.
}
window.addEventListener("storage", (e) => e.key === "phx:theme" && setTheme(e.newValue || "system"));

window.addEventListener("phx:set-theme", (e) => setTheme(e.target.dataset.phxTheme));

const theme = ["system", "light", "dark"].includes(savedTheme) ? savedTheme : "system";
const appliedTheme = theme === "system"
? (window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")
: theme;

document.documentElement.setAttribute("data-theme", appliedTheme);
})();
</script>
<link rel="stylesheet" href={asset_path(@conn, :css)} />
<script defer src={asset_path(@conn, :js)}>
</script>
</head>
<body>
{@inner_content}
Expand Down
8 changes: 8 additions & 0 deletions priv/static/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22227,6 +22227,9 @@ removing illegal node: "${("outerHTML" in childNode && childNode.outerHTML || ch
}
};
function setTheme(theme) {
if (!["system", "light", "dark"].includes(theme)) {
theme = "system";
}
localStorage.setItem("theme", theme);
let appliedTheme = theme;
if (theme === "system") {
Expand All @@ -22251,6 +22254,11 @@ removing illegal node: "${("outerHTML" in childNode && childNode.outerHTML || ch
setTheme(theme);
}
});
window.addEventListener("storage", (e) => {
if (e.key === "theme") {
setTheme(e.newValue || "system");
}
});
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
const currentTheme = localStorage.getItem("theme");
if (currentTheme === "system" || !currentTheme) {
Expand Down
20 changes: 20 additions & 0 deletions test/aludel_web/live/dashboard_live_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@ defmodule Aludel.Web.DashboardLiveTest do
assert render(view) =~ "Dashboard"
end

test "applies the saved theme before loading styles", %{conn: conn} do
html = conn |> get("/") |> html_response(200)

theme_script_position =
html
|> :binary.match(~s|localStorage.getItem("theme")|)
|> elem(0)

stylesheet_position =
html
|> :binary.match(~s(rel="stylesheet"))
|> elem(0)

assert theme_script_position < stylesheet_position
assert html =~ ~s|document.documentElement.setAttribute("data-theme", appliedTheme)|
refute html =~ "phx:theme"
assert html =~ ~s|href="/prompts"|
assert html =~ ~s|data-phx-link="redirect"|
end

test "shows recent runs", %{conn: conn} do
_run = run_fixture(%{name: "Recent Test Run"})

Expand Down
Loading