Skip to content

Commit

Permalink
feat: add listener do key 'esc', when pressed the value resets
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelLimaC committed Mar 1, 2024
1 parent c8b4437 commit 33bf379
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/components/RangeSlider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ export default function RangeSlider({
let isMouseDown = false;
let startX = 0;
let currentValue = Math.min(Math.max(value, minimum), maximum);
let prevValue = value;

const handleStart = (clientX) => {
isMouseDown = true;
startX = clientX;
prevValue = currentValue;
this.emit('interactionStart', currentValue);
};

Expand Down Expand Up @@ -84,6 +86,15 @@ export default function RangeSlider({
moveBackground(clientX);
};

const escKeyDown = (event) => {
if (event.key === 'Escape') {
isMouseDown = false;
currentValue = prevValue;
this.setValue(prevValue);
this.emit('interactionEnd', currentValue);
}
};

containerElement.addEventListener('mousedown', (event) => {
event.preventDefault();
handleStart(event.clientX);
Expand All @@ -99,13 +110,15 @@ export default function RangeSlider({
document.addEventListener('mouseup', handleEnd);
document.addEventListener('touchmove', handleTouchMove);
document.addEventListener('touchend', handleEnd);
document.addEventListener('keydown', escKeyDown);
});

this.listen('unmount', () => {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleEnd);
document.removeEventListener('touchmove', handleTouchMove);
document.removeEventListener('touchend', handleEnd);
document.removeEventListener('keydown', escKeyDown);
});
}

Expand Down

0 comments on commit 33bf379

Please sign in to comment.