Skip to content

Commit

Permalink
fix: reduce cognitive complexity from button also use output instead …
Browse files Browse the repository at this point in the history
…of the status role on spinner
  • Loading branch information
darioegb committed Feb 6, 2024
1 parent a19ac73 commit 596ffbd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
44 changes: 34 additions & 10 deletions packages/components/button/src/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,27 @@ const renderContent = (
)
}

const generateButtonAttributes = (
variant: string,
className: string,
disabled: boolean,
isBusy: boolean,
colorScheme?: DefaultColorPalette | keyof CustomColorPalette,
size?: Size,
shape?: ButtonShape,
): Record<string, string> => {
const color = (colorScheme && getMergedConfig().colors[colorScheme]) || '#d3d3d3'
const contrastColor = getContrastColor(color)
const sizeClass = size && (size !== 'md' || variant === 'unstyled') ? styles[size] : ''
const shapeClass = shape && (shape !== 'rounded' || variant === 'unstyled') ? styles[shape] : ''
const variantClass = variant !== 'unstyled' ? styles[variant] : ''
const disabledClass = disabled || isBusy ? styles.disabled : ''
const buttonClassNames = `${sizeClass} ${shapeClass} ${variantClass}
${disabledClass} ${className}`.trim()

return { color, contrastColor, buttonClassNames }
}

export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
(
{
Expand All @@ -92,16 +113,19 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
}: ButtonProps,
ref: ForwardedRef<HTMLButtonElement>,
) => {
const { size = variant !== 'unstyled' && 'md', shape = variant !== 'unstyled' && 'rounded' } =
rest
const color = (colorScheme && getMergedConfig().colors[colorScheme]) || '#d3d3d3'
const contrastColor = getContrastColor(color)
const sizeClass = size && (size !== 'md' || variant === 'unstyled') ? styles[size] : ''
const shapeClass = shape && (shape !== 'rounded' || variant === 'unstyled') ? styles[shape] : ''
const variantClass = variant !== 'unstyled' ? styles[variant] : ''
const disabledClass = disabled || isBusy ? styles.disabled : ''
const buttonClassNames = `${sizeClass} ${shapeClass} ${variantClass}
${disabledClass} ${className}`.trim()
const {
size = variant !== 'unstyled' ? 'md' : undefined,
shape = variant !== 'unstyled' ? 'rounded' : undefined,
} = rest
const { color, contrastColor, buttonClassNames } = generateButtonAttributes(
variant,
className,
disabled,
isBusy,
colorScheme,
size,
shape as ButtonShape | undefined,
)

return (
<button
Expand Down
5 changes: 2 additions & 3 deletions packages/components/spinner/src/spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const createSpinnerStyles = ({
...(style ?? {}),
})

export const Spinner = forwardRef<HTMLSpanElement, SpinnerProps>(
export const Spinner = forwardRef<HTMLOutputElement, SpinnerProps>(
(
{
style,
Expand Down Expand Up @@ -67,11 +67,10 @@ export const Spinner = forwardRef<HTMLSpanElement, SpinnerProps>(
`.trim()

return (
<span
<output
aria-label="loading"
className={spinnerClass}
ref={ref}
role="status"
style={spinnerStyles}
{...rest}
/>
Expand Down

0 comments on commit 596ffbd

Please sign in to comment.