Skip to content

Commit c84db0a

Browse files
authored
feat(Slider): be able to change value label position and color (#2734)
1 parent f9ba468 commit c84db0a

File tree

9 files changed

+106
-21
lines changed

9 files changed

+106
-21
lines changed

packages/core/src/components/Slider/Slider.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { SliderColor as SliderColorEnum } from "./SliderConstants";
1111
import cx from "classnames";
1212
import { withStaticProps } from "../../types";
1313
import styles from "./Slider.module.scss";
14-
import { SliderColor, SliderSize } from "./Slider.types";
14+
import { SliderColor, SliderLabelColor, SliderLabelPosition, SliderSize } from "./Slider.types";
1515

1616
export type SliderProps = {
1717
/**
@@ -76,6 +76,14 @@ export type SliderProps = {
7676
* Always show `value` instead of Tooltip
7777
*/
7878
showValue?: boolean;
79+
/**
80+
* Position of the `value` when `showValue` is true
81+
*/
82+
valueLabelPosition?: SliderLabelPosition;
83+
/**
84+
* Color of the `value` when `showValue` is true
85+
*/
86+
valueLabelColor?: SliderLabelColor;
7987
/**
8088
* Size small/medium/large of the component (Slider)
8189
*/
@@ -136,6 +144,8 @@ const Slider: React.FC<SliderProps> & {
136144
ranged = false,
137145
step = 1,
138146
showValue = false,
147+
valueLabelPosition = "top",
148+
valueLabelColor = "primary",
139149
size = "small",
140150
value,
141151
defaultValue = 0,
@@ -167,6 +177,8 @@ const Slider: React.FC<SliderProps> & {
167177
onChange={onChange}
168178
ranged={ranged}
169179
showValue={showValue}
180+
valueLabelPosition={valueLabelPosition}
181+
valueLabelColor={valueLabelColor}
170182
size={size}
171183
step={step}
172184
value={value}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { BASE_SIZES } from "../../constants";
2+
import { DialogPosition } from "../Dialog";
3+
import { TypographyColor } from "../Typography";
24

35
export type SliderColor = "primary" | "negative" | "positive";
46

7+
export type SliderLabelColor = Extract<TypographyColor, "primary" | "secondary">;
8+
9+
export type SliderLabelPosition = Extract<DialogPosition, "top" | "bottom">;
10+
511
export type SliderSize = (typeof BASE_SIZES)[keyof typeof BASE_SIZES];
612

713
export type InfixKind = "prefix" | "postfix";

packages/core/src/components/Slider/SliderBase/SliderThumb.module.scss

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
.dragging {
1717
cursor: grabbing !important;
18-
transform: translate(-50%, -50%) scale(1.33, 1.33);
18+
transform: translate(-50%, -50%) scale(1.15, 1.15);
1919
transform-origin: center center;
2020
transition: transform var(--motion-productive-long) var(--motion-timing-enter);
2121
box-shadow: var(--box-shadow-small);
@@ -62,8 +62,8 @@
6262
}
6363

6464
.medium {
65-
height: 24px;
66-
width: 24px;
65+
height: 20px;
66+
width: 20px;
6767
}
6868

6969
.large {
@@ -72,12 +72,27 @@
7272
}
7373

7474
.label {
75-
bottom: calc(100% + 8px);
7675
left: 50%;
7776
max-width: 50px;
7877
overflow: hidden;
7978
position: absolute;
8079
text-overflow: ellipsis;
8180
transform: translate(-50%, 0);
8281
white-space: nowrap;
82+
83+
&.positionTop {
84+
bottom: calc(100% + 8px);
85+
}
86+
87+
&.positionBottom {
88+
top: calc(100% + 8px);
89+
}
90+
91+
&.colorPrimary {
92+
color: var(--primary-text-color);
93+
}
94+
95+
&.colorSecondary {
96+
color: var(--secondary-text-color);
97+
}
8398
}

packages/core/src/components/Slider/SliderBase/SliderThumb.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import cx from "classnames";
88
import styles from "./SliderThumb.module.scss";
99
import { getStyle } from "../../../helpers/typesciptCssModulesHelper";
1010
import { SliderColor, SliderSize } from "../Slider.types";
11+
import { camelCase } from "lodash-es";
1112

1213
export interface SliderThumbProps extends VibeComponentProps {
1314
/**
@@ -34,7 +35,18 @@ const SliderThumb: FC<SliderThumbProps> = ({ className, index = 0, onMove = NOOP
3435
const { max, min, ranged, value: valueOrValues, valueText: valueOrValuesText } = useSliderSelection();
3536
const value = ranged ? (valueOrValues as unknown as number[])[index] : (valueOrValues as number);
3637
const valueText = ranged ? (valueOrValuesText as unknown as string[])[index] : (valueOrValuesText as string);
37-
const { active, ariaLabel, ariaLabelledby, disabled, dragging, focused, shapeTestId, showValue } = useSliderUi();
38+
const {
39+
active,
40+
ariaLabel,
41+
ariaLabelledby,
42+
disabled,
43+
dragging,
44+
focused,
45+
shapeTestId,
46+
showValue,
47+
valueLabelPosition,
48+
valueLabelColor
49+
} = useSliderUi();
3850
const { setActive, setFocused, setDragging } = useSliderActions();
3951
const ref = useRef(null);
4052

@@ -108,7 +120,17 @@ const SliderThumb: FC<SliderThumbProps> = ({ className, index = 0, onMove = NOOP
108120
style={{ left: `${position}%` }}
109121
tabIndex={disabled ? -1 : 0}
110122
>
111-
{showValue && <label className={styles.label}>{valueText}</label>}
123+
{showValue && (
124+
<label
125+
className={cx(
126+
styles.label,
127+
getStyle(styles, camelCase("color-" + valueLabelColor)),
128+
getStyle(styles, camelCase("position-" + valueLabelPosition))
129+
)}
130+
>
131+
{valueText}
132+
</label>
133+
)}
112134
</div>
113135
</Tooltip>
114136
);

packages/core/src/components/Slider/SliderConstants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IconType } from "../Icon";
22
import { ReactElement } from "react";
3-
import { SliderSize, SliderColor as SliderColorType } from "./Slider.types";
3+
import { SliderSize, SliderColor as SliderColorType, SliderLabelPosition, SliderLabelColor } from "./Slider.types";
44

55
export const BEM_PREFIX = "monday";
66

@@ -47,6 +47,8 @@ export type SliderContextUI = {
4747
size: SliderSize;
4848
shapeTestId: (subElement: string) => string;
4949
showValue: boolean;
50+
valueLabelPosition: SliderLabelPosition;
51+
valueLabelColor: SliderLabelColor;
5052
};
5153

5254
export type SliderContextActions = {

packages/core/src/components/Slider/SliderContext.tsx

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export function SliderProvider({
4949
onChange,
5050
ranged,
5151
showValue,
52+
valueLabelPosition,
53+
valueLabelColor,
5254
size,
5355
step,
5456
value,
@@ -69,8 +71,34 @@ export function SliderProvider({
6971
const [dragging, setDragging, getDragging] = useDragging();
7072

7173
const uiContextValue: SliderContextUI = useMemo(
72-
() => ({ active, ariaLabel, ariaLabelledby, color, disabled, dragging, focused, size, shapeTestId, showValue }),
73-
[active, ariaLabel, ariaLabelledby, color, disabled, dragging, focused, size, shapeTestId, showValue]
74+
() => ({
75+
active,
76+
ariaLabel,
77+
ariaLabelledby,
78+
color,
79+
disabled,
80+
dragging,
81+
focused,
82+
size,
83+
shapeTestId,
84+
showValue,
85+
valueLabelPosition,
86+
valueLabelColor
87+
}),
88+
[
89+
active,
90+
ariaLabel,
91+
ariaLabelledby,
92+
color,
93+
disabled,
94+
dragging,
95+
focused,
96+
size,
97+
shapeTestId,
98+
showValue,
99+
valueLabelPosition,
100+
valueLabelColor
101+
]
74102
);
75103

76104
const selectionContextValue: SliderContextSelection = useMemo(

packages/core/src/components/Slider/__tests__/__snapshots__/Slider-non-ranged.test.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ Snapshot Diff:
223223
tabindex="0"
224224
>
225225
<label
226-
class="label"
226+
class="label colorPrimary positionTop"
227227
>
228228
- 20%
229229
+ 18%
@@ -261,7 +261,7 @@ Snapshot Diff:
261261
tabindex="0"
262262
>
263263
<label
264-
class="label"
264+
class="label colorPrimary positionTop"
265265
>
266266
- 20%
267267
+ 22%
@@ -299,7 +299,7 @@ Snapshot Diff:
299299
tabindex="0"
300300
>
301301
<label
302-
class="label"
302+
class="label colorPrimary positionTop"
303303
>
304304
- 20%
305305
+ 10%
@@ -337,7 +337,7 @@ Snapshot Diff:
337337
tabindex="0"
338338
>
339339
<label
340-
class="label"
340+
class="label colorPrimary positionTop"
341341
>
342342
- 20%
343343
+ 40%
@@ -375,7 +375,7 @@ Snapshot Diff:
375375
tabindex="0"
376376
>
377377
<label
378-
class="label"
378+
class="label colorPrimary positionTop"
379379
>
380380
- 20%
381381
+ 40%

packages/core/src/components/Slider/__tests__/__snapshots__/Slider-ranged.test.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ Snapshot Diff:
347347
tabindex="0"
348348
>
349349
<label
350-
class="label"
350+
class="label colorPrimary positionTop"
351351
>
352352
- 25%
353353
+ 23%
@@ -386,7 +386,7 @@ Snapshot Diff:
386386
tabindex="0"
387387
>
388388
<label
389-
class="label"
389+
class="label colorPrimary positionTop"
390390
>
391391
- 65%
392392
+ 67%
@@ -425,7 +425,7 @@ Snapshot Diff:
425425
tabindex="0"
426426
>
427427
<label
428-
class="label"
428+
class="label colorPrimary positionTop"
429429
>
430430
- 65%
431431
+ 55%
@@ -462,7 +462,7 @@ Snapshot Diff:
462462
tabindex="0"
463463
>
464464
<label
465-
class="label"
465+
class="label colorPrimary positionTop"
466466
>
467467
- 25%
468468
+ 45%
@@ -508,7 +508,7 @@ Snapshot Diff:
508508
tabindex="0"
509509
>
510510
<label
511-
class="label"
511+
class="label colorPrimary positionTop"
512512
>
513513
- 25%
514514
+ 45%

packages/core/src/components/Slider/__tests__/__snapshots__/Slider.snapshot.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ Snapshot Diff:
261261
- />
262262
+ >
263263
+ <label
264-
+ class="label"
264+
+ class="label colorPrimary positionTop"
265265
+ >
266266
+ 0%
267267
+ </label>

0 commit comments

Comments
 (0)