Skip to content

Commit d51edfc

Browse files
committed
feat(ui): merge brand and account nav into one slim header
The brand lived in app.html.heex and the account nav in root.html.heex, so they rendered as two stacked bars (~100px). They are now one 56px row: logo and version on the left, theme toggle and account links on the right. Structure, per the request: a `site_header/1` container composing two independent components, `brand/1` and `account_nav/1`. The header moves from root into the app and devices layouts. root keeps only the skip link, which must stay the first tab stop on every page. Router: the public scope had no live_session, so :current_user was never assigned there. With the header rendering inside the LiveView rather than root, those pages would have shown "Register / Log in" to a logged-in user. Wrapped them in live_session :public with mount_current_user (assign_new, so anonymous mounts are unaffected). Verified on / while logged in: shows the email, Settings and Log out. The devices layout gets the same header, so /admin/devices keeps the theme toggle it would otherwise have lost. Hit targets: slimming the row shrank the toggle to 32px and the logo to 28px, below the 44px minimum. Restored via a ::before overlay on the toggle buttons and -my-2 + fixed sizing on the logo and nav links, so targets are 44px while the row stays 56px. Verified in the browser: every header target measures 44px effective. Responsive: the version badge hides below sm, and the user email below lg, so the row does not wrap on narrow screens. Verified: one row (all three groups share vertical center 28), single nav landmark, skip link still first tab stop, zero contrast failures and zero light-painting elements in dark on / and /admin/devices. Tests unchanged at 9 pre-existing failures.
1 parent 993c82b commit d51edfc

5 files changed

Lines changed: 118 additions & 80 deletions

File tree

‎fullstack/lib/fullstack_web/components/layouts.ex‎

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,95 @@ defmodule FullstackWeb.Layouts do
2323
]
2424
end
2525

