mirror of
https://github.com/getwax/bls-wallet.git
synced 2026-04-23 03:00:37 -04:00
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { IconProps } from 'phosphor-react';
|
|
import { ReactElement, ReactNode, useState } from 'react';
|
|
import onAction from '../helpers/onAction';
|
|
|
|
const Button = (props: {
|
|
className?: string;
|
|
onPress: () => unknown;
|
|
loading?: boolean;
|
|
children?: ReactNode;
|
|
icon?: IconProps;
|
|
iconLeft?: IconProps;
|
|
}): ReactElement => {
|
|
const [pressLoading, setPressLoading] = useState(false);
|
|
|
|
return (
|
|
<div
|
|
className={
|
|
props.loading || pressLoading
|
|
? 'btn-loading'
|
|
: `flex gap-2 items-center select-none ${props.className}`
|
|
}
|
|
{...onAction(async () => {
|
|
const pressResult = props.onPress();
|
|
|
|
if (pressResult instanceof Promise) {
|
|
setPressLoading(true);
|
|
|
|
try {
|
|
await pressResult;
|
|
} catch (err) {
|
|
// Enhancement: Give the button an error state
|
|
console.error(err);
|
|
} finally {
|
|
setPressLoading(false);
|
|
}
|
|
}
|
|
})}
|
|
>
|
|
{props.iconLeft && <div>{props.iconLeft}</div>}
|
|
<div>{props.children}</div>
|
|
{props.icon && <div>{props.icon}</div>}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default Button;
|