Skip to content

Commit 100a569

Browse files
committed
feat: add mobile suport
1 parent 976e990 commit 100a569

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "svelte-knobs",
33
"description": "Svelte component library for building customizable knob controls.",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"repository": {
66
"url": "https://github.com/eye-wave/svelte-knobs"
77
},

src/lib/Knob.svelte

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,34 @@
106106
isDragging = false;
107107
}
108108
109+
$effect(() => {
110+
// this was easier in svelte 4 :/
111+
window.addEventListener('touchmove', handleTouchMove, { passive: false });
112+
return () => window.removeEventListener('touchmove', handleTouchMove);
113+
});
114+
115+
function handleTouchStart(event: TouchEvent) {
116+
isDragging = true;
117+
const touch = event.touches?.[0];
118+
if (touch === undefined) return;
119+
startY = touch.clientY;
120+
startValue = normalizedValue;
121+
}
122+
123+
function handleTouchMove(event: TouchEvent) {
124+
if (!isDragging) return;
125+
126+
event.preventDefault();
127+
if (disabled) return;
128+
129+
const touch = event.touches?.[0];
130+
if (touch === undefined) return;
131+
132+
const deltaY = startY - touch.clientY;
133+
const deltaValue = deltaY / 200;
134+
setValue(Math.max(0, Math.min(1, startValue + deltaValue)));
135+
}
136+
109137
function setValue(newNormalizedValue: number) {
110138
if (param.type === 'enum-param') {
111139
const newValue = unnormalizeToString(newNormalizedValue, param);
@@ -182,7 +210,7 @@
182210
}
183211
</script>
184212

185-
<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} />
213+
<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} ontouchend={handleMouseUp} />
186214

187215
<div class="container" {style}>
188216
<svg
@@ -197,6 +225,7 @@
197225
stroke-linejoin="round"
198226
stroke-width={lineWidth}
199227
onmousedown={handleMouseDown}
228+
ontouchstart={handleTouchStart}
200229
>
201230
<circle cx={center} cy={center} r={circleRadius} fill={bgColor}></circle>
202231
{#if snapValues.length > 0 || param.type === 'enum-param'}

0 commit comments

Comments
 (0)