Skip to content

Commit

Permalink
feat(Slider): make thumb position clearer when the value is set near …
Browse files Browse the repository at this point in the history
…the edge
  • Loading branch information
Thulof committed Jul 18, 2024
1 parent b147de9 commit 4640038
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "feat(Slider): make thumb position clearer when the value is set near edge",
"packageName": "@fluentui/react-slider",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts
import { sliderCSSVars } from './useSliderStyles.styles';
import type { SliderState, SliderProps } from './Slider.types';

const { sliderStepsPercentVar, sliderProgressVar, sliderDirectionVar } = sliderCSSVars;
const { sliderStepsPercentVar, sliderProgressVar, sliderDirectionVar, sliderThumbOffsetRatio } = sliderCSSVars;

const getPercent = (value: number, min: number, max: number) => {
return max === min ? 0 : ((value - min) / (max - min)) * 100;
Expand All @@ -22,6 +22,7 @@ export const useSliderState_unstable = (state: SliderState, props: SliderProps)
});
const clampedValue = clamp(currentValue, min, max);
const valuePercent = getPercent(clampedValue, min, max);
const thumbOffsetRatio = 0.5 - (valuePercent / 100);

const inputOnChange = state.input.onChange;
const propsOnChange = props.onChange;
Expand All @@ -41,6 +42,7 @@ export const useSliderState_unstable = (state: SliderState, props: SliderProps)
[sliderDirectionVar]: state.vertical ? '0deg' : dir === 'ltr' ? '90deg' : '270deg',
[sliderStepsPercentVar]: step && step > 0 ? `${(step * 100) / (max - min)}%` : '',
[sliderProgressVar]: `${valuePercent}%`,
[sliderThumbOffsetRatio]: thumbOffsetRatio,
};

// Root props
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export const sliderCSSVars = {
sliderDirectionVar: `--fui-Slider--direction`,
sliderProgressVar: `--fui-Slider--progress`,
sliderStepsPercentVar: `--fui-Slider--steps-percent`,
sliderThumbOffsetRatio: `--fui-Slider__thumb--offset-ratio`,
};

const { sliderDirectionVar, sliderStepsPercentVar, sliderProgressVar } = sliderCSSVars;
const { sliderDirectionVar, sliderStepsPercentVar, sliderProgressVar, sliderThumbOffsetRatio } = sliderCSSVars;

/**
* Styles for the root slot
Expand Down Expand Up @@ -214,11 +215,11 @@ const useThumbStyles = makeStyles({
},
horizontal: {
transform: 'translateX(-50%)',
left: `var(${sliderProgressVar})`,
left: `calc(var(${sliderProgressVar}) + var(${sliderThumbOffsetRatio}) * var(${thumbSizeVar}))`,
},
vertical: {
transform: 'translateY(50%)',
bottom: `var(${sliderProgressVar})`,
bottom: `calc(var(${sliderProgressVar}) + var(${sliderThumbOffsetRatio}) * var(${thumbSizeVar}))`,
},
});

Expand Down

0 comments on commit 4640038

Please sign in to comment.