Skip to content

Commit 4c05079

Browse files
committed
💄 Improve RangeSlider visuals
1 parent 1a65cd3 commit 4c05079

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

src/components/RangeSlider/RangeSlider.astro

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@ const labelStyle = updateLabels ? `min-width:${minLabelWidth};` : null
138138
}
139139

140140
const updateRange = ({ range, minValue, maxValue, min, max }: RangeParams) => {
141-
range.style.left = `${interpolate(minValue, [min, max], [0, 100])}%`
142-
range.style.right = `${interpolate(maxValue, [min, max], [100, 0])}%`
141+
const minAdjust = minValue > (max / 2) ? -1 : 1
142+
const maxAdjust = maxValue < (max / 2) ? 1 : -1
143+
144+
range.style.left = `${interpolate(minValue + minAdjust, [min, max], [0, 100])}%`
145+
range.style.right = `${interpolate(maxValue + maxAdjust, [min, max], [100, 0])}%`
143146
}
144147

145148
const updateLabels = (wrapper: HTMLDivElement, minValue: number, maxValue: number) => {
@@ -172,22 +175,18 @@ const labelStyle = updateLabels ? `min-width:${minLabelWidth};` : null
172175
const value = Number(target.value)
173176
const min = Number(target.min)
174177
const max = Number(target.max)
178+
const step = Number(target.step)
175179
const gap = Number(wrapper.dataset.gap)
176180
const shouldUpdateLabels = !!wrapper.dataset.updateLabels
177181
const prevInputValue = prevInput instanceof HTMLInputElement ? prevInput.value : 0
178182
const nextInputValue = nextInput instanceof HTMLInputElement ? nextInput.value : 0
179183
const minValue = target.dataset.min ? value : Number(prevInputValue)
180184
const maxValue = target.dataset.max ? value : Number(nextInputValue)
181185

182-
if (maxValue - minValue >= gap) {
183-
updateRange({
184-
range,
185-
minValue,
186-
maxValue,
187-
min,
188-
max
189-
})
186+
let currentMin = minValue
187+
let currentMax = maxValue
190188

189+
if (maxValue - minValue >= gap) {
191190
if (shouldUpdateLabels) {
192191
updateLabels(wrapper, minValue, maxValue)
193192
}
@@ -197,10 +196,20 @@ const labelStyle = updateLabels ? `min-width:${minLabelWidth};` : null
197196
max: maxValue
198197
})
199198
} else if (target.dataset.min) {
200-
target.value = String(maxValue - gap)
199+
currentMin = maxValue - Math.max(step, gap)
200+
target.value = String(currentMin)
201201
} else {
202-
target.value = String(minValue + gap)
202+
currentMax = minValue + Math.max(step, gap)
203+
target.value = String(currentMax)
203204
}
205+
206+
updateRange({
207+
range,
208+
minValue: currentMin,
209+
maxValue: currentMax,
210+
min,
211+
max
212+
})
204213
}, true)
205214

206215
on('[data-id="w-range-slider"] button', 'click', (event: Event) => {

src/components/RangeSlider/RangeSlider.svelte

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
let dynamicMinLabel = $state(minLabel)
4646
let dynamicMaxLabel = $state(maxLabel)
4747
48-
const rangeLeftPercent = $derived(interpolate(minValue || min, [min, max], [0, 100]))
49-
const rangeRightPercent = $derived(interpolate(maxValue || max, [min, max], [100, 0]))
48+
const minAdjust = $derived(minValue > (max / 2) ? -1 : 1)
49+
const maxAdjust = $derived(maxValue < (max / 2) ? 1 : -1)
50+
const rangeLeftPercent = $derived(interpolate((minValue || min) + minAdjust, [min, max], [0, 100]))
51+
const rangeRightPercent = $derived(interpolate((maxValue || max) + maxAdjust, [min, max], [100, 0]))
5052
5153
const updateDynamicLabels = (minValue: number, maxValue: number) => {
5254
if (dynamicMinLabel && dynamicMaxLabel) {
@@ -72,9 +74,11 @@
7274
max: maxValue
7375
})
7476
} else if (target.dataset.min) {
75-
target.value = String(maxValue - minGap)
77+
minValue = maxValue - Math.max(step, minGap)
78+
target.value = String(minValue)
7679
} else {
77-
target.value = String(minValue + minGap)
80+
maxValue = minValue + Math.max(step, minGap)
81+
target.value = String(maxValue)
7882
}
7983
}
8084

src/components/RangeSlider/RangeSlider.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,28 @@ const RangeSlider = ({
6363

6464
const value = Number(target.value)
6565

66-
if (target.dataset.min) {
67-
if (value <= maxValue - minGap) {
68-
setMinValue(value)
69-
}
70-
} else if (value >= minValue + minGap) {
71-
setMaxValue(value)
72-
}
66+
let currentMin = target.dataset.min ? value : minValue
67+
let currentMax = target.dataset.max ? value : maxValue
7368

74-
const newMin = target.dataset.min ? value : minValue
75-
const newMax = target.dataset.max ? value : maxValue
69+
if (currentMax - currentMin >= minGap) {
70+
if (updateLabels) {
71+
updateDynamicLabels(currentMin, currentMax)
72+
}
7673

77-
if (updateLabels) {
78-
updateDynamicLabels(newMin, newMax)
74+
onChange?.({
75+
min: currentMin,
76+
max: currentMax
77+
})
78+
} else if (target.dataset.min) {
79+
currentMin = currentMax - Math.max(step, minGap)
80+
target.value = String(currentMin)
81+
} else {
82+
currentMax = currentMin + Math.max(step, minGap)
83+
target.value = String(currentMax)
7984
}
8085

81-
onChange?.({
82-
min: newMin,
83-
max: newMax
84-
})
86+
setMinValue(currentMin)
87+
setMaxValue(currentMax)
8588
}
8689

8790
const handleClick = (event: React.MouseEvent, direction: 'left' | 'right') => {
@@ -113,8 +116,11 @@ const RangeSlider = ({
113116
}
114117

115118
useEffect(() => {
116-
rangeLeftPercent.current = interpolate(minValue || min, [min, max], [0, 100])
117-
rangeRightPercent.current = interpolate(maxValue || max, [min, max], [100, 0])
119+
const minAdjust = minValue > (max / 2) ? -1 : 1
120+
const maxAdjust = maxValue < (max / 2) ? 1 : -1
121+
122+
rangeLeftPercent.current = interpolate((minValue || min) + minAdjust, [min, max], [0, 100])
123+
rangeRightPercent.current = interpolate((maxValue || max) + maxAdjust, [min, max], [100, 0])
118124
}, [minValue, maxValue])
119125

120126
return (

src/components/RangeSlider/rangeslider.module.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ body {
3434
@include border-radius(xl);
3535
@include size(h10px);
3636
@include background(var(--w-range-slider-background));
37+
@include visibility(hidden);
3738

3839
flex: 1;
3940
}
@@ -42,7 +43,6 @@ body {
4243
@include position(absolute);
4344
@include background(var(--w-range-slider-color));
4445
@include size('h100%');
45-
@include border-radius(xl);
4646

4747
&[data-disabled] {
4848
@include background(primary-30);

0 commit comments

Comments
 (0)