refactor: share dom ready

This commit is contained in:
2026-06-01 22:25:47 +02:00
committed by Milas Holsting
parent 4a1467467c
commit e269d15199
4 changed files with 13 additions and 26 deletions

View File

@@ -1,5 +1,7 @@
export {};
import { onReady } from "./utils";
type ToastFn = (opts: { message: string; duration?: number }) => void;
const getToast = (): ToastFn | null => {
@@ -33,15 +35,6 @@ const getTriggerFromHtmxEvent = (event: Event): Element | null => {
return detail.detail?.elt ?? null;
};
const onReady = (fn: () => void): void => {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fn, { once: true });
return;
}
fn();
};
onReady(() => {
document.addEventListener("htmx:beforeRequest", (event) => {
setBusy(getTriggerFromHtmxEvent(event), true);

View File

@@ -1,13 +1,6 @@
export {};
const onReady = (fn: () => void): void => {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fn, { once: true });
return;
}
fn();
};
import { onReady } from "./utils";
const isMobileViewport = (): boolean => window.matchMedia("(max-width: 1023px)").matches;

View File

@@ -11,3 +11,12 @@ export const parseClassList = (value: string | null): string[] => {
.map((entry: string): string => entry.trim())
.filter((entry: string): boolean => entry.length > 0);
};
export const onReady = (fn: () => void): void => {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fn, { once: true });
return;
}
fn();
};

View File

@@ -458,15 +458,6 @@ const getRenderedWatchlistIds = (): number[] => {
return Array.from(ids);
};
const onReady = (fn: () => void): void => {
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", fn, { once: true });
return;
}
fn();
};
const installDelegatedHandlers = (): void => {
document.addEventListener("click", (event) => {
const target = event.target;
@@ -537,3 +528,4 @@ onReady(() => {
installDelegatedHandlers();
});
import { onReady } from "./utils";