Skip to content

Commit 2f5e62c

Browse files
committed
add percent to the formatter functions
this allows some ability to add ramping (easing) to the slider if provided as a easing function in the formatter (see test folder changes)
1 parent f59893f commit 2f5e62c

File tree

8 files changed

+653
-425
lines changed

8 files changed

+653
-425
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ prop | type | default | description
113113
**hoverable** | `Boolean` | `true` | Whether hover styles are enabled for both handles and pips/values
114114
**disabled** | `Boolean` | `false` | Determine if the slider is disabled, or enabled _(only disables interactions, and events)_
115115
**id** | `String` | `""` | Give the slider a unique ID for use in styling
116-
**formatter** | `Function` | `(v,i) => v` | A function to re-format values before they are displayed (`v = value, i = pip index`)
117-
**handleFormatter** | `Function` | `formatter` | A function to re-format values on the handle/float before they are displayed. Defaults to the same function given to the `formatter` property (`v = value, i = handle index`)
116+
**formatter** | `Function` | `(v,i,p) => v` | A function to re-format values before they are displayed (`v = value, i = pip index, p = percent`)
117+
**handleFormatter** | `Function` | `formatter` | A function to re-format values on the handle/float before they are displayed. Defaults to the same function given to the `formatter` property (`v = value, i = handle index, p = percent`)
118118
**springValues** | `Object` | `{ stiffness: 0.15, damping: 0.4 }` | Svelte spring physics object to change the behaviour of the handle when moving
119119

120120
### slider events (dispatched)

dist/svelte-range-slider-pips.js

Lines changed: 301 additions & 201 deletions
Large diffs are not rendered by default.

dist/svelte-range-slider-pips.mjs

Lines changed: 301 additions & 201 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "svelte-range-slider-pips",
3-
"version": "1.8.1",
3+
"version": "2.0.0",
44
"svelte": "src/index.js",
55
"module": "dist/svelte-range-slider-pips.mjs",
66
"main": "dist/svelte-range-slider-pips.js",

