brewlog/templates/partials/nav.html
Jon Seager 195c4e5ef4
fix(ui): follow OS dark/light mode changes after manual toggle
Only persist theme to localStorage when the user's choice differs from
the OS preference. Clear it when they match so the prefers-color-scheme
listener remains active and automatically tracks system theme changes.
2026-02-06 16:49:16 +00:00

93 lines
5.5 KiB
HTML

{% import "partials/icons.html" as icons %}
<nav class="relative border-b pb-4 text-sm" data-signals:_nav-open="false">
<div class="flex items-center justify-between">
<div class="font-semibold uppercase tracking-[0.25em] text-accent"><a href="/">B{rew}log</a></div>
<div class="relative flex items-center gap-1">
<a class="rounded-md p-1.5 transition {% if nav_active == "home" %}text-accent{% else %}text-text-muted hover:text-text-secondary{% endif %}" href="/" title="Home" aria-label="Home">
{% call icons::home("h-5 w-5") %}
</a>
<button type="button" class="rounded-md p-1.5 text-text-muted transition hover:text-text-secondary" title="Toggle theme" aria-label="Toggle theme" onclick="toggleTheme()">
<span class="theme-icon-light">{% call icons::sun("h-5 w-5") %}</span>
<span class="theme-icon-dark hidden">{% call icons::moon("h-5 w-5") %}</span>
</button>
<button class="rounded-md p-1.5 text-text-muted transition hover:text-text-secondary" data-on:click="$_navOpen = !$_navOpen" title="Menu" aria-label="Toggle menu">
<span data-show="!$_navOpen">{% call icons::hamburger("h-5 w-5") %}</span>
<span data-show="$_navOpen" style="display:none">{% call icons::close("h-5 w-5") %}</span>
</button>
<!-- Backdrop (click outside to close) -->
<div class="fixed inset-0 z-40" data-show="$_navOpen" data-on:click="$_navOpen = false" style="display:none"></div>
<!-- Dropdown menu -->
<div class="absolute right-0 top-full z-50 mt-2 flex flex-col gap-1 rounded-lg border bg-surface p-3 whitespace-nowrap" data-show="$_navOpen" style="display:none">
<a class="rounded-md px-4 py-2.5 transition flex items-center justify-end gap-4 {% if nav_active == "data" %}text-accent font-medium{% else %}text-text-muted hover:text-text hover:bg-surface-alt{% endif %}" href="/data">
Data {% call icons::squares("h-4 w-4 shrink-0") %}
</a>
<a class="rounded-md px-4 py-2.5 transition flex items-center justify-end gap-4 {% if nav_active == "timeline" %}text-accent font-medium{% else %}text-text-muted hover:text-text hover:bg-surface-alt{% endif %}" href="/timeline">
Timeline {% call icons::timeline("h-4 w-4 shrink-0") %}
</a>
{% if is_authenticated %}
<a class="rounded-md px-4 py-2.5 transition flex items-center justify-end gap-4 {% if nav_active == "checkin" %}text-accent font-medium{% else %}text-text-muted hover:text-text hover:bg-surface-alt{% endif %}" href="/check-in">
Check In {% call icons::checkin("h-4 w-4 shrink-0") %}
</a>
<a class="rounded-md px-4 py-2.5 transition flex items-center justify-end gap-4 {% if nav_active == "account" %}text-accent font-medium{% else %}text-text-muted hover:text-text hover:bg-surface-alt{% endif %}" href="/account">
Admin {% call icons::cog("h-4 w-4 shrink-0") %}
</a>
<form method="post" action="/logout">
<button type="submit" class="w-full rounded-md px-4 py-2.5 transition flex items-center justify-end gap-4 text-text-muted hover:text-text hover:bg-surface-alt">
Sign Out {% call icons::logout("h-4 w-4 shrink-0") %}
</button>
</form>
{% else %}
<a class="rounded-md px-4 py-2.5 transition flex items-center justify-end gap-4 text-text-muted hover:text-text hover:bg-surface-alt" href="/login">
Login {% call icons::user("h-4 w-4 shrink-0") %}
</a>
{% endif %}
</div>
</div>
</nav>
<script>
const toggleTheme = () => {
const html = document.documentElement;
const isDark = html.getAttribute("data-theme") === "dark";
const osDark = matchMedia("(prefers-color-scheme: dark)").matches;
if (isDark) {
html.removeAttribute("data-theme");
// Only persist if this choice differs from OS preference
if (osDark) localStorage.setItem("theme", "light");
else localStorage.removeItem("theme");
} else {
html.setAttribute("data-theme", "dark");
if (!osDark) localStorage.setItem("theme", "dark");
else localStorage.removeItem("theme");
}
updateThemeIcons();
};
const updateThemeIcons = () => {
const isDark = document.documentElement.getAttribute("data-theme") === "dark";
document.querySelectorAll(".theme-icon-light").forEach((el) => {
el.classList.toggle("hidden", isDark);
});
document.querySelectorAll(".theme-icon-dark").forEach((el) => {
el.classList.toggle("hidden", !isDark);
});
const favicon = document.getElementById("favicon");
if (favicon) {
favicon.href = isDark ? "/favicon-dark.svg" : "/favicon-light.svg";
}
};
// Set correct icon state on load
updateThemeIcons();
// Follow OS theme changes when the user hasn't manually chosen
matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
if (localStorage.getItem("theme")) return;
const html = document.documentElement;
if (e.matches) {
html.setAttribute("data-theme", "dark");
} else {
html.removeAttribute("data-theme");
}
updateThemeIcons();
});
</script>