Keep account controls permanent and add system error pages
This commit is contained in:
1 parent
61b05abe44
commit
60b584b7c1
50 files changed
+1287
-56
No files matched your search
@@ -0,0 +1,115 @@
|
||||
import { iconSvg } from "../icons/icons.js";
|
||||
let sequence = 0;
|
||||
/** Fixed account controls live outside the API-driven service navigation. */
|
||||
export function mountAccountMenu(
|
||||
root,
|
||||
{ onNavigate, t = (ru) => ru, name = "", email = "" } = {},
|
||||
) {
|
||||
if (typeof onNavigate !== "function")
|
||||
throw new TypeError("Account menu needs onNavigate");
|
||||
const abort = new AbortController(),
|
||||
{ signal } = abort,
|
||||
id = `anidev-account-${++sequence}`;
|
||||
root.classList.add("dock-account-area");
|
||||
root.dataset.fixedAccount = "";
|
||||
root.innerHTML = `<div id="${id}" class="account-menu" popover="auto" role="menu" aria-label="${t("Меню аккаунта", "Account menu")}"><button type="button" role="menuitem" data-account-page="account">${iconSvg("profile", { size: 20 })}<span>${t("Аккаунт", "Account")}</span></button><button type="button" role="menuitem" data-account-page="account-settings">${iconSvg("settings", { size: 20 })}<span>${t("Настройки аккаунта", "Account settings")}</span></button></div><button type="button" class="dock-account" popovertarget="${id}" aria-haspopup="menu" aria-expanded="false" aria-controls="${id}" aria-label="${t("Открыть меню аккаунта", "Open account menu")}"><span class="avatar">${iconSvg("profile", { size: 20 })}</span><span class="account-identity"><b></b><small></small></span><span class="account-more" aria-hidden="true">···</span></button>`;
|
||||
const popup = root.querySelector(".account-menu"),
|
||||
trigger = root.querySelector(".dock-account");
|
||||
function update(identity = {}) {
|
||||
root.querySelector(".account-identity b").textContent =
|
||||
identity.name || t("Аккаунт", "Account");
|
||||
root.querySelector(".account-identity small").textContent =
|
||||
identity.email || t("Личные настройки", "Personal settings");
|
||||
}
|
||||
update({ name, email });
|
||||
const close = (restore = false) => {
|
||||
if (popup.matches(":popover-open")) popup.hidePopover();
|
||||
if (restore) trigger.focus({ preventScroll: true });
|
||||
};
|
||||
popup.addEventListener(
|
||||
"toggle",
|
||||
() =>
|
||||
trigger.setAttribute(
|
||||
"aria-expanded",
|
||||
String(popup.matches(":popover-open")),
|
||||
),
|
||||
{ signal },
|
||||
);
|
||||
popup.addEventListener(
|
||||
"click",
|
||||
(event) => {
|
||||
const target = event.target.closest("[data-account-page]");
|
||||
if (target) {
|
||||
close(true);
|
||||
onNavigate(target.dataset.accountPage);
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
popup.addEventListener(
|
||||
"keydown",
|
||||
(event) => {
|
||||
if (event.key === "Tab") close(true);
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
close(true);
|
||||
}
|
||||
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
|
||||
event.preventDefault();
|
||||
const items = [...popup.querySelectorAll("button")],
|
||||
current = items.indexOf(document.activeElement);
|
||||
const index =
|
||||
event.key === "Home"
|
||||
? 0
|
||||
: event.key === "End"
|
||||
? items.length - 1
|
||||
: (current +
|
||||
(event.key === "ArrowDown" ? 1 : -1) +
|
||||
items.length) %
|
||||
items.length;
|
||||
items[index].focus();
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
trigger.addEventListener(
|
||||
"keydown",
|
||||
(event) => {
|
||||
if (["ArrowDown", "ArrowUp"].includes(event.key)) {
|
||||
event.preventDefault();
|
||||
popup.showPopover();
|
||||
const buttons = popup.querySelectorAll("button");
|
||||
buttons[event.key === "ArrowDown" ? 0 : buttons.length - 1].focus();
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
const shell = root.closest(".navigation");
|
||||
shell?.addEventListener(
|
||||
"anidev:dockchange",
|
||||
(event) => {
|
||||
if (!event.detail.open) close();
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
return {
|
||||
update,
|
||||
close,
|
||||
setActive(id) {
|
||||
popup.querySelectorAll("[data-account-page]").forEach((button) => {
|
||||
if (button.dataset.accountPage === id)
|
||||
button.setAttribute("aria-current", "page");
|
||||
else button.removeAttribute("aria-current");
|
||||
});
|
||||
trigger.dataset.active = String(
|
||||
["account", "account-settings"].includes(id),
|
||||
);
|
||||
},
|
||||
dispose() {
|
||||
close();
|
||||
abort.abort();
|
||||
trigger.removeAttribute("popovertarget");
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user