import "./async-button.css"; import { forwardRef, useState } from "react"; import Spinner from "./spinner"; import classNames from "classnames"; interface AsyncButtonProps extends React.ButtonHTMLAttributes { disabled?: boolean; onClick?: (e: React.MouseEvent) => Promise | void; children?: React.ReactNode; } const AsyncButton = forwardRef((props: AsyncButtonProps, ref) => { const [loading, setLoading] = useState(false); async function handle(e: React.MouseEvent) { e.stopPropagation(); if (loading || props.disabled) return; setLoading(true); try { if (props.onClick) { await props.onClick(e); } } finally { setLoading(false); } } return ( ); }); export default AsyncButton;