Skip to content

Commit 71ffcd8

Browse files
author
Stephane Comeau
authored
fix: improve slider thumb animations (#2187)
* enable more granular slider anim control, improve dragging * tweak transitions
1 parent cdeed84 commit 71ffcd8

File tree

5 files changed

+66
-11
lines changed

5 files changed

+66
-11
lines changed

packages/fast-components-class-name-contracts-base/src/slider/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ export interface SliderClassNameContract {
6868
*/
6969
slider__disabled?: string;
7070

71+
/**
72+
* The dragging modifier
73+
*/
74+
slider__dragging?: string;
75+
76+
/**
77+
* The incrmenting modifier
78+
*/
79+
slider__incrementing?: string;
80+
7181
/**
7282
* rtl modifier
7383
*/

packages/fast-components-react-base/src/slider-track-item/slider-track-item.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Foundation, { HandledProps } from "@microsoft/fast-components-foundation-
33
import { classNames } from "@microsoft/fast-web-utilities";
44
import React from "react";
55
import { SliderContext, SliderContextType } from "../slider/slider-context";
6+
import { SliderThumb } from "../slider/slider";
67
import { SliderOrientation } from "../slider/slider.props";
78
import { DisplayNamePrefix } from "../utilities";
89
import {
@@ -58,7 +59,8 @@ class SliderTrackItem extends Foundation<
5859
sliderTrackItem_vertical,
5960
sliderTrackItem_horizontal,
6061
}: SliderTrackItemClassNameContract = this.props.managedClasses;
61-
const orientation: SliderOrientation = this.context.sliderOrientation;
62+
const orientation: SliderOrientation =
63+
this.context.sliderOrientation || SliderOrientation.horizontal;
6264

6365
return super.generateClassNames(
6466
classNames(
@@ -117,12 +119,18 @@ class SliderTrackItem extends Foundation<
117119
switch (anchor) {
118120
case SliderTrackItemAnchor.selectedRangeMax:
119121
return this.context.sliderValueAsPercent(
120-
this.context.sliderState.upperValue
122+
this.context.sliderState.isDragging &&
123+
this.context.sliderState.activeThumb === SliderThumb.upperThumb
124+
? this.context.sliderState.dragValue
125+
: this.context.sliderState.upperValue
121126
);
122127

123128
case SliderTrackItemAnchor.selectedRangeMin:
124129
return this.context.sliderValueAsPercent(
125-
this.context.sliderState.lowerValue
130+
this.context.sliderState.isDragging &&
131+
this.context.sliderState.activeThumb === SliderThumb.lowerThumb
132+
? this.context.sliderState.dragValue
133+
: this.context.sliderState.lowerValue
126134
);
127135

128136
case SliderTrackItemAnchor.totalRangeMax:

packages/fast-components-react-base/src/slider/slider.spec.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const managedClasses: SliderClassNameContract = {
3636
slider_thumb__horizontal: "slider_thumb__horizontal",
3737
slider_thumb__vertical: "slider_thumb__vertical",
3838
slider__disabled: "slider__disabled",
39+
slider__dragging: "slider__dragging",
40+
slider__incrementing: "slider__incrementing",
3941
slider__rtl: "slider__rtl",
4042
slider__modeSingle: "slider__modeSingle",
4143
slider__modeAdjustLower: "slider__modeAdjustLower",

packages/fast-components-react-base/src/slider/slider.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class Slider extends Foundation<SliderHandledProps, SliderUnhandledProps, Slider
6060
};
6161

6262
private static baseIncrementDelay: number = 300;
63-
private static minIncrementDelay: number = 40;
64-
private static incrementAcceleration: number = 40;
63+
private static minIncrementDelay: number = 100;
64+
private static incrementAcceleration: number = 50;
6565

6666
protected handledProps: HandledProps<SliderHandledProps> = {
6767
disabled: void 0,
@@ -278,6 +278,8 @@ class Slider extends Foundation<SliderHandledProps, SliderUnhandledProps, Slider
278278
const {
279279
slider,
280280
slider__disabled,
281+
slider__dragging,
282+
slider__incrementing,
281283
slider__vertical,
282284
slider__horizontal,
283285
slider__rtl,
@@ -292,6 +294,8 @@ class Slider extends Foundation<SliderHandledProps, SliderUnhandledProps, Slider
292294
classNames(
293295
slider,
294296
[slider__disabled, this.props.disabled],
297+
[slider__dragging, this.state.isDragging],
298+
[slider__incrementing, this.state.isIncrementing],
295299
[slider__vertical, isVertical],
296300
[slider__horizontal, !isVertical],
297301
[slider__rtl, this.direction === "rtl"],
@@ -857,6 +861,7 @@ class Slider extends Foundation<SliderHandledProps, SliderUnhandledProps, Slider
857861
isDragging: true,
858862
activeThumb: thumb,
859863
});
864+
this.updateDragValue(this.getDragValue(e.nativeEvent, thumb), thumb);
860865
};
861866

862867
/**
@@ -866,6 +871,16 @@ class Slider extends Foundation<SliderHandledProps, SliderUnhandledProps, Slider
866871
if (this.props.disabled || event.defaultPrevented) {
867872
return;
868873
}
874+
this.updateDragValue(
875+
this.getDragValue(event, this.state.activeThumb),
876+
this.state.activeThumb
877+
);
878+
};
879+
880+
/**
881+
* Get dragvalue from mouse event
882+
*/
883+
private getDragValue = (event: MouseEvent, thumb: SliderThumb): number => {
869884
this.updateDirection();
870885
this.updateSliderDimensions();
871886
const pixelCoordinate: number =
@@ -876,13 +891,13 @@ class Slider extends Foundation<SliderHandledProps, SliderUnhandledProps, Slider
876891
(this.props.range.maxValue - this.props.range.minValue) *
877892
this.convertPixelToPercent(pixelCoordinate) +
878893
this.props.range.minValue;
879-
this.updateDragValue(dragValue);
894+
return dragValue;
880895
};
881896

882897
/**
883898
* Updates the current drag value
884899
*/
885-
private updateDragValue = (dragValue: number): void => {
900+
private updateDragValue = (dragValue: number, thumb: SliderThumb): void => {
886901
const constrainedRange: SliderRange = this.getConstrainedRange(true);
887902

888903
const newDragValue: number = this.constrainToRange(dragValue, constrainedRange);
@@ -891,7 +906,7 @@ class Slider extends Foundation<SliderHandledProps, SliderUnhandledProps, Slider
891906
dragValue: newDragValue,
892907
});
893908

894-
if (this.state.activeThumb === SliderThumb.lowerThumb) {
909+
if (thumb === SliderThumb.lowerThumb) {
895910
this.updateValues(newDragValue, null);
896911
} else {
897912
this.updateValues(null, newDragValue);

packages/fast-components-styles-msft/src/slider/index.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const styles: ComponentStyles<SliderClassNameContract, DesignSystem> = {
5555
border: "none",
5656
background: neutralForegroundRest,
5757
"border-radius": "50%",
58-
transition: "all 0.1s ease",
58+
transition: "all 0.2s ease",
5959
...applyElevation(ElevationMultiplier.e4),
6060
...applyFocusVisible<DesignSystem>({
6161
"box-shadow": format(
@@ -95,7 +95,7 @@ const styles: ComponentStyles<SliderClassNameContract, DesignSystem> = {
9595
slider_foregroundTrack: {
9696
...applyCornerRadius(),
9797
background: neutralForegroundHint,
98-
transition: "all 0.1s ease",
98+
transition: "all 0.2s ease",
9999
[highContrastSelector]: {
100100
background: "Highlight",
101101
},
@@ -114,6 +114,22 @@ const styles: ComponentStyles<SliderClassNameContract, DesignSystem> = {
114114
},
115115
},
116116
},
117+
slider__dragging: {
118+
"& $slider_thumb": {
119+
transition: "none",
120+
},
121+
"& $slider_foregroundTrack": {
122+
transition: "none",
123+
},
124+
},
125+
slider__incrementing: {
126+
"& $slider_thumb": {
127+
transition: "all 0.10s linear",
128+
},
129+
"& $slider_foregroundTrack": {
130+
transition: "all 0.10s linear",
131+
},
132+
},
117133
slider__horizontal: {
118134
"&$slider": {
119135
width: "100%",
@@ -241,7 +257,11 @@ const styles: ComponentStyles<SliderClassNameContract, DesignSystem> = {
241257
},
242258
},
243259
},
244-
slider__modeSingle: {},
260+
slider__modeSingle: {
261+
"& $slider_foregroundTrack": {
262+
display: "none",
263+
},
264+
},
245265
slider__modeAdjustLower: {},
246266
slider__modeAdjustUpper: {},
247267
slider__modeAdjustBoth: {

0 commit comments

Comments
 (0)