Skip to content

Commit 82e9919

Browse files
committed
feat: set default value on dblclick
1 parent d698294 commit 82e9919

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

package.json

+1-1
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.1",
4+
"version": "0.2.2",
55
"repository": {
66
"url": "https://github.com/eye-wave/svelte-knobs"
77
},

src/lib/Knob.svelte

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
onChange?: (value: number | string) => void;
1313
param: FloatParam | EnumParam<readonly string[]>;
1414
value: number | string;
15+
defaultValue?: number | string;
1516
stiffness?: number;
1617
decimalDigits?: number;
1718
snapValues?: Array<number>;
@@ -32,6 +33,7 @@
3233
size = 80,
3334
onChange,
3435
value = $bindable(),
36+
defaultValue,
3537
param,
3638
stiffness = 0.5,
3739
decimalDigits = 0,
@@ -106,6 +108,17 @@
106108
isDragging = false;
107109
}
108110
111+
function handleDblClick() {
112+
const val =
113+
defaultValue ??
114+
(param as FloatParam)?.range.min ??
115+
(param as EnumParam<string[]>).variants?.[0];
116+
if (val === undefined) return;
117+
118+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
119+
setValue(Math.max(0, Math.min(1, normalize(val as any, param as any))));
120+
}
121+
109122
$effect(() => {
110123
// this was easier in svelte 4 :/
111124
window.addEventListener('touchmove', handleTouchMove, { passive: false });
@@ -226,6 +239,7 @@
226239
stroke-width={lineWidth}
227240
onmousedown={handleMouseDown}
228241
ontouchstart={handleTouchStart}
242+
ondblclick={handleDblClick}
229243
>
230244
<circle cx={center} cy={center} r={circleRadius} fill={bgColor}></circle>
231245
{#if snapValues.length > 0 || param.type === 'enum-param'}

src/routes/examples/LogarithmicKnob.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
</script>
1212

1313
<Knob param={freqParam} bind:value={freqValue} label="Frequency" unit="hz" />
14-
<Knob param={gainParam} bind:value={gainValue} label="Gain" unit="dB" decimalDigits={1} />
14+
<Knob param={gainParam} bind:value={gainValue} label="Gain" unit="dB" decimalDigits={1} defaultValue={0} />
1515
<Knob param={qParam} bind:value={qValue} label="Q" unit="dB" decimalDigits={2} />

0 commit comments

Comments
 (0)