Skip to content

Commit

Permalink
fix: focus the slider on mousedown (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
web-padawan authored Aug 7, 2021
1 parent 651dbc2 commit 602962c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib/components/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export abstract class Slider {
event.preventDefault();
// event.button is 0 in mousedown for left button activation
if (!isValid(event) || (!hasTouched && (event as MouseEvent).button != 0)) return;
this.el.focus();
pointerMove(this, event);
this.dragging = true;
break;
Expand Down
7 changes: 7 additions & 0 deletions src/test/color-picker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ describe('hex-color-picker', () => {
});

describe('interaction', () => {
it('should focus the slider on mousedown', () => {
const spy = sinon.spy(hue, 'focus');
const { x, y } = middleOfNode(hue);
hue.dispatchEvent(new FakeMouseEvent('mousedown', { pageX: x + 10, pageY: y }));
expect(spy.callCount).to.equal(1);
});

it('should dispatch color-changed event on mousedown', () => {
const spy = sinon.spy();
picker.addEventListener('color-changed', spy);
Expand Down

5 comments on commit 602962c

@lpellegr
Copy link

@lpellegr lpellegr commented on 602962c Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@web-padawan This is a breaking change :(

I am using your great library and its rgba-string-color-picker element in association with an input field. When the input has the focus, I display the color picker. When the input field loses focus (blur event), I hide the color picker. Before this patch, when a slider was moving in the color picker, the focus was remaining on the input field thus keeping the picker opened. With your change, the slider is taking the focus from the input field as soon as we use the color picker.

Any suggestions?

@web-padawan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry about that, I didn't consider this use case. As a workaround, you could check the event.relatedTarget on focusout event, to detect whether focus moves to the color picker or not. I will create a code example tomorrow.

@lpellegr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@web-padawan Thanks for your help. Using event.relatedTarget I can detect if the picker get the focus and prevent hiding it. However, once the picker has the focus, detecting whether a click is made outside the picker element (before hiding it) requires yet another logic.

@web-padawan
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's a bit complicated. Here is the full example using two focusout listeners.
Sorry about the delay. I'm going to add this example and maybe some more examples to README soon.

@lpellegr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the full example.

Please sign in to comment.