Skip to content

Commit

Permalink
Conclude that inline conditions are simplest anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentSmedinga committed Nov 4, 2024
1 parent bd7fd38 commit f79b546
Showing 1 changed file with 19 additions and 47 deletions.
66 changes: 19 additions & 47 deletions packages/react/src/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import clsx from 'clsx'
import { forwardRef } from 'react'
import type { ButtonHTMLAttributes, ForwardedRef, PropsWithChildren, ReactNode } from 'react'
import type { ButtonHTMLAttributes, ForwardedRef, PropsWithChildren } from 'react'
import { Icon } from '../Icon'
import type { IconProps } from '../Icon'

Expand Down Expand Up @@ -42,52 +42,24 @@ export const Button = forwardRef(
(
{ children, className, disabled, icon, iconBefore, iconOnly, type, variant = 'primary', ...restProps }: ButtonProps,
ref: ForwardedRef<HTMLButtonElement>,
) => {
const content = (): ReactNode => {
switch (true) {
case !icon:
return children
case iconBefore:
return (
<>
<Icon svg={icon} size="level-5" />
{children}
</>
)
case iconOnly:
return (
<>
<Icon svg={icon} size="level-5" square />
<span className="ams-visually-hidden">{children}</span>
</>
)
default:
return (
<>
{children}
<Icon svg={icon} size="level-5" />
</>
)
}
}

return (
<button
{...restProps}
className={clsx(
'ams-button',
`ams-button--${variant}`,
icon && iconOnly && !iconBefore && `ams-button--icon-only`,
className,
)}
disabled={disabled}
ref={ref}
type={type || 'button'}
>
{content()}
</button>
)
},
) => (
<button
{...restProps}
className={clsx(
'ams-button',
`ams-button--${variant}`,
icon && iconOnly && !iconBefore && `ams-button--icon-only`,
className,
)}
disabled={disabled}
ref={ref}
type={type || 'button'}
>
{icon && (iconBefore || iconOnly) && <Icon svg={icon} size="level-5" square={iconOnly} />}
{icon && iconOnly ? <span className="ams-visually-hidden">{children}</span> : children}
{icon && !iconBefore && <Icon svg={icon} size="level-5" />}
</button>
),
)

Button.displayName = 'Button'

0 comments on commit f79b546

Please sign in to comment.