memorium/islands/Link.tsx

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-01-20 23:37:03 +01:00
import { useEffect } from "preact/hooks";
declare global {
// deno-lint-ignore no-var
2025-01-20 23:42:22 +01:00
var loadingTimeout: ReturnType<typeof setTimeout> | undefined;
2025-01-20 23:37:03 +01:00
}
export function Link(
{ href, children, class: _class, style }: {
href?: string;
class?: string;
style?: preact.JSX.CSSProperties;
children: preact.ComponentChildren;
},
) {
function handleClick() {
if (globalThis.loadingTimeout) {
return;
}
globalThis.loadingTimeout = setTimeout(() => {
document.querySelector("main")?.classList.add("loading");
}, 100);
}
useEffect(() => {
2025-01-21 00:10:05 +01:00
if (globalThis.loadingTimeout) {
clearTimeout(globalThis.loadingTimeout);
delete globalThis.loadingTimeout;
setTimeout(() => {
globalThis.dispatchEvent(new CustomEvent("loading-finished"));
2025-01-21 00:10:05 +01:00
document.querySelector("main")?.classList.remove("loading");
}, 100);
} else {
globalThis.dispatchEvent(new CustomEvent("loading-finished"));
2025-01-20 23:37:03 +01:00
document.querySelector("main")?.classList.remove("loading");
2025-01-21 00:10:05 +01:00
}
2025-01-20 23:37:03 +01:00
});
return (
<a
href={href}
style={style}
onClick={handleClick}
class={_class}
>
{children}
</a>
);
}