Skip to content

fix(slider): ensure input fires on track interaction #5554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-cars-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@spectrum-web-components/slider': patch
---

Editable sliders will now reliably emit `input` events when interaction starts with the track.
16 changes: 8 additions & 8 deletions packages/slider/src/HandleController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ export class HandleController {
if (!elements) return;

const { input } = elements;
if (input.valueAsNumber === handle.value) {
if (handle.dragging) {
handle.dispatchInputEvent();
}
} else {
input.valueAsNumber = handle.value;
this.requestUpdate();
}

input.valueAsNumber = handle.value;
this.requestUpdate();
// reset to potentially clamped value
handle.value = input.valueAsNumber;

if (handle.dragging) {
handle.dispatchInputEvent();
}
}

public handleHasChanged(handle: SliderHandle): void {
Expand Down
112 changes: 94 additions & 18 deletions packages/slider/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,24 +110,22 @@ export const testEditableSlider = (type: string): void => {
it('dispatches `input` of the animation frame', async () => {
const inputSpy = spy();
const changeSpy = spy();
const el = await fixture<Slider>(
html`
<sp-slider
editable
hide-stepper
min="1"
max="100"
step="1"
label="Slider label"
@input=${(event: Event & { target: Slider }) => {
inputSpy(event.target.value);
}}
@change=${(event: Event & { target: Slider }) => {
changeSpy(event.target.value);
}}
></sp-slider>
`
);
const el = await fixture<Slider>(html`
<sp-slider
editable
hide-stepper
min="1"
max="100"
step="1"
label="Slider label"
@input=${(event: Event & { target: Slider }) => {
inputSpy(event.target.value);
}}
@change=${(event: Event & { target: Slider }) => {
changeSpy(event.target.value);
}}
></sp-slider>
`);
await elementUpdated(el);
expect(el.value).to.equal(50.5);

Expand Down Expand Up @@ -364,5 +362,83 @@ export const testEditableSlider = (type: string): void => {
el.handleController.inputForHandle(el)
);
});

// regression test for https://github.com/adobe/spectrum-web-components/issues/5522
it('dispatches `input` on track interaction after handle interaction', async () => {
const inputSpy = spy();
const changeSpy = spy();

const el = await fixture<Slider>(html`
<sp-slider
editable
hide-stepper
min="1"
max="100"
step="1"
label="Slider label"
@input=${(event: Event & { target: Slider }) => {
inputSpy(event.target.value);
}}
@change=${(event: Event & { target: Slider }) => {
changeSpy(event.target.value);
}}
></sp-slider>
`);
await elementUpdated(el);
expect(el.value).to.equal(50.5);

expect(inputSpy.callCount, 'start clean').to.equal(0);
expect(changeSpy.callCount, 'start clean').to.equal(0);

const handle = el.shadowRoot.querySelector(
'.handle'
) as HTMLDivElement;

const rect = handle.getBoundingClientRect();

// click handle once
await sendMouse({
steps: [
{
type: 'move',
position: [
rect.left + rect.width / 2,
rect.top + rect.height / 2,
],
},
{
type: 'down',
},
{ type: 'up' },
],
});

await elementUpdated(el);

expect(changeSpy.callCount, 'one change').to.equal(1);
expect(inputSpy.callCount, 'no input').to.equal(0);

// move to and click track once
await sendMouse({
steps: [
{
type: 'move',
position: [
rect.left - rect.width,
rect.top + rect.height / 2,
],
},
{
type: 'down',
},
{ type: 'up' },
],
});

await elementUpdated(el);

expect(changeSpy.callCount, 'one additional change').to.equal(2);
expect(inputSpy.callCount, 'one input').to.equal(1);
});
});
};
Loading