mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
Currently for a user with a screen reader it would be near impossible to use this page. This change will read clearly what the swatch is and for what group it belongs.
58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
import cn from 'classnames'
|
|
import { FC } from 'react'
|
|
import s from './Swatch.module.css'
|
|
import { Check } from '@components/icons'
|
|
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<Omit<ButtonProps, 'variant'> & Props> = ({
|
|
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 } : {}}
|
|
role="option"
|
|
aria-label={`${variant} ${label}`}
|
|
aria-selected={active}
|
|
{...props}
|
|
>
|
|
{variant === 'color' && active && (
|
|
<span>
|
|
<Check />
|
|
</span>
|
|
)}
|
|
{variant === 'size' ? label : null}
|
|
</Button>
|
|
)
|
|
}
|
|
|
|
export default Swatch
|