Twipped / Kalendae

A javascript date picker that just works.
MIT License
1.99k stars 285 forks source link

Does not work on devices with both mouse and touch screen #228

Open kostyantin2216 opened 5 years ago

kostyantin2216 commented 5 years ago

This library does not support devices with a mouse and a touch screen.

We are using a Windows 8 laptop with a touch screen, what happens is if we select dates or change months with the touch screen everything works fine but if we use the mouse nothing happens.

I looked into the source code and found inside util.js the addEvent function on line 77 has a conditional check which checks if the event type that is being registered is mousedown and if the device is a touch device, if it is then instead of registering mousedown it registers touchstart.

After reading up on touch events and comparing them to click events i saw that both touches and clicks emit one mousedown event, this can be seen here, so my question is if this check is even necessary? I might be missing something but shouldn't this work if we registered mousedown also for touch devices?

Here is the code snippet i am talking about:

if(eventName === 'mousedown' && util.isTouchDevice()) {
    //works on touch devices
    elem.addEventListener('touchstart', listener, false);
} else {
    elem.addEventListener(eventName, listener, false);
}
Twipped commented 5 years ago

I'm not sure if this is the case still, but the reason this was done back when Kalendae was created was because mouse events did not trigger at the same speed as touch events on touch devices. There was always a noticeable pause of about 300ms between when touch events fired and mouse events followed suit. This was done deliberately by webkit (which was the only mobile engine at the time) because of gesture detection. See https://www.sitepoint.com/5-ways-prevent-300ms-click-delay-mobile-devices/

This is a very old library, in internet time. It needs to be completely rewritten for a version 2.0, which would let the library dump IE support and remove a lot of unneeded cruft, but I've not had any reason to do that. If somebody wants to do a PR to add PointerEvents support, which would solve this problem in Edge, I'd be open to accepting it. It just needs to degrade gracefully in older browsers, since 1.x still officially supports IE8.