mirror of
https://code.castopod.org/adaures/castopod.git
synced 2024-08-15 06:23:23 +02:00
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
|
const SidebarToggler = (): void => {
|
||
|
const sidebar = document.querySelector(
|
||
|
"aside[id='admin-sidebar']"
|
||
|
) as HTMLElement;
|
||
|
const toggler = document.querySelector(
|
||
|
"button[id='sidebar-toggler']"
|
||
|
) as HTMLButtonElement;
|
||
|
const sidebarBackdrop = document.querySelector(
|
||
|
"div[id='sidebar-backdrop']"
|
||
|
) as HTMLElement;
|
||
|
|
||
|
const setAriaExpanded = (isExpanded: "true" | "false") => {
|
||
|
toggler.setAttribute("aria-expanded", isExpanded);
|
||
|
sidebarBackdrop.setAttribute("aria-expanded", isExpanded);
|
||
|
};
|
||
|
|
||
|
const hideSidebar = () => {
|
||
|
setAriaExpanded("false");
|
||
|
sidebar.classList.add("-translate-x-full");
|
||
|
sidebarBackdrop.classList.add("hidden");
|
||
|
toggler.style.transform = "translateX(0px)";
|
||
|
};
|
||
|
|
||
|
const showSidebar = () => {
|
||
|
setAriaExpanded("true");
|
||
|
sidebar.classList.remove("-translate-x-full");
|
||
|
sidebarBackdrop.classList.remove("hidden");
|
||
|
toggler.style.transform =
|
||
|
"translateX(" + sidebar.getBoundingClientRect().width + "px)";
|
||
|
};
|
||
|
|
||
|
toggler.addEventListener("click", () => {
|
||
|
if (sidebar.classList.contains("-translate-x-full")) {
|
||
|
showSidebar();
|
||
|
} else {
|
||
|
hideSidebar();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
sidebarBackdrop.addEventListener("click", () => {
|
||
|
if (!sidebar.classList.contains("-translate-x-full")) {
|
||
|
hideSidebar();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
const setAriaExpandedOnWindowEvent = () => {
|
||
|
const isExpanded =
|
||
|
!sidebar.classList.contains("-translate-x-full") ||
|
||
|
window.innerWidth >= 768;
|
||
|
const ariaExpanded = toggler.getAttribute("aria-expanded");
|
||
|
if (isExpanded && (!ariaExpanded || ariaExpanded === "false")) {
|
||
|
setAriaExpanded("true");
|
||
|
} else if (!isExpanded && (!ariaExpanded || ariaExpanded === "true")) {
|
||
|
setAriaExpanded("false");
|
||
|
}
|
||
|
};
|
||
|
|
||
|
window.addEventListener("load", setAriaExpandedOnWindowEvent);
|
||
|
window.addEventListener("resize", setAriaExpandedOnWindowEvent);
|
||
|
};
|
||
|
|
||
|
export default SidebarToggler;
|