Files
bls-wallet/extension/source/components/Button.tsx
2022-07-07 11:33:57 +10:00

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;