Move account pages into a menu under the user's name (SCR-3)
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m41s
CI / test (push) Successful in 4m13s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s

The header's name link and Sign out become a button opening a menu of Profile, Sharing,
Apps, Sessions, Import and Sign out; the main nav keeps Overview, History and Album art.
Keyboard and screen-reader friendly. The Profile page now refetches the user after a
failed change instead of showing stale state.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
mattandClaude Sonnet 5.5 committed 2026-10-13 22:12:02 +01:00
1 parent a7ba6360e5
commit 7f8afdcdbb
6 files changed
+193 -33

No files matched your search

+2
View File
@@ -29,6 +29,8 @@ async function change(action, done) {
say(h("p", { class: "notice notice-ok" }, done));
} catch (error) {
say(errorBox(describeError(error)));
// The change may have half-happened, or the page was out of date: show what's true now.
user = await api("/auth/me").catch(() => user);
}
render();
const slot = document.querySelector(".account-avatar");
+102 -17
View File
@@ -8,6 +8,12 @@ const NAV = [
["index.html", "Overview"],
["history.html", "History"],
["art.html", "Album art"],
];
// Account pages live in the menu under the user's name. "Sharing" joins them (after
// Profile) when the server federates.
const MENU = [
["account.html", "Profile"],
["apps.html", "Apps"],
["sessions.html", "Sessions"],
["import.html", "Import"],
@@ -22,20 +28,9 @@ export async function signedInPage(active) {
const header = document.getElementById("site-header");
replace(header, brand(), nav(active));
const user = await api("/auth/me");
addSharingLink(header.querySelector("nav ul"), active);
header.append(
h(
"div",
{ class: "account" },
h(
"a",
{ class: "account-link", href: "account.html", title: "Your profile image" },
h("span", { class: "account-avatar" }, avatarImage(user, "avatar-sm")),
h("span", { class: "account-name" }, user.username),
),
h("button", { type: "button", class: "link-button", onclick: signOut }, "Sign out"),
),
);
const menu = accountMenu(user, active);
header.append(menu.element);
addSharingLink(menu.list, active);
return user;
}
@@ -67,6 +62,95 @@ function nav(active) {
);
}
// The account menu: a button showing the user's picture and name that opens a list of
// account pages and "Sign out". Returns the element and its <ul> (for late additions).
function accountMenu(user, active) {
const list = h("ul", { class: "account-menu", id: "account-menu", hidden: true });
for (const [href, label] of MENU) list.append(menuLink(href, label, active));
list.append(
h(
"li",
{ class: "account-menu-end" },
h("button", { type: "button", class: "account-menu-item", onclick: signOut }, "Sign out"),
),
);
const trigger = h(
"button",
{
type: "button",
class: "account-trigger",
"aria-haspopup": "true",
"aria-expanded": "false",
"aria-controls": "account-menu",
"aria-label": `Account menu for ${user.username}`,
},
h("span", { class: "account-avatar" }, avatarImage(user, "avatar-sm")),
h("span", { class: "account-name" }, user.username),
h("span", { class: "account-icon", "aria-hidden": "true" }),
);
const element = h("div", { class: "account" }, trigger, list);
const items = () => [...list.querySelectorAll("a, button")];
const setOpen = (open, focus) => {
list.hidden = !open;
trigger.setAttribute("aria-expanded", String(open));
if (open && focus === "first") items()[0]?.focus();
if (open && focus === "last") items().at(-1)?.focus();
if (!open && focus) trigger.focus();
};
trigger.addEventListener("click", () => setOpen(list.hidden));
trigger.addEventListener("keydown", (event) => {
if (event.key === "ArrowDown") {
event.preventDefault();
setOpen(true, "first");
} else if (event.key === "ArrowUp") {
event.preventDefault();
setOpen(true, "last");
}
});
list.addEventListener("keydown", (event) => {
const all = items();
const at = all.indexOf(document.activeElement);
if (event.key === "Escape") {
setOpen(false, true);
} else if (event.key === "ArrowDown") {
event.preventDefault();
all[(at + 1) % all.length].focus();
} else if (event.key === "ArrowUp") {
event.preventDefault();
all[(at - 1 + all.length) % all.length].focus();
} else if (event.key === "Home") {
event.preventDefault();
all[0].focus();
} else if (event.key === "End") {
event.preventDefault();
all.at(-1).focus();
}
});
// Close when focus or a click lands outside the menu.
document.addEventListener("click", (event) => {
if (!element.contains(event.target)) setOpen(false);
});
element.addEventListener("focusout", (event) => {
if (event.relatedTarget && !element.contains(event.relatedTarget)) setOpen(false);
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && !list.hidden) setOpen(false, true);
});
// Mark the trigger when the page being viewed is one of the menu's.
if (MENU.some(([href]) => href === active) || active === SHARING) {
trigger.setAttribute("data-current", "true");
}
return { element, list };
}
function menuLink(href, label, active) {
return h("li", {}, h("a", { class: "account-menu-item", href, "aria-current": href === active ? "page" : false }, label));
}
async function signOut() {
try {
await api("/auth/logout", { method: "POST" });
@@ -101,9 +185,10 @@ async function federationAvailable() {
return available;
}
const SHARING = "sharing.html";
async function addSharingLink(list, active) {
if (!list || !(await federationAvailable())) return;
const href = "sharing.html";
list.append(h("li", {}, h("a", { href, "aria-current": href === active ? "page" : false }, "Sharing")));
if (!(await federationAvailable())) return;
list.children[0].after(menuLink(SHARING, "Sharing", active)); // right after Profile
}