Skip to content

Commit 8f1913e

Browse files
committed
fix(potentiometer): value property not updated
After this fix, the `value` property will always reflect the current value of the potentiometer. In addition, setting the `value` property will update the UI accordingly. close #12
1 parent 2547a24 commit 8f1913e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/potentiometer-element.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { css, customElement, html, LitElement, property } from 'lit-element';
2+
import { styleMap } from 'lit-html/directives/style-map';
23

34
interface Point {
45
x: number;
@@ -48,6 +49,9 @@ export class PotentiometerElement extends LitElement {
4849
}
4950

5051
render() {
52+
const percent = this.clamp(0, 1, this.percentFromMinMax(this.value, this.min, this.max));
53+
const knobDeg = (this.endDegree - this.startDegree) * percent + this.startDegree;
54+
5155
return html`
5256
<svg
5357
width="20mm"
@@ -61,6 +65,9 @@ export class PotentiometerElement extends LitElement {
6165
@touchstart=${this.down}
6266
@touchmove=${this.move}
6367
@touchend=${this.up}
68+
style=${styleMap({
69+
'--knob-angle': knobDeg + 'deg',
70+
})}
6471
>
6572
<rect
6673
x=".15"
@@ -163,17 +170,10 @@ export class PotentiometerElement extends LitElement {
163170
this.updateValue(value);
164171
}
165172

166-
private updatePotentiometerPointer(value: number) {
167-
const percent = this.percentFromMinMax(value, this.min, this.max);
168-
const deg = Math.round((this.endDegree - this.startDegree) * percent + this.startDegree);
169-
this.style.setProperty('--knob-angle', `${deg}deg`);
170-
}
171-
172173
private updateValue(value: number) {
173174
const clamped = this.clamp(this.min, this.max, value);
174175
const updated = Math.round(clamped / this.step) * this.step;
175-
const rounded = Math.round(updated * 100) / 100;
176-
this.updatePotentiometerPointer(rounded);
177-
this.dispatchEvent(new InputEvent('input', { data: `${rounded}` }));
176+
this.value = Math.round(updated * 100) / 100;
177+
this.dispatchEvent(new InputEvent('input', { data: `${this.value}` }));
178178
}
179179
}

0 commit comments

Comments
 (0)