Skip to content

Commit

Permalink
feat: show red spinner on danger buttons when isLoading
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan-chaturvedi committed Nov 16, 2023
1 parent d0986ad commit 7f328bf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 3 additions & 1 deletion frontend/components/common/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,16 @@ export function Button(buttonProps: ButtonProps) {
/>
)

const spinnerColor = variant === 'danger' ? 'red' : 'emerald'

return (
<button
{...buttonProps}
className={computedClassName}
disabled={buttonProps.disabled || isLoading}
>
{!isLoading && arrow === 'left' && arrowIcon}
{isLoading && <Spinner size={'sm'} />}
{isLoading && <Spinner size={'sm'} color={spinnerColor} />}
{children}
{!isLoading && arrow === 'right' && arrowIcon}
</button>
Expand Down
19 changes: 15 additions & 4 deletions frontend/components/common/Spinner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import clsx from 'clsx'
import { color } from 'framer-motion'

const SIZES = {
sm: 'border-2 h-4 w-4',
Expand All @@ -7,16 +8,26 @@ const SIZES = {
xl: 'border-6 h-12 w-12',
}

const COLORS = {
emerald: 'border-emerald-500',
red: 'border-red-500',
}

interface SpinnerProps {
size: 'sm' | 'md' | 'lg' | 'xl'
color?: 'emerald' | 'red'
}

export default function Spinner(props: SpinnerProps = { size: 'lg' }) {
const { size } = props
export default function Spinner(props: SpinnerProps = { size: 'lg', color: 'emerald' }) {
const { size, color } = props

const spinnerColor = color || 'emerald'

const BASE_STYLE =
'inline-block animate-spin rounded-full border-4 border-solid border-emerald-500 border-r-transparent align-[-0.125em] motion-reduce:animate-[spin_1.5s_linear_infinite]'
'inline-block animate-spin rounded-full border-4 border-solid border-r-transparent align-[-0.125em] motion-reduce:animate-[spin_1.5s_linear_infinite]'

return (
<div className={clsx(BASE_STYLE, SIZES[size])} role="status">
<div className={clsx(BASE_STYLE, SIZES[size], COLORS[spinnerColor])} role="status">
<span className="!absolute !-m-px !h-px !w-px !overflow-hidden !whitespace-nowrap !border-0 !p-0 ![clip:rect(0,0,0,0)]">
Loading...
</span>
Expand Down

0 comments on commit 7f328bf

Please sign in to comment.