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

Function useColorSlider

packages/react-aria/src/color/useColorSlider.ts:55–172  ·  view source on GitHub ↗
(
  props: AriaColorSliderOptions,
  state: ColorSliderState
)

Source from the content-addressed store, hash-verified

53 * Color sliders allow users to adjust an individual channel of a color value.
54 */
55export function useColorSlider(
56 props: AriaColorSliderOptions,
57 state: ColorSliderState
58): ColorSliderAria {
59 let {trackRef, inputRef, orientation, channel, 'aria-label': ariaLabel, name, form} = props;
60
61 let {locale, direction} = useLocale();
62
63 // Provide a default aria-label if there is no other label provided.
64 if (!props.label && !ariaLabel && !props['aria-labelledby']) {
65 ariaLabel = state.value.getChannelName(channel, locale);
66 }
67
68 let {groupProps, trackProps, labelProps, outputProps} = useSlider(
69 // @ts-ignore - ignore unused incompatible props
70 {...props, 'aria-label': ariaLabel},
71 state,
72 trackRef
73 );
74 let {inputProps, thumbProps} = useSliderThumb(
75 {
76 index: 0,
77 orientation,
78 isDisabled: props.isDisabled,
79 name,
80 form,
81 trackRef,
82 inputRef
83 },
84 state
85 );
86
87 let value = state.getDisplayColor();
88 let generateBackground = () => {
89 let to: string;
90 if (orientation === 'vertical') {
91 to = 'top';
92 } else if (direction === 'ltr') {
93 to = 'right';
94 } else {
95 to = 'left';
96 }
97 switch (channel) {
98 case 'hue': {
99 let stops = [0, 60, 120, 180, 240, 300, 360]
100 .map(hue => value.withChannelValue('hue', hue).toString('css'))
101 .join(', ');
102 return `linear-gradient(to ${to}, ${stops})`;
103 }
104 case 'lightness': {
105 // We have to add an extra color stop in the middle so that the hue shows up at all.
106 // Otherwise it will always just be black to white.
107 let min = state.getThumbMinValue(0);
108 let max = state.getThumbMaxValue(0);
109 let start = value.withChannelValue(channel, min).toString('css');
110 let middle = value.withChannelValue(channel, (max - min) / 2).toString('css');
111 let end = value.withChannelValue(channel, max).toString('css');
112 return `linear-gradient(to ${to}, ${start}, ${middle}, ${end})`;

Callers 2

ColorSlider.tsxFile · 0.90
ColorSlider.tsxFile · 0.90

Calls 10

useLocaleFunction · 0.90
useSliderFunction · 0.90
useSliderThumbFunction · 0.90
useVisuallyHiddenFunction · 0.90
mergePropsFunction · 0.90
generateBackgroundFunction · 0.85
getChannelNameMethod · 0.80
getHueNameMethod · 0.80
getColorNameMethod · 0.80
getDisplayColorMethod · 0.65

Tested by

no test coverage detected