From 7c7490c770e14a3c2814e5629d55a4a4af0b7bb6 Mon Sep 17 00:00:00 2001 From: Ritesh Date: Wed, 4 Nov 2026 13:03:33 +0530 Subject: [PATCH] Slider Examples: Move the thumb on pointer down when the rail is pressed The color viewer and rating sliders only moved the thumb once the pointer moved or was released, and the temperature and seek sliders only moved it on the click event after release. Pressing the rail now moves the thumb to the pointer position immediately and keeps tracking the pointer until it is released, in all four examples, consistent with the native range input. Pressing the thumb itself still starts a drag without jumping. Resolves #2446 --- .../slider/examples/js/slider-color-viewer.js | 79 ++++++++----------- .../slider/examples/js/slider-rating.js | 27 +++---- .../slider/examples/js/slider-seek.js | 40 ++++------ .../slider/examples/js/slider-temperature.js | 30 +++---- 4 files changed, 70 insertions(+), 106 deletions(-) diff --git a/content/patterns/slider/examples/js/slider-color-viewer.js b/content/patterns/slider/examples/js/slider-color-viewer.js index 0d601ca33e..6943a1d5d2 100644 --- a/content/patterns/slider/examples/js/slider-color-viewer.js +++ b/content/patterns/slider/examples/js/slider-color-viewer.js @@ -40,10 +40,7 @@ class ColorViewerSliders { this.initSliderRefs(this.sliders, 'green'); this.initSliderRefs(this.sliders, 'blue'); - document.body.addEventListener( - 'pointerup', - this.onThumbPointerUp.bind(this) - ); + document.body.addEventListener('pointerup', this.onPointerUp.bind(this)); this.colorBoxNode = domNode.querySelector('.color-box'); this.colorValueHexNode = domNode.querySelector('input.color-value-hex'); @@ -103,11 +100,6 @@ class ColorViewerSliders { this.sliders[slider].sliderNode.tabIndex = 0; } - this.sliders[slider].railNode.addEventListener( - 'click', - this.onRailClick.bind(this) - ); - this.sliders[slider].sliderNode.addEventListener( 'keydown', this.onSliderKeyDown.bind(this) @@ -115,7 +107,7 @@ class ColorViewerSliders { this.sliders[slider].sliderNode.addEventListener( 'pointerdown', - this.onThumbPointerDown.bind(this) + this.onSliderPointerDown.bind(this) ); this.sliders[slider].valueNode.addEventListener( @@ -123,14 +115,9 @@ class ColorViewerSliders { this.onSliderKeyDown.bind(this) ); - this.sliders[slider].valueNode.addEventListener( - 'pointerdown', - this.onThumbPointerDown.bind(this) - ); - this.sliders[slider].sliderNode.addEventListener( 'pointermove', - this.onThumbPointerMove.bind(this) + this.onPointerMove.bind(this) ); this.moveSliderTo( @@ -269,55 +256,55 @@ class ColorViewerSliders { } } - onThumbPointerDown(event) { - this.pointerSlider = this.getSlider(event.currentTarget); + // Calculate the slider value from the horizontal position of a pointer event + getValueFromPointer(slider, event) { + var x = this.getSVGPoint(slider, event).x; + var min = this.getValueMin(slider); + var max = this.getValueMax(slider); + var diffX = x - this.railX; + return Math.round((diffX * (max - min)) / this.railWidth); + } + + onSliderPointerDown(event) { + var slider = this.getSlider(event.currentTarget); - // Set focus to the clicked on - this.pointerSlider.sliderNode.focus(); + this.pointerSlider = slider; + + // Move the thumb to the pointer position on the down event, unless the + // thumb itself was pressed, then track the pointer until it is released + if ( + event.target !== slider.thumbNode && + event.target !== slider.focusNode + ) { + this.moveSliderTo(slider, this.getValueFromPointer(slider, event)); + } + + // Set focus to the slider + slider.sliderNode.focus(); event.preventDefault(); event.stopPropagation(); } - onThumbPointerUp() { + onPointerUp() { this.pointerSlider = false; } - onThumbPointerMove(event) { + onPointerMove(event) { if ( this.pointerSlider && this.pointerSlider.sliderNode.contains(event.target) ) { - let x = this.getSVGPoint(this.pointerSlider, event).x; - let min = this.getValueMin(this.pointerSlider); - let max = this.getValueMax(this.pointerSlider); - let diffX = x - this.railX; - let value = Math.round((diffX * (max - min)) / this.railWidth); - this.moveSliderTo(this.pointerSlider, value); + this.moveSliderTo( + this.pointerSlider, + this.getValueFromPointer(this.pointerSlider, event) + ); event.preventDefault(); event.stopPropagation(); } } - // handle click event on the rail - onRailClick(event) { - var slider = this.getSlider(event.currentTarget); - - var x = this.getSVGPoint(slider, event).x; - var min = this.getValueMin(slider); - var max = this.getValueMax(slider); - var diffX = x - this.railX; - var value = Math.round((diffX * (max - min)) / this.railWidth); - this.moveSliderTo(slider, value); - - event.preventDefault(); - event.stopPropagation(); - - // Set focus to the clicked handle - slider.sliderNode.focus(); - } - getColorHex() { var r = parseInt( this.sliders.red.sliderNode.getAttribute('aria-valuenow') diff --git a/content/patterns/slider/examples/js/slider-rating.js b/content/patterns/slider/examples/js/slider-rating.js index 4eea74a725..22c3b5fd20 100644 --- a/content/patterns/slider/examples/js/slider-rating.js +++ b/content/patterns/slider/examples/js/slider-rating.js @@ -42,12 +42,12 @@ class RatingSlider { ); this.labelTexts.forEach((lt) => { - lt.addEventListener('click', this.onTargetClick.bind(this)); + lt.addEventListener('pointerdown', this.onTargetPointerDown.bind(this)); + lt.addEventListener('pointermove', this.onPointerMove.bind(this)); }); this.targetRects.forEach((tr) => { - tr.addEventListener('click', this.onTargetClick.bind(this)); - tr.addEventListener('pointerdown', this.onSliderPointerDown.bind(this)); + tr.addEventListener('pointerdown', this.onTargetPointerDown.bind(this)); tr.addEventListener('pointermove', this.onPointerMove.bind(this)); }); @@ -382,7 +382,10 @@ class RatingSlider { this.sliderNode.setAttribute('aria-valuetext', valuetext); } - onTargetClick(event) { + onTargetPointerDown(event) { + // Move the thumb to the pressed rating on the down event, then track the + // pointer until it is released + this.isMoving = true; this.moveSliderTo( event.currentTarget.parentNode.getAttribute('data-value') ); @@ -390,18 +393,10 @@ class RatingSlider { event.preventDefault(); event.stopPropagation(); - // Set focus to the clicked handle + // Set focus to the slider this.sliderNode.focus(); } - onSliderPointerDown(event) { - if (!this.isMoving) { - this.isMoving = true; - } - event.preventDefault(); - event.stopPropagation(); - } - onPointerMove(event) { if (this.isMoving) { this.moveSliderTo( @@ -413,11 +408,7 @@ class RatingSlider { } onPointerUp() { - if (this.isMoving) { - this.isMoving = false; - // Set focus to the clicked handle - this.sliderNode.focus(); - } + this.isMoving = false; } onResize() { diff --git a/content/patterns/slider/examples/js/slider-seek.js b/content/patterns/slider/examples/js/slider-seek.js index 725a54917a..4393b97f61 100644 --- a/content/patterns/slider/examples/js/slider-seek.js +++ b/content/patterns/slider/examples/js/slider-seek.js @@ -56,17 +56,12 @@ class SliderSeek { // define possible slider positions - this.svgNode.addEventListener('click', this.onRailClick.bind(this)); + this.svgNode.addEventListener('pointerdown', this.onPointerDown.bind(this)); this.sliderNode.addEventListener( 'keydown', this.onSliderKeydown.bind(this) ); - this.sliderNode.addEventListener( - 'pointerdown', - this.onSliderPointerDown.bind(this) - ); - // bind a pointermove event handler to move pointer this.svgNode.addEventListener('pointermove', this.onPointerMove.bind(this)); @@ -308,39 +303,36 @@ class SliderSeek { ); } - onRailClick(event) { - var x = this.getSVGPoint(event).x; + // Calculate the slider value from the horizontal position of a pointer event + calcValue(x) { var min = this.getValueMin(); var max = this.getValueMax(); var diffX = x - this.railX; - var value = Math.round((diffX * (max - min)) / this.railWidth); - this.moveSliderTo(value); - - event.preventDefault(); - event.stopPropagation(); - - // Set focus to the clicked handle - this.sliderNode.focus(); + return Math.round((diffX * (max - min)) / this.railWidth); } - onSliderPointerDown(event) { + onPointerDown(event) { this.isMoving = true; + // Move the thumb to the pointer position on the down event, unless the + // thumb itself was pressed, then track the pointer until it is released + if ( + event.target !== this.sliderThumbNode && + event.target !== this.sliderFocusNode + ) { + this.moveSliderTo(this.calcValue(this.getSVGPoint(event).x)); + } + event.preventDefault(); event.stopPropagation(); - // Set focus to the clicked handle + // Set focus to the slider this.sliderNode.focus(); } onPointerMove(event) { if (this.isMoving) { - var x = this.getSVGPoint(event).x; - var min = this.getValueMin(); - var max = this.getValueMax(); - var diffX = x - this.railX; - var value = Math.round((diffX * (max - min)) / this.railWidth); - this.moveSliderTo(value); + this.moveSliderTo(this.calcValue(this.getSVGPoint(event).x)); event.preventDefault(); event.stopPropagation(); diff --git a/content/patterns/slider/examples/js/slider-temperature.js b/content/patterns/slider/examples/js/slider-temperature.js index 9f4770c338..148cdc5504 100644 --- a/content/patterns/slider/examples/js/slider-temperature.js +++ b/content/patterns/slider/examples/js/slider-temperature.js @@ -59,15 +59,11 @@ class SliderTemperature { this.sliderValueNode.setAttribute('x', this.valueX); this.sliderFocusNode.setAttribute('x', this.focusX); - this.svgNode.addEventListener('click', this.onRailClick.bind(this)); + this.svgNode.addEventListener('pointerdown', this.onPointerDown.bind(this)); this.sliderNode.addEventListener( 'keydown', this.onSliderKeydown.bind(this) ); - this.sliderNode.addEventListener( - 'pointerdown', - this.onSliderPointerDown.bind(this) - ); // bind a pointermove event handler to move pointer this.svgNode.addEventListener('pointermove', this.onPointerMove.bind(this)); @@ -212,24 +208,22 @@ class SliderTemperature { return max - (diffY * (max - min)) / this.railHeight; } - onRailClick(event) { - var value = this.calcValue(this.getSVGPoint(event).y); - this.moveSliderTo(value); - - event.preventDefault(); - event.stopPropagation(); - - // Set focus to the clicked handle - this.sliderNode.focus(); - } - - onSliderPointerDown(event) { + onPointerDown(event) { this.isMoving = true; + // Move the thumb to the pointer position on the down event, unless the + // thumb itself was pressed, then track the pointer until it is released + if ( + event.target !== this.sliderThumbNode && + event.target !== this.sliderFocusNode + ) { + this.moveSliderTo(this.calcValue(this.getSVGPoint(event).y)); + } + event.preventDefault(); event.stopPropagation(); - // Set focus to the clicked handle + // Set focus to the slider this.sliderNode.focus(); }