Skip to content

Commit

Permalink
range-slider does not need a name; pass event obj to callback instead…
Browse files Browse the repository at this point in the history
… of slider object
  • Loading branch information
justvanrossum committed Sep 14, 2023
1 parent f1b4c75 commit ce084a7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 26 deletions.
19 changes: 9 additions & 10 deletions src/fontra/client/web-components/designspace-location.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class DesignspaceLocation extends UnlitElement {
// Event was triggered by us -- ignore
return;
}
const slider = this.shadowRoot.querySelector(`range-slider[name="${event.key}"]`);
const slider = this.shadowRoot.querySelector(`#slider-${event.key}`);
if (slider) {
slider.value = event.newValue;
}
Expand All @@ -85,17 +85,15 @@ export class DesignspaceLocation extends UnlitElement {
this._values = { ...values };

for (const [axisName, value] of Object.entries(values)) {
const slider = this.shadowRoot.querySelector(`range-slider[name="${axisName}"]`);
const slider = this.shadowRoot.querySelector(`#slider-${axisName}`);
if (slider) {
slider.value = value;
}
}

for (const axis of this.axes || []) {
if (!(axis.name in values)) {
const slider = this.shadowRoot.querySelector(
`range-slider[name="${axis.name}"]`
);
const slider = this.shadowRoot.querySelector(`#slider-${axis.name}`);
if (slider) {
slider.value = axis.defaultValue;
}
Expand Down Expand Up @@ -133,12 +131,13 @@ export class DesignspaceLocation extends UnlitElement {
);
elements.push(
html.createDomElement("range-slider", {
name: axis.name,
id: `slider-${axis.name}`,
minValue: axis.minValue,
maxValue: axis.maxValue,
defaultValue: axis.defaultValue,
value: modelValue !== undefined ? modelValue : axis.defaultValue,
onChangeCallback: (event) => this._dispatchLocationChangedEvent(event),
onChangeCallback: (event) =>
this._dispatchLocationChangedEvent(axis.name, event.value),
})
);
elements.push(infoBox);
Expand All @@ -157,11 +156,11 @@ export class DesignspaceLocation extends UnlitElement {
}
}

_dispatchLocationChangedEvent(slider) {
_dispatchLocationChangedEvent(name, value) {
if (this.controller) {
this.controller.setItem(slider.name, slider.value, this);
this.controller.setItem(name, value, this);
} else {
this.values[slider.name] = slider.value;
this.values[name] = value;
const event = new CustomEvent("locationChanged", {
bubbles: false,
detail: this,
Expand Down
19 changes: 3 additions & 16 deletions src/fontra/client/web-components/range-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@ export class RangeSlider extends LitElement {
font-feature-settings: "tnum" 1;
}
.slider-name {
min-width: 7ch;
overflow: hidden;
text-overflow: ellipsis;
text-align: right;
}
.slider-name:hover {
cursor: pointer;
}
.range-container {
position: relative;
flex-grow: 1;
Expand Down Expand Up @@ -157,7 +146,6 @@ export class RangeSlider extends LitElement {
`;

static properties = {
name: { type: String, reflect: true },
minValue: { type: Number },
maxValue: { type: Number },
defaultValue: { type: Number },
Expand All @@ -170,7 +158,6 @@ export class RangeSlider extends LitElement {
constructor() {
super();
// Fallbacks for attributes that are not defined when calling the component
this.name = "Slider";
this.minValue = 0;
this.maxValue = 100;
this.defaultValue = this.minValue;
Expand Down Expand Up @@ -247,7 +234,7 @@ export class RangeSlider extends LitElement {
event.preventDefault();
this.value = clamp(newValue, this.minValue, this.maxValue);
this.updateIsAtDefault();
this.onChangeCallback(this);
this.onChangeCallback({ value: this.value });
}

handleMouseDown(event) {
Expand Down Expand Up @@ -282,7 +269,7 @@ export class RangeSlider extends LitElement {
}
}
this.updateIsAtDefault();
this.onChangeCallback(this);
this.onChangeCallback({ value: this.value });
}

updateIsAtDefault() {
Expand All @@ -304,7 +291,7 @@ export class RangeSlider extends LitElement {

reset(event) {
this.value = this.defaultValue;
this.onChangeCallback(this);
this.onChangeCallback({ value: this.value });
}

buildTickmarks() {
Expand Down

0 comments on commit ce084a7

Please sign in to comment.