MCPcopy Create free account
hub / github.com/adobe/react-spectrum / useColorSwatch

Function useColorSwatch

packages/react-aria/src/color/useColorSwatch.ts:46–81  ·  view source on GitHub ↗
(props: AriaColorSwatchProps)

Source from the content-addressed store, hash-verified

44 * A color swatch displays a preview of a selected color.
45 */
46export 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}

Callers 3

ColorSwatch.tsxFile · 0.90
ColorSwatch.tsxFile · 0.90
ColorSwatchFunction · 0.90

Calls 10

parseColorFunction · 0.90
useLocaleFunction · 0.90
filterDOMPropsFunction · 0.90
useIdFunction · 0.90
getChannelValueMethod · 0.80
getColorNameMethod · 0.80
filterMethod · 0.65
toStringMethod · 0.65
formatMethod · 0.45

Tested by

no test coverage detected