forked from crowetic/commerce
55 lines
1.1 KiB
TypeScript
55 lines
1.1 KiB
TypeScript
import cn from 'classnames'
|
|
import { FC } from 'react'
|
|
import s from './Swatch.module.css'
|
|
import { Check } from '@components/icon'
|
|
import Button, { ButtonProps } from '@components/ui/Button'
|
|
import { isDark } from '@lib/colors'
|
|
interface Props {
|
|
active?: boolean
|
|
children?: any
|
|
className?: string
|
|
label?: string
|
|
variant?: 'size' | 'color' | string
|
|
color?: string
|
|
}
|
|
|
|
const Swatch: FC<Props & ButtonProps> = ({
|
|
className,
|
|
color = '',
|
|
label,
|
|
variant = 'size',
|
|
active,
|
|
...props
|
|
}) => {
|
|
variant = variant?.toLowerCase()
|
|
label = label?.toLowerCase()
|
|
|
|
const rootClassName = cn(
|
|
s.root,
|
|
{
|
|
[s.active]: active,
|
|
[s.size]: variant === 'size',
|
|
[s.color]: color,
|
|
[s.dark]: color ? isDark(color) : false,
|
|
},
|
|
className
|
|
)
|
|
|
|
return (
|
|
<Button
|
|
className={rootClassName}
|
|
style={color ? { backgroundColor: color } : {}}
|
|
{...props}
|
|
>
|
|
{variant === 'color' && active && (
|
|
<span>
|
|
<Check />
|
|
</span>
|
|
)}
|
|
{variant === 'size' ? label : null}
|
|
</Button>
|
|
)
|
|
}
|
|
|
|
export default Swatch
|