(props: AriaColorSwatchProps)
| 44 | * A color swatch displays a preview of a selected color. |
| 45 | */ |
| 46 | export function useColorSwatch(props: AriaColorSwatchProps): ColorSwatchAria { |
| 47 | let {color: value, colorName} = props; |
| 48 | let nonNullValue = value || '#fff0'; |
| 49 | let color = useMemo( |
| 50 | () => (typeof nonNullValue === 'string' ? parseColor(nonNullValue) : nonNullValue), |
| 51 | [nonNullValue] |
| 52 | ); |
| 53 | let {locale} = useLocale(); |
| 54 | let DOMProps = filterDOMProps(props, {labelable: true}); |
| 55 | let stringFormatter = useLocalizedStringFormatter(intlMessages, '@react-aria/color'); |
| 56 | let id = useId(props.id); |
| 57 | |
| 58 | if (!colorName) { |
| 59 | colorName = |
| 60 | color.getChannelValue('alpha') === 0 |
| 61 | ? stringFormatter.format('transparent') |
| 62 | : color.getColorName(locale); |
| 63 | } |
| 64 | |
| 65 | return { |
| 66 | colorSwatchProps: { |
| 67 | ...DOMProps, |
| 68 | role: 'img', |
| 69 | 'aria-roledescription': stringFormatter.format('colorSwatch'), |
| 70 | 'aria-label': [colorName, props['aria-label'] || ''].filter(Boolean).join(', '), |
| 71 | 'aria-labelledby': props['aria-labelledby'] ? `${id} ${props['aria-labelledby']}` : undefined, |
| 72 | id, |
| 73 | style: { |
| 74 | backgroundColor: color.toString('css'), |
| 75 | // @ts-ignore |
| 76 | forcedColorAdjust: 'none' |
| 77 | } |
| 78 | }, |
| 79 | color: color || null |
| 80 | }; |
| 81 | } |
no test coverage detected