feat(SW-1255): Add loading state to button component

This commit is contained in:
Tobias Johansson
2025-04-23 10:03:33 +02:00
committed by Simon Emanuelsson
parent 80ccdc0e44
commit 89468bc37f
8 changed files with 295 additions and 3 deletions

View File

@@ -68,6 +68,13 @@ export const PrimaryDisabled: Story = {
},
}
export const PrimaryLoading: Story = {
args: {
...PrimaryDefault.args,
isPending: true,
},
}
export const PrimaryLarge: Story = {
args: {
...PrimaryDefault.args,
@@ -106,6 +113,13 @@ export const PrimaryInvertedDisabled: Story = {
},
}
export const PrimaryInvertedLoading: Story = {
args: {
...PrimaryInvertedDefault.args,
isPending: true,
},
}
export const PrimaryInvertedLarge: Story = {
args: {
...PrimaryInvertedDefault.args,
@@ -181,6 +195,13 @@ export const SecondaryInvertedDisabled: Story = {
},
}
export const SecondaryInvertedLoading: Story = {
args: {
...SecondaryInvertedDefault.args,
isPending: true,
},
}
export const SecondaryInvertedLarge: Story = {
args: {
...SecondaryInvertedDefault.args,
@@ -218,6 +239,12 @@ export const TertiaryDisabled: Story = {
},
}
export const TertiaryLoading: Story = {
args: {
...TertiaryDefault.args,
isPending: true,
},
}
export const TertiaryLarge: Story = {
args: {
...TertiaryDefault.args,

View File

@@ -3,6 +3,8 @@ import { Button as ButtonRAC } from 'react-aria-components'
import { variants } from './variants'
import type { ButtonProps } from './types'
import { Spinner } from '../Spinner'
import styles from './button.module.css'
export function Button({
variant,
@@ -12,7 +14,8 @@ export function Button({
typography,
className,
children,
isPending,
...props
}: ButtonProps) {
const classNames = variants({
@@ -20,10 +23,24 @@ export function Button({
color,
size,
wrapping,
typography,
className,
})
return <ButtonRAC {...props} className={classNames} />
return (
<ButtonRAC {...props} className={classNames} isPending={isPending}>
{({ isPending }) => {
return (
<>
{children}
{isPending && (
<div className={styles.spinnerWrapper}>
<Spinner size="Small" color="CurrentColor" />
</div>
)}
</>
)
}}
</ButtonRAC>
)
}

View File

@@ -167,3 +167,9 @@
.variant-text.color-inverted:disabled {
color: var(--Component-Button-Brand-Secondary-On-fill-Disabled);
}
.spinnerWrapper {
display: flex;
align-items: center;
margin-left: var(--Space-x1);
}