50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { useEffect } from "preact/hooks";
|
|
|
|
declare global {
|
|
// deno-lint-ignore no-var
|
|
var loadingTimeout: ReturnType<typeof setTimeout> | undefined;
|
|
}
|
|
|
|
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(() => {
|
|
if (globalThis.loadingTimeout) {
|
|
clearTimeout(globalThis.loadingTimeout);
|
|
delete globalThis.loadingTimeout;
|
|
setTimeout(() => {
|
|
globalThis.dispatchEvent(new CustomEvent("loading-finished"));
|
|
document.querySelector("main")?.classList.remove("loading");
|
|
}, 100);
|
|
} else {
|
|
globalThis.dispatchEvent(new CustomEvent("loading-finished"));
|
|
document.querySelector("main")?.classList.remove("loading");
|
|
}
|
|
});
|
|
|
|
return (
|
|
<a
|
|
href={href}
|
|
style={style}
|
|
onClick={handleClick}
|
|
class={_class}
|
|
>
|
|
{children}
|
|
</a>
|
|
);
|
|
}
|