Brendan Offer 1c3bd853fb add roles and aria-selected to the product page's swatches
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.
2021-04-12 20:56:11 -04:00

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