Add four-icon mobile bottom Dock with recent sections
This commit is contained in:
1 parent
c3257a0f4d
commit
03c590e172
20 files changed
+547
-5
No files matched your search
@@ -0,0 +1,162 @@
|
||||
import { iconSvg, iconNames } from "../icons/icons.js";
|
||||
/** Host may retain this across shell re-renders. Never persist across sign-out. */
|
||||
export const createMobileHistory = () => ({
|
||||
current: null,
|
||||
previous: null,
|
||||
context: null,
|
||||
});
|
||||
export const mobileDockQuery =
|
||||
"(max-width:600px), (max-width:900px) and (hover:none) and (pointer:coarse)";
|
||||
export function mountMobileDock(
|
||||
navigation,
|
||||
{
|
||||
trigger,
|
||||
getPage,
|
||||
onNavigate,
|
||||
onCloseMenu,
|
||||
t = (ru) => ru,
|
||||
history = createMobileHistory(),
|
||||
} = {},
|
||||
) {
|
||||
navigation.dataset.mobileDock = "";
|
||||
const abort = new AbortController(),
|
||||
{ signal } = abort,
|
||||
media = matchMedia(mobileDockQuery);
|
||||
const current = document.createElement("button"),
|
||||
previous = document.createElement("button"),
|
||||
menuIcon = document.createElement("span");
|
||||
current.type = previous.type = "button";
|
||||
current.className = "mobile-recent mobile-recent-current";
|
||||
previous.className = "mobile-recent mobile-recent-previous";
|
||||
current.setAttribute("aria-current", "page");
|
||||
menuIcon.className = "mobile-menu-icon";
|
||||
menuIcon.setAttribute("aria-hidden", "true");
|
||||
navigation.prepend(current, previous);
|
||||
trigger.append(menuIcon);
|
||||
const links = navigation.querySelector(".nav-links"),
|
||||
account = navigation.querySelector(".dock-account-area");
|
||||
const controls = trigger.getAttribute("aria-controls");
|
||||
if (links) {
|
||||
links.id ||= (navigation.id || "anidev-navigation") + "-sections";
|
||||
links.dataset.mobileMenuLabel = t("Полное меню", "All sections");
|
||||
}
|
||||
function paint(button, page, fallback) {
|
||||
button.disabled = !page;
|
||||
button.innerHTML = iconSvg(
|
||||
page && iconNames.includes(page.icon) ? page.icon : fallback,
|
||||
{ size: 24 },
|
||||
);
|
||||
button.setAttribute(
|
||||
"aria-label",
|
||||
page
|
||||
? `${button === current ? t("Текущий раздел", "Current section") : t("Предыдущий раздел", "Previous section")}: ${page.label}`
|
||||
: button === current
|
||||
? t("Нет текущего раздела", "No current section")
|
||||
: t("Предыдущего раздела пока нет", "No previous section yet"),
|
||||
);
|
||||
button.title = button.getAttribute("aria-label");
|
||||
}
|
||||
function sync() {
|
||||
const mobile = media.matches,
|
||||
open = document.body.classList.contains("dock-open");
|
||||
if (account) {
|
||||
if (mobile) account.dataset.mobileAccount = "";
|
||||
else delete account.dataset.mobileAccount;
|
||||
}
|
||||
menuIcon.innerHTML = iconSvg(open ? "close" : "menu", { size: 24 });
|
||||
if (mobile) {
|
||||
trigger.setAttribute(
|
||||
"aria-label",
|
||||
open
|
||||
? t("Закрыть полное меню", "Close all sections")
|
||||
: t("Полное меню", "All sections"),
|
||||
);
|
||||
trigger.setAttribute("aria-controls", links?.id || navigation.id);
|
||||
} else {
|
||||
trigger.setAttribute(
|
||||
"aria-label",
|
||||
open
|
||||
? t("Свернуть меню", "Collapse navigation")
|
||||
: t("Развернуть меню", "Expand navigation"),
|
||||
);
|
||||
if (controls) trigger.setAttribute("aria-controls", controls);
|
||||
}
|
||||
}
|
||||
function update(id, context = "default") {
|
||||
if (history.context !== context) {
|
||||
history.current = null;
|
||||
history.previous = null;
|
||||
history.context = context;
|
||||
}
|
||||
const page = id ? getPage(id) : null;
|
||||
if (!page) {
|
||||
history.current = null;
|
||||
history.previous = null;
|
||||
} else if (history.current?.id !== page.id) {
|
||||
history.previous = history.current;
|
||||
history.current = { ...page };
|
||||
} else history.current = { ...page };
|
||||
if (history.previous)
|
||||
history.previous = getPage(history.previous.id) || null;
|
||||
paint(current, history.current, "dashboard");
|
||||
paint(previous, history.previous, "arrow-left");
|
||||
sync();
|
||||
}
|
||||
previous.addEventListener(
|
||||
"click",
|
||||
() => {
|
||||
const page = history.previous && getPage(history.previous.id);
|
||||
if (page) {
|
||||
onCloseMenu?.();
|
||||
onNavigate(page.id);
|
||||
}
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
navigation.addEventListener(
|
||||
"click",
|
||||
(event) => {
|
||||
if (
|
||||
media.matches &&
|
||||
event.target.closest("[data-page],[data-nav-id],[data-account-page]")
|
||||
)
|
||||
onCloseMenu?.();
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
media.addEventListener(
|
||||
"change",
|
||||
() => {
|
||||
onCloseMenu?.();
|
||||
sync();
|
||||
},
|
||||
{ signal },
|
||||
);
|
||||
const observer = new MutationObserver(sync);
|
||||
observer.observe(document.body, {
|
||||
attributes: true,
|
||||
attributeFilter: ["class"],
|
||||
});
|
||||
sync();
|
||||
paint(current, null, "dashboard");
|
||||
paint(previous, null, "arrow-left");
|
||||
return {
|
||||
update,
|
||||
reset() {
|
||||
history.current = null;
|
||||
history.previous = null;
|
||||
history.context = null;
|
||||
paint(current, null, "dashboard");
|
||||
paint(previous, null, "arrow-left");
|
||||
},
|
||||
dispose() {
|
||||
abort.abort();
|
||||
observer.disconnect();
|
||||
current.remove();
|
||||
previous.remove();
|
||||
menuIcon.remove();
|
||||
delete navigation.dataset.mobileDock;
|
||||
if (account) delete account.dataset.mobileAccount;
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user