refactor(AsyncButton): add loader

This commit is contained in:
Lukas Jakob 2023-02-27 09:20:49 -06:00
parent 2e78da732a
commit 0b7d070146
2 changed files with 51 additions and 1 deletions

View File

@ -0,0 +1,27 @@
button {
position: relative;
}
.spinner {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
display: flex;
justify-content: center;
align-items: center;
animation-name: spin;
animation-duration: 800ms;
animation-iteration-count: infinite;
animation-timing-function: linear;
}
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}

View File

@ -1,3 +1,4 @@
import "./AsyncButton.css";
import { useState } from "react";
interface AsyncButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
@ -5,6 +6,27 @@ interface AsyncButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement>
children?: React.ReactNode;
}
interface LoaderProps {
className: string
}
const Loader = ({ className }: LoaderProps ) => (
<div className={className}>
<svg
width="13"
height="14"
viewBox="0 0 13 14"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M4.38798 12.616C3.36313 12.2306 2.46328 11.5721 1.78592 10.7118C1.10856 9.85153 0.679515 8.82231 0.545268 7.73564C0.411022 6.64897 0.576691 5.54628 1.02433 4.54704C1.47197 3.54779 2.1845 2.69009 3.08475 2.06684C3.98499 1.4436 5.03862 1.07858 6.13148 1.01133C7.22435 0.944078 8.31478 1.17716 9.28464 1.68533C10.2545 2.19349 11.0668 2.95736 11.6336 3.89419C12.2004 4.83101 12.5 5.90507 12.5 7"
stroke="white"
/>
</svg>
</div>
)
export default function AsyncButton(props: AsyncButtonProps) {
const [loading, setLoading] = useState<boolean>(false);
@ -25,7 +47,8 @@ export default function AsyncButton(props: AsyncButtonProps) {
return (
<button type="button" disabled={loading} {...props} onClick={handle}>
{props.children}
<span style={{visibility: (loading ? "hidden" : "visible")}}>{props.children}</span>
{loading && <Loader className="spinner" />}
</button>
);
}