Skip to content

Commit

Permalink
feat: reverse slider mode
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-hv committed Feb 14, 2024
1 parent 2c7a059 commit 9555ba5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/Slider/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SliderOptions
borderSelectorColor?: string
label?: string
hint?: string
reverse?: boolean
step?: number
type?: Type
values?: number[]
Expand All @@ -47,6 +48,7 @@ export const SliderComponent = forwardRef<'div', SliderProps>(
max,
min,
onChange,
reverse = false,
step = 1,
tooltip,
type,
Expand Down Expand Up @@ -114,15 +116,21 @@ export const SliderComponent = forwardRef<'div', SliderProps>(
// Updates the slider range when user drag the slider
useEffect(() => {
if (range.current) {
range.current.style.backgroundSize = `${getPercent(localValue)}% 100%`
if (!reverse) {
range.current.style.backgroundSize = `${getPercent(localValue)}% 100%`
} else {
const percentage = 100 - getPercent(localValue)
range.current.style.backgroundSize = `${percentage}%`
range.current.style.backgroundPosition = 'right'
}
}
if (tooltipRef.current) {
const fraction = getPercent(localValue) / 100
tooltipRef.current.style.left = `calc(${fraction * 100}% + ${
(0.5 - fraction) * thumbWidth
}px)`
}
}, [localValue, getPercent])
}, [localValue, getPercent, reverse])

// Give the possibility to the parent to modify the value from outside but we need to check if it respect the step
useEffect(() => {
Expand Down

0 comments on commit 9555ba5

Please sign in to comment.