Merge pull request #368 from lujakob/feat/add-spinner-to-button

Feat/add spinner to button
This commit is contained in:
Kieran 2023-02-28 14:16:47 +00:00 committed by GitHub
commit 10e96bddb5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 5 deletions

View File

@ -0,0 +1,14 @@
button {
position: relative;
}
.spinner-wrapper {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
}

View File

@ -1,6 +1,9 @@
import "./AsyncButton.css";
import { useState } from "react";
import Spinner from "../Icons/Spinner";
interface AsyncButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
disabled?: boolean;
onClick(e: React.MouseEvent): Promise<void> | void;
children?: React.ReactNode;
}
@ -9,7 +12,7 @@ export default function AsyncButton(props: AsyncButtonProps) {
const [loading, setLoading] = useState<boolean>(false);
async function handle(e: React.MouseEvent) {
if (loading) return;
if (loading || props.disabled) return;
setLoading(true);
try {
if (typeof props.onClick === "function") {
@ -24,8 +27,13 @@ export default function AsyncButton(props: AsyncButtonProps) {
}
return (
<button type="button" disabled={loading} {...props} onClick={handle}>
{props.children}
<button className="spinner-button" type="button" disabled={loading || props.disabled} {...props} onClick={handle}>
<span style={{ visibility: loading ? "hidden" : "visible" }}>{props.children}</span>
{loading && (
<span className="spinner-wrapper">
<Spinner />
</span>
)}
</button>
);
}

View File

@ -16,6 +16,7 @@ import { HexKey } from "@snort/nostr";
import useFileUpload from "Upload";
import messages from "./messages";
import AsyncButton from "../../Element/AsyncButton";
export interface ProfileSettingsProps {
avatar?: boolean;
@ -166,9 +167,9 @@ export default function ProfileSettings(props: ProfileSettingsProps) {
<div className="form-group card">
<div></div>
<div>
<button type="button" onClick={() => saveProfile()}>
<AsyncButton onClick={() => saveProfile()}>
<FormattedMessage {...messages.Save} />
</button>
</AsyncButton>
</div>
</div>
</div>