Skip to content

Commit c116f65

Browse files
committed
change to pointer up/down for label clicks
- add a check to see if user moved pointer - don't fire a handleMove() event on the original label if it wasn't a click - the handleMove() was setting the position back to the original label position on touchend, before resolves #90
1 parent f417992 commit c116f65

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/RangePips.svelte

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
export let percentOf = undefined;
3232
export let moveHandle = undefined;
3333
export let fixFloat = undefined;
34+
export let normalisedClient = undefined;
35+
36+
let clientStart;
3437
3538
$: pipStep = pipstep || ((max - min) / step >= ( vertical ? 50 : 100 ) ? (max - min) / ( vertical ? 10 : 20 ) : 1);
3639
@@ -54,9 +57,30 @@
5457
}
5558
};
5659
57-
function labelClick(val) {
60+
/**
61+
* function to run when the user clicks on a label
62+
* we store the original client position so we can check if the user has moved the mouse/finger
63+
* @param {event} e the event from browser
64+
**/
65+
function labelDown(e) {
66+
e = normalisedClient(e);
67+
clientStart = { x: e.clientX, y: e.clientY };
68+
}
69+
70+
/**
71+
* function to run when the user releases the mouse/finger
72+
* we check if the user has moved the mouse/finger, if not we "click" the label
73+
* and move the handle it to the label position
74+
* @param {number} val the value of the label
75+
* @param {event} e the event from browser
76+
*/
77+
function labelUp(val,e) {
78+
e = normalisedClient(e);
5879
if ( !disabled ) {
59-
moveHandle( undefined, val );
80+
const distanceMoved = Math.sqrt( Math.pow( clientStart.x - e.clientX, 2 ) + Math.pow( clientStart.y - e.clientY, 2 ) );
81+
if ( clientStart && ( distanceMoved <= 5 ) ) {
82+
moveHandle( undefined, val );
83+
}
6084
}
6185
}
6286
</script>
@@ -181,8 +205,8 @@
181205
class:selected={isSelected(min)}
182206
class:in-range={inRange(min)}
183207
style="{orientationStart}: 0%;"
184-
on:click={labelClick(min)}
185-
on:touchend|preventDefault={labelClick(min)}
208+
on:pointerdown={(e)=>{labelDown(e)}}
209+
on:pointerup={(e)=>{labelUp(pipVal(min),e)}}
186210
>
187211
{#if all === 'label' || first === 'label'}
188212
<span class="pipVal">
@@ -200,8 +224,8 @@
200224
class:selected={isSelected(pipVal(i))}
201225
class:in-range={inRange(pipVal(i))}
202226
style="{orientationStart}: {percentOf(pipVal(i))}%;"
203-
on:click={labelClick(pipVal(i))}
204-
on:touchend|preventDefault={labelClick(pipVal(i))}
227+
on:pointerdown={(e)=>{labelDown(e)}}
228+
on:pointerup={(e)=>{labelUp(pipVal(i),e)}}
205229
>
206230
{#if all === 'label' || rest === 'label'}
207231
<span class="pipVal">
@@ -219,8 +243,8 @@
219243
class:selected={isSelected(max)}
220244
class:in-range={inRange(max)}
221245
style="{orientationStart}: 100%;"
222-
on:click={labelClick(max)}
223-
on:touchend|preventDefault={labelClick(max)}
246+
on:pointerdown={(e)=>{labelDown(e)}}
247+
on:pointerup={(e)=>{labelUp(pipVal(max),e)}}
224248
>
225249
{#if all === 'label' || last === 'label'}
226250
<span class="pipVal">

src/RangeSlider.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
**/
181181
function normalisedClient(e) {
182182
if (e.type.includes("touch")) {
183-
return e.touches[0];
183+
return e.touches[0] || e.changedTouches[0];
184184
} else {
185185
return e;
186186
}
@@ -864,6 +864,7 @@
864864
{percentOf}
865865
{moveHandle}
866866
{fixFloat}
867+
{normalisedClient}
867868
/>
868869
{/if}
869870
</div>

0 commit comments

Comments
 (0)