src/RangePips.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
>
179179
{#if all === 'label' || first === 'label'}
180180
<span class="pipVal">
181-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(min,0)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
181+
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(min,0,0)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
182182
</span>
183183
{/if}
184184
</span>
@@ -197,7 +197,7 @@
197197
>
198198
{#if all === 'label' || rest === 'label'}
199199
<span class="pipVal">
200-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(pipVal(i),i)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
200+
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(pipVal(i),i,percentOf(pipVal(i)))}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
201201
</span>
202202
{/if}
203203
</span>
@@ -216,7 +216,7 @@
216216
>
217217
{#if all === 'label' || last === 'label'}
218218
<span class="pipVal">
219-
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(max,pipCount)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
219+
{#if prefix}<span class="pipVal-prefix">{prefix}</span>{/if}{formatter(max,pipCount,100)}{#if suffix}<span class="pipVal-suffix">{suffix}</span>{/if}
220220
</span>
221221
{/if}
222222
</span>

src/RangeSlider.svelte

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
export let id = undefined;
2828
export let prefix = "";
2929
export let suffix = "";
30-
export let formatter = (v,i) => v;
30+
export let formatter = (v,i,p) => v;
3131
export let handleFormatter = formatter;
3232
3333
// stylistic props
@@ -266,20 +266,20 @@
266266
// of the slider, as it may have changed size
267267
const dims = getSliderDimensions();
268268
// calculate the interaction position, percent and value
269-
let hPos = 0;
270-
let hPercent = 0;
271-
let hVal = 0;
269+
let handlePos = 0;
270+
let handlePercent = 0;
271+
let handleVal = 0;
272272
if (vertical) {
273-
hPos = clientPos.clientY - dims.top;
274-
hPercent = (hPos / dims.height) * 100;
275-
hVal = ((max - min) / 100) * hPercent + min;
273+
handlePos = clientPos.clientY - dims.top;
274+
handlePercent = (handlePos / dims.height) * 100;
275+
handleVal = ((max - min) / 100) * handlePercent + min;
276276
} else {
277-
hPos = clientPos.clientX - dims.left;
278-
hPercent = (hPos / dims.width) * 100;
279-
hVal = ((max - min) / 100) * hPercent + min;
277+
handlePos = clientPos.clientX - dims.left;
278+
handlePercent = (handlePos / dims.width) * 100;
279+
handleVal = ((max - min) / 100) * handlePercent + min;
280280
}
281281
// move handle to the value
282-
moveHandle(activeHandle, hVal);
282+
moveHandle(activeHandle, handleVal);
283283
}
284284
285285
/**
@@ -777,7 +777,7 @@
777777
aria-valuemin={range === true && index === 1 ? values[0] : min}
778778
aria-valuemax={range === true && index === 0 ? values[1] : max}
779779
aria-valuenow={value}
780-
aria-valuetext="{prefix}{handleFormatter(value,index)}{suffix}"
780+
aria-valuetext="{prefix}{handleFormatter(value,index,percentOf(value))}{suffix}"
781781
aria-orientation={vertical ? 'vertical' : 'horizontal'}
782782
aria-disabled={disabled}
783783
{disabled}
@@ -786,7 +786,7 @@
786786
<span class="rangeNub" />
787787
{#if float}
788788
<span class="rangeFloat">
789-
{#if prefix}<span class="rangeFloat-prefix">{prefix}</span>{/if}{handleFormatter(value,index)}{#if suffix}<span class="rangeFloat-suffix">{suffix}</span>{/if}
789+
{#if prefix}<span class="rangeFloat-prefix">{prefix}</span>{/if}{handleFormatter(value,index,percentOf(value))}{#if suffix}<span class="rangeFloat-suffix">{suffix}</span>{/if}
790790
</span>
791791
{/if}
792792
</span>
@@ -817,7 +817,7 @@
817817
{formatter}
818818
{focus}
819819
{percentOf}
820-
{moveHandle}
820+
{moveHandle}
821821
/>
822822
{/if}
823823
</div>

test/public/global.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ button:not(:disabled):active {
6464
button:focus {
6565
border-color: #666;
6666
}
67+
68+
table {
69+
margin: 10px;
70+
width: 100%;
71+
}
72+
73+
td, th {
74+
padding: 5px;
75+
}

test/src/App.svelte

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
let perc2 = [100 - perc1];
2020
$: perc2max = 100 - perc1[0];
2121
22+
let big = [500,1000];
23+
let hramp = ( v,i,p ) => { return ((v) * (((p/100)*(p/100)) )).toFixed(0) + " (" + p + "%)"; };
24+
let ramp = ( v,i,p ) => { return ((v) * (((p/100)*(p/100)) )).toFixed(0); };
25+
2226
let hue = [244];
2327
$: lightColor = `hsl(${Math.round(hue[0]) - 10}, 65%, 70%)`;
2428
$: color = `hsl(${Math.round(hue[0])}, 63%, 54%)`;
@@ -140,10 +144,25 @@
140144

141145
<RangeSlider bind:values={perc1} min={0} max={50} pips all="label" float />
142146
<RangeSlider bind:values={perc2} min={0} max={perc2max} pips all="label" float />
143-
<hr>
144-
percent1: {perc1}<br>percent2: {perc2}
147+
<hr> {perc1} / {perc2}
145148

146-
<br><br>
149+
<br>
150+
151+
<h2>BIG, ramped value</h2>
152+
<RangeSlider bind:values={big} min={0} max={10000} pips first="label" last="label" float formatter={ramp} handleFormatter={hramp} />
153+
<table>
154+
<tr>
155+
<th>real value</th><th>formatted (ramp(v,i,p))</th>
156+
</tr>
157+
<tr>
158+
<td>{big}</td><td>{[ ramp(big[0],0,parseFloat((big[0] / 10000 * 100).toFixed(1))), ramp(big[1],0,parseFloat((big[1] / 10000 * 100).toFixed(1))) ]}</td>
159+
</tr>
160+
<tr>
161+
<td colspan=2>{[ parseInt((big[0] / 10000 * 100).toFixed(1)) + "%", parseInt((big[1] / 10000 * 100).toFixed(1)) + "%" ]}</td>
162+
</tr>
163+
</table>
164+
165+
<br>
147166

148167
<RangeSlider bind:values={zero} min={zeromin} max={zeromax} range float pips all="label" step={1} pipstep={5} />
149168
<br><button on:click={()=>{ zeromin = 10; zeromax = 30; zero = [3,70]; }}>increase min/max</button> - {zero}

0 commit comments

Comments
 (0)