Skip to content

Commit 6c13d5d

Browse files
committed
resolve issue with spring() and push() / pop()
resolves #22 After investigating, it seems that the original way I was handling the spring function being updated one the values[] array change was not accounting for new values in the array. so the spring function would only apply to the original values, and no new ones. Resolved this by explicitly re-setting the spring function values after detecting a change in the length of the array, but only applying a .set() function if the length has not changed.
1 parent 3088a73 commit 6c13d5d

File tree

3 files changed

+64
-28
lines changed

3 files changed

+64
-28
lines changed

src/RangeSlider.svelte

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
let slider;
4242
4343
// state management
44+
let valueLength = 0;
4445
let focus = false;
4546
let handleActivated = false;
4647
let handlePressed = false;
@@ -51,25 +52,35 @@
5152
5253
// copy the initial values in to a spring function which
5354
// will update every time the values array is modified
55+
5456
let springPositions;
5557
5658
$: {
59+
5760
// check that "values" is an array, or set it as array
5861
// to prevent any errors in springs, or range trimming
5962
if ( !Array.isArray( values ) ) {
6063
values = [(max + min) / 2];
6164
console.error( "'values' prop should be an Array (https://github.com/simeydotme/svelte-range-slider-pips#slider-props)" );
6265
}
63-
// trim the range as needed
64-
values = trimRange(values);
65-
// clamp the values to the steps and boundaries set up in the slider
66-
values = values.map((v) => alignValueToStep(v));
67-
// update the spring function so that movement can happen in the UI
68-
if ( springPositions ) {
69-
springPositions.set(values.map((v) => percentOf(v)));
66+
// trim the range so it remains as a min/max (only 2 handles)
67+
// and also align the handles to the steps
68+
values = trimRange(values.map((v) => alignValueToStep(v)));
69+
70+
// check if the valueLength (length of values[]) has changed,
71+
// because if so we need to re-seed the spring function with the
72+
// new values array.
73+
if ( valueLength !== values.length ) {
74+
// set the initial spring values when the slider initialises,
75+
// or when values array length has changed
76+
springPositions = spring(values.map((v) => percentOf(v)), springValues );
7077
} else {
71-
springPositions = spring( values.map((v) => percentOf(v)), springValues );
78+
// update the value of the spring function for animated handles
79+
// whenever the values has updated
80+
springPositions.set(values.map((v) => percentOf(v)));
7281
}
82+
// set the valueLength for the next check
83+
valueLength = values.length;
7384
};
7485
7586
/**
@@ -795,7 +806,6 @@
795806
{suffix}
796807
{formatter}
797808
{focus}
798-
{disabled}
799809
{percentOf} />
800810
{/if}
801811
</div>

test/public/testing.css

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ input {
1818
width: 100px;
1919
}
2020

21+
hr {
22+
height: 1px;
23+
border: none;
24+
border-bottom: 1px dashed rgb(125, 129, 138);
25+
margin: 50px 0;
26+
}
27+
2128
.rangeSlider.rangeSlider {
2229
margin: 60px;
2330
}
2431

2532
h2,h3,h4 {
26-
margin: 20px 0 10px;
33+
margin: 30px 0 10px;
2734
}
2835

2936
h2 + .rangeSlider.rangeSlider, h3 + .rangeSlider.rangeSlider, h4 + .rangeSlider.rangeSlider {

test/src/App.svelte

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@
2828
let zeromax = 0;
2929
3030
let disabled = false;
31+
32+
let arrayPush = [11,99];
33+
let pushValues = () => {
34+
values.push(Math.random()*100);
35+
values = values
36+
}
37+
let popValues = () => {
38+
values.pop();
39+
values = values
40+
}
3141
3242
</script>
3343

@@ -63,31 +73,22 @@
6373

6474
<div class="content" style="--range-handle-focus: {color}; --range-handle: {lightColor}">
6575

66-
<RangeSlider vertical range values={[10,30]} pips all="label" {disabled} />
76+
<RangeSlider vertical range values={[10,30]} pips all="label" />
6777
<RangeSlider vertical range="min" values={[10]} pips all />
6878
<RangeSlider vertical range="max" values={[30]} pips />
6979

7080
<br>
7181
<h2>Spring & Colors Test</h2>
72-
<RangeSlider id="test-id" springValues={{ stiffness: 0.03, damping: 0.08 }} values={[30,50,70]} />
82+
<RangeSlider id="test-id" springValues={{ stiffness: 0.03, damping: 0.08 }} bind:values />
7383
<br>
74-
75-
<RangeSlider bind:values {disabled}
76-
on:start={(e) => { console.log("start",e.detail)}}
77-
on:stop={(e) => { console.log("end",e.detail)}}
78-
on:change={(e) => { console.log("change",e.detail)}}
79-
/>
80-
<hr>
81-
{values}
82-
<br>
83-
<input type="number" bind:value={values[0]} />
84-
<input type="number" bind:value={values[1]} />
85-
<input type="number" bind:value={values[2]} />
86-
<input type="number" bind:value={values[3]} />
87-
<hr>
84+
85+
8886
<RangeSlider float />
8987
<RangeSlider float pips all="label" />
90-
<RangeSlider float pips first="label" last="label" {disabled} />
88+
<RangeSlider float pips first="label" last="label" {disabled} />
89+
<h2>trim/align</h2>
90+
<RangeSlider values="{[-10,12,103]}" float pips step={5} all="label" {disabled} />
91+
<h2>events</h2>
9192
<RangeSlider float pips first="label" last="label" rest="label"
9293
on:start={(e) => { console.log("start",e.detail)}}
9394
on:stop={(e) => { console.log("stop",e.detail)}}
@@ -140,9 +141,27 @@
140141
<RangeSlider bind:values={zero} min={zeromin} max={zeromax} range float pips all="label" step={1} pipstep={5} />
141142
<br><button on:click={()=>{ zeromin = 10; zeromax = 30; zero = [3,70]; }}>increase min/max</button> - {zero}
142143

143-
<RangeSlider bind:values float pips all="label" {disabled} />
144+
<h2>disable / enable</h2>
145+
<RangeSlider bind:values float pips all="label" {disabled} />
144146
<button on:click={()=>{disabled=!disabled}}>toggle disabled</button>
145147

148+
<h2>push & pop values</h2>
149+
<RangeSlider bind:values float pips all="label" />
150+
<button on:click={pushValues}>push</button>
151+
<button on:click={popValues}>pop</button>
152+
<br>({values})
153+
154+
<h2>Binding to inputs</h2>
155+
<RangeSlider bind:values {disabled}
156+
on:start={(e) => { console.log("start",e.detail)}}
157+
on:stop={(e) => { console.log("end",e.detail)}}
158+
on:change={(e) => { console.log("change",e.detail)}}
159+
/>
160+
{#each values as v}
161+
<input type="number" bind:value={v} {disabled} />
162+
{/each}
163+
<hr>
164+
146165
</div>
147166

148167

0 commit comments

Comments
 (0)