Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading in button #1788

Merged
merged 9 commits into from
Aug 1, 2024
24 changes: 19 additions & 5 deletions src/blocks/button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled, { FlattenSimpleInterpolation } from 'styled-components';
import type { TransformedHTMLAttributes } from '../Blocks.types';
import type { ButtonSize, ButtonVariant } from './Button.types';
import { getButtonSizeStyles, getButtonVariantStyles } from './Button.utils';
import { Spinner } from '../spinner';

export type ButtonProps = {
/* Child react nodes rendered by Box */
Expand All @@ -26,6 +27,8 @@ export type ButtonProps = {
variant?: ButtonVariant;
/* Button takes the full width if enabled */
block?: boolean;
/* Button loading state */
loading?: boolean;
} & TransformedHTMLAttributes<HTMLButtonElement>;

const StyledButton = styled.button<ButtonProps>`
Expand All @@ -44,9 +47,10 @@ const StyledButton = styled.button<ButtonProps>`
align-items: center;
justify-content: center;
}
mishramonalisha76 marked this conversation as resolved.
Show resolved Hide resolved

/* Button variant CSS styles */
${({ variant }) => getButtonVariantStyles(variant || 'primary')}
${({ variant, loading }) => getButtonVariantStyles(variant || 'primary', loading!)}

${({ loading }) => loading && 'opacity: var(--opacity-80);'}

/* Button and font size CSS styles */
${({ iconOnly, size }) => getButtonSizeStyles({ iconOnly: !!iconOnly, size: size || 'medium' })}
Expand All @@ -61,6 +65,9 @@ const StyledButton = styled.button<ButtonProps>`
${(props) => props.css || ''}
`;

const SpinnerContainer = styled.div`
padding: 5px;
`;
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
(
{
Expand All @@ -69,6 +76,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(
size = 'medium',
leadingIcon,
trailingIcon,
loading = false,
iconOnly,
circular = false,
children,
Expand All @@ -77,20 +85,26 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(
ref
) => (
<StyledButton
{...(disabled ? { 'aria-disabled': true } : {})}
{...(disabled || loading ? { 'aria-disabled': true } : {})}
circular={circular}
mishramonalisha76 marked this conversation as resolved.
Show resolved Hide resolved
disabled={disabled}
disabled={disabled || loading}
iconOnly={iconOnly}
loading={loading}
role="button"
ref={ref}
size={size}
variant={variant}
{...props}
>
{loading && (
<SpinnerContainer>
<Spinner />
</SpinnerContainer>
)}
{leadingIcon && <span className="icon icon-text">{leadingIcon}</span>}
{!iconOnly && children}
{trailingIcon && <span className="icon icon-text">{trailingIcon}</span>}
{iconOnly && !children && <span className="icon icon-only">{iconOnly}</span>}
{iconOnly && !loading && !children && <span className="icon icon-only">{iconOnly}</span>}
</StyledButton>
)
);
Expand Down
Loading
Loading