Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Slider): be able to change value label position and color #2734

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/core/src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SliderColor as SliderColorEnum } from "./SliderConstants";
import cx from "classnames";
import { withStaticProps } from "../../types";
import styles from "./Slider.module.scss";
import { SliderColor, SliderSize } from "./Slider.types";
import { SliderColor, SliderLabelColor, SliderLabelPosition, SliderSize } from "./Slider.types";

export type SliderProps = {
/**
Expand Down Expand Up @@ -76,6 +76,14 @@ export type SliderProps = {
* Always show `value` instead of Tooltip
*/
showValue?: boolean;
/**
* Position of the `value` when `showValue` is true
*/
valueLabelPosition?: SliderLabelPosition;
/**
* Color of the `value` when `showValue` is true
*/
valueLabelColor?: SliderLabelColor;
/**
* Size small/medium/large of the component (Slider)
*/
Expand Down Expand Up @@ -136,6 +144,8 @@ const Slider: React.FC<SliderProps> & {
ranged = false,
step = 1,
showValue = false,
valueLabelPosition = "top",
valueLabelColor = "primary",
size = "small",
value,
defaultValue = 0,
Expand Down Expand Up @@ -167,6 +177,8 @@ const Slider: React.FC<SliderProps> & {
onChange={onChange}
ranged={ranged}
showValue={showValue}
valueLabelPosition={valueLabelPosition}
valueLabelColor={valueLabelColor}
size={size}
step={step}
value={value}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/components/Slider/Slider.types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { BASE_SIZES } from "../../constants";
import { DialogPosition } from "../Dialog";
import { TypographyColor } from "../Typography";

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

export type SliderLabelColor = Extract<TypographyColor, "primary" | "secondary">;

export type SliderLabelPosition = Extract<DialogPosition, "top" | "bottom">;

export type SliderSize = (typeof BASE_SIZES)[keyof typeof BASE_SIZES];

export type InfixKind = "prefix" | "postfix";
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

.dragging {
cursor: grabbing !important;
transform: translate(-50%, -50%) scale(1.33, 1.33);
transform: translate(-50%, -50%) scale(1.15, 1.15);
transform-origin: center center;
transition: transform var(--motion-productive-long) var(--motion-timing-enter);
box-shadow: var(--box-shadow-small);
Expand Down Expand Up @@ -62,8 +62,8 @@
}

.medium {
height: 24px;
width: 24px;
height: 20px;
width: 20px;
}

.large {
Expand All @@ -72,12 +72,27 @@
}

.label {
bottom: calc(100% + 8px);
left: 50%;
max-width: 50px;
overflow: hidden;
position: absolute;
text-overflow: ellipsis;
transform: translate(-50%, 0);
white-space: nowrap;

&.positionTop {
bottom: calc(100% + 8px);
}

&.positionBottom {
top: calc(100% + 8px);
}

&.colorPrimary {
color: var(--primary-text-color);
}

&.colorSecondary {
color: var(--secondary-text-color);
}
}
26 changes: 24 additions & 2 deletions packages/core/src/components/Slider/SliderBase/SliderThumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cx from "classnames";
import styles from "./SliderThumb.module.scss";
import { getStyle } from "../../../helpers/typesciptCssModulesHelper";
import { SliderColor, SliderSize } from "../Slider.types";
import { camelCase } from "lodash-es";

export interface SliderThumbProps extends VibeComponentProps {
/**
Expand All @@ -34,7 +35,18 @@ const SliderThumb: FC<SliderThumbProps> = ({ className, index = 0, onMove = NOOP
const { max, min, ranged, value: valueOrValues, valueText: valueOrValuesText } = useSliderSelection();
const value = ranged ? (valueOrValues as unknown as number[])[index] : (valueOrValues as number);
const valueText = ranged ? (valueOrValuesText as unknown as string[])[index] : (valueOrValuesText as string);
const { active, ariaLabel, ariaLabelledby, disabled, dragging, focused, shapeTestId, showValue } = useSliderUi();
const {
active,
ariaLabel,
ariaLabelledby,
disabled,
dragging,
focused,
shapeTestId,
showValue,
valueLabelPosition,
valueLabelColor
} = useSliderUi();
const { setActive, setFocused, setDragging } = useSliderActions();
const ref = useRef(null);

Expand Down Expand Up @@ -108,7 +120,17 @@ const SliderThumb: FC<SliderThumbProps> = ({ className, index = 0, onMove = NOOP
style={{ left: `${position}%` }}
tabIndex={disabled ? -1 : 0}
>
{showValue && <label className={styles.label}>{valueText}</label>}
{showValue && (
<label
className={cx(
styles.label,
getStyle(styles, camelCase("color-" + valueLabelColor)),
getStyle(styles, camelCase("position-" + valueLabelPosition))
)}
>
{valueText}
</label>
)}
</div>
</Tooltip>
);
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/components/Slider/SliderConstants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IconType } from "../Icon";
import { ReactElement } from "react";
import { SliderSize, SliderColor as SliderColorType } from "./Slider.types";
import { SliderSize, SliderColor as SliderColorType, SliderLabelPosition, SliderLabelColor } from "./Slider.types";

export const BEM_PREFIX = "monday";

Expand Down Expand Up @@ -47,6 +47,8 @@ export type SliderContextUI = {
size: SliderSize;
shapeTestId: (subElement: string) => string;
showValue: boolean;
valueLabelPosition: SliderLabelPosition;
valueLabelColor: SliderLabelColor;
};

export type SliderContextActions = {
Expand Down
32 changes: 30 additions & 2 deletions packages/core/src/components/Slider/SliderContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export function SliderProvider({
onChange,
ranged,
showValue,
valueLabelPosition,
valueLabelColor,
size,
step,
value,
Expand All @@ -69,8 +71,34 @@ export function SliderProvider({
const [dragging, setDragging, getDragging] = useDragging();

const uiContextValue: SliderContextUI = useMemo(
() => ({ active, ariaLabel, ariaLabelledby, color, disabled, dragging, focused, size, shapeTestId, showValue }),
[active, ariaLabel, ariaLabelledby, color, disabled, dragging, focused, size, shapeTestId, showValue]
() => ({
active,
ariaLabel,
ariaLabelledby,
color,
disabled,
dragging,
focused,
size,
shapeTestId,
showValue,
valueLabelPosition,
valueLabelColor
}),
[
active,
ariaLabel,
ariaLabelledby,
color,
disabled,
dragging,
focused,
size,
shapeTestId,
showValue,
valueLabelPosition,
valueLabelColor
]
);

const selectionContextValue: SliderContextSelection = useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 20%
+ 18%
Expand Down Expand Up @@ -261,7 +261,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 20%
+ 22%
Expand Down Expand Up @@ -299,7 +299,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 20%
+ 10%
Expand Down Expand Up @@ -337,7 +337,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 20%
+ 40%
Expand Down Expand Up @@ -375,7 +375,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 20%
+ 40%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 25%
+ 23%
Expand Down Expand Up @@ -386,7 +386,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 65%
+ 67%
Expand Down Expand Up @@ -425,7 +425,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 65%
+ 55%
Expand Down Expand Up @@ -462,7 +462,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 25%
+ 45%
Expand Down Expand Up @@ -508,7 +508,7 @@ Snapshot Diff:
tabindex="0"
>
<label
class="label"
class="label colorPrimary positionTop"
>
- 25%
+ 45%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Snapshot Diff:
- />
+ >
+ <label
+ class="label"
+ class="label colorPrimary positionTop"
+ >
+ 0%
+ </label>
Expand Down
Loading