chore: format htmx and shell
This commit is contained in:
@@ -4,7 +4,7 @@ type ToastFn = (opts: { message: string; duration?: number }) => void;
|
||||
|
||||
const getToast = (): ToastFn | null => {
|
||||
const anyWindow = window as unknown as { showToast?: ToastFn };
|
||||
return typeof anyWindow.showToast === 'function' ? anyWindow.showToast : null;
|
||||
return typeof anyWindow.showToast === "function" ? anyWindow.showToast : null;
|
||||
};
|
||||
|
||||
const toast = (message: string): void => {
|
||||
@@ -13,15 +13,15 @@ const toast = (message: string): void => {
|
||||
|
||||
const setBusy = (el: Element | null, busy: boolean): void => {
|
||||
if (!(el instanceof HTMLElement)) return;
|
||||
el.toggleAttribute('aria-busy', busy);
|
||||
el.dataset.htmxLoading = busy ? 'true' : 'false';
|
||||
el.toggleAttribute("aria-busy", busy);
|
||||
el.dataset.htmxLoading = busy ? "true" : "false";
|
||||
|
||||
if (el instanceof HTMLButtonElement) {
|
||||
el.disabled = busy;
|
||||
}
|
||||
|
||||
if (busy) {
|
||||
el.dataset.htmxBusy = 'true';
|
||||
el.dataset.htmxBusy = "true";
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -34,8 +34,8 @@ const getTriggerFromHtmxEvent = (event: Event): Element | null => {
|
||||
};
|
||||
|
||||
const onReady = (fn: () => void): void => {
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', fn, { once: true });
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", fn, { once: true });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -43,29 +43,29 @@ const onReady = (fn: () => void): void => {
|
||||
};
|
||||
|
||||
onReady(() => {
|
||||
document.addEventListener('htmx:beforeRequest', event => {
|
||||
document.addEventListener("htmx:beforeRequest", (event) => {
|
||||
setBusy(getTriggerFromHtmxEvent(event), true);
|
||||
});
|
||||
|
||||
document.addEventListener('htmx:afterRequest', event => {
|
||||
document.addEventListener("htmx:afterRequest", (event) => {
|
||||
setBusy(getTriggerFromHtmxEvent(event), false);
|
||||
|
||||
const remaining = document.querySelectorAll('.continue-watching-item').length;
|
||||
const remaining = document.querySelectorAll(".continue-watching-item").length;
|
||||
if (remaining !== 0) return;
|
||||
|
||||
const section = document.getElementById('continue-watching-section');
|
||||
const section = document.getElementById("continue-watching-section");
|
||||
section?.remove();
|
||||
});
|
||||
|
||||
document.addEventListener('htmx:responseError', () => {
|
||||
toast('Something went wrong');
|
||||
document.addEventListener("htmx:responseError", () => {
|
||||
toast("Something went wrong");
|
||||
});
|
||||
|
||||
document.addEventListener('htmx:sendError', () => {
|
||||
toast('Network error');
|
||||
document.addEventListener("htmx:sendError", () => {
|
||||
toast("Network error");
|
||||
});
|
||||
|
||||
document.addEventListener('htmx:timeout', () => {
|
||||
toast('Request timed out');
|
||||
document.addEventListener("htmx:timeout", () => {
|
||||
toast("Request timed out");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
export {};
|
||||
|
||||
const onReady = (fn: () => void): void => {
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', fn, { once: true });
|
||||
if (document.readyState === "loading") {
|
||||
document.addEventListener("DOMContentLoaded", fn, { once: true });
|
||||
return;
|
||||
}
|
||||
|
||||
fn();
|
||||
};
|
||||
|
||||
const isMobileViewport = (): boolean => window.matchMedia('(max-width: 1023px)').matches;
|
||||
const isMobileViewport = (): boolean => window.matchMedia("(max-width: 1023px)").matches;
|
||||
|
||||
const initSidebarTransitions = (): void => {
|
||||
requestAnimationFrame(() => {
|
||||
document.documentElement.classList.add('sidebar-ready');
|
||||
document.documentElement.classList.add("sidebar-ready");
|
||||
});
|
||||
};
|
||||
|
||||
const initMobileMenu = (): void => {
|
||||
const menu = document.getElementById('mobile-menu');
|
||||
const backdrop = document.getElementById('mobile-menu-backdrop');
|
||||
const toggle = document.querySelector('[data-mobile-menu-toggle]');
|
||||
const menu = document.getElementById("mobile-menu");
|
||||
const backdrop = document.getElementById("mobile-menu-backdrop");
|
||||
const toggle = document.querySelector("[data-mobile-menu-toggle]");
|
||||
|
||||
if (!(menu instanceof HTMLElement)) return;
|
||||
if (!(backdrop instanceof HTMLElement)) return;
|
||||
@@ -30,34 +30,34 @@ const initMobileMenu = (): void => {
|
||||
let lastFocused: HTMLElement | null = null;
|
||||
|
||||
const setOpen = (nextOpen: boolean): void => {
|
||||
menu.dataset.mobileOpen = nextOpen ? 'true' : 'false';
|
||||
backdrop.dataset.mobileOpen = nextOpen ? 'true' : 'false';
|
||||
backdrop.classList.toggle('hidden', !nextOpen);
|
||||
toggle.setAttribute('aria-expanded', nextOpen ? 'true' : 'false');
|
||||
body.classList.toggle('overflow-hidden', nextOpen);
|
||||
menu.dataset.mobileOpen = nextOpen ? "true" : "false";
|
||||
backdrop.dataset.mobileOpen = nextOpen ? "true" : "false";
|
||||
backdrop.classList.toggle("hidden", !nextOpen);
|
||||
toggle.setAttribute("aria-expanded", nextOpen ? "true" : "false");
|
||||
body.classList.toggle("overflow-hidden", nextOpen);
|
||||
};
|
||||
|
||||
const openMenu = (): void => {
|
||||
if (!isMobileViewport()) return;
|
||||
if (menu.dataset.mobileOpen === 'true') return;
|
||||
if (menu.dataset.mobileOpen === "true") return;
|
||||
|
||||
lastFocused = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
setOpen(true);
|
||||
|
||||
const focusTarget = menu.querySelector<HTMLElement>(
|
||||
'a, button, input, [tabindex]:not([tabindex="-1"])'
|
||||
'a, button, input, [tabindex]:not([tabindex="-1"])',
|
||||
);
|
||||
focusTarget?.focus();
|
||||
};
|
||||
|
||||
const closeMenu = (): void => {
|
||||
if (menu.dataset.mobileOpen !== 'true') return;
|
||||
if (menu.dataset.mobileOpen !== "true") return;
|
||||
setOpen(false);
|
||||
lastFocused?.focus();
|
||||
};
|
||||
|
||||
toggle.addEventListener('click', () => {
|
||||
if (menu.dataset.mobileOpen === 'true') {
|
||||
toggle.addEventListener("click", () => {
|
||||
if (menu.dataset.mobileOpen === "true") {
|
||||
closeMenu();
|
||||
return;
|
||||
}
|
||||
@@ -65,23 +65,23 @@ const initMobileMenu = (): void => {
|
||||
openMenu();
|
||||
});
|
||||
|
||||
backdrop.addEventListener('click', closeMenu);
|
||||
backdrop.addEventListener("click", closeMenu);
|
||||
|
||||
document.addEventListener('keydown', event => {
|
||||
if (event.key !== 'Escape') return;
|
||||
if (menu.dataset.mobileOpen !== 'true') return;
|
||||
document.addEventListener("keydown", (event) => {
|
||||
if (event.key !== "Escape") return;
|
||||
if (menu.dataset.mobileOpen !== "true") return;
|
||||
event.preventDefault();
|
||||
closeMenu();
|
||||
});
|
||||
|
||||
menu.querySelectorAll<HTMLElement>('a, button').forEach(el => {
|
||||
el.addEventListener('click', () => {
|
||||
menu.querySelectorAll<HTMLElement>("a, button").forEach((el) => {
|
||||
el.addEventListener("click", () => {
|
||||
if (!isMobileViewport()) return;
|
||||
closeMenu();
|
||||
});
|
||||
});
|
||||
|
||||
window.addEventListener('resize', () => {
|
||||
window.addEventListener("resize", () => {
|
||||
if (!isMobileViewport()) {
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user