26+
@doc """
27+
The site header: brand on the left, account controls on the right, one row.
28+
29+
A container around `brand/1` and `account_nav/1` so the two halves stay
30+
independently editable. Lives in the `app` and `devices` layouts rather
31+
than `root`, so it renders inside the LiveView and can react to
32+
`@current_user`.
33+
"""
34+
attr :current_user, :any, default: nil
35+
36+
def site_header(assigns) do
37+
~H"""
38+
<header class="border-b border-base-300">
39+
<div class="flex h-14 items-center justify-between gap-4 px-4 sm:px-6 lg:px-8">
40+
<.brand />
41+
<.account_nav current_user={@current_user} />
42+
</div>
43+
</header>
44+
"""
45+
end
46+
47+
@doc """
48+
Left half of the site header: logo and the Phoenix version badge.
49+
50+
The logo carries the accessible name; the `<img>` is decorative and
51+
intentionally has an empty `alt`, so the link is announced once rather
52+
than twice.
53+
"""
54+
def brand(assigns) do
55+
~H"""
56+
<div class="flex min-w-0 items-center gap-3">
57+
<a
58+
href="/"
59+
aria-label="Fullstack home"
60+
class="-my-2 flex h-11 w-11 flex-none items-center justify-center rounded-md focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
61+
>
62+
<img src={~p"/img/logo.svg"} width="28" height="28" alt="" />
63+
</a>
64+
<p class="badge badge-outline badge-sm hidden text-brand-content border-brand-content/40 font-medium sm:inline-flex">
65+
v{Application.spec(:phoenix, :vsn)}
66+
</p>
67+
</div>
68+
"""
69+
end
70+
71+
@doc """
72+
Right half of the site header: theme toggle and account links.
73+
74+
The `<nav>` carries `aria-label="Account"` — it is the app's only
75+
navigation landmark.
76+
"""
77+
attr :current_user, :any, default: nil
78+
79+
def account_nav(assigns) do
80+
~H"""
81+
<nav aria-label="Account" class="flex flex-none items-center gap-2 sm:gap-3">
82+
<.theme_toggle />
83+
<ul class="flex items-center gap-2 text-[0.8125rem] leading-6 sm:gap-3">
84+
<%= if @current_user do %>
85+
<li class="hidden max-w-[16rem] truncate text-muted lg:block">
86+
{@current_user.email}
87+
</li>
88+
<li>
89+
<.link href={~p"/users/settings"} class={nav_link_class()}>Settings</.link>
90+
</li>
91+
<li>
92+
<.link href={~p"/users/log_out"} method="delete" class={nav_link_class()}>
93+
Log out
94+
</.link>
95+
</li>
96+
<% else %>
97+
<li>
98+
<.link href={~p"/users/register"} class={nav_link_class()}>Register</.link>
99+
</li>
100+
<li>
101+
<.link href={~p"/users/log_in"} class={nav_link_class()}>Log in</.link>
102+
</li>
103+
<% end %>
104+
</ul>
105+
</nav>
106+
"""
107+
end
108+
109+
defp nav_link_class do
110+
"-my-2 inline-flex min-h-[2.75rem] items-center rounded-sm px-2 font-semibold hover:text-primary " <>
111+
"focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 " <>
112+
"focus-visible:outline-primary"
113+
end
114+
26115
@doc """
27116
Renders the light/dark theme toggle.
28117
@@ -48,15 +137,15 @@ defmodule FullstackWeb.Layouts do
48137
type="button"
49138
data-set-theme="light"
50139
aria-label="Light theme"
51-
class="theme-option flex h-8 w-8 items-center justify-center rounded-full focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
140+
class="theme-option relative flex h-8 w-8 items-center justify-center rounded-full before:absolute before:left-1/2 before:top-1/2 before:h-11 before:w-11 before:-translate-x-1/2 before:-translate-y-1/2 before:content-[''] focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
52141
>
53142
<.icon name="hero-sun-mini" class="h-4 w-4" aria-hidden="true" />
54143
</button>
55144
<button
56145
type="button"
57146
data-set-theme="dark"
58147
aria-label="Dark theme"
59-
class="theme-option flex h-8 w-8 items-center justify-center rounded-full focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
148+
class="theme-option relative flex h-8 w-8 items-center justify-center rounded-full before:absolute before:left-1/2 before:top-1/2 before:h-11 before:w-11 before:-translate-x-1/2 before:-translate-y-1/2 before:content-[''] focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary"
60149
>
61150
<.icon name="hero-moon-mini" class="h-4 w-4" aria-hidden="true" />
62151
</button>

‎fullstack/lib/fullstack_web/components/layouts/app.html.heex‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
1-
<header class="px-4 sm:px-6 lg:px-8">
2-
<div class="flex items-center justify-between border-b border-base-300 py-3 text-sm">
3-
<div class="flex items-center gap-4">
4-
<a href="/" aria-label="Fullstack home">
5-
<img src={~p"/img/logo.svg"} width="36" alt="" />
6-
</a>
7-
<p class="badge badge-outline text-brand-content border-brand-content/40 font-medium">
8-
v<%= Application.spec(:phoenix, :vsn) %>
9-
</p>
10-
</div>
11-
<div class="flex items-center gap-4 font-semibold leading-6 text-base-content"></div>
12-
</div>
13-
</header>
14-
<main id="main-content" tabindex="-1" class="px-4 py-10 sm:px-4 lg:px-8">
1+
<.site_header current_user={assigns[:current_user]} />
2+
<main id="main-content" tabindex="-1" class="px-4 py-8 sm:px-6 lg:px-8">
153
<div class="mx-auto max-w-2xl">
164
<LiveToast.toast_group
175
flash={@flash}

‎fullstack/lib/fullstack_web/components/layouts/devices.html.heex‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<main id="main-content" tabindex="-1" class="px-4 py-10 sm:px-4 lg:px-8">
1+
<.site_header current_user={assigns[:current_user]} />
2+
<main id="main-content" tabindex="-1" class="px-4 py-8 sm:px-6 lg:px-8">
23
<div class="mx-auto max-w-xl">
34
<.flash_group flash={@flash} />
45
</div>
56
<.header>
6-
<.icon name="hero-box-solid" /> <%= @page_title %>
7+
<.icon name="hero-box-solid" aria-hidden="true" /> <%= @page_title %>
78
<:actions>
89
<.link patch={~p"/admin/devices/new"}>
910
<.button>New Device</.button>

‎fullstack/lib/fullstack_web/components/layouts/root.html.heex‎

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -32,53 +32,6 @@
3232
>
3333
Skip to main content
3434
</a>
35-
<nav
36-
aria-label="Account"
37-
class="relative z-10 flex items-center gap-4 px-4 sm:px-6 lg:px-8 justify-end"
38-
>
39-
<.theme_toggle />
40-
<ul class="flex items-center gap-4">
41-
<%= if @current_user do %>
42-
<li class="text-[0.8125rem] leading-6 text-muted">
43-
{@current_user.email}
44-
</li>
45-
<li>
46-
<.link
47-
href={~p"/users/settings"}
48-
class="text-[0.8125rem] leading-6 font-semibold hover:text-primary focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary rounded-sm"
49-
>
50-
Settings
51-
</.link>
52-
</li>
53-
<li>
54-
<.link
55-
href={~p"/users/log_out"}
56-
method="delete"
57-
class="text-[0.8125rem] leading-6 font-semibold hover:text-primary focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary rounded-sm"
58-
>
59-
Log out
60-
</.link>
61-
</li>
62-
<% else %>
63-
<li>
64-
<.link
65-
href={~p"/users/register"}
66-
class="text-[0.8125rem] leading-6 font-semibold hover:text-primary focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary rounded-sm"
67-
>
68-
Register
69-
</.link>
70-
</li>
71-
<li>
72-
<.link
73-
href={~p"/users/log_in"}
74-
class="text-[0.8125rem] leading-6 font-semibold hover:text-primary focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary rounded-sm"
75-
>
76-
Log in
77-
</.link>
78-
</li>
79-
<% end %>
80-
</ul>
81-
</nav>
8235
{@inner_content}
8336
</body>
8437
</html>

‎fullstack/lib/fullstack_web/router.ex‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,33 @@ defmodule FullstackWeb.Router do
1919

2020
scope "/", FullstackWeb do
2121
pipe_through :browser
22-
live "/fibonacci", Public.FibonacciLive.Index, :index
23-
live "/transactions/:id", Public.TransactionLive
24-
live "/transactions", Public.TransactionsTableLive
25-
live "/Analitics", Public.TransactionsLive.PublicTransactions, :public_transactions
26-
live "/devices", Public.DevicesLive.Index, :index
27-
live "/agent", Public.AgentLive
2822

2923
resources "/about", AboutController, only: [:index]
30-
live "/chat", ChatLive
31-
live "/channels-chat", ChannelsChatLive
24+
get "/u/:key", UrlRedirectController, :index
3225

33-
live "/urls", UrlLive.Index, :index
34-
live "/urls/new", UrlLive.Index, :new
35-
live "/urls/:id/edit", UrlLive.Index, :edit
26+
# The site header renders inside the LiveView, so these public routes need
27+
# :current_user assigned to show the right account links. mount_current_user
28+
# uses assign_new and does not require a session, so anonymous mounts are fine.
29+
live_session :public, on_mount: [{FullstackWeb.UserAuth, :mount_current_user}] do
30+
live "/fibonacci", Public.FibonacciLive.Index, :index
31+
live "/transactions/:id", Public.TransactionLive
32+
live "/transactions", Public.TransactionsTableLive
33+
live "/Analitics", Public.TransactionsLive.PublicTransactions, :public_transactions
34+
live "/devices", Public.DevicesLive.Index, :index
35+
live "/agent", Public.AgentLive
3636

37-
live "/urls/:id", UrlLive.Show, :show
38-
live "/urls/:id/show/edit", UrlLive.Show, :edit
39-
get "/u/:key", UrlRedirectController, :index
37+
live "/chat", ChatLive
38+
live "/channels-chat", ChannelsChatLive
39+
40+
live "/urls", UrlLive.Index, :index
41+
live "/urls/new", UrlLive.Index, :new
42+
live "/urls/:id/edit", UrlLive.Index, :edit
4043

41-
live "/", HomeLive.Index, :index
44+
live "/urls/:id", UrlLive.Show, :show
45+
live "/urls/:id/show/edit", UrlLive.Show, :edit
46+
47+
live "/", HomeLive.Index, :index
48+
end
4249
end
4350

4451
# Enable LiveDashboard and Swoosh mailbox preview in development

0 commit comments

Comments
 (0)