Skip to content

Commit 6f3b4c8

Browse files
authored
Fix label position and custom height for vertical ColorSlider (#1524)
* Fix label position and custom height for vertical ColorSlider * Hide label and value label on vertical color sliders * Fix lightness gradient
1 parent d8e8599 commit 6f3b4c8

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

packages/@adobe/spectrum-css-temp/components/colorslider/index.css

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@
2929
pointer-events: none;
3030
}
3131

32-
.spectrum-ColorSlider-container {
32+
.spectrum-ColorSlider-container--horizontal {
3333
width: var(--spectrum-colorslider-default-length);
3434
}
3535

36+
.spectrum-ColorSlider-container--vertical {
37+
display: flex;
38+
flex-direction: column;
39+
height: var(--spectrum-colorslider-default-length);
40+
}
41+
3642
.spectrum-ColorSlider {
3743
position: relative;
3844
display: block;
@@ -62,7 +68,7 @@
6268
display: inline-block;
6369

6470
width: var(--spectrum-colorslider-vertical-width);
65-
height: var(--spectrum-colorslider-vertical-default-length);
71+
flex: 1;
6672

6773
.spectrum-ColorSlider-handle {
6874
left: 50%;

packages/@react-aria/color/src/useColorSlider.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,17 @@ export function useColorSlider(props: ColorSliderAriaOptions, state: ColorSlider
5656
switch (channel) {
5757
case 'hue':
5858
return `linear-gradient(to ${to}, rgb(255, 0, 0) 0%, rgb(255, 255, 0) 17%, rgb(0, 255, 0) 33%, rgb(0, 255, 255) 50%, rgb(0, 0, 255) 67%, rgb(255, 0, 255) 83%, rgb(255, 0, 0) 100%)`;
59+
case 'lightness': {
60+
// We have to add an extra color stop in the middle so that the hue shows up at all.
61+
// Otherwise it will always just be black to white.
62+
let min = state.getThumbMinValue(0);
63+
let max = state.getThumbMaxValue(0);
64+
let start = value.withChannelValue(channel, min).toString('css');
65+
let middle = value.withChannelValue(channel, (max - min) / 2).toString('css');
66+
let end = value.withChannelValue(channel, max).toString('css');
67+
return `linear-gradient(to ${to}, ${start}, ${middle}, ${end})`;
68+
}
5969
case 'saturation':
60-
case 'lightness':
6170
case 'brightness':
6271
case 'red':
6372
case 'green':

packages/@react-spectrum/color/src/ColorSlider.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ function ColorSlider(props: SpectrumColorSliderProps, ref: FocusableRef<HTMLDivE
4141
let defaultLabel = channel[0].toUpperCase() + channel.slice(1);
4242

4343
let labelText = props.label;
44-
if (props.label === undefined) {
45-
if (!vertical) {
46-
labelText = defaultLabel;
47-
}
48-
}
4944
let ariaLabel = props['aria-label'] ?? (labelText == null ? defaultLabel : undefined);
5045

5146
let numberFormatter = useNumberFormatter();
@@ -85,12 +80,20 @@ function ColorSlider(props: SpectrumColorSliderProps, ref: FocusableRef<HTMLDivE
8580
<div
8681
ref={domRef}
8782
{...styleProps}
88-
className={classNames(styles, 'spectrum-ColorSlider-container')}>
89-
<Flex direction="row" justifyContent={alignLabel}>
90-
{labelText && <Label {...labelProps}>{labelText}</Label>}
91-
{/* TODO: is it on purpose that aria-labelledby isn't passed through? */}
92-
{showValueLabel && <Label aria-labelledby={labelProps.id}>{state.getThumbValueLabel(0)}</Label>}
93-
</Flex>
83+
className={classNames(
84+
styles,
85+
{
86+
'spectrum-ColorSlider-container--horizontal': !vertical,
87+
'spectrum-ColorSlider-container--vertical': vertical
88+
}
89+
)}>
90+
{!vertical &&
91+
<Flex direction="row" justifyContent={alignLabel}>
92+
{labelText && <Label {...labelProps}>{labelText}</Label>}
93+
{/* TODO: is it on purpose that aria-labelledby isn't passed through? */}
94+
{showValueLabel && <Label aria-labelledby={labelProps.id}>{state.getThumbValueLabel(0)}</Label>}
95+
</Flex>
96+
}
9497
<div
9598
{...groupProps}
9699
className={classNames(

packages/@react-spectrum/color/stories/ColorSlider.stories.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ storiesOf('ColorSlider', module)
5454
'custom width',
5555
() => <ColorSlider defaultValue="#7f0000" channel={'red'} width={300} />
5656
)
57+
.add(
58+
'custom height',
59+
() => <ColorSlider defaultValue="#7f0000" channel={'red'} orientation="vertical" height={300} />
60+
)
5761
.add(
5862
'rgba',
5963
() => {

packages/@react-stately/color/src/useColorSliderState.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ export function useColorSliderState(props: ColorSliderStateOptions): ColorSlider
7070
case 'hue':
7171
return parseColor(`hsl(${c.getChannelValue('hue')}, 100%, 50%)`);
7272
case 'lightness':
73-
c = c.withChannelValue('saturation', 0);
7473
case 'brightness':
7574
case 'saturation':
7675
case 'red':

0 commit comments

Comments
 (0)