@coveord/plasma-mantine
Version:
A Plasma flavoured Mantine theme
22 lines (18 loc) • 683 B
text/typescript
import {MouseEvent, MouseEventHandler, useState} from 'react';
export const useClickWithLoading = (handler?: MouseEventHandler<HTMLButtonElement>) => {
const [isLoading, setIsLoading] = useState(false);
const handleClick = async (event: MouseEvent<HTMLButtonElement>) => {
const possiblePromise: unknown = handler?.(event);
try {
if (possiblePromise instanceof Promise) {
setIsLoading(true);
await possiblePromise;
setIsLoading(false);
}
} catch (err) {
setIsLoading(false);
console.error(err);
}
};
return {isLoading, handleClick};
};