Files
anidev-design-system/components/account-menu.js
T

143 lines
4.7 KiB
JavaScript

import { iconSvg } from "../icons/icons.js";
import {
accountSubmenuMarkup,
mountAccountSubmenu,
} from "./account-submenu.js";
let sequence = 0;
/** Fixed account controls live outside the API-driven service navigation. */
export function mountAccountMenu(
root,
{ onNavigate, t = (ru) => ru, name = "", email = "", items } = {},
) {
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 = "";
items ||= [
{ id: "account", label: t("Управление", "Management"), icon: "profile" },
{
id: "account-security",
label: t("Безопасность", "Security"),
icon: "security",
},
{
id: "account-sessions",
label: t("Устройства и сеансы", "Devices and sessions"),
icon: "sessions",
},
{
id: "account-settings",
label: t("Настройки", "Settings"),
icon: "settings",
},
];
root.innerHTML = `<div id="${id}" class="account-menu" popover="auto" role="menu" aria-label="${t("Меню аккаунта", "Account menu")}">${accountSubmenuMarkup({ id: id + "-sections", items, t })}</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");
const sections = mountAccountSubmenu(popup);
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) => {
sections.close();
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")].filter(
(button) => !button.closest(".account-submenu"),
),
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")].filter(
(button) => !button.closest(".account-submenu"),
);
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(items.some((item) => item.id === id));
},
dispose() {
close();
abort.abort();
sections.dispose();
trigger.removeAttribute("popovertarget");
},
};